diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000..342cc272 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,16 @@ +FROM php:8.1.13-cli + +RUN apt-get update && apt-get install -y vim git sudo + +RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \ + && php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \ + && php composer-setup.php \ + && php -r "unlink('composer-setup.php');" \ + && sudo mv composer.phar /usr/local/bin/composer + +ARG USERNAME=developer +ARG USER_UID=1000 +ARG USER_GID=$USER_UID + +RUN groupadd --gid $USER_GID $USERNAME \ + && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..174dee0c --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,7 @@ +{ + "name": "progpilot-linux", + "build": { + "dockerfile": "Dockerfile" + }, + "remoteUser": "developer" +} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 495b0f8c..e34be740 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,8 +10,9 @@ jobs: strategy: matrix: php-version: - - "7.3" - "7.4" + - "8.0" + - "8.1" steps: - name: "Checkout" uses: "actions/checkout@v2" diff --git a/.gitignore b/.gitignore index cd9da0b9..e11208c6 100755 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ articles/ builds/*.phar projects/tests/vendor projects/tests/composer.lock +projects/tests/.phpunit.result.cache projects/example/vendor projects/example/composer.lock projects/example_config/vendor diff --git a/composer.json b/composer.json index 01c50308..e006082a 100644 --- a/composer.json +++ b/composer.json @@ -9,19 +9,26 @@ } ], "require": { - "php": ">=7.2.5", - "ircmaxell/php-cfg": "1.0.x-dev", + "php": ">=7.4", + "ircmaxell/php-cfg": "^0.6.0", "symfony/yaml": ">=3.3.6", - "symfony/console": ">=3.3.5" + "symfony/console": ">=3.3.5", + "myclabs/deep-copy": "^1.10.2" }, "require-dev": { - "phpunit/phpunit": "^6.0", - "phpro/grumphp": "^1.3" + "phpunit/phpunit": "^8.0 || ^9.0", + "phpro/grumphp": "^1.3", + "squizlabs/php_codesniffer": "^3.5" }, "bin": ["projects/phar/progpilot"], "autoload": { "psr-0": { "progpilot": "package/src" } + }, + "config": { + "allow-plugins": { + "phpro/grumphp": true + } } } diff --git a/docs/API.md b/docs/API.md index 04cbc935..372a4870 100644 --- a/docs/API.md +++ b/docs/API.md @@ -20,17 +20,20 @@ To retrieve the value of $file, $code and $folder use these methods: *** - $obj_context->inputs->setLanguages($array); Languages you want to analyze (["php", "js"] but js is in development), default is *["php"]* -- $obj_context->inputs->setFrameworks($array); -Frameworks you want to analyze (default is *["suitecrm", "codeigniter"]*) - $obj_context->inputs->setDev($bool); If you want to use security data relative to development of progpilot (default is *false*) *** *** +- $obj_context->inputs->addSources($files_sources); - $obj_context->inputs->setSources($files_sources); +- $obj_context->inputs->addSinks($files_sinks); - $obj_context->inputs->setSinks($files_sinks); +- $obj_context->inputs->addSanitizers($files_sanitizers); - $obj_context->inputs->setSanitizers($files_sanitizers); +- $obj_context->inputs->addValidators($files_validators); - $obj_context->inputs->setValidators($files_validators); +- $obj_context->inputs->addCustomRules($files_custom); - $obj_context->inputs->setCustomRules($files_custom); - $obj_context->inputs->getCustomRules(); - $obj_context->inputs->getSources(); @@ -49,15 +52,15 @@ These functions are explained in the chapter about [**handling false positives** *** *** -- $obj_context->inputs->setIncludes($mixed); -- $obj_context->inputs->setExcludes($mixed); +- $obj_context->inputs->setInclusions($mixed); +- $obj_context->inputs->setExclusions($mixed); For include or exclude files and folders during the analysis, see an [**example here**](./../projects/tests/exclude_files.json) with a json file configuration and an [**example here**](./../projects/tests/run_exclude_files.php) with a php array. *** ## Outputs *** -- $obj_context->outputs->resolveIncludes($bool); -- $obj_context->outputs->resolveIncludesFile($file); +- $obj_context->outputs->setWriteIncludeFailures($bool); +- $obj_context->outputs->setIncludeFailuresFile($file); These functions are explained in the chapter about [**included files**](./INCLUDES.md) - $obj_context->outputs->getAst(); - $obj_context->outputs->getCfg(); @@ -72,25 +75,19 @@ print the number of files analyzed (it does not count the included files (with * ## Options *** -- $obj_context->setLimitDefs($nb); +- $obj_context->setMaxDefinitions($nb); to prevent memory exhaustion you could limit the number of definitions by file during the analysis (default is *3000*) -- $obj_context->setLimitTime($time_sec); +- $obj_context->setMaxFileAnalysisDuration($time_sec); max execution time by file for some steps of the analysis (default is *10 seconds*) -- $obj_context->setLimitSize($size_bytes); +- $obj_context->setMaxFileSize($size_bytes); do not analyze file that are larger than this defined size (default is 500 000 bytes) -- $obj_context->setPrintFile($bool); -*true* if you want to print the name of files analyzed by progpilot, default is *false* -- $obj_context->setPrintWarning($bool); -*true* if you want to print warnings during the analysis, default is *false* +- $obj_context->setDebugMode($bool); +*true* if you want to output warnings during the analysis, default is *false* - $obj_context->setPrettyPrint($bool); *true* if you want to pretty print the JSON output of standalone progpilot application, default is *true* -- $obj_context->setAnalyzeFunctions($bool); -*true* if you want to analyze all functions (*false* only *main function* is analyzed), default is *true* - $obj_context->setAnalyzeIncludes($bool); *true* or *false* if you want to analyze included files, default is *true* - $obj_context->setConfiguration($config); you can use an yaml file to specify the configuration of analysis, see an [**example here**](./../projects/example_config/configuration.yml). -- $obj_context->setAnalyzeHardRules($bool); -If you want to check custom rules that can take a lot a time (default is false) These rules are explained in the chapter [**customize an analyze**](./CUSTOM_ANALYSIS.md) *** diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index 03addf1c..c22d6292 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -16,21 +16,7 @@ That will allow the ability of contributors to reproduce the bug. All php code must adhere to [PSR-2 standard](https://www.php-fig.org/psr/psr-2/) (except for tests). ### GrumPHP -Developers can use [GrumPHP](https://github.com/phpro/grumphp/) to ensure each progpilot commit reaches code style (phpcs) and security (progpilot itself) requirements. -Install GrumPHP globally or in the progpilot repository: -```shell -composer require --dev phpro/grumphp -``` -Install the [required tasks](../grumphp.yml) for Progpilot: -```shell -composer config minimum-stability dev -composer require --dev squizlabs/php_codesniffer -composer require --dev designsecurity/progpilot -``` -Configure the following env variable to instruct GrumPHP to locate tasks executables: -```shell -export GRUMPHP_BIN_DIR="/path/to/vendor/bin" -``` +Developers can use [GrumPHP](https://github.com/phpro/grumphp/) to ensure each progpilot commit reaches code style (phpcs) requirements. ### Frameworks support Most of the time the analysis of progpilot can be extended simply with adding the corresponding [sources, sinks, validators and sanitizers](./SPECIFY_ANALYSIS.md): look at how it was done for [current frameworks](https://github.com/designsecurity/progpilot/tree/master/package/src/uptodate_data/php/frameworks). diff --git a/docs/FAQ.md b/docs/FAQ.md index 24cc4008..eaaab659 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -1,7 +1,7 @@ # FAQ #### Which version of PHP do I need? -The minimum version of PHP needed to run Progpilot is 7.2.5 +The minimum version of PHP needed to run Progpilot is 7.4 #### Where can I find the updated security files configuration (sinks, sources, validators, sanitizers and rules) of Progpilot? You can find the updated security files configuration of Progpilot in [package/src/uptodate_data](../package/src/uptodate_data) folder. @@ -11,8 +11,8 @@ Example of control flow graph and call graph of source code transformed to dot f #### When I use progpilot I often run out of memory? Static analyzers use a lot of memory but you could try to handle this with [these functions](./API.md): -- *$obj_context->setLimitDefs($nb);* -- *$obj_context->setLimitSize($size_bytes);* +- *$obj_context->setMaxDefinitions($nb);* +- *$obj_context->setMaxFileSize($size_bytes);* And by increasing the maximum memory amount for a script (*memory_limit*) in the configuration of PHP (*php.ini*). @@ -20,4 +20,3 @@ And by increasing the maximum memory amount for a script (*memory_limit*) in the At this moment, these frameworks are supported: - suiteCRM - codeIgniter -- wordpress diff --git a/docs/INCLUDES.md b/docs/INCLUDES.md index 115fb1de..6781c159 100644 --- a/docs/INCLUDES.md +++ b/docs/INCLUDES.md @@ -13,13 +13,13 @@ include($dir."myfile$suf.php"); ``` To bypass this limitation use these functions: -- $obj_context->outputs->resolveIncludes($bool); -- $obj_context->outputs->resolveIncludesFile($file); +- $obj_context->outputs->setWriteIncludeFailures($bool); +- $obj_context->outputs->setIncludeFailuresFile($file); When *$bool* set to *true* and *$file* set to *resolve_includes.json* for example For each include not resolved an entry will be printed in the *$file* with the location of the include function (file, line, column): ```javascript -{"includes_not_resolved":[["/home/dev/projects/tests/includes/simple5.php",11,11]]} +{"include_failures":[["/home/dev/projects/tests/includes/simple5.php",11,11]]} ``` Next create a *resolved_includes.json* file with the good value for each include function call: ```javascript diff --git a/docs/SPECIFY_ANALYSIS.md b/docs/SPECIFY_ANALYSIS.md index ee2e5498..0e821caa 100644 --- a/docs/SPECIFY_ANALYSIS.md +++ b/docs/SPECIFY_ANALYSIS.md @@ -9,6 +9,7 @@ To specify the way vulnerabilities are detected, customize the sources, sinks, s ## Configure sources - $obj_context->inputs->setSources($files_sources); +- $obj_context->inputs->addSources($files_sources); - $obj_context->inputs->getSources(); Where *$file_sources* is a json file (or an array of json files) like below: @@ -32,6 +33,7 @@ Optional properties: ## Configure sanitizers - $obj_context->inputs->setSanitizers($file_sanitizers); +- $obj_context->inputs->addSanitizers($file_sanitizers); - $obj_context->inputs->getSanitizers(); Where *$file_sanitizers* is a json file (or an array of json files) like below: @@ -63,6 +65,7 @@ Optional properties: ## Configure sinks - $obj_context->inputs->setSinks($file_sinks); +- $obj_context->inputs->addSinks($file_sinks); - $obj_context->inputs->getSinks(); Where *$file_sinks* is a json file (or an array of json files) like below: @@ -88,6 +91,7 @@ Optional properties: - [instanceof](#instanceof-property), [prevent](#prevent-property), [parameters](#parameters-property) ## Configure validators +- $obj_context->inputs->addValidators($file_validators); - $obj_context->inputs->setValidators($file_validators); - $obj_context->inputs->getValidators(); diff --git a/docs/dev/STATES.md b/docs/dev/STATES.md new file mode 100644 index 00000000..942d61a2 --- /dev/null +++ b/docs/dev/STATES.md @@ -0,0 +1,89 @@ +# definition states API + +Each definition has at least one state holding attributes like isTainted. The goal is to handle properties and array dataflow. + +For simple variable, the defaultState is enough, as dataflow is correctly performed by visitorDataFlow: +``` +// block 1 +// foo defined in blockid 1 (defaultState = 1) +// state 1 of foo tainted +$foo = $_GET["p"]; + +if(rand()) { + // block 2 + // bar defined in blockid 2 (defaultState = 2) + // state 2 of foo bar (get value of foo->currentState()) tainted + $bar = $foo; +} +else { + // block 3 + // bar defined in blockid 3 (defaultState = 3) + // state 3 of foo bar empty + $bar = null; +} + +// block 4 +// bar search def: +// * block2 $bar->getCurrentState() +// * block3 $bar->getCurrentState() +// merge states on block 4 of echo_arg0 +echo $bar; +``` + +For instances/properties variable, we need different states: +``` +// block 1 +// instance defined in blockid 1 (defaultState = 1) +$instance = new Object; + +if(rand()) { + // block 2 + // instance defined in blockid 1 (defaultState = 1) + // state 2 of instance prop tainted + $instance->prop = $_GET["p"]; + echo $instance->prop; +} +else { + // block 3 + // instance defined in blockid 1 (defaultState = 1) + // state 3 of instance prop "null" + $instance->prop = "null"; + echo $instance->prop; +} + +// block 4 +// we launch dataflow analysis for properties +// parent of 4 = block 2, 3 +// state 4 = merge(state 2,3) +echo $instance->prop; +``` + + +Chained calls: +``` +// block 1 +// instance1 defined in blockid 1 (defaultState = 1) +$instance1 = new Object1; + +/* +function func1() { + // block 2 + // instance2 defined in blockid 2 (defaultState = 2) + $instance2 = new Object2; + return $instance2; +} + +function func2() { + // block 3 + // instance3 defined in blockid 3 (defaultState = 3) + $instance3 = new Object3; + return $instance3; +} + +function func3() { + echo $this->prop; +} +*/ + +$instance1->func1()->func2()->func3(); +``` \ No newline at end of file diff --git a/grumphp.yml b/grumphp.yml index 65337c12..2809ba43 100644 --- a/grumphp.yml +++ b/grumphp.yml @@ -6,7 +6,3 @@ grumphp: metadata: priority: 300 ignore_patterns: ["*/projects/tests/*"] - - progpilot: - config_file: progpilot.yml - triggered_by: [php] diff --git a/package/composer.json b/package/composer.json index 8f2fa852..e976ee08 100644 --- a/package/composer.json +++ b/package/composer.json @@ -9,13 +9,13 @@ } ], "require": { - "php": "^7.0", - "ircmaxell/php-cfg": "1.0.x-dev", + "php": ">=7.4", + "ircmaxell/php-cfg": "^0.6.0", "symfony/yaml": ">=3.3.6", "symfony/console": ">=3.3.5" }, "require-dev": { - "phpunit/phpunit": "^6.0" + "phpunit/phpunit": "^8.0 || ^9.0" }, "autoload": { "psr-0": { diff --git a/package/src/progpilot/AbstractLayer/Analysis.php b/package/src/progpilot/AbstractLayer/Analysis.php deleted file mode 100644 index 050ec967..00000000 --- a/package/src/progpilot/AbstractLayer/Analysis.php +++ /dev/null @@ -1,171 +0,0 @@ -getObjects()->getObjects() as $id => $objectClass) { - $params = array($context, $id, $objectClass, $myClass); - call_user_func_array(__NAMESPACE__ ."\\$func", $params); - } - } - - public static function forDefsInFunctions($context, $func, $myClass) - { - foreach ($context->getFunctions()->getFunctions() as $functionsBlocks) { - foreach ($functionsBlocks as $functionBlock) { - foreach ($functionBlock->getDefs()->getDefs() as $defsBlocks) { - foreach ($defsBlocks as $defBlock) { - $params = array($context, $defBlock, $myClass); - call_user_func_array(__NAMESPACE__ ."\\$func", $params); - } - } - } - } - } - - public static function checkIfFuncEqualMySpecify($context, $mySpecify, $myFunc, $stackClass = null) - { - $checkName = false; - if ($myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - $properties = $myFunc->property->getProperties(); - if (is_array($properties) && isset($properties[count($properties) - 1])) { - $lastproperty = $properties[count($properties) - 1]; - if ($lastproperty === $mySpecify->getName()) { - $checkName = true; - } - } - } - - $checkInstance = false; - if (($mySpecify->getName() === $myFunc->getName()) || $checkName) { - $checkName = true; - $checkInstance = true; - - if ($mySpecify->isInstance() && !is_null($stackClass)) { - if ($mySpecify->getLanguage() === "php") { - $propertiesRule = explode("->", $mySpecify->getInstanceOfName()); - } elseif ($mySpecify->getLanguage() === "js") { - $propertiesRule = explode(".", $mySpecify->getInstanceOfName()); - } - - if (is_array($propertiesRule)) { - $i = 0; - foreach ($propertiesRule as $propertyName) { - // if(!isset($stackClass[$i])) && count() == 0 => - //$test = new ClassInconnu; - //$test->db->call() (db has no known instance) - $foundProperty = true; - - if (isset($stackClass[$i]) && count($stackClass[$i]) > 0) { - $foundProperty = false; - foreach ($stackClass[$i] as $propClass) { - $objectId = $propClass->getObjectId(); - $myClass = $context->getObjects()->getMyClassFromObject($objectId); - - if (!is_null($myClass) - && ($myClass->getName() === $propertyName - || $myClass->getExtendsOf() === $propertyName)) { - $foundProperty = true; - break; - } - } - } - - if (!$foundProperty) { - $checkInstance = false; - break; - } - - $i ++; - } - } - } elseif ($mySpecify->isInstance() && is_null($stackClass)) { - $checkInstance = false; - } - } - - return $checkInstance & $checkName; - } - - public static function checkIfDefEqualDefRule($context, $defs, $rule, $def, $stackClass = null) - { - $definition = $rule->getDefinition(); - $checkName = false; - if ($def->isType(MyDefinition::TYPE_PROPERTY)) { - $properties = $def->property->getProperties(); - if (is_array($properties) && isset($properties[count($properties) - 1])) { - $lastproperty = $properties[count($properties) - 1]; - if ($lastproperty === $definition->getName()) { - $checkName = true; - } - } - } - - $checkInstance = false; - if (!is_null($definition) && ($definition->getName() === $def->getName()) || $checkName) { - $checkName = true; - $checkInstance = true; - - if (is_null($stackClass) && !is_null($defs)) { - $stackClass = ResolveDefs::propertyClass($context, $defs, $def); - } - - if ($definition->isInstance() && !is_null($stackClass)) { - if ($definition->getLanguage() === "php") { - $propertiesRule = explode("->", $definition->getInstanceOfName()); - } elseif ($definition->getLanguage() === "js") { - $propertiesRule = explode(".", $definition->getInstanceOfName()); - } - - if (is_array($propertiesRule)) { - $i = 0; - foreach ($propertiesRule as $propertyName) { - // if(!isset($stackClass[$i])) && count() == 0 => - //$test = new ClassInconnu; - //$test->db->call() (db has no known instance) - $foundProperty = true; - - if (isset($stackClass[$i]) && count($stackClass[$i]) > 0) { - $foundProperty = false; - foreach ($stackClass[$i] as $propClass) { - $objectId = $propClass->getObjectId(); - $myClass = $context->getObjects()->getMyClassFromObject($objectId); - - if (!is_null($myClass) - && ($myClass->getName() === $propertyName - || $myClass->getExtendsOf() === $propertyName)) { - $foundProperty = true; - break; - } - } - } - - if (!$foundProperty) { - $checkInstance = false; - break; - } - - $i ++; - } - } - } - } - - return $checkInstance & $checkName; - } -} diff --git a/package/src/progpilot/AbstractLayer/Callbacks.php b/package/src/progpilot/AbstractLayer/Callbacks.php deleted file mode 100644 index a1ee1305..00000000 --- a/package/src/progpilot/AbstractLayer/Callbacks.php +++ /dev/null @@ -1,29 +0,0 @@ -getName() === $newMyclass->getName()) { - $context->getObjects()->addMyclassToObject($idObject, $newMyclass); - } - } - - public static function modifyMyclassOfObjectFromDef($context, $def, $myClass) - { - if ($def->getClassName() === $myClass->getName()) { - $idObject = $def->getObjectId(); - $context->getObjects()->addMyclassToObject($idObject, $myClass); - } - } -} diff --git a/package/src/progpilot/Analysis/ArrayAnalysis.php b/package/src/progpilot/Analysis/ArrayAnalysis.php deleted file mode 100644 index 25955544..00000000 --- a/package/src/progpilot/Analysis/ArrayAnalysis.php +++ /dev/null @@ -1,146 +0,0 @@ -isType(MyDefinition::TYPE_COPY_ARRAY) - && ($searchedDef->isType(MyDefinition::TYPE_ARRAY) || $isIterator))) { - foreach ($def->getCopyArrays() as $defCopyArray) { - $myDef_arr = $defCopyArray[0]; - $myDef_tmp = $defCopyArray[1]; - - if (($myDef_arr === $searchedDef->getArrayValue()) || $isIterator) { - $goodDefs[] = $myDef_tmp; - } - } - } elseif ($searchedDef->isType(MyDefinition::TYPE_ARRAY)) { - // I'm looking for def[arr], I want to find def[arr], but I can have def (must be eliminated) - if (($def->isType(MyDefinition::TYPE_ARRAY) - && ($searchedDef->getArrayValue() === $def->getArrayValue()) || $isIterator) - || $def->getArrayValue() === "PROGPILOT_ALL_INDEX_TAINTED") { - if ($def->getArrayValue() === "PROGPILOT_ALL_INDEX_TAINTED") { - $searchedDef->setTainted(true); - //TaintAnalysis::setTainted($context, $data, $searchedDef, $def, $def->getExpr(), false); - - if (ResolveDefs::getVisibilityFromInstances($context, $data, $def)) { - ValueAnalysis::copyValues($searchedDef, $def); - TaintAnalysis::setTainted($searchedDef, $def, $def->getExpr()); - } - - $goodDefs[] = $searchedDef; - } else { - $goodDefs[] = $def; - } - } - } elseif (!$searchedDef->isType(MyDefinition::TYPE_ARRAY)) { - // I'm looking for def, I want to find def, but I can have def[arr] (must be eliminated) - if (!$def->isType(MyDefinition::TYPE_ARRAY) || $isIterator) { - $goodDefs[] = $def; - } - } - - return $goodDefs; - } - - public static function copyArray($context, $data, $originalTab, $originalArr, $copyTab, $copyArr) - { - if (!is_null($originalTab) && !is_null($copyTab)) { - if ($originalTab->isType(MyDefinition::TYPE_PROPERTY)) { - $defs = ResolveDefs::selectProperties( - $context, - $data, - $originalTab, - true - ); - } else { - $defs = ResolveDefs::selectDefinitions( - $context, - $data, - $originalTab, - true - ); - } - - foreach ($defs as $defa) { - ArrayAnalysis::copyArrayFromDef($defa, $originalArr, $copyTab, $copyArr); - } - } - } - - public static function copyArrayFromDef($originalTab, $originalArr, $copyTab, $copyArr) - { - if ($originalTab->isType(MyDefinition::TYPE_COPY_ARRAY)) { - $copyArrays = $originalTab->getCopyArrays(); - - foreach ($copyArrays as $value) { - $arrValue = $value[0]; - $defArr = $value[1]; - - $extract = BuildArrays::extractArrayFromArr($arrValue, $originalArr); - if ($extract !== false) { - $extractBis = BuildArrays::buildArrayFromArr($copyArr, $extract); - - $copyTab->addCopyArray($extractBis, $defArr); - $copyTab->addType(MyDefinition::TYPE_COPY_ARRAY); - if ($copyTab->isType(MyDefinition::TYPE_ARRAY)) { - $copyTab->removeType(MyDefinition::TYPE_ARRAY); - } - $copyTab->setArrayValue(false); - - unset($defArr); - } - } - } elseif ($originalTab->getArrayValue() !== "PROGPILOT_ALL_INDEX_TAINTED") { - $extract = BuildArrays::extractArrayFromArr($originalTab->getArrayValue(), $originalArr); - - // si on cherchait $copy = $array[11] ici il y a des arrays de type $array[11][quelquechose] - // ou deuxieme cas - // si on cherchait $copy = $arrays ici il y a des arrays de type $arrays[quelquechose] - if ($extract !== false) { - // si on a $copy[11] = $array[12] on veut $copy[11][12] - if ($copyArr !== false) { - $extract = BuildArrays::buildArrayFromArr($copyArr, $extract); - } - - $copyTab->addCopyArray($extract, $originalTab); - $copyTab->addType(MyDefinition::TYPE_COPY_ARRAY); - - if ($copyTab->isType(MyDefinition::TYPE_ARRAY)) { - $copyTab->removeType(MyDefinition::TYPE_ARRAY); - } - - $copyTab->setArrayValue(false); - } - } - } -} diff --git a/package/src/progpilot/Analysis/AssertionAnalysis.php b/package/src/progpilot/Analysis/AssertionAnalysis.php index 10e49e87..8b2ea624 100644 --- a/package/src/progpilot/Analysis/AssertionAnalysis.php +++ b/package/src/progpilot/Analysis/AssertionAnalysis.php @@ -14,7 +14,7 @@ class AssertionAnalysis { - public static function temporarySimple($context, $data, $myBlock, $resolveTemporary, $tempDef) + public static function checkDefIsAssert($myBlock, $def) { $assertions = $myBlock->getAssertions(); @@ -26,34 +26,16 @@ public static function temporarySimple($context, $data, $myBlock, $resolveTempor $myDefAssertion = $assertion->getDef(); $typeAssertion = $assertion->getType(); - // there was not resolution so we simply check name (or better equality values) - if ($resolveTemporary === $tempDef) { - if ($myDefAssertion->getName() === $tempDef->getName()) { - $tempDef->setTainted(false); - } - - $equality = true; - } - - if ($myDefAssertion === $resolveTemporary) { - if ($myDefAssertion->getName() === $tempDef->getName()) { - $tempDef->setTainted(false); - } - + if ($myDefAssertion->getName() === $def->getName()) { $equality = true; break; } } - if ($equality && $typeAssertion !== "string") { $safe = true; } - if ($resolveTemporary->getCast() === MyDefinition::CAST_SAFE) { - $safe = true; - } - return $safe; } } diff --git a/package/src/progpilot/Analysis/CustomAnalysis.php b/package/src/progpilot/Analysis/CustomAnalysis.php index c9e34f4c..3887f94b 100644 --- a/package/src/progpilot/Analysis/CustomAnalysis.php +++ b/package/src/progpilot/Analysis/CustomAnalysis.php @@ -18,7 +18,7 @@ use progpilot\Objects\MyClass; use progpilot\Code\MyInstruction; use progpilot\Utils; -use progpilot\AbstractLayer\Analysis as AbstractAnalysis; +use progpilot\Helpers\Analysis as HelpersAnalysis; class CustomAnalysis { @@ -28,7 +28,7 @@ public static function disclosureOfInformation($context, $defs, $defassign) foreach ($customRules as $customRule) { if ($customRule->getType() === MyCustomRule::TYPE_VARIABLE && $customRule->getAction() === "ASSIGNMENT_DISCLOSE_HIGH_VALUE") { - $result = AbstractAnalysis::checkIfDefEqualDefRule($context, $defs, $customRule, $defassign); + $result = HelpersAnalysis::checkIfDefEqualDefRule($context, $defs, $customRule, $defassign); if ($result) { $hashedValue = $defassign->getLine(); @@ -54,29 +54,28 @@ public static function disclosureOfInformation($context, $defs, $defassign) return null; } - public static function defineObject($context, $myFuncorDef, $stackClass) + public static function defineObject($context, $instruction, $myFuncorDef, $myClassFound, $virtualReturnDef) { $customRules = $context->inputs->getCustomRules(); foreach ($customRules as $customRule) { if ($customRule->getType() === MyCustomRule::TYPE_VARIABLE && $customRule->getAction() === "DEFINE_OBJECT" && !is_null($customRule->getExtra())) { - $result = AbstractAnalysis::checkIfDefEqualDefRule( + $result = HelpersAnalysis::checkIfDefEqualDefRule( $context, null, $customRule, $myFuncorDef, - $stackClass + $myClassFound ); - + if ($result) { - $myClassNew = new MyClass( - $myFuncorDef->getLine(), - $myFuncorDef->getColumn(), - $customRule->getExtra() + return CustomAnalysis::returnObjectCreateObject( + $context, + $customRule, + $myFuncorDef, + $virtualReturnDef ); - - return $myClassNew; } } } @@ -84,63 +83,56 @@ public static function defineObject($context, $myFuncorDef, $stackClass) return null; } - public static function returnObjectCreateObject($context, $myExpr, $customRule, $myFuncorDef) + public static function returnObjectCreateObject($context, $customRule, $myFuncorDef, $virtualReturnDef) { - $defAssign = $myExpr->getAssignDef(); - - $objectId = $context->getObjects()->addObject(); - - $defAssign->addType(MyDefinition::TYPE_INSTANCE); - $defAssign->setObjectId($objectId); - - $myClass = $context->getClasses()->getMyClass($customRule->getExtra()); - - if (is_null($myClass)) { - $myClass = new MyClass( - $defAssign->getLine(), - $defAssign->getColumn(), - $customRule->getExtra() - ); + $myFakeInstance = null; + + // $this->foo->bar (we want to define an object on bar) + if ($myFuncorDef->isType(MyDefinition::TYPE_PROPERTY)) { + $myFakeInstance = $myFuncorDef; + $myFakeInstance->addType(MyDefinition::TYPE_INSTANCE); + $myFakeInstance->setClassName($customRule->getExtra()); + } elseif (!is_null($virtualReturnDef)) { + $myFakeInstance = $virtualReturnDef; + $myFakeInstance->addType(MyDefinition::TYPE_INSTANCE); + $myFakeInstance->setClassName($customRule->getExtra()); } - $context->getObjects()->addMyclassToObject($objectId, $myClass); - - $myBackDef = $myFuncorDef->getBackDef(); - if (!is_null($myBackDef)) { - $objectId = $myBackDef->getObjectId(); - $context->getObjects()->addMyclassToObject($objectId, $myClass); + if (!is_null($myFakeInstance)) { + HelpersAnalysis::createObject($context, $myFakeInstance); + return $myFakeInstance; } } - public static function returnObject($context, $myFuncorDef, $stackClass, $myExpr) + public static function returnObject($context, $myFuncorDef, $myClass, $instruction, $virtualReturnDef) { - if (!is_null($myExpr) && $myExpr->isAssign()) { - $customRules = $context->inputs->getCustomRules(); - foreach ($customRules as $customRule) { - if ($customRule->getType() === MyCustomRule::TYPE_FUNCTION + $customRules = $context->inputs->getCustomRules(); + foreach ($customRules as $customRule) { + if ($customRule->getType() === MyCustomRule::TYPE_FUNCTION && $customRule->getAction() === "DEFINE_OBJECT" && !is_null($customRule->getExtra())) { - $result = AbstractAnalysis::checkIfDefEqualDefRule( + $result = HelpersAnalysis::checkIfDefEqualDefRule( + $context, + null, + $customRule, + $myFuncorDef, + $myClass + ); + if ($result) { + return CustomAnalysis::returnObjectCreateObject( $context, - null, $customRule, $myFuncorDef, - $stackClass + $virtualReturnDef ); - if ($result) { - CustomAnalysis::returnObjectCreateObject( - $context, - $myExpr, - $customRule, - $myFuncorDef - ); - } } } } + + return null; } - public static function mustVerifyDefinition($context, $instruction, $myFunc, $stackClass = null) + public static function mustVerifyDefinition($context, $instruction, $myFunc, $myClass = null) { $customRules = $context->inputs->getCustomRules(); foreach ($customRules as $customRule) { @@ -149,32 +141,33 @@ public static function mustVerifyDefinition($context, $instruction, $myFunc, $st || $customRule->getAction() === "MUST_NOT_VERIFY_DEFINITION")) { $functionDefinition = $customRule->getDefinition(); - $result = AbstractAnalysis::checkIfFuncEqualMySpecify( - $context, - $functionDefinition, - $myFunc, - $stackClass - ); - if (!is_null($functionDefinition)) { + $result = HelpersAnalysis::checkIfFuncEqualMySpecify( + $context, + $functionDefinition, + $myFunc, + $myClass + ); if ($result) { - $params = $functionDefinition->getParameters(); - - if($myFunc->getNbParams() < $functionDefinition->getMinNbArgs() + if ($myFunc->getNbParams() < $functionDefinition->getMinNbArgs() || $myFunc->getNbParams() > $functionDefinition->getMaxNbArgs()) { $isValid = true; - } - else { + } else { $isValid = false; $params = $functionDefinition->getParameters(); + + // if one parameter is not valid all the rule is not valid foreach ($params as $param) { $isValid = false; $idParam = $param[0] - 1; $valuesParameter = $param[1]; - $validbydefault = $customRule->getAction() === "MUST_NOT_VERIFY_DEFINITION" ? !$param[2] : $param[2]; + $validbydefault = $param[2]; $isParameterFixed = $param[3]; - + $isParameterSufficient = $param[4]; + $isParameterFailIfNotVerifed = $param[5]; + $isParameterNotEquals = $param[6]; + if (!is_null($valuesParameter)) { if (!$instruction->isPropertyExist("argdef$idParam")) { $isValid = $validbydefault; @@ -189,52 +182,98 @@ public static function mustVerifyDefinition($context, $instruction, $myFunc, $st if (isset($valueParameter->is_array) && $valueParameter->is_array === true && isset($valueParameter->array_index)) { - $arrayfound = false; - if ($defArg->isType(MyDefinition::TYPE_COPY_ARRAY)) { - $copyArrays = $defArg->getCopyArrays(); - foreach ($copyArrays as $copyArray) { - foreach ($copyArray[0] as $copyIndex => $copyValue) { - if ($copyIndex === $valueParameter->array_index) { - $defLastKnownValues = $copyArray[1]->getLastKnownValues(); - $arrayfound = true; - - break 2; - } + + if ($defArg->getCurrentState()->isType(MyDefinition::TYPE_ARRAY)) { + foreach ($defArg->getCurrentState()->getArrayIndexes() as $arrayIndex) { + if ($arrayIndex->index === $valueParameter->array_index) { + $defLastKnownValues = + $arrayIndex->def->getCurrentState()->getLastKnownValues(); + $arrayfound = true; + + break; } } } if (!$arrayfound) { $isValid = $validbydefault; - break; + break 2; } } else { - $defLastKnownValues = $defArg->getLastKnownValues(); + $defLastKnownValues = $defArg->getCurrentState()->getLastKnownValues(); } if (count($defLastKnownValues) === 0) { $isValid = false; } - + + // we check all the values of a param + $validForAllValues = false; foreach ($defLastKnownValues as $lastKnownValue) { - if ($valueParameter->value === $lastKnownValue) { - $isValid = true; - break 2; + // if it's valid we continue + if (($valueParameter->value === $lastKnownValue + && !$isParameterNotEquals) + || ($valueParameter->value !== $lastKnownValue + && $isParameterNotEquals)) { + $validForAllValues = true; + } else { + // it's not valid we can break + $validForAllValues = false; + + // we just need to found one parameter that is equals + if ($isParameterNotEquals) { + break 2; + } else { + break; + } } } + + $isValid = $validForAllValues; + // we found a value of the param valid, we don't need to continue on + // other possible values of the rule + if ($isValid) { + break; + } + } + + // for must_verify definition + // * if one parameter is valid (respect conditions): + // * it can be enough (if sufficient) to valid the rule (no issue) + // * if one parameter is not valid: + // * the rule is not valid except if raise_if_not_verified set to false + // * the rule is valid if fixed (required) option is set + + // for must_not_verify definition + // * if one parameter is valid (respect conditions): + // * the rule is not valid except if raise_if_not_verified set to false + // * the rule is vali except if fixed (required) option is set + // * if one parameter is not valid (doesn't respect conditions): + // * it can be enough (if sufficient) to valid the rule (no issue) + + // one parameter is not valid and required + if (!$isValid + && $isParameterFixed) { + $isValid = true; + break; } - // one parameter is valid but MUST_NOT_VERIFY_DEFINITION + if ($customRule->getAction() === "MUST_NOT_VERIFY_DEFINITION") { $isValid = !$isValid; + } + + // one parameter is valid and enough + if ($isValid + && $isParameterSufficient) { + $isValid = true; break; } - - // one parameter is not valid and required + + // one parameter is not valid but should not fail and continue with other params if (!$isValid - && $isParameterFixed) { + && !$isParameterFailIfNotVerifed) { $isValid = true; - break; } // one parameter is not valid @@ -269,55 +308,61 @@ public static function mustVerifyDefinition($context, $instruction, $myFunc, $st } } - public static function mustVerifyCallFlow($context) + public static function mustVerifyCallFlow($context, $callgraph) { - if ($context->getAnalyzeHardrules()) { - $rulesVerifyCallFlow = []; - $customRules = $context->inputs->getCustomRules(); - foreach ($customRules as $customRule) { - if ($customRule->getType() === MyCustomRule::TYPE_SEQUENCE + $firstCustomFunctions = []; + $rulesVerifyCallFlow = []; + $customRules = $context->inputs->getCustomRules(); + foreach ($customRules as $customRule) { + if ($customRule->getType() === MyCustomRule::TYPE_SEQUENCE && $customRule->getAction() === "MUST_VERIFY_CALL_FLOW") { - $sequence = $customRule->getSequence(); + $sequence = $customRule->getSequence(); - $customRule->setCurrentOrderNumber(0); - $rulesVerifyCallFlow[] = $customRule; + $customRule->setCurrentOrderNumber(0); + $rulesVerifyCallFlow[] = $customRule; + if (is_array($sequence) && !empty($sequence)) { + $firstCustomFunctions[] = $sequence[0]; foreach ($sequence as $customFunction) { $customFunction->setOrderNumberReal(-1); } } } + } - $function = $context->getFunctions()->getFunction("{main}"); - if (!is_null($function)) { - $context->outputs->callgraph->addNode($function, null); + foreach ($firstCustomFunctions as $firstCustomFunction) { + $functions = $context->getFunctions()->getAllFunctions($firstCustomFunction->getName()); + foreach ($functions as $function) { + if (!is_null($function)) { + $callgraph->addNode($function, null); - foreach ($function->getBlocks() as $firstMyBlock) { - $calls = $context->outputs->callgraph->getCalls($firstMyBlock); - if (!is_null($calls)) { - foreach ($calls as $call) { - $context->outputs->callgraph->addEdge($function, null, $call[0], $call[1]); + foreach ($function->getBlocks() as $firstMyBlock) { + $calls = $callgraph->getCalls($firstMyBlock); + if (!is_null($calls)) { + foreach ($calls as $call) { + $callgraph->addEdge($function, null, $call[0], $call[1]); + } } - } - break; - } - - $NodeCG = new NodeCG( - $function->getName(), - $function->getLine(), - $function->getColumn(), - $function->getSourceMyFile()->getName(), - null - ); - $nodes = $context->outputs->callgraph->getNodes(); + break; + } - if (array_key_exists($NodeCG->getId(), $nodes)) { - $depthFirstSearch = new DepthFirstSearch( - $nodes, - new DFSVisitor($context, $rulesVerifyCallFlow) + $NodeCG = new NodeCG( + $function->getName(), + $function->getLine(), + $function->getColumn(), + $function->getSourceMyFile()->getName(), + null ); - $depthFirstSearch->init($nodes[$NodeCG->getId()]); + $nodes = $callgraph->getNodes(); + + if (array_key_exists($NodeCG->getId(), $nodes)) { + $depthFirstSearch = new DepthFirstSearch( + $nodes, + new DFSVisitor($context, $rulesVerifyCallFlow) + ); + $depthFirstSearch->init($nodes[$NodeCG->getId()]); + } } } } diff --git a/package/src/progpilot/Analysis/FuncAnalysis.php b/package/src/progpilot/Analysis/FuncAnalysis.php index ec1cc803..f947859e 100644 --- a/package/src/progpilot/Analysis/FuncAnalysis.php +++ b/package/src/progpilot/Analysis/FuncAnalysis.php @@ -10,64 +10,91 @@ namespace progpilot\Analysis; +use progpilot\Objects\MyFunction; use progpilot\Objects\MyDefinition; use progpilot\Code\Opcodes; use progpilot\Code\MyInstruction; +use progpilot\Helpers\Analysis as HelpersAnalysis; + class FuncAnalysis { - public static function funccallAfter($context, $data, $myFuncCall, $myFunc, $arrFuncCall, $instruction, $opAfter) - { - $exprReturn = $instruction->getProperty(MyInstruction::EXPR); - - if (!is_null($myFunc)) { - $defsReturn = $myFunc->getReturnDefs(); - foreach ($defsReturn as $defReturn) { - if (($arrFuncCall !== false - && $defReturn->isType(MyDefinition::TYPE_ARRAY) - && $defReturn->getArrayValue() === $arrFuncCall) - || ($arrFuncCall === false && !$defReturn->isType(MyDefinition::TYPE_ARRAY))) { - $copyDefReturn = $defReturn; - - // copydefreturn = return $defReturn; - $copyDefReturn->setExpr($exprReturn); - $defReturn->setCast($myFuncCall->getCastReturn()); - // exprreturn = $def = exprreturn(funccall()); - $exprReturn->addDef($copyDefReturn); - - $expr = $copyDefReturn->getExpr(); - if ($expr->isAssign()) { - $defAssign = $expr->getAssignDef(); - //TaintAnalysis::setTainted($copyDefReturn, $defAssign, $expr); - - if (ResolveDefs::getVisibilityFromInstances($context, $data, $defAssign)) { - ValueAnalysis::copyValues($copyDefReturn, $defAssign); - TaintAnalysis::setTainted($copyDefReturn->isTainted(), $defAssign, $expr); - } - - ArrayAnalysis::copyArrayFromDef( - $defReturn, - $arrFuncCall, - $defAssign, - $defAssign->getArrayValue() - ); - } - } - } - - if ($opAfter->getOpcode() === Opcodes::DEFINITION) { - $copyTab = $opAfter->getProperty(MyInstruction::DEF); + public static function funccallAfter( + $myFunc, + $context, + $myClass, + $myFuncCall, + $instruction + ) { + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $opInformation = $context->getCurrentFunc()->getOpInformation($resultid); + + $myFuncReturn = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $myFuncCall->getLine(), + $myFuncCall->getColumn(), + $myFuncCall->getName()."_return" + ); + + $opInformation["chained_results"][] = $myFuncReturn; + + \progpilot\Analysis\CustomAnalysis::defineObject( + $context, + $instruction, + $myFuncCall, + $myClass, + $myFuncReturn + ); + + \progpilot\Analysis\CustomAnalysis::returnObject( + $context, + $myFuncCall, + $myClass, + $instruction, + $myFuncReturn + ); + + $opInformationValid = TaintAnalysis::funccallValidator( + $context, + $myFunc, + $myClass, + $instruction + ); + + TaintAnalysis::funccallSanitizer( + $myFunc, + $context, + $myClass, + $instruction, + $myFuncReturn + ); + + TaintAnalysis::funccallSource($context, $myClass, $instruction, $myFuncReturn); + + if (is_null($myFunc)) { + ResolveDefs::funccallReturnValues($myFuncCall, $instruction, $myFuncReturn); + } + SecurityAnalysis::funccall($context, $instruction, $myClass); - $originalTabs = $myFunc->getReturnDefs(); + if (!is_null($opInformationValid)) { + $opInformation["condition_defs"] = $opInformationValid["condition_defs"]; + $opInformation["valid_when_returning"] = $opInformationValid["valid_when_returning"]; + } - foreach ($originalTabs as $originalTab) { - ArrayAnalysis::copyArrayFromDef($originalTab, $arrFuncCall, $copyTab, $copyTab->getArrayValue()); + // could be merged with funccallclass (new instance) + if (!is_null($myFunc)) { + if ($myFuncCall->getName() !== "__construct" && !empty($myFunc->getReturnDefs())) { + foreach ($myFunc->getReturnDefs() as $returnDef) { + $opInformation["chained_results"][] = $returnDef; } } } + + $context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); } - public static function funccallBefore($context, $data, $myFunc, $myFuncCall, $instruction) + public static function funccallBefore($myFunc, $instruction) { $nbParams = 0; $params = $myFunc->getParams(); @@ -75,42 +102,15 @@ public static function funccallBefore($context, $data, $myFunc, $myFuncCall, $in foreach ($params as $param) { if ($instruction->isPropertyExist("argdef$nbParams")) { $defArg = $instruction->getProperty("argdef$nbParams"); - $exprArg = $instruction->getProperty("argexpr$nbParams"); - - $newParam = clone $param; - $myFuncCall->addParam($newParam); - - $newParam->setLastKnownValues($defArg->getLastKnownValues()); - ArrayAnalysis::copyArrayFromDef($defArg, $defArg->getArrayValue(), $newParam, false); - - $param->setCopyArrays($newParam->getCopyArrays()); - $param->setLastKnownValues($newParam->getLastKnownValues()); - $param->setType($newParam->getType()); - $expr = $param->getExpr(); - - if ($defArg->isTainted()) { - // useful just for inside the function - TaintAnalysis::setTainted($defArg->isTainted(), $param, $exprArg); - - if (!is_null($expr) && $expr->isAssign()) { - $defAssign = $expr->getAssignDef(); - - if (ResolveDefs::getVisibilityFromInstances($context, $data, $defAssign)) { - TaintAnalysis::setTainted($defArg->isTainted(), $defAssign, $expr); - ValueAnalysis::copyValues($defArg, $defAssign); - } - } - } + $param->setParamToArg($defArg); + $defArg->setArgToParam($param); - // useful for copy all tainted array progpilot - ValueAnalysis::copyValues($defArg, $param); + $state = $defArg->createState(); + $defArg->assignStateToBlockId($state->getId(), $param->getBlockId()); $nbParams ++; - unset($defArg); } } - - unset($params); } } diff --git a/package/src/progpilot/Analysis/IncludeAnalysis.php b/package/src/progpilot/Analysis/IncludeAnalysis.php index f166576b..8e3bd69a 100644 --- a/package/src/progpilot/Analysis/IncludeAnalysis.php +++ b/package/src/progpilot/Analysis/IncludeAnalysis.php @@ -22,16 +22,15 @@ use progpilot\Code\Opcodes; use progpilot\Code\MyInstruction; -use progpilot\AbstractLayer\Analysis as AbstractAnalysis; +use progpilot\Helpers\Analysis as HelpersAnalysis; +use progpilot\Helpers\State as HelpersState; use progpilot\Utils; +use function DeepCopy\deep_copy; + class IncludeAnalysis { - public function __construct() - { - } - public static function isCircularInclude($myFile) { $includedMyFile = $myFile->getIncludedFromMyfile(); @@ -46,10 +45,9 @@ public static function isCircularInclude($myFile) return false; } - public static function funccall($context, $defs, $blocks, $instruction, $code, $index) + public static function funccall($context, $blocks, $defs, $instruction) { $funcName = $instruction->getProperty(MyInstruction::FUNCNAME); - $arrFuncCall = $instruction->getProperty(MyInstruction::ARR); $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); // require, require_once ... already handled in transform Expr/include_ @@ -60,32 +58,43 @@ public static function funccall($context, $defs, $blocks, $instruction, $code, $ $atLeastOneIncludeResolved = false; $myDefArg = $instruction->getProperty("argdef0"); - - if (count($myDefArg->getLastKnownValues()) !== 0) { - foreach ($myDefArg->getLastKnownValues() as $lastKnownValue) { + if (count($myDefArg->getCurrentState()->getLastKnownValues()) !== 0) { + foreach ($myDefArg->getCurrentState()->getLastKnownValues() as $lastKnownValue) { $realFile = false; - // else it's maybe a relative path - $file = $context->getPath()."/".$lastKnownValue; - $realFile = realpath($file); + if (strlen($lastKnownValue) > 0 + && (substr($lastKnownValue, 0, 1) !== DIRECTORY_SEPARATOR + || preg_match("/^[a-bA-B]*:/", $lastKnownValue) === 0)) { + $file = $context->getPath().DIRECTORY_SEPARATOR.$lastKnownValue; + $realFile = realpath($file); + } // if $lastKnownValue is a absolute path to the file : // /home/dev/file.php if (!$realFile) { $realFile = realpath($lastKnownValue); } - if (!$realFile) { $continueInclude = false; + // check is the file is manually set by the developer with json $myInclude = $context->inputs->getIncludeByLocation( - $context->getCurrentLine(), - $context->getCurrentColumn(), + $myFuncCall->getLine(), + $myFuncCall->getColumn(), $myFuncCall->getSourceMyFile()->getName() ); if (!is_null($myInclude)) { - $nameIncludedFile = realpath($myInclude->getValue()); + $manuallyIncludedFile = $myInclude->getValue(); + if (strlen($manuallyIncludedFile) > 0 + && (substr($manuallyIncludedFile, 0, 1) !== DIRECTORY_SEPARATOR + || preg_match("/^[a-bA-B]*:/", $manuallyIncludedFile) === 0)) { + $manuallyIncludedFile = + $context->getPath().DIRECTORY_SEPARATOR.$manuallyIncludedFile; + } + + $nameIncludedFile = realpath($manuallyIncludedFile); + if ($nameIncludedFile) { $continueInclude = true; } @@ -94,141 +103,226 @@ public static function funccall($context, $defs, $blocks, $instruction, $code, $ $continueInclude = true; $nameIncludedFile = $realFile; } - + if ($continueInclude) { $atLeastOneIncludeResolved = true; - $arrayIncludes = $context->getArrayIncludes(); - $arrayRequires = $context->getArrayRequires(); - - if ((!in_array($nameIncludedFile, $arrayIncludes, true) && $typeInclude === 2) + $arrayIncludes = &$context->getArrayIncludes(); + $arrayRequires = &$context->getArrayRequires(); + + if (!$context->inputs->isExcludedFile($nameIncludedFile) + && ((!in_array($nameIncludedFile, $arrayIncludes, true) && $typeInclude === 2) || (!in_array($nameIncludedFile, $arrayRequires, true) && $typeInclude === 4) - || ($typeInclude === 1 || $typeInclude === 3)) { - $myFile = new MyFile( + || ($typeInclude === 1 || $typeInclude === 3))) { + $myFileIncluded = new MyFile( $nameIncludedFile, $myFuncCall->getLine(), $myFuncCall->getColumn() ); - $myFile->setIncludedFromMyfile($myFuncCall->getSourceMyFile()); - if (!IncludeAnalysis::isCircularInclude($myFile)) { + $myFileIncluded->setIncludedFromMyfile(clone $context->getCurrentMyFile()); + if (!IncludeAnalysis::isCircularInclude($myFileIncluded)) { + // include once if ($typeInclude === 2) { $arrayIncludes[] = $nameIncludedFile; } + // require once if ($typeInclude === 4) { $arrayRequires[] = $nameIncludedFile; } - $contextInclude = clone $context; - - $contextInclude->resetInternalValues(); - - // not need to propagate files already included to the current analyzed file - // because of clone context and reset values that don't - // ie : $contextInclude->set_array_includes($arrayIncludes); ect - $contextInclude->inputs->setFile($nameIncludedFile); - $contextInclude->inputs->setCode(null); - $contextInclude->setCurrentMyfile($myFile); - //$contextInclude->setOutputs(new \progpilot\Outputs\MyOutputs); - $contextInclude->outputs->resetRepresentations(); - - $mainFunctionSave = $context->getFunctions()->getFunction("{main}"); - $saveMain = $contextInclude->getFunctions()->delFunction("{main}"); - $analyzerInclude = new \progpilot\Analyzer; - $analyzerInclude->runInternalPhp( - $contextInclude, - $defs->getOutMinusKill($myFuncCall->getBlockId()) + $saveCurrentMyfile = $context->getCurrentMyfile(); + $saveCurrentOp = $context->getCurrentOp(); + $saveCurrentBlock = $context->getCurrentBlock(); + $saveCurrentLine = $context->getCurrentLine(); + $saveCurrentColumn = $context->getCurrentColumn(); + $saveCurrentFunc = $context->getCurrentFunc(); + $saveCurrentCode = $context->getCurrentMyCode(); + + $context->setCurrentMyfile($myFileIncluded); + + // could be analyzed with namespaces pre-includes, better to check + if (!$context->isFileDataAnalyzed($nameIncludedFile)) { + $context->inputs->setFile($nameIncludedFile); + $analyzerInclude->computeDataFlow($context); + + // the file is now data analyzed + $context->addDataAnalyzedFile($nameIncludedFile); + + $currentFile = $myFuncCall->getSourceMyFile()->getName(); + $context->inputs->setFile($currentFile); + $context->setPath(dirname($currentFile)); + } + + $fileNameHash = hash("sha256", $context->getCurrentMyfile()->getName()); + $mainInclude = $context->getFunctions()->getFunction( + "{main}", + "function", + $fileNameHash ); - if (count($contextInclude->outputs->currentIncludesFile) > 0) { - foreach ($contextInclude->outputs->currentIncludesFile as $includeFile) { - $context->currentIncludesFile[] = $includeFile; + // we include defs of the current file to the included file + $saveMyFile = []; + $defsIncluded = []; + if (!is_null($mainInclude)) { + foreach ($mainInclude->getLastBlockIds() as $lastBlockId) { + $lastMyBlockConstuctor = + $mainInclude->getBlockById($lastBlockId); + if (!is_null($context->getCurrentBlock()) + && !is_null($lastMyBlockConstuctor)) { + $saveCurrentBlock->addVirtualParent($lastMyBlockConstuctor); + } } - } - - if (!is_null($contextInclude->outputs->getResults())) { - foreach ($contextInclude->outputs->getResults() as $resultInclude) { - $context->outputs->addResult($resultInclude); + + // we change the source of defs nevermind the file is really analyzed or not + foreach ($mainInclude->getDefs()->getDefs() as $defOfMainArray) { + foreach ($defOfMainArray as $defOfMain) { + $defOfMain->setSourceMyFile($myFileIncluded); + } } - } - $context->setCurrentNbDefs($contextInclude->getCurrentNbDefs()); + // already visited from include or not? + if (!$mainInclude->isVisitedFromInclude()) { + $currentFileDefs = $defs->getOutMinusKill($myFuncCall->getBlockId()); + if (!empty($currentFileDefs)) { + foreach ($currentFileDefs as $defToInclude) { + if ($defToInclude->getLine() < $myFuncCall->getLine() + || ($defToInclude->getLine() === $myFuncCall->getLine() + && $defToInclude->getColumn() < $myFuncCall->getColumn())) { + $saveMyFile[$defToInclude->getId()] + = $defToInclude->getSourceMyFile(); + $defsIncluded[] = $defToInclude; + + $tmp = clone $defToInclude->getSourceMyFile(); + $tmp->setIncludedToMyfile($myFileIncluded); + $defToInclude->setSourceMyFile($tmp); + + $mainInclude->getDefs()->addDef( + $defToInclude->getName(), + $defToInclude + ); + $mainInclude->getDefs()->addGen( + $mainInclude->getFirstBlockId(), + $defToInclude + ); + } + } - $mainInclude = $contextInclude->getFunctions()->getFunction("{main}"); + $mainInclude->getDefs()->computeKill($mainInclude->getFirstBlockId()); + $mainInclude->getDefs()->reachingDefs($mainInclude->getBlocks()); + } + + // we analyze the main of the function again + // we can have tainted def included and vice-versa + $mainInclude->setIsVisited(false); + $mainInclude->setIsVisitedFromInclude(true); + + $saveCallBacks = $context->getCallStack(); + $context->resetCallStack(); + + $analyzerInclude->runFunctionAnalysis($context, $mainInclude, false); + + $context->setCurrentOp($saveCurrentOp); + $context->setCurrentBlock($saveCurrentBlock); + $context->setCurrentLine($saveCurrentLine); + $context->setCurrentColumn($saveCurrentColumn); + $context->setCurrentFunc($saveCurrentFunc); + $context->setCurrentMyCode($saveCurrentCode); + $context->setCurrentMyfile($saveCurrentMyfile); + + $context->setCallStack($saveCallBacks); + + foreach ($defsIncluded as $defIncluded) { + if (!is_null($saveMyFile[$defIncluded->getId()])) { + $defIncluded->setSourceMyFile($saveMyFile[$defIncluded->getId()]); + } + } - $defsOutputIncludedFinal = []; - if (!is_null($mainInclude)) { - FuncAnalysis::funccallAfter( - $contextInclude, - $defs->getOutMinusKill($myFuncCall->getBlockId()), - $mainInclude, - $mainInclude, - $arrFuncCall, - $instruction, - $code[$index + 3] - ); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $opInformation = $context->getCurrentFunc()->getOpInformation($resultid); + $opInformation["chained_results"] = $mainInclude->getReturnDefs(); + $context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + } + // even if we don't visit again the function + // we are interested by the return def + // to include them in the file performing the include + // it's an approximation ... + $defsOutputIncludedFinal = []; if (!is_null($mainInclude->getDefs())) { - $defsMainReturn = - $mainInclude->getDefs()->getDefRefByName("{main}_return"); - - if (is_array($defsMainReturn)) { - foreach ($defsMainReturn as $defMainReturn) { - $defsOutputIncluded = $mainInclude->getDefs()->getOutMinusKill( - $defMainReturn->getBlockId() - ); - if (!is_null($defsOutputIncluded)) { - foreach ($defsOutputIncluded as $defOutputIncluded) { - if (!in_array( - $defOutputIncluded, - $defsOutputIncludedFinal, - true - )) { - $defsOutputIncludedFinal[] = $defOutputIncluded; - } - } + $defsOutputIncluded = []; + foreach ($mainInclude->getLastBlockIds() as $lastBlockId) { + $defsOutputIncluded = array_merge( + $mainInclude->getDefs()->getOutMinusKill($lastBlockId), + $defsOutputIncluded + ); + } + + if (!is_null($defsOutputIncluded)) { + foreach ($defsOutputIncluded as $defOutputIncludedId) { + if (!in_array( + $defOutputIncludedId, + $defsOutputIncludedFinal, + true + ) && !in_array($defOutputIncludedId, $defsIncluded, true)) { + $defsOutputIncludedFinal[] = $defOutputIncludedId; } } } } - } - // for all class found, we resolve previous seen objects of this class - $myClassesIncluded = $contextInclude->getClasses()->getListClasses(); - foreach ($myClassesIncluded as $myClassIncluded) { - $funcCallBack = "Callbacks::modifyMyclassOfObject"; - AbstractAnalysis::forAllobjects($context, $funcCallBack, $myClassIncluded); - } + // for all class found, we resolve previous seen objects of this class + $myClassesIncluded = $context->getClasses()->getListClasses(); + foreach ($myClassesIncluded as $myClassIncluded) { + $funcCallBack = "Callbacks::modifyMyclassOfObject"; + HelpersAnalysis::forAllobjects($context, $funcCallBack, $myClassIncluded); + } - $newDefs = false; - if (count($defsOutputIncludedFinal) > 0) { - foreach ($defsOutputIncludedFinal as $defOutputIncludedFinal) { - $ret1 = $defs->addDef( - $defOutputIncludedFinal->getName(), - $defOutputIncludedFinal - ); - $ret2 = $defs->addGen( - $myFuncCall->getBlockId(), - $defOutputIncludedFinal - ); + $newDefs = false; + if (!empty($defsOutputIncludedFinal)) { + foreach ($defsOutputIncludedFinal as $defOutputIncludedFinal) { + $ret1 = $defs->addDef( + $defOutputIncludedFinal->getName(), + $defOutputIncludedFinal + ); + $ret2 = $defs->addGen( + $myFuncCall->getBlockId(), + $defOutputIncludedFinal + ); + + $newDefs = true; + } + } - $newDefs = true; + if ($newDefs) { + $defs->computeKill($myFuncCall->getBlockId()); + $defs->reachingDefs($blocks); + + $defsOutputIncluded = $defs->getOutMinusKill( + $myFuncCall->getBlockId() + ); } - } - if ($newDefs) { - $defs->computeKill($context, $myFuncCall->getBlockId()); - $defs->reachingDefs($blocks); + HelpersState::blockSwitching($context, $context->getCurrentFunc()); } + + + $context->setCurrentOp($saveCurrentOp); + $context->setCurrentBlock($saveCurrentBlock); + $context->setCurrentLine($saveCurrentLine); + $context->setCurrentColumn($saveCurrentColumn); + $context->setCurrentFunc($saveCurrentFunc); + $context->setCurrentMyCode($saveCurrentCode); + $context->setCurrentMyfile($saveCurrentMyfile); } } } } } - if (!$atLeastOneIncludeResolved && $context->outputs->getResolveIncludes()) { + if (!$atLeastOneIncludeResolved && $context->outputs->getWriteIncludeFailures()) { $myFileTemp = new MyFile( $myFuncCall->getSourceMyFile()->getName(), $myFuncCall->getLine(), diff --git a/package/src/progpilot/Analysis/ResolveDefs.php b/package/src/progpilot/Analysis/ResolveDefs.php index 113b2d45..d41a5b05 100644 --- a/package/src/progpilot/Analysis/ResolveDefs.php +++ b/package/src/progpilot/Analysis/ResolveDefs.php @@ -20,6 +20,7 @@ use progpilot\Code\Opcodes; use progpilot\Code\MyInstruction; +use progpilot\Objects\MyProperty; use progpilot\Objects\MyClass; use progpilot\Objects\MyOp; use progpilot\Objects\MyInstance; @@ -28,339 +29,90 @@ use progpilot\Dataflow\Definitions; +use progpilot\Helpers\Analysis as HelpersAnalysis; + class ResolveDefs { - public static function funccallReturnValues($context, $myFuncCall, $instruction, $myCode, $index) + public static function funccallReturnValues($myFuncCall, $instruction, $virtualReturnDef) { if ($myFuncCall->getName() === "dirname") { - $codes = $myCode->getCodes(); - if (isset($codes[$index + 2]) && $codes[$index + 2]->getOpcode() === Opcodes::END_ASSIGN) { - $instructionDef = $codes[$index + 3]; - $myDefReturn = $instructionDef->getProperty(MyInstruction::DEF); - - if ($instruction->isPropertyExist("argdef0")) { - $defarg = $instruction->getProperty("argdef0"); - foreach ($defarg->getLastKnownValues() as $knownValue) { - $myDefReturn->addLastKnownValue(dirname($knownValue)); - } + if ($instruction->isPropertyExist("argdef0")) { + $defarg = $instruction->getProperty("argdef0"); + + foreach ($defarg->getCurrentState()->getLastKnownValues() as $knownValue) { + $virtualReturnDef->getCurrentState()->addLastKnownValue(dirname($knownValue)); } } } } - public static function propertyClass($context, $data, $mydef) + public static function funccallClass($context, $data, $myFuncCall, $instruction) { + $varid = $instruction->getProperty(MyInstruction::VARID); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $i = 0; $classStackName = []; - $currentFunc = $context->getCurrentFunc(); - if ($mydef->isType(MyDefinition::TYPE_PROPERTY)) { - $properties = $mydef->property->getProperties(); - $tmpProperties = []; - - while (true) { - $myDefTmp = new MyDefinition( - $mydef->getLine(), - $mydef->getColumn(), - $mydef->getName() - ); - $myDefTmp->setBlockId($mydef->getBlockId()); - $myDefTmp->setSourceMyFile($mydef->getSourceMyFile()); - $myDefTmp->property->setProperties($tmpProperties); - $myDefTmp->addType(MyDefinition::TYPE_PROPERTY); - $myDefTmp->setId($mydef->getId()); - - $classStackName[$i] = []; - if ($i === 0) { - $instances = ResolveDefs::selectInstances( - $context, - $data->getOutMinusKill($mydef->getBlockId()), - $myDefTmp - ); - } else { - $instances = ResolveDefs::selectProperties( - $context, - $data->getOutMinusKill($mydef->getBlockId()), - $myDefTmp - ); - } - - // if we have document.url but document hasn't been defined - // we use the original name of the instance (document) - if (count($instances) === 0) { - $myClassNew = \progpilot\Analysis\CustomAnalysis::defineObject( - $context, - $mydef, - $classStackName - ); - - if (!is_null($myClassNew)) { - $mydef->addType(MyDefinition::TYPE_INSTANCE); - $idObject = $context->getObjects()->addObject(); - $mydef->setObjectId($idObject); - $context->getObjects()->addMyclassToObject($idObject, $myClassNew); - } - - $classStackName[$i][] = $mydef; - } else { - foreach ($instances as $instance) { - if ($instance->isType(MyDefinition::TYPE_INSTANCE)) { - $objectId = $instance->getObjectId(); - $myClass = $context->getObjects()->getMyClassFromObject($objectId); - - if (!is_null($myClass) && isset($properties[$i])) { - $property = $myClass->getProperty($properties[$i]); - - // if property doesn't exist by default it's a public property - if (is_null($property) || (!is_null($property) - && (ResolveDefs::getVisibility($myDefTmp, $property, $currentFunc)))) { - $myClassNew = \progpilot\Analysis\CustomAnalysis::defineObject( - $context, - $myClass, - $classStackName - ); - - if (!is_null($myClassNew)) { - $idObject = $context->getObjects()->addObject(); - $instance->setObjectId($idObject); - $context->getObjects()->addMyclassToObject($idObject, $myClassNew); - } - - $classStackName[$i][] = $instance; - } - } else { - $classStackName[$i][] = $instance; - } - } - } - } - - if (!isset($properties[$i])) { - break; - } - - $tmpProperties[] = $properties[$i]; - - $i ++; - - if (!isset($properties[$i])) { - break; - } - } - } + if ($myFuncCall->getName() === "__construct") { + $myFakeInstance = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $myFuncCall->getLine(), + $myFuncCall->getColumn(), + "return__construct" + ); + $myFakeInstance->addType(MyDefinition::TYPE_INSTANCE); + $myFakeInstance->getCurrentState()->addType(MyDefinition::TYPE_INSTANCE); + $myFakeInstance->setClassName($myFuncCall->getInstanceClassName()); - return $classStackName; - } + HelpersAnalysis::createObject($context, $myFakeInstance); - public static function funccallClass($context, $data, $myFuncCall) - { - $i = 0; - $classStackName = []; + $opInformation["chained_results"][] = $myFakeInstance; + $context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); - if ($myFuncCall->getName() === "__construct") { - $classStackName[$i][] = $myFuncCall->getBackDef(); + $classStackName[$i][] = $myFakeInstance->getCurrentState(); } elseif ($myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD)) { - $properties = $myFuncCall->getBackDef()->property->getProperties(); - - $tmpProperties = []; + $myDefTmp = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentFunc()->getSourceMyFile(), + $myFuncCall->getLine(), + $myFuncCall->getColumn(), + $myFuncCall->getNameInstance() + ); - while (true) { - $propValue = []; + $myDefTmp->addType(MyDefinition::TYPE_PROPERTY); + // we don't want the backdef but the original instance + + $classStackName[$i] = []; + $previousChainedResults = $context->getCurrentFunc()->getOpInformation($varid); - $myDefTmp = new MyDefinition( - $myFuncCall->getLine(), - $myFuncCall->getColumn(), - $myFuncCall->getNameInstance() + if (!is_null($previousChainedResults) + && isset($previousChainedResults["chained_results"])) { + $instances = $previousChainedResults["chained_results"]; + } else { + $instances = ResolveDefs::selectDefinitions( + $context, + $data, + $myDefTmp ); - $myDefTmp->setBlockId($myFuncCall->getBlockId()); - $myDefTmp->setSourceMyFile($myFuncCall->getSourceMyFile()); - $myDefTmp->property->setProperties($tmpProperties); - $myDefTmp->addType(MyDefinition::TYPE_PROPERTY); - $myDefTmp->setId($myFuncCall->getBackDef()->getId() - 1); - // we don't want the backdef but the original instance - - $classStackName[$i] = []; - if ($i === 0) { - $instances = ResolveDefs::selectInstances( - $context, - $data, - $myDefTmp - ); - } else { - $instances = ResolveDefs::selectProperties($context, $data, $myDefTmp); - } - - foreach ($instances as $instance) { - if ($instance->isType(MyDefinition::TYPE_INSTANCE)) { - $classStackName[$i][] = $instance; - } - } - - if (!isset($properties[$i])) { - break; - } - - $tmpProperties[] = $properties[$i]; - - $i ++; } - } - - return $classStackName; - } - - public static function instanceBuildBack($context, $data, $myFunc, $myClass, $myFuncCall, $visibility) - { - if (!is_null($myFunc) && $myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - if ($myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD)) { - $myBackDef = $myFuncCall->getBackDef(); - //$myClass = $myFunc->getMyClass(); - $method = $myClass->getMethod($myFuncCall->getName()); - $newMyBackMyClass = $context->getObjects()->getMyClassFromObject($myBackDef->getObjectId()); - - if (is_null($newMyBackMyClass)) { - $newMyBackMyClass = new MyClass( - $myClass->getLine(), - $myClass->getColumn(), - $myClass->getName() - ); - - $newMyBackMyClass->setExtendsOf($myClass->getExtendsOf()); - $context->getObjects()->addMyclassToObject($myBackDef->getObjectId(), $newMyBackMyClass); - } - - $copyMyClass = clone $myClass; - - foreach ($copyMyClass->getProperties() as $property) { - $myDef = new MyDefinition($myFunc->getLastLine() + 1, $myFunc->getLastColumn(), "this"); - - $myDef->addType(MyDefinition::TYPE_PROPERTY); - $myDef->property->setProperties($property->property->getProperties()); - $myDef->setBlockId($myFunc->getLastBlockId()); - $myDef->setSourceMyFile($myBackDef->getSourceMyFile()); - - $newProperty = $newMyBackMyClass->getProperty($property->property->getProperties()[0]); - if (is_null($newProperty)) { - $newMyBackMyClass->addProperty($property); - $newProperty = $property; + foreach ($instances as $instance) { + if ($instance->isType(MyDefinition::TYPE_PROPERTY)) { + $state = $instance->getState($context->getCurrentBlock()->getId()); + if (!is_null($state) && $state->isType(MyDefinition::TYPE_INSTANCE)) { + $classStackName[$i][] = $state; } - - $propertiesInside = ResolveDefs::selectProperties( - $context, - $myFunc->getDefs()->getOutMinusKill($myDef->getBlockId()), - $myDef - ); - - foreach ($propertiesInside as $propertyInside) { - ValueAnalysis::copyValues($propertyInside, $newProperty); - - TaintAnalysis::setTainted( - $propertyInside->isTainted(), - $newProperty, - $propertyInside->getTaintedByExpr() - ); - - if ($propertyInside->isSanitized()) { - $newProperty->setSanitized(true); - foreach ($propertyInside->getTypeSanitized() as $typeSanitized) { - $newProperty->addTypeSanitized($typeSanitized); - } - } - - if ($propertyInside->isType(MyDefinition::TYPE_INSTANCE) - && !$newProperty->isType(MyDefinition::TYPE_INSTANCE)) { - $newProperty->addType(MyDefinition::TYPE_INSTANCE); - $newProperty->setObjectId($propertyInside->getObjectId()); - $newProperty->setClassName($propertyInside->getClassName()); - - $myClass = $context->getObjects()->getMyClassFromObject($propertyInside->getObjectId()); - } + } else { + if ($instance->getCurrentState()->isType(MyDefinition::TYPE_INSTANCE)) { + $classStackName[$i][] = $instance->getCurrentState(); } - - $newProperty->setName($myBackDef->getName()); - - ArrayAnalysis::copyArray( - $context, - $myFunc->getDefs()->getOutMinusKill($myDef->getBlockId()), - $myDef, - $myDef->getArrayValue(), - $newProperty, - $newProperty->getArrayValue() - ); - } - - foreach ($copyMyClass->getMethods() as $method) { - $newMethod = clone $method; - $newMyBackMyClass->addMethod($newMethod); } } - } elseif (is_null($myFunc) - && $myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD) - && !is_null($myClass) - && $visibility) { - // query return object custom rule - //$query = $this->db->query("YOUR QUERY"); - - // backdef instance should be update with the correct object - // $row = $query->result(); - $myBackDef = $myFuncCall->getBackDef(); - $myClassTmp = $context->getClasses()->getMyClass($myClass->getName()); - - if (is_null($myClassTmp)) { - $myClassTmp = new MyClass( - $myClass->getLine(), - $myClass->getColumn(), - $myClass->getName() - ); - } - - $context->getObjects()->addMyclassToObject($myBackDef->getObjectId(), $myClassTmp); } - } - - public static function instanceBuildThis($context, $data, $objectId, $myClass, $myFunc, $myFuncCall) - { - if (!is_null($myFunc) && $myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD)) { - //$copyMyClass = clone $myClass; - //<= It was good ? clone for backdef and thisdef or only one of these two ? - $copyMyClass = $myClass; - - foreach ($copyMyClass->getProperties() as $property) { - $myDef = new MyDefinition( - $myFuncCall->getLine(), - $myFuncCall->getColumn(), - $myFuncCall->getNameInstance() - ); - $myDef->addType(MyDefinition::TYPE_PROPERTY); - $myDef->property->setProperties($property->property->getProperties()); - $myDef->setBlockId($myFuncCall->getBlockId()); - $myDef->setSourceMyFile($myFuncCall->getSourceMyFile()); - $myDef->setId($myFuncCall->getId()); - - $defsFound = ResolveDefs::selectProperties($context, $data, $myDef, true); - foreach ($defsFound as $defFound) { - if ($defFound->isType(MyDefinition::TYPE_COPY_ARRAY)) { - $property->setCopyArrays($defFound->getCopyArrays()); - $property->addType(MyDefinition::TYPE_COPY_ARRAY); - } - - ValueAnalysis::copyValues($defFound, $property); - TaintAnalysis::setTainted($defFound->isTainted(), $property, $defFound->getTaintedByExpr()); - - if ($defFound->isSanitized()) { - $property->setSanitized(true); - foreach ($defFound->getTypeSanitized() as $typeSanitized) { - $property->addTypeSanitized($typeSanitized); - } - } - } - - $property->setName("this"); - } - $context->getObjects()->addMyclassToObject($objectId, $copyMyClass); - } + return $classStackName; } // def1 and def2 defined in different files @@ -368,78 +120,125 @@ public static function instanceBuildThis($context, $data, $objectId, $myClass, $ public static function isNearestIncludes($def1, $def2) { $def1IncludedByDef2 = false; + $def1IncludedToDef2 = false; + + /* + file1.php + echo $def1; <= def1 (file1.php from file2.php) - $myFile = $def1->getSourceMyFile(); - while (!is_null($myFile)) { - $myFileFrom = $myFile->getIncludedFromMyfile(); + file2.php + $def1 <= def2 (file2.php to file2.php) + include("file1.php") + */ + + $myFilef = $def1->getSourceMyFile(); + while (!is_null($myFilef)) { + $myFileFrom = $myFilef->getIncludedFromMyfile(); if (!is_null($myFileFrom) && ($myFileFrom->getName() === $def2->getSourceMyFile()->getName())) { $def1IncludedByDef2 = true; break; } - $myFile = $myFileFrom; + $myFilef = $myFileFrom; + } + + $myFilet = $def1->getSourceMyFile(); + while (!is_null($myFilet)) { + $myFileTo = $myFilet->getIncludedToMyfile(); + if (!is_null($myFileTo) && ($myFileTo->getName() === $def2->getSourceMyFile()->getName())) { + $def1IncludedToDef2 = true; + break; + } + + $myFilet = $myFileTo; } - if (!$def1IncludedByDef2) { + /* same thing but for def2 */ + if (!$def1IncludedToDef2 && !$def1IncludedByDef2) { $def2IncludedByDef1 = false; - $myFile = $def2->getSourceMyFile(); - while (!is_null($myFile)) { - $myFileFrom = $myFile->getIncludedFromMyfile(); + $def2IncludedToDef1 = false; + + $myFilef = $def2->getSourceMyFile(); + while (!is_null($myFilef)) { + $myFileFrom = $myFilef->getIncludedFromMyfile(); if (!is_null($myFileFrom) && ($myFileFrom->getName() === $def1->getSourceMyFile()->getName())) { $def2IncludedByDef1 = true; break; } - $myFile = $myFileFrom; + $myFilef = $myFileFrom; } - } - // the two defs are defined in different included file - if (!$def1IncludedByDef2 && !$def2IncludedByDef1) { - $myFileDef1 = $def1->getSourceMyFile(); - while (!is_null($myFileDef1)) { - $myFileDef2 = $def2->getSourceMyFile(); - while (!is_null($myFileDef2)) { - // we found the file from where the include chain start - if ($myFileDef1->getName() === $myFileDef2->getName()) { - // if the file of def1 is included later so def1 is deeper - if (($myFileDef1->getLine() > $myFileDef2->getLine()) - || ($myFileDef1->getLine() === $myFileDef2->getLine() - && $myFileDef1->getColumn() >= $myFileDef2->getColumn())) { - return true; - } else { - return false; - } - } - - $myFileDef2 = $myFileDef2->getIncludedFromMyfile(); + $myFilet = $def2->getSourceMyFile(); + while (!is_null($myFilet)) { + $myFileTo = $myFilet->getIncludedToMyfile(); + if (!is_null($myFileTo) && ($myFileTo->getName() === $def1->getSourceMyFile()->getName())) { + $def2IncludedToDef1 = true; + break; } - $myFileDef1 = $myFileDef1->getIncludedFromMyfile(); + $myFilet = $myFileTo; } } // def1 is included by file from def2 // but def2 defined before or after the include ? + + /* + file1.php + echo $def1; <= def1 (file1.php from file2.php) + + file2.php + $def1 <= def2 (file2.php to file2.php) + include("file1.php") + */ if ($def1IncludedByDef2) { - // def2 defined after the include so def2 is deeper - if (($def2->getLine() > $myFile->getLine()) - || ($def2->getLine() === $myFile->getLine() - && $def2->getColumn() >= $myFile->getColumn())) { + // def2 defined after the include + if (($def2->getLine() > $myFilef->getLine()) + || ($def2->getLine() === $myFilef->getLine() + && $def2->getColumn() >= $myFilef->getColumn())) { return false; } return true; } + if ($def1IncludedToDef2) { + // def2 defined before the include + if (($def1->getLine() > $myFilet->getLine()) + || ($def1->getLine() === $myFilet->getLine() + && $def1->getColumn() >= $myFilet->getColumn())) { + return false; + } + + return true; + } + + /* + file1.php + $def1 + + file2.php + echo $def1 (def2) <= def1 (file1.php from file2.php) + include("file1.php") + */ // def2 is included by file from def1 // but def1 defined before or after the include ? if ($def2IncludedByDef1) { - // def1 defined after the include so def1 is deeper + // def1 defined after the include + if (($def1->getLine() > $myFilef->getLine()) + || ($def1->getLine() === $myFilef->getLine() + && $def1->getColumn() >= $myFilef->getColumn())) { + return true; + } - if (($def1->getLine() > $myFile->getLine()) - || ($def1->getLine() === $myFile->getLine() - && $def1->getColumn() >= $myFile->getColumn())) { + return false; + } + if ($def2IncludedToDef1) { + // def1 defined before the include + if (($def2->getLine() > $myFilet->getLine()) + || ($def2->getLine() === $myFilet->getLine() + && $def2->getColumn() >= $myFilet->getColumn())) { return true; } @@ -493,21 +292,17 @@ public static function getVisibilityMethod($defName, $method) public static function getVisibility($def, $property, $currentFunc) { if (!is_null($def) - && $def->isType(MyDefinition::TYPE_PROPERTY) - && $def->getName() === "this") { + && $def->getName() === "this") { return true; } if (!is_null($def) && !is_null($currentFunc) && !is_null($currentFunc->getMyClass()) - && $def->isType(MyDefinition::TYPE_STATIC_PROPERTY) && $def->getName() === $currentFunc->getMyClass()->getName()) { return true; } if (!is_null($property) - && ($property->isType(MyDefinition::TYPE_PROPERTY) - || $property->isType(MyDefinition::TYPE_STATIC_PROPERTY)) - && $property->property->getVisibility() === "public") { + && $property->getVisibility() === "public") { return true; } @@ -521,42 +316,37 @@ public static function getVisibilityFromInstances($context, $data, $defAssign) if ($defAssign->isType(MyDefinition::TYPE_PROPERTY)) { $copyDefAssign = clone $defAssign; - $prop = $copyDefAssign->property->popProperty(); + //$prop = $copyDefAssign->property->popProperty(); + $prop = $defAssign->getName(); $visibilityFinal = false; - $instances = ResolveDefs::selectInstances($context, $data, $copyDefAssign); - + $instances = ResolveDefs::selectDefinitions($context, $data, $copyDefAssign); foreach ($instances as $instance) { if ($instance->isType(MyDefinition::TYPE_INSTANCE)) { - $idObject = $instance->getObjectId(); + $idObject = $instance->getCurrentState()->getObjectId(); $tmpMyClass = $context->getObjects()->getMyClassFromObject($idObject); if (!is_null($tmpMyClass)) { - $property = $tmpMyClass->getProperty($prop); + $property = $tmpMyClass->getProperty($context, $instance, $prop); if (!is_null($property) && (ResolveDefs::getVisibility($copyDefAssign, $property, $currentFunc))) { $visibilityFinal = true; break; - } elseif (is_null($property)) { + }/* elseif (is_null($property)) { $visibilityFinal = true; // property doesn't exist - } + }*/ } else { $visibilityFinal = true; // class not defined } } } -/* - if (count($instances) === 0) { - $visibilityFinal = true; - } - */ } elseif ($defAssign->isType(MyDefinition::TYPE_STATIC_PROPERTY)) { $visibilityFinal = false; $myClass = $context->getClasses()->getMyClass($defAssign->getName()); if (!is_null($myClass)) { - $property = $myClass->getProperty($defAssign->property->getProperties()[0]); + $property = $myClass->getProperty($defAssign->property->getProperties()); if (!is_null($property) && (ResolveDefs::getVisibility($defAssign, $property, $currentFunc))) { $visibilityFinal = true; @@ -567,7 +357,7 @@ public static function getVisibilityFromInstances($context, $data, $defAssign) return $visibilityFinal; } - public static function selectDefinitions($context, $data, $searchedDed, $bypassIsNearest = false) + public static function selectDefinitions($context, $data, $searchedDed) { $defsFound = []; if (is_null($data)) { @@ -575,28 +365,10 @@ public static function selectDefinitions($context, $data, $searchedDed, $bypassI } foreach ($data as $def) { - if (Definitions::defEquality($def, $searchedDed, $bypassIsNearest) - && ResolveDefs::isNearest($context, $searchedDed, $def)) { - // CA SERT A QUOI ICI REDONDANT AVEC LE DERNIER ? - if ($def->isType(MyDefinition::TYPE_INSTANCE) - && $searchedDed->isType(MyDefinition::TYPE_INSTANCE)) { - $defsFound[$def->getBlockId()][] = $def; - } elseif (($def->isType(MyDefinition::TYPE_PROPERTY) === - $searchedDed->isType(MyDefinition::TYPE_PROPERTY)) - || ($def->isType(MyDefinition::TYPE_INSTANCE) === - $searchedDed->isType(MyDefinition::TYPE_INSTANCE))) { - if ($def->isType(MyDefinition::TYPE_PROPERTY) - && $searchedDed->isType(MyDefinition::TYPE_PROPERTY)) { - $defsFound[$def->getBlockId()][] = $def; - } elseif (!$def->isType(MyDefinition::TYPE_PROPERTY) - && !$searchedDed->isType(MyDefinition::TYPE_PROPERTY)) { - $defsFound[$def->getBlockId()][] = $def; - } - } elseif (!$def->isType(MyDefinition::TYPE_INSTANCE) - && $searchedDed->isType(MyDefinition::TYPE_PROPERTY)) { - // we are looking for the nearest not instance of a property - $defsFound[$def->getBlockId()][] = $def; - } + if (Definitions::defEquality($def, $searchedDed) + && ResolveDefs::isNearest($context, $searchedDed, $def)) { + // we are looking for the nearest not instance of a property + $defsFound[$def->getBlockId()][] = $def; } } @@ -613,373 +385,150 @@ public static function selectDefinitions($context, $data, $searchedDed, $bypassI foreach ($defsFoundGood as $blockDefs) { $nearestDef = null; foreach ($blockDefs as $blockId => $defLast) { - if (!$bypassIsNearest) { - if (ResolveDefs::isNearest($context, $searchedDed, $defLast)) { - if (is_null($nearestDef) || ResolveDefs::isNearest($context, $defLast, $nearestDef)) { - $nearestDef = $defLast; - } + if (ResolveDefs::isNearest($context, $searchedDed, $defLast)) { + if (is_null($nearestDef) || ResolveDefs::isNearest($context, $defLast, $nearestDef)) { + $nearestDef = $defLast; } - } else { - $trueDefsFound[] = $defLast; } } - if (!is_null($nearestDef) && !$bypassIsNearest) { + if (!is_null($nearestDef)) { $trueDefsFound[] = $nearestDef; } } - return $trueDefsFound; + + $trueDefsFoundParams = []; + foreach ($trueDefsFound as $trueDef) { + if (!is_null($trueDef->getParamToArg())) { + $trueDef = $trueDef->getParamToArg(); + } + $trueDefsFoundParams[] = $trueDef; + } + + return $trueDefsFoundParams; } - public static function selectInstances($context, $data, $tempDefa, $bypassIsNearest = false) + public static function selectArrays($context, $data, $tempDefa, $arrayDim) { - $instancesDefs = []; + $arrayDefs = []; - // we can have multiple instances with the same property assigned - // we are looking for and instance, not a property $copyTempDefa = clone $tempDefa; + $copyTempDefa->addType(MyDefinition::TYPE_ARRAY); - if (!$copyTempDefa->isType(MyDefinition::TYPE_INSTANCE)) { - $copyTempDefa->addType(MyDefinition::TYPE_INSTANCE); - } - - if ($copyTempDefa->isType(MyDefinition::TYPE_ARRAY)) { - $copyTempDefa->removeType(MyDefinition::TYPE_ARRAY); - } - - $copyTempDefa->setArrayValue(false); - - $instancesDefs = ResolveDefs::selectDefinitions( + $arrayDefsTmp = ResolveDefs::selectDefinitions( $context, $data, - $copyTempDefa, - $bypassIsNearest + $copyTempDefa ); - return $instancesDefs; + foreach ($arrayDefsTmp as $arrayDef) {/* + if (!is_null($arrayDef->getParamToArg())) { + $arrayDef = $arrayDef->getParamToArg(); + } +*/ + $arrayDefs[] = $arrayDef + ->getCurrentState() + ->getOrCreateDefArrayIndex($tempDefa->getBlockId(), $arrayDef, $arrayDim); + } + + return $arrayDefs; } - public static function selectProperties($context, $data, $tempDefa, $bypassVisibility = false) + public static function selectStaticProperties($context, $tempDef, $propertyName) { - $propertiesDefs = []; $currentFunc = $context->getCurrentFunc(); + $className = $tempDef->getName(); - if ($tempDefa->isType(MyDefinition::TYPE_PROPERTY)) { - $propLine = $tempDefa->getLine(); - $propColumn = $tempDefa->getColumn(); - - $tempDefaProp = clone $tempDefa; - $firstProperties = []; - $isFirstProperty = true; - $propertyExist = false; - - if (is_array($tempDefa->property->getProperties())) { - $myProperties = $tempDefa->property->getProperties(); - for ($indexProperty = count($myProperties) - 1; $indexProperty !== -1; $indexProperty --) { - $tempDefaProp->setLine($propLine); - $tempDefaProp->setColumn($propColumn); - - $defs = ResolveDefs::selectDefinitions( - $context, - $data, - $tempDefaProp, - $bypassVisibility - ); - - $tempDefaProp->property->popProperty(); - $prop = $myProperties[$indexProperty]; - - if (count($defs) > 0) { - foreach ($defs as $defa) { - if ($defa->isType(MyDefinition::TYPE_PROPERTY)) { - // if we found a property, we are looking for the nearest instance or not instance - // and we are looking for an instance that contains this visible property - $instances = ResolveDefs::selectInstances($context, $data, $tempDefaProp); - - foreach ($instances as $instance) { - $idObject = $instance->getObjectId(); - $tmpMyClass = $context->getObjects()->getMyClassFromObject($idObject); - - if (!is_null($tmpMyClass)) { - $property = $tmpMyClass->getProperty($prop); - - if (!is_null($property) - && (ResolveDefs::getVisibility($defa, $property, $currentFunc) - || $bypassVisibility)) { - $propertyExist = true; - - if ($isFirstProperty || $bypassVisibility) { - $isFirstProperty = false; - - // if the instance is nearest (deeper) than the property, - // it has the priority - if (ResolveDefs::isNearest($context, $instance, $defa)) { - $firstProperties[] = $property; - } else { - // else property exist in the nearest instance - //but property has the priority - $firstProperties[] = $defa; - } - } - } - } - } - - if (count($instances) === 0 && !$isFirstProperty) { - $propertyExist = true; - $firstProperties[] = $defa; - } - } - } - } else { - // we didn't find a property, we are looking for the nearest instance - // or not instance - - $instances = ResolveDefs::selectInstances($context, $data, $tempDefaProp); - - foreach ($instances as $instance) { - if ($instance->isType(MyDefinition::TYPE_INSTANCE)) { - if ($instance->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED")) { - //$propertiesDefs[] = $tempDefa; - $tempDefa->setTaintedByExpr($instance->getTaintedByExpr()); - $tempDefa->setTainted(true); - } else { - for ($i = $indexProperty; $i < count($myProperties); $i++) { - $idObject = $instance->getObjectId(); - $tmpMyClass = $context->getObjects()->getMyClassFromObject($idObject); - - if (!is_null($tmpMyClass)) { - $prop = $myProperties[$i]; - $property = $tmpMyClass->getProperty($prop); - - if (!is_null($property) - && (ResolveDefs::getVisibility( - $tempDefaProp, - $property, - $currentFunc - ) - || $bypassVisibility)) { - $limit = count($myProperties) - 1; - - if ($property->isType(MyDefinition::TYPE_INSTANCE) - && $i < (count($myProperties) - 1)) { - $instance = $property; - } elseif ($i === (count($myProperties) - 1)) { - $propertiesDefs[] = $property; - } - } - } - } - } - } - } - } - } - } + $myClass = $context->getClasses()->getMyClass($className); - if ($propertyExist) { - foreach ($firstProperties as $first_property) { - $propertiesDefs[] = $first_property; - } + if (!is_null($myClass)) { + $property = $myClass->getProperty($context, $tempDef->getBlockId(), $tempDef, $propertyName); + + if (!is_null($property) + && (ResolveDefs::getVisibility($tempDef, $property, $currentFunc))) { + return $property; } } - return $propertiesDefs; + return null; } - public static function selectGlobals($globalName, $context, $data, $tempDefa, $isIterator, $isAssign, $callStack) + public static function selectProperties($context, $data, $tempDefa, $propertyName) { - if (is_array($callStack)) { - for ($callNumber = count($callStack) - 1; $callNumber !== 0; $callNumber --) { - $currentContextCall = $callStack[$callNumber][4]; + $propertiesDefs = []; + $currentFunc = $context->getCurrentFunc(); - // ca peut arriver si on est dans le main() est qu'on appelle une globale - if (!is_null($currentContextCall->func_called) && !is_null($currentContextCall->func_callee)) { - // we can't looking for an element of a global array in PHP - $tempDefa->removeType(MyDefinition::TYPE_ARRAY); - $tempDefa->setArrayValue(false); - - $tempDefa->setName($globalName); - $tempDefa->setLine($currentContextCall->func_called->getLine()); - $tempDefa->setColumn($currentContextCall->func_called->getColumn()); - $tempDefa->setBlockId($currentContextCall->func_callee->getLastBlockId()); - - $resGlobal = ResolveDefs::temporarySimple( - $context, - $currentContextCall->func_callee->getDefs(), - $tempDefa, - $isIterator, - $isAssign, - $currentContextCall - ); - if (!(count($resGlobal) === 1 && $resGlobal[0] === $tempDefa)) { - return $resGlobal; - } - } - } - } + $tempDefaProp = clone $tempDefa; - return array(); - } - - public static function selectStaticProperty($context, $data, $tempDefa, $isIterator, $isAssign, $callStack) - { - if (is_array($callStack)) { - // we are looking in first for a possible static property defined inside the function - $resStatic = ResolveDefs::temporarySimple( - $context, - $data, - $tempDefa, - $isIterator, - $isAssign, - $callStack, - true - ); - - if (!(count($resStatic) === 1 && $resStatic[0] === $tempDefa)) { - return $resStatic; + $instances = ResolveDefs::selectDefinitions($context, $data, $tempDefaProp); + + foreach ($instances as $instance) { + if ($instance->isType(MyDefinition::TYPE_ITERATOR) + && !empty($instance->getIteratorValues()) + && $instance->getIteratorValues()[0]->getCurrentState()->isType(MyDefinition::TYPE_INSTANCE)) { + // source of type array of properties + $instance = $instance->getIteratorValues()[0]; } - // if no result we are looking inside the callee functions - for ($callNumber = count($callStack) - 1; $callNumber >= 0; $callNumber --) { - $currentContextCall = $callStack[$callNumber][4]; + if ($instance->isType(MyDefinition::TYPE_PROPERTY) + || $instance->isType(MyDefinition::TYPE_ARRAY_ELEMENT)) { + $state = $instance->getState($tempDefaProp->getBlockId()); + $stateId = $tempDefaProp->getBlockId(); + } else { + $state = $instance->getCurrentState(); + $stateId = $instance->getBlockId(); + } - // ca peut arriver si on est dans le main() est qu'on appelle une static - if (!is_null($currentContextCall->func_called) && !is_null($currentContextCall->func_callee)) { - $tempDefa->setLine($currentContextCall->func_called->getLine()); - $tempDefa->setColumn($currentContextCall->func_called->getColumn()); - $tempDefa->setBlockId($currentContextCall->func_callee->getLastBlockId()); - - $resGlobal = ResolveDefs::temporarySimple( - $context, - $currentContextCall->func_callee->getDefs(), - $tempDefa, - $isIterator, - $isAssign, - $callStack, - true - ); - if (!(count($resGlobal) === 1 && $resGlobal[0] === $tempDefa)) { - return $resGlobal; + if (!is_null($state) && $state->isType(MyDefinition::TYPE_INSTANCE)) { + $idObject = $state->getObjectId(); + $tmpMyClass = $context->getObjects()->getMyClassFromObject($idObject); + + if (!is_null($tmpMyClass)) { + $property = $tmpMyClass->getProperty($context, $stateId, $instance, $propertyName); + if (!is_null($property)) { + if (ResolveDefs::getVisibility($tempDefaProp, $property, $currentFunc)) { + $propertiesDefs[] = [$property, $tmpMyClass]; + } } } } } - return array(); + return $propertiesDefs; } + public static function selectGlobals($context, $defa) + { + $callStack = $context->getCallStack(); - public static function temporarySimple( - $context, - $data, - $tempDefa, - $isIterator, - $isAssign, - $callStack, - $bypassStatic = false - ) { - if ($tempDefa->isType(MyDefinition::TYPE_STATIC_PROPERTY) && !$bypassStatic) { - return ResolveDefs::selectStaticProperty( - $context, - $data, - $tempDefa, - $isIterator, - $isAssign, - $callStack - ); - } elseif ($tempDefa->isType(MyDefinition::TYPE_ARRAY) && $tempDefa->getName() === "GLOBALS") { - return ResolveDefs::selectGlobals( - key($tempDefa->getArrayValue()), - $context, - $data, - $tempDefa, - $isIterator, - $isAssign, - $callStack - ); - } else { - $myExpr = $tempDefa->getExpr(); - - if ($tempDefa->isType(MyDefinition::TYPE_PROPERTY)) { - $defs = ResolveDefs::selectProperties( - $context, - $data->getOutMinusKill($tempDefa->getBlockId()), - $tempDefa - ); - } else { - $defs = ResolveDefs::selectDefinitions( - $context, - $data->getOutMinusKill($tempDefa->getBlockId()), - $tempDefa, - $isIterator - ); - } + if (is_array($callStack)) { + for ($callNumber = count($callStack) - 1; $callNumber !== 0; $callNumber --) { + $currentContextCall = $callStack[$callNumber][3]; + // ca peut arriver si on est dans le main() est qu'on appelle une globale + if (!is_null($currentContextCall->func_called) && !is_null($currentContextCall->func_callee)) { + // we can't looking for an element of a global array in PHP + foreach ($currentContextCall->func_callee->getLastBlockIds() as $lastBlockId) { + $tempDefa = clone $defa; + $tempDefa->setLine($currentContextCall->func_called->getLine()); + $tempDefa->setColumn($currentContextCall->func_called->getColumn()); + $tempDefa->setBlockId($lastBlockId); - $goodDefs = []; - if (count($defs) > 0) { - foreach ($defs as $defz) { - if ($defz->isType(MyDefinition::TYPE_GLOBAL)) { - return ResolveDefs::selectGlobals( - $defz->getName(), - $context, - $data, - $tempDefa, - $isIterator, - $isAssign, - $callStack - ); - } else { - $defaa = ArrayAnalysis::temporarySimple( + $resGlobal = ResolveDefs::selectDefinitions( $context, - $data->getOutMinusKill($tempDefa->getBlockId()), - $tempDefa, - $defz, - $isIterator, - $isAssign + $currentContextCall->func_callee->getDefs()->getOutMinusKill($tempDefa->getBlockId()), + $tempDefa ); - foreach ($defaa as $defa) { - if ($defa->isType(MyDefinition::TYPE_REFERENCE)) { - $refDef = new MyDefinition( - $tempDefa->getLine(), - $tempDefa->getColumn(), - $defa->getRefName() - ); - $refDef->setBlockId($tempDefa->getBlockId()); - $refDef->setSourceMyFile($tempDefa->getSourceMyFile()); - - if ($defa->isType(MyDefinition::TYPE_ARRAY_REFERENCE)) { - $refDef->addType(MyDefinition::TYPE_ARRAY); - $refDef->setArrayValue($defa->getRefArrValue()); - } - - $trueRefs = ResolveDefs::selectDefinitions( - $context, - $data->getOutMinusKill($refDef->getBlockId()), - $refDef - ); - - foreach ($trueRefs as $ref) { - $myExpr->addDef($ref); - $goodDefs[] = $ref; - } - - unset($trueRefs); - } else { - $myExpr->addDef($defa); - $goodDefs[] = $defa; - } + // to not call recursively this function + if (!(count($resGlobal) === 1 && $resGlobal[0] === $tempDefa)) { + return $resGlobal; } } } - } else { - $myExpr->addDef($tempDefa); - $goodDefs[] = $tempDefa; } - - return $goodDefs; } + + return array(); } } diff --git a/package/src/progpilot/Analysis/SecurityAnalysis.php b/package/src/progpilot/Analysis/SecurityAnalysis.php index 1d3e97de..0d3d7163 100644 --- a/package/src/progpilot/Analysis/SecurityAnalysis.php +++ b/package/src/progpilot/Analysis/SecurityAnalysis.php @@ -22,15 +22,17 @@ use progpilot\Objects\MyDefinition; use progpilot\Code\MyInstruction; +use progpilot\Helpers\Analysis as HelpersAnalysis; + class SecurityAnalysis { - public static function inArraySource($temp, $name, $line, $column, $file) + public static function inArrayStateSource($temp, $ret) { for ($i = 0; $i < count($temp["source_name"]); $i ++) { - if ($temp["source_name"][$i] === $name - && $temp["source_line"][$i] === $line - && $temp["source_column"][$i] === $column - && $temp["source_file"][$i] === $file) { + if ($temp["source_name"][$i] === $ret["source_name"] + && $temp["source_line"][$i] === $ret["source_line"] + && $temp["source_column"][$i] === $ret["source_column"] + && $temp["source_file"][$i] === $ret["source_file"]) { return true; } } @@ -38,20 +40,83 @@ public static function inArraySource($temp, $name, $line, $column, $file) return false; } - public static function isSafe($indexParameter, $myDef, $mySink, $isFlow = false) + public static function funccall($context, $instruction, $myClass = null) + { + $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); + + $mySink = $context->inputs->getSinkByName($myFuncCall, $myClass); + if (!is_null($mySink)) { + $nbParams = $myFuncCall->getNbParams(); + for ($i = 0; $i < $nbParams; $i ++) { + $myDefArg = $instruction->getProperty("argdef$i"); + + if (!$mySink->hasParameters() || ($mySink->hasParameters() && $mySink->isParameter($i + 1))) { + SecurityAnalysis::callbis( + $i + 1, + $myFuncCall, + $context, + $mySink, + $myDefArg + ); + } + } + } + } + + public static function taintedStateFlow($context, $mySink, $indexParameter, $taintedDef, $taintedState) + { + $resultTaintedFlow = []; + $idFlow = \progpilot\Utils::printDefinition($mySink->getLanguage(), $taintedDef); + + do { + $fromTaintedByDefs = $taintedState->getTaintedByDefs(); + + $taintedDef = null; + + foreach ($fromTaintedByDefs as $fromTaintedByDef) { + $fromTaintedDef = $fromTaintedByDef[0]; + $fromTaintedState = $fromTaintedByDef[1]; + + $possibleConditions = ["QUOTES", "object_tainted", "array_tainted", "variable_tainted", null]; + foreach ($possibleConditions as $condition) { + if ($mySink->isParameterCondition($indexParameter, $condition)) { + if (!SecurityAnalysis::isSafeStateCondition( + $context, + $mySink, + $fromTaintedState, + $condition, + true + )) { + $ret = SecurityAnalysis::getPrintableTaintedDef($mySink, $fromTaintedDef); + + $oneTainted["flow_name"] = $ret["source_name"]; + $oneTainted["flow_line"] = $ret["source_line"]; + $oneTainted["flow_column"] = $ret["source_column"]; + $oneTainted["flow_file"] =$ret["source_file"]; + + $resultTaintedFlow[] = $oneTainted; + + $idFlow .= \progpilot\Utils::printDefinition($mySink->getLanguage(), $fromTaintedDef); + $idFlow .= "-".$fromTaintedDef->getSourceMyFile()->getName(); + $taintedDef = $fromTaintedDef; + $taintedState = $fromTaintedState; + break 2; + } + } + } + } + } while (!is_null($taintedDef)); + + return [$resultTaintedFlow, $idFlow]; + } + + public static function isSafeState($context, $mySink, $indexParameter, $myState) { $possibleConditions = ["QUOTES", "object_tainted", "array_tainted", "variable_tainted", null]; - foreach ($possibleConditions as $possibleCondition) { - if ($mySink->isParameterCondition($indexParameter, $possibleCondition)) { - if (!SecurityAnalysis::isSafeCondition( - $indexParameter, - $myDef, - $mySink, - $possibleCondition, - $isFlow - ) - ) { + foreach ($possibleConditions as $condition) { + if ($mySink->isParameterCondition($indexParameter, $condition)) { + if (!SecurityAnalysis::isSafeStateCondition($context, $mySink, $myState, $condition)) { return false; } } @@ -59,190 +124,78 @@ public static function isSafe($indexParameter, $myDef, $mySink, $isFlow = false) return true; } - - public static function isSafeCondition($indexParameter, $myDef, $mySink, $condition, $isFlow) + + public static function isSafeStateCondition($context, $mySink, $state, $condition, $isFlow = false) { - if ($myDef->isTainted() - && $myDef->getCast() === MyDefinition::CAST_NOT_SAFE - && $myDef->getArrayValue() !== "PROGPILOT_ALL_INDEX_TAINTED" - && !$myDef->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED")) { - if ($myDef->isSanitized()) { - if ($myDef->isTypeSanitized($mySink->getAttack()) - || $myDef->isTypeSanitized("ALL")) { + if (($state->isTainted()) + && $state->getCast() === MyDefinition::CAST_NOT_SAFE + && $condition !== "object_tainted" + && $condition !== "array_tainted") { + if ($state->isSanitized()) { + if ($state->isTypeSanitized($mySink->getAttack()) + || $state->isTypeSanitized("ALL")) { // 1° the argument of sink must be quoted - if ($condition === "QUOTES" && !$myDef->isTypeSanitized("ALL")) { + if ($condition === "QUOTES" && !$state->isTypeSanitized("ALL")) { // the def is embedded into quotes but quotes are not sanitized - if (!$myDef->isTypeSanitized("QUOTES") && $myDef->getIsEmbeddedByChar("'")) { + if (!$state->isTypeSanitized("QUOTES") + && $state->getIsEmbeddedByChar("'")) { return false; } // the def is not embedded into quotes - if (!$myDef->getIsEmbeddedByChar("'")) { + if (!$state->getIsEmbeddedByChar("'")) { return false; } } if ($mySink->isGlobalCondition("QUOTES_HTML") - && !$myDef->isTypeSanitized("ALL")) { - if (!$myDef->isTypeSanitized("QUOTES") && $myDef->getIsEmbeddedByChar("<") - && $myDef->getIsEmbeddedByChar("'")) { + && !$state->isTypeSanitized("ALL")) { + if (!$state->isTypeSanitized("QUOTES") && $state->getIsEmbeddedByChar("<") + && $state->getIsEmbeddedByChar("'")) { return false; } - if ($myDef->getIsEmbeddedByChar("<") && !$myDef->getIsEmbeddedByChar("'")) { + if ($state->getIsEmbeddedByChar("<") + && !$state->getIsEmbeddedByChar("'")) { return false; } } - - return true; + } else { + return false; // not safe because type not sanitized } + + return true; } - return false; - } elseif (($condition === "array_tainted" || $isFlow) - && $myDef->getArrayValue() === "PROGPILOT_ALL_INDEX_TAINTED") { return false; } elseif (($condition === "object_tainted" || $isFlow) - && $myDef->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED")) { + && $state->isType(MyDefinition::TYPE_INSTANCE) + && $state->isType(MyDefinition::ALL_PROPERTIES_TAINTED)) { + return false; + } elseif (($condition === "array_tainted" || $isFlow) + && $state->isType(MyDefinition::TYPE_ARRAY) + && $state->isType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED)) { return false; } - + return true; } - public static function funccall($stackClass, $context, $instruction, $myClass = null) - { - $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); - $nameInstance = null; - if ($myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD)) { - $nameInstance = $myFuncCall->getNameInstance(); - } - - $mySink = $context->inputs->getSinkByName($context, $stackClass, $myFuncCall, $myClass); - if (!is_null($mySink)) { - $nbParams = $myFuncCall->getNbParams(); - $conditionRespected = true; - if ($mySink->hasParameters()) { - for ($i = 0; $i < $nbParams; $i ++) { - if ($mySink->isParameter($i + 1)) { - $conditionRespected = false; - - $myDefArg = $instruction->getProperty("argdef$i"); - $taintedExpr = $myDefArg->getTaintedByExpr(); - - if ($myDefArg->isType(MyDefinition::TYPE_COPY_ARRAY) - && $mySink->isParameterCondition($i + 1, "array_tainted")) { - foreach ($myDefArg->getCopyArrays() as $copyarray) { - if (!SecurityAnalysis::isSafe($i + 1, $copyarray[1], $mySink)) { - $conditionRespected = true; - } - } - } elseif (!$myDefArg->isType(MyDefinition::TYPE_COPY_ARRAY) - && (!$mySink->isParameterCondition($i + 1, "array_tainted") - || $mySink->isParameterCondition($i + 1, "variable_tainted"))) { - if (!is_null($taintedExpr)) { - $defsExpr = $taintedExpr->getDefs(); - foreach ($defsExpr as $defExpr) { - if (!SecurityAnalysis::isSafe($i + 1, $defExpr, $mySink)) { - $conditionRespected = true; - } - } - } - } elseif ($myDefArg->getArrayValue() === "PROGPILOT_ALL_INDEX_TAINTED" - && $mySink->isParameterCondition($i + 1, "array_tainted")) { - $conditionRespected = true; - } elseif ($myDefArg->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED") - && $mySink->isParameterCondition($i + 1, "object_tainted")) { - $conditionRespected = true; - } - - if (!$conditionRespected) { - break; - } - } - } - } - - if ($conditionRespected) { - for ($i = 0; $i < $nbParams; $i ++) { - $myDefArg = $instruction->getProperty("argdef$i"); - $myDefExpr = $instruction->getProperty("argexpr$i"); - - if (!$mySink->hasParameters() || ($mySink->hasParameters() && $mySink->isParameter($i + 1))) { - if ($myDefArg->isType(MyDefinition::TYPE_COPY_ARRAY) - && $mySink->isParameterCondition($i + 1, "array_tainted")) { - foreach ($myDefArg->getCopyArrays() as $copyarray) { - SecurityAnalysis::call( - $i + 1, - $myFuncCall, - $context, - $mySink, - $copyarray[1], - $myDefExpr - ); - } - } else { - SecurityAnalysis::call( - $i + 1, - $myFuncCall, - $context, - $mySink, - $myDefArg, - $myDefExpr - ); - } - } - } - } - } - } - - public static function taintedFlow($indexParameter, $context, $defExprFlow, $mySink) + public static function isSafeStatesCondition($context, $mySink, $myDef, $condition) { - $resultTaintedFlow = []; - - $idFlow = \progpilot\Utils::printDefinition($mySink->getLanguage(), $defExprFlow); - - while ($defExprFlow->getTaintedByExpr() !== null) { - $taintedFlowExpr = $defExprFlow->getTaintedByExpr(); - $defsExprTainted = $taintedFlowExpr->getDefs(); - - foreach ($defsExprTainted as $defExprFlowFrom) { - if (!SecurityAnalysis::isSafe($indexParameter, $defExprFlowFrom, $mySink, true)) { - $tmpname = \progpilot\Utils::printDefinition($mySink->getLanguage(), $defExprFlowFrom); - $oneTainted["flow_name"] = $tmpname; - $oneTainted["flow_line"] = $defExprFlowFrom->getLine(); - $oneTainted["flow_column"] = $defExprFlowFrom->getColumn(); - $oneTainted["flow_file"] = \progpilot\Utils::encodeCharacters( - $defExprFlowFrom->getSourceMyFile()->getName() - ); - - // just in case - if (in_array($oneTainted, $resultTaintedFlow, true)) { - break 2; - } - - $resultTaintedFlow[] = $oneTainted; - - $idFlow .= \progpilot\Utils::printDefinition($mySink->getLanguage(), $defExprFlowFrom); - $idFlow .= "-".$defExprFlowFrom->getSourceMyFile()->getName(); - $defExprFlow = $defExprFlowFrom; - break; - } - } - - if ($defExprFlow->getTaintedByExpr() === $taintedFlowExpr) { - break; + $statesMyDef = $myDef->getStates(); + foreach ($statesMyDef as $stateMyDef) { + if (!SecurityAnalysis::isSafeStateCondition($context, $mySink, $stateMyDef, $condition)) { + return false; } } - - return [$resultTaintedFlow, $idFlow]; + + return true; } - public static function call($indexParameter, $myFuncCall, $context, $mySink, $myDef, $myExpr) + public static function callbis($indexParameter, $myFuncCall, $context, $mySink, $myDef) { - //$results = &$context->outputs->getResults(); $hashIdVuln = ""; $temp["source_name"] = []; @@ -255,60 +208,93 @@ public static function call($indexParameter, $myFuncCall, $context, $mySink, $my } $nbtainted = 0; - $taintedExpr = $myDef->getTaintedByExpr(); - if (!is_null($taintedExpr)) { - $defsExpr = $taintedExpr->getDefs(); - - foreach ($defsExpr as $defExpr) { - if (!SecurityAnalysis::isSafe($indexParameter, $defExpr, $mySink)) { - $sourceName = \progpilot\Utils::printDefinition($mySink->getLanguage(), $defExpr); - $sourceLine = $defExpr->getLine(); - $sourceColumn = $defExpr->getColumn(); - $sourceFile = \progpilot\Utils::encodeCharacters($defExpr->getSourceMyFile()->getName()); - - if (!SecurityAnalysis::inArraySource( - $temp, - $sourceName, - $sourceLine, - $sourceColumn, - $sourceFile - )) { - $resultsFlow = SecurityAnalysis::taintedFlow($indexParameter, $context, $defExpr, $mySink); + + // arg param of sink = always current state + if (!SecurityAnalysis::isSafeState($context, $mySink, $indexParameter, $myDef->getCurrentState())) { + $taintedDefs = $myDef->getCurrentState()->getTaintedByDefs(); + foreach ($taintedDefs as $taintedByDef) { + $taintedDef = $taintedByDef[0]; + $taintedState = $taintedByDef[1]; + + if (!SecurityAnalysis::isSafeState($context, $mySink, $indexParameter, $taintedState)) { + $ret = SecurityAnalysis::getPrintableTaintedDef($mySink, $taintedDef); + if (!SecurityAnalysis::inArrayStateSource($temp, $ret)) { + $resultsFlow = SecurityAnalysis::taintedStateFlow( + $context, + $mySink, + $indexParameter, + $taintedDef, + $taintedState + ); $resultTaintedFlow = $resultsFlow[0]; $hashIdVuln .= $resultsFlow[1]; + $hashIdVuln .= $ret["source_name"]; - $temp["source_name"][] = $sourceName; + $temp["source_name"][] = $ret["source_name"]; if ($context->outputs->getTaintedFlow()) { $temp["tainted_flow"][] = $resultTaintedFlow; } - $temp["source_line"][] = $sourceLine; - $temp["source_column"][] = $sourceColumn; - $temp["source_file"][] = $sourceFile; + $temp["source_line"][] = $ret["source_line"]; + $temp["source_column"][] = $ret["source_column"]; + $temp["source_file"][] = $ret["source_file"]; $nbtainted ++; } } } + + $hashedValue = $hashIdVuln."-".$mySink->getName()."-".$myFuncCall->getSourceMyFile()->getName(); + $hashIdVuln = hash("sha256", $hashedValue); + + if ($nbtainted && is_null($context->inputs->getFalsePositiveById($hashIdVuln))) { + $temp["sink_name"] = \progpilot\Utils::encodeCharacters($mySink->getName()); + $temp["sink_line"] = $myFuncCall->getLine(); + $temp["sink_column"] = $myFuncCall->getColumn(); + $temp["sink_file"] = \progpilot\Utils::encodeCharacters($myFuncCall->getSourceMyFile()->getName()); + $temp["vuln_name"] = \progpilot\Utils::encodeCharacters($mySink->getAttack()); + $temp["vuln_cwe"] = \progpilot\Utils::encodeCharacters($mySink->getCwe()); + $temp["vuln_id"] = $hashIdVuln; + $temp["vuln_type"] = "taint-style"; + + $context->outputs->addResult($temp); + } } + } - $hashedValue = $hashIdVuln."-".$mySink->getName()."-".$myFuncCall->getSourceMyFile()->getName(); - $hashIdVuln = hash("sha256", $hashedValue); - - if ($nbtainted && is_null($context->inputs->getFalsePositiveById($hashIdVuln))) { - $temp["sink_name"] = \progpilot\Utils::encodeCharacters($mySink->getName()); - $temp["sink_line"] = $myFuncCall->getLine(); - $temp["sink_column"] = $myFuncCall->getColumn(); - $temp["sink_file"] = \progpilot\Utils::encodeCharacters($myFuncCall->getSourceMyFile()->getName()); - $temp["vuln_name"] = \progpilot\Utils::encodeCharacters($mySink->getAttack()); - $temp["vuln_cwe"] = \progpilot\Utils::encodeCharacters($mySink->getCwe()); - $temp["vuln_id"] = $hashIdVuln; - $temp["vuln_type"] = "taint-style"; - - $context->outputs->addResult($temp); - //if (!in_array($temp, $results, true)) - // $results[] = $temp; + + public static function getPrintableTaintedDef($mySink, $myDef) + { + if (!is_null($myDef->getArgToParam())) { + $param = $myDef->getArgToParam(); + $return["source_name"] = \progpilot\Utils::printDefinition($mySink->getLanguage(), $param); + $return["source_line"] = $param->getLine(); + $return["source_column"]= $param->getColumn(); + $return["source_file"] = \progpilot\Utils::encodeCharacters($param->getSourceMyFile()->getName()); + + return $return; } + + if (!is_null($myDef->original->getDef())) { + $return["source_name"] = \progpilot\Utils::printDefinition( + $mySink->getLanguage(), + $myDef, + $myDef->original->getDef() + ); + $return["source_line"] = $myDef->original->getDef()[0]->getLine(); + $return["source_column"] = $myDef->original->getDef()[0]->getColumn(); + $return["source_file"] = \progpilot\Utils::encodeCharacters( + $myDef->original->getDef()[0]->getSourceMyFile()->getName() + ); + return $return; + } + + $return["source_name"] = \progpilot\Utils::printDefinition($mySink->getLanguage(), $myDef); + $return["source_line"] = $myDef->getLine(); + $return["source_column"] = $myDef->getColumn(); + $return["source_file"] = \progpilot\Utils::encodeCharacters($myDef->getSourceMyFile()->getName()); + + return $return; } } diff --git a/package/src/progpilot/Analysis/TaintAnalysis.php b/package/src/progpilot/Analysis/TaintAnalysis.php index 21083bde..692d57a5 100644 --- a/package/src/progpilot/Analysis/TaintAnalysis.php +++ b/package/src/progpilot/Analysis/TaintAnalysis.php @@ -17,103 +17,52 @@ use progpilot\Objects\MyDefinition; use progpilot\Objects\MyInstance; use progpilot\Objects\MyAssertion; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyFunction; use progpilot\Dataflow\Definitions; use progpilot\Code\Opcodes; use progpilot\Code\MyInstruction; use progpilot\Inputs\MySource; +use progpilot\Helpers\Analysis as HelpersAnalysis; class TaintAnalysis { - public static function funccallSpecifyAnalysis( - $myFunc, - $stackClass, + public static function funccallValidator( $context, - $data, + $myFunc, $myClass, - $myFuncCall, - $arrFuncCall, - $instruction, - $myCode, - $index + $instruction ) { - $stackClass = ResolveDefs::funccallClass($context, $data, $myFuncCall); - - \progpilot\Analysis\CustomAnalysis::returnObject( - $context, - $myFuncCall, - $stackClass, - $instruction->getProperty(MyInstruction::EXPR) - ); - - TaintAnalysis::funccallValidator($stackClass, $context, $data, $myClass, $instruction, $myCode, $index); - TaintAnalysis::funccallSanitizer( - $myFunc, - $stackClass, - $context, - $data, - $myClass, - $instruction, - $myCode, - $index - ); - - $hasSources = TaintAnalysis::funccallSource($stackClass, $context, $data, $myClass, $instruction); - - // for example for document.write - if (is_null($myClass) && $myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD)) { - $myClass = new MyClass( - $myFuncCall->getLine(), - $myFuncCall->getColumn(), - $myFuncCall->getNameInstance() - ); - } - - SecurityAnalysis::funccall($stackClass, $context, $instruction, $myClass); - - return $hasSources; - } - - public static function funccallValidator($stackClass, $context, $data, $myClass, $instruction, $myCode, $index) - { - $funcName = $instruction->getProperty(MyInstruction::FUNCNAME); - $arrFuncCall = $instruction->getProperty(MyInstruction::ARR); $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); $nbParams = 0; $defsValid = []; $conditionsRespected = true; - $myValidator = $context->inputs->getValidatorByName($context, $stackClass, $myFuncCall, $myClass); + $myValidator = $context->inputs->getValidatorByName($myFuncCall, $myClass); if (!is_null($myValidator)) { - while (true) { - if (!$instruction->isPropertyExist("argdef$nbParams")) { - break; - } + $validwhenreturning = $myValidator->getValidWhenReturning(); + } else { + $validwhenreturning = true; + } + + while (true) { + if (!$instruction->isPropertyExist("argdef$nbParams")) { + break; + } - $defArg = $instruction->getProperty("argdef$nbParams"); - $exprArg = $instruction->getProperty("argexpr$nbParams"); + $defArg = $instruction->getProperty("argdef$nbParams"); + if (!is_null($myValidator)) { $conditions = $myValidator->getParameterconditions($nbParams + 1); - if ($conditions === "valid" && !$exprArg->getIsConcat()) { - $theDefsArgs = $exprArg->getDefs(); - - foreach ($theDefsArgs as $theDefsArg) { - $defsValid[] = $theDefsArg; - } + if ($conditions === "valid") { + $defsValid[] = $defArg; } elseif ($conditions === "array_not_tainted") { - if ($defArg->isType(MyDefinition::TYPE_ARRAY) && $defArg->isTainted()) { - $conditionsRespected = false; - } elseif ($defArg->isType(MyDefinition::TYPE_COPY_ARRAY)) { - $copyArrays = $defArg->getCopyArrays(); - foreach ($copyArrays as $copyArray) { - $arrvalue = $copyArray[0]; - $defarr = $copyArray[1]; - - if ($defarr->isTainted()) { + if ($defArg->getCurrentState()->isType(MyDefinition::TYPE_ARRAY)) { + foreach ($defArg->getCurrentState()->getArrayIndexes() as $arrayIndex) { + if ($arrayIndex->def->getCurrentState()->isTainted()) { $conditionsRespected = false; + break; } } } @@ -121,18 +70,17 @@ public static function funccallValidator($stackClass, $context, $data, $myClass, if ($defArg->isTainted()) { $conditionsRespected = false; } - } elseif ($conditions === "equals") { + } elseif ($conditions === "equals" || $conditions === "notequals") { $conditionsRespectedEquals = false; $values = $myValidator->getParameterValues($nbParams + 1); if (!is_null($values)) { - $theDefsArgs = $exprArg->getDefs(); - if (count($theDefsArgs) > 0) { - foreach ($values as $value) { - foreach ($theDefsArgs[0]->getLastKnownValues() as $lastKnownValue) { - if ($value->value === $lastKnownValue) { - $conditionsRespectedEquals = true; - } + foreach ($values as $value) { + foreach ($defArg->getCurrentState()->getLastKnownValues() as $lastKnownValue) { + if ($value->value === $lastKnownValue && $conditions === "equals") { + $conditionsRespectedEquals = true; + } elseif ($value->value !== $lastKnownValue && $conditions === "notequals") { + $conditionsRespectedEquals = true; } } } @@ -142,79 +90,91 @@ public static function funccallValidator($stackClass, $context, $data, $myClass, $conditionsRespected = false; } } + } else { + if (!is_null($myFunc)) { + $ambiguous = true; + + foreach ($myFunc->getReturnDefs() as $returnDef) { + $ambiguous = false; + // we have a return def from a known validator + if ($returnDef->getReturnedFromValidator()) { + $returnTrue = false; + $returnFalse = false; + + // we overwrite the value for below + $validwhenreturning = $returnDef->getValidWhenReturning(); + $validNotBoolean = $returnDef->getValidNotBoolean(); + + foreach ($returnDef->getCurrentState()->getLastKnownValues() as $lastKnownValue) { + if ($lastKnownValue === "true" + || $lastKnownValue === "TRUE" + || (is_numeric($lastKnownValue) && $lastKnownValue > 0)) { + $returnTrue = true; + } elseif ($lastKnownValue === "false" + || $lastKnownValue === "FALSE" + || (is_numeric($lastKnownValue) && $lastKnownValue <= 0)) { + $returnFalse = true; + } else { + $ambiguous = true; + break 2; + } + } - $nbParams ++; + // ambiguous + if ($returnTrue && $returnFalse) { + $ambiguous = true; + break; + } + } + } + + // all the returns are true OR all the returns are false + if (!$ambiguous) { + $defsValid[] = $defArg; + } + } } + + $nbParams ++; } - if (count($defsValid) > 0) { + if (!empty($defsValid)) { if ($conditionsRespected) { - $codes = $myCode->getCodes(); - - if (isset($codes[$index + 2])) { - $instructionIf = $codes[$index + 2]; - if ($instructionIf->getOpcode() === Opcodes::COND_START_IF) { - $myBlockIf = $instructionIf->getProperty(MyInstruction::MYBLOCK_IF); - $myBlockElse = $instructionIf->getProperty(MyInstruction::MYBLOCK_ELSE); - - foreach ($defsValid as $defValid) { - $type = "valid"; - $myAssertion = new MyAssertion($defValid, $type); - - if ($instructionIf->isPropertyExist(MyInstruction::NOT_BOOLEAN)) { - $myBlockElse->addAssertion($myAssertion); - } else { - $myBlockIf->addAssertion($myAssertion); - } - } - } + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $opInformation = $context->getCurrentFunc()->getOpInformation($resultid); + foreach ($defsValid as $defValid) { + $opInformation["condition_defs"][] = $defValid; } + + $opInformation["valid_when_returning"] = $validwhenreturning; + return $opInformation; } } + + return null; } public static function funccallSanitizer( $myFunc, - $stackClass, $context, - $data, $myClass, $instruction, - $myCode, - $index + $virtualReturnDef ) { $funcName = $instruction->getProperty(MyInstruction::FUNCNAME); - $arrFuncCall = $instruction->getProperty(MyInstruction::ARR); $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); $conditionsSanitize = false; - $conditionsTaint = false; - $paramsTaintedconditionsTaint = false; $paramsTainted = false; $paramsSanitized = false; $paramsTypeSanitized = []; + $paramsTaintedDefs = []; $nbParams = 0; $conditionsRespectedFinal = true; - $myTempReturn = new MyDefinition( - $myFuncCall->getLine(), - $myFuncCall->getColumn(), - "return_".$myFuncCall->getName() - ); - $myTempReturn->setSourceMyFile($context->getCurrentMyfile()); - - $myExprReturn1 = new MyExpr($myFuncCall->getLine(), $myFuncCall->getColumn()); - $myExprReturn1->setAssign(true); - $myExprReturn1->setAssignDef($myTempReturn); - $myExprReturn1->addDef($myTempReturn); - - $myExprReturn2 = new MyExpr($myFuncCall->getLine(), $myFuncCall->getColumn()); - $myExprReturn2->setAssign(true); - $myExprReturn2->setAssignDef($myTempReturn); - - $mySanitizer = $context->inputs->getSanitizerByName($context, $stackClass, $myFuncCall, $myClass); + $mySanitizer = $context->inputs->getSanitizerByName($myFuncCall, $myClass); if (!is_null($mySanitizer)) { $preventFinal = $mySanitizer->getPrevent(); @@ -224,24 +184,21 @@ public static function funccallSanitizer( if (!$instruction->isPropertyExist("argdef$nbParams")) { break; } - + $defArg = $instruction->getProperty("argdef$nbParams"); - $exprArg = $instruction->getProperty("argexpr$nbParams"); - if (is_null($myFunc) || !is_null($mySanitizer)) { - if ($defArg->isTainted()) { - $paramsTainted = true; - $myExprReturn2->addDef($defArg); - } + if ($defArg->getCurrentState()->isTainted()) { + $paramsTainted = true; + $paramsTaintedDefs[] = $defArg; + } - if ($defArg->isSanitized()) { - $paramsSanitized = true; - $tmps = $defArg->getTypeSanitized(); + if ($defArg->getCurrentState()->isSanitized()) { + $paramsSanitized = true; + $tmps = $defArg->getCurrentState()->getTypeSanitized(); - foreach ($tmps as $tmp) { - if (!in_array($tmp, $paramsTypeSanitized, true)) { - $paramsTypeSanitized[] = $tmp; - } + foreach ($tmps as $tmp) { + if (!in_array($tmp, $paramsTypeSanitized, true)) { + $paramsTypeSanitized[] = $tmp; } } } @@ -249,21 +206,18 @@ public static function funccallSanitizer( if (!is_null($mySanitizer)) { $conditions = $mySanitizer->getParameterconditions($nbParams + 1); - if ($conditions === "equals") { + if ($conditions === "equals" || $conditions === "notequals") { $conditionsRespected = false; $values = $mySanitizer->getParameterValues($nbParams + 1); if (!is_null($values)) { - $theDefsArgs = $exprArg->getDefs(); - if (count($theDefsArgs) > 0) { - foreach ($values as $value) { - foreach ($theDefsArgs[0]->getLastKnownValues() as $lastKnownValue) { - if ($value->value === $lastKnownValue) { - $conditionsRespected = true; - - if (isset($value->prevent)) { - $preventFinal = array_merge($preventFinal, $value->prevent); - } + foreach ($values as $value) { + foreach ($defArg->getCurrentState()->getLastKnownValues() as $lastKnownValue) { + if (($value->value === $lastKnownValue && $conditions === "equals") + || ($value->value !== $lastKnownValue && $conditions === "notequals")) { + $conditionsRespected = true; + if (isset($value->prevent)) { + $preventFinal = array_merge($preventFinal, $value->prevent); } } } @@ -275,92 +229,67 @@ public static function funccallSanitizer( } } elseif ($conditions === "taint") { $conditionsTaint = true; - if ($defArg->isTainted()) { + if ($defArg->getCurrentState()->isTainted()) { $paramsTaintedconditionsTaint = true; - $myExprReturn2->addDef($defArg); } } elseif ($conditions === "sanitize") { $conditionsSanitize = true; - $exprsTaintedconditionsSanitize[] = $exprArg; + $exprsTaintedconditionsSanitize[] = $defArg; } } $nbParams ++; } - $returnSanitizer = false; + $funcReturnDefs = (!is_null($myFunc) && !empty($myFunc->getReturnDefs())) ? true : false; - $codes = $myCode->getCodes(); - if (isset($codes[$index + 2]) && $codes[$index + 2]->getOpcode() === Opcodes::END_ASSIGN) { - $instructionDef = $codes[$index + 3]; - $myDefReturn = $instructionDef->getProperty(MyInstruction::DEF); - $returnSanitizer = true; - } - - // the return of func will be tainted if one of arg is tainted - if ($returnSanitizer) { - TaintAnalysis::setTainted($paramsTainted, $myTempReturn, $myExprReturn2); + $mySource = $context->inputs->getSourceByName($myFuncCall, $myClass); + + // the default return of func will be tainted if one of arg is tainted + // AND no defs are returned/defined + // AND the func is not a sanitizer (with all conditions respected) + // AND the func is not a source (will return already a def) + if (!$funcReturnDefs + && is_null($mySource)) { + $virtualReturnDef->getCurrentState()->setTainted($paramsTainted); + + foreach ($paramsTaintedDefs as $paramsTaintedDef) { + $virtualReturnDef->getCurrentState()->addTaintedByDef( + [$paramsTaintedDef, $paramsTaintedDef->getCurrentState()] + ); + } } - if ($returnSanitizer || $conditionsSanitize) { - if (!is_null($mySanitizer) && $conditionsRespectedFinal) { - if ($conditionsSanitize) { - foreach ($exprsTaintedconditionsSanitize as $exprsanitize) { - foreach ($exprsanitize->getDefs() as $oneDef) { - $oneDef->setSanitized(true); - if (is_array($preventFinal)) { - foreach ($preventFinal as $preventFinalValue) { - $oneDef->addTypeSanitized($preventFinalValue); - } - } - } - } - } else { - $myTempReturn->setSanitized(true); - $myDefReturn->setSanitized(true); - if (is_array($preventFinal)) { - foreach ($preventFinal as $preventFinalValue) { - $myTempReturn->addTypeSanitized($preventFinalValue); - $myDefReturn->addTypeSanitized($preventFinalValue); - } + if (!is_null($mySanitizer) && $conditionsRespectedFinal) { + if ($conditionsSanitize) { + foreach ($exprsTaintedconditionsSanitize as $defTaintedconditionsSanitize) { + $callback = "Callbacks::addSanitizedTypes"; + HelpersAnalysis::forEachTaintedByDefs($defTaintedconditionsSanitize, $preventFinal, $callback); + } + } else { + $virtualReturnDef->getCurrentState()->setSanitized(true); + if (is_array($preventFinal)) { + foreach ($preventFinal as $preventFinalValue) { + $virtualReturnDef->getCurrentState()->addTypeSanitized($preventFinalValue); } } } } - if ($returnSanitizer && $paramsSanitized) { - $myTempReturn->setSanitized(true); - $myDefReturn->setSanitized(true); + if ($paramsSanitized) { + $virtualReturnDef->getCurrentState()->setSanitized(true); foreach ($paramsTypeSanitized as $tmp) { - $myTempReturn->addTypeSanitized($tmp); - $myDefReturn->addTypeSanitized($tmp); - } - } - - if ($returnSanitizer) { - if (ResolveDefs::getVisibilityFromInstances($context, $data, $myDefReturn)) { - ValueAnalysis::copyValues($myTempReturn, $myDefReturn); - TaintAnalysis::setTainted($myTempReturn->isTainted(), $myDefReturn, $myExprReturn1); + $virtualReturnDef->getCurrentState()->addTypeSanitized($tmp); } } } - public static function funccallSource($stackClass, $context, $data, $myClass, $instruction) + public static function funccallSource($context, $myClass, $instruction, $virtualReturnDef) { - $hasSources = false; - - $funcName = $instruction->getProperty(MyInstruction::FUNCNAME); - $arrFuncCall = $instruction->getProperty(MyInstruction::ARR); $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); - - $className = false; - if ($myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD) && !is_null($myClass)) { - $className = $myClass->getName(); - } - - $mySource = $context->inputs->getSourceByName($stackClass, $myFuncCall, true, $className, false); + + $mySource = $context->inputs->getSourceByName($myFuncCall, $myClass); if (!is_null($mySource)) { - $hasSources = true; if ($mySource->hasParameters()) { $nbParams = 0; while (true) { @@ -368,21 +297,21 @@ public static function funccallSource($stackClass, $context, $data, $myClass, $i break; } - $defArg = $instruction->getProperty("argdef$nbParams"); - if ($mySource->isParameter($nbParams + 1)) { - $defFrom = $defArg->getValueFromDef(); + $defFrom = $instruction->getProperty("argoriginaldef$nbParams"); if (!is_null($defFrom)) { $arrayIndex = $mySource->getconditionsParameter($nbParams + 1, MySource::CONDITION_ARRAY); if (!is_null($arrayIndex)) { - $trueArrayIndex = array($arrayIndex => false); - $defFrom->addType(MyDefinition::TYPE_ARRAY); - $defFrom->setArrayValue($trueArrayIndex); + $newArrs = $defFrom->getCurrentState()->getOrCreateDefArrayIndex( + $defFrom->getBlockId(), + $defFrom, + $arrayIndex + ); + + $myElement = $newArrs[1][0]; + $myElement->getCurrentState()->setTainted(true); } - - // no expression needed it's a source - $defFrom->setTainted(true); } } @@ -390,88 +319,46 @@ public static function funccallSource($stackClass, $context, $data, $myClass, $i } } - $exprReturn = $instruction->getProperty(MyInstruction::EXPR); - - if ($exprReturn->isAssign()) { - $defAssign = $exprReturn->getAssignDef(); - - // sanitizers are deleted - $defAssign->setSanitized(false); - $defAssign->setTypeSanitized([]); - $defAssign->setCast(MyDefinition::CAST_NOT_SAFE); - - $myDef = new MyDefinition( - $myFuncCall->getLine(), - $myFuncCall->getColumn(), - $myFuncCall->getName()."_return" + $myDef = $virtualReturnDef; + + // sanitizers are deleted + $myDef->getCurrentState()->setSanitized(false); + $myDef->getCurrentState()->setTypeSanitized([]); + $myDef->getCurrentState()->setCast(MyDefinition::CAST_NOT_SAFE); + //$myDef->getCurrentState()->setTainted(true); + // no need to taintedbyexpr because it's source like _GET + + if ($mySource->getIsObject()) { + $myDef->getCurrentState()->addType(MyDefinition::TYPE_INSTANCE); + $myDef->getCurrentState()->addType(MyDefinition::ALL_PROPERTIES_TAINTED); + + $myDef->setClassName($myFuncCall->getName()."_built-in-class"); + HelpersAnalysis::createObject($context, $myDef); + } elseif ($mySource->getIsArrayOfArrays()) { + $myDef->getCurrentState()->addType(MyDefinition::TYPE_ARRAY_ARRAY); + $myDef->getCurrentState()->addType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED); + } elseif ($mySource->getIsArrayOfObjects()) { + $myDef->getCurrentState()->addType(MyDefinition::TYPE_ARRAY); + $myDef->getCurrentState()->addType(MyDefinition::TYPE_INSTANCE); + $myDef->getCurrentState()->addType(MyDefinition::ALL_PROPERTIES_TAINTED); + + $myDef->setClassName($myFuncCall->getName()."_built-in-class"); + HelpersAnalysis::createObject($context, $myDef); + } elseif ($mySource->getIsArray()) { + $myDef->getCurrentState()->addType(MyDefinition::TYPE_ARRAY); + $myDef->getCurrentState()->addType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED); + } elseif ($mySource->getIsReturnArray()) { + $newArrs = $myDef->getCurrentState()->getOrCreateDefArrayIndex( + $myDef->getBlockId(), + $myDef, + $mySource->getReturnArrayValue() ); - $myDef->setSourceMyFile($defAssign->getSourceMyFile()); - $myDef->setTainted(true); - // no need to taintedbyexpr because it's source like _GET - - if ($mySource->getIsObject()) { - $defAssign->addType(MyDefinition::TYPE_INSTANCE); - $defAssign->property->addProperty("PROGPILOT_ALL_PROPERTIES_TAINTED"); - - $defAssign->setTaintedByExpr($exprReturn); - $defAssign->setExpr($exprReturn); - - $exprReturn->addDef($defAssign); - } elseif ($mySource->getIsArray()) { - $defAssign->setArrayValue("PROGPILOT_ALL_INDEX_TAINTED"); - // we don't add type of TYPE_ARRAY because is a virtual array - // $row = mysqli_fetch_row - // echo $row[0] - // we don't want row as an array because it's value is constance PROGPILOT_ALL_INDEX_TAINTED - // which doesn't mean anything for the user - //$defAssign->addType(MyDefinition::TYPE_ARRAY); - - $defAssign->setTaintedByExpr($exprReturn); - $defAssign->setExpr($exprReturn); - - $exprReturn->addDef($defAssign); - } elseif ($mySource->getIsReturnArray() && $arrFuncCall === false) { - $valueArray = array($mySource->getReturnArrayValue() => false); - - $defAssign->addCopyArray($valueArray, $myDef); - $defAssign->addType(MyDefinition::TYPE_COPY_ARRAY); - - $exprReturn->addDef($myDef); - $myDef->setExpr($exprReturn); - } elseif ($mySource->getIsReturnArray()) { - $valueArray = array($mySource->getReturnArrayValue() => false); - - if ($arrFuncCall === $valueArray) { - $exprReturn->addDef($myDef); - $myDef->setExpr($exprReturn); - - if ($exprReturn->isAssign()) { - if (ResolveDefs::getVisibilityFromInstances($context, $data, $defAssign)) { - ValueAnalysis::copyValues($myDef, $defAssign); - TaintAnalysis::setTainted($myDef->isTainted(), $defAssign, $exprReturn); - } - } - } - } elseif (!$mySource->getIsReturnArray()) { - $exprReturn->addDef($myDef); - if ($exprReturn->isAssign()) { - if (ResolveDefs::getVisibilityFromInstances($context, $data, $defAssign)) { - ValueAnalysis::copyValues($myDef, $defAssign); - TaintAnalysis::setTainted($myDef->isTainted(), $defAssign, $exprReturn); - } - } - } - } - } - - return $hasSources; - } - public static function setTainted($tainted, $defAssign, $expr) - { - if ($tainted) { - $defAssign->setTainted(true); - $defAssign->setTaintedByExpr($expr); + $myElement = $newArrs[1][0]; + $myElement->getCurrentState()->setTainted(true); + } elseif (!$mySource->getIsReturnArray()) { + $myDef->getCurrentState()->setTainted(true); + } } } } diff --git a/package/src/progpilot/Analysis/TwigAnalysis.php b/package/src/progpilot/Analysis/TwigAnalysis.php index 7304d51f..a97a93bd 100644 --- a/package/src/progpilot/Analysis/TwigAnalysis.php +++ b/package/src/progpilot/Analysis/TwigAnalysis.php @@ -34,7 +34,7 @@ public static function funccall($context, $myFuncCall, $instruction) $template = $instruction->getProperty("argdef0"); $variable = $instruction->getProperty("argdef1"); - $file = $path."/".$template->getLastKnownValues()[0]; + $file = $path.DIRECTORY_SEPARATOR.$template->getLastKnownValues()[0]; $myJavascriptFile = new MyFile($file, $myFuncCall->getLine(), $myFuncCall->getColumn()); if (file_exists($file)) { @@ -47,8 +47,13 @@ public static function funccall($context, $myFuncCall, $instruction) $arrIndex = "{{".key($arr)."}}"; - $myDef = new MyDefinition($def->getLine(), $def->getColumn(), $arrIndex); - $myDef->setSourceMyFile($myJavascriptFile); + $myDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $def->getLine(), + $def->getColumn(), + $arrIndex + ); if ($def->isTainted()) { $myDef->setTainted(true); diff --git a/package/src/progpilot/Analysis/ValueAnalysis.php b/package/src/progpilot/Analysis/ValueAnalysis.php deleted file mode 100644 index 4fad6bf6..00000000 --- a/package/src/progpilot/Analysis/ValueAnalysis.php +++ /dev/null @@ -1,206 +0,0 @@ -getLastKnownValues() - // value = "id_temp1"."id_temp2"."id_temp3"; - foreach ($knownValues as $idTemp => $defsKnownValues) { // 1 - // def - // "id_temp1" - $defValues = []; - foreach ($defsKnownValues as $defKnownValues) { // 2 - // def->getLastKnownValues() - // "def_id_temp1" - foreach ($defKnownValues as $defKnownValue) { - if (Common::validLastKnownValue($defKnownValue) - && !in_array($defKnownValue, $defValues, true)) { - $defValues[] = $defKnownValue; - } - } - } - - if (count($finalDefValues) === 0) { - $finalDefValues = $defValues; - } else { - $newFinalDefValues = []; - - foreach ($finalDefValues as $finalDefValue) { - foreach ($defValues as $defValue) { - $newValue = $finalDefValue.$defValue; - if (Common::validLastKnownValue($defKnownValue) - && !in_array($newValue, $newFinalDefValues, true)) { - $newFinalDefValues[] = $newValue; - } - } - } - - $finalDefValues = $newFinalDefValues; - } - } - - foreach ($finalDefValues as $finalDefValue) { - $defAssign->addLastKnownValue($finalDefValue); - } - } - } - } - - public static function computeCastValues($defAssign, $expr) - { - if (isset(ValueAnalysis::$exprsCast[$expr])) { - $nbCastSafe = 0; - $castValues = ValueAnalysis::$exprsCast[$expr]; - - foreach ($castValues as $castValue) { - if ($castValue === MyDefinition::CAST_SAFE) { - $nbCastSafe ++; - } - } - - if ($nbCastSafe === count($castValues)) { - $defAssign->setCast(MyDefinition::CAST_SAFE); - } else { - $defAssign->setCast(MyDefinition::CAST_NOT_SAFE); - } - } - } - - public static function computeEmbeddedChars($defAssign, $expr) - { - $concatEmbeddedChars = []; - foreach ($expr->getDefs() as $def) { - foreach ($def->getIsEmbeddedByChars() as $embeddedChar => $boolean) { - $concatEmbeddedChars[] = $embeddedChar; - } - } - - foreach ($concatEmbeddedChars as $embeddedChar) { - $embeddedValue = false; - - foreach ($expr->getDefs() as $def) { - $boolean = $def->getIsEmbeddedByChar($embeddedChar); - - if ($boolean && $embeddedValue) { - $embeddedValue = false; - } - - if ($boolean && !$embeddedValue) { - $embeddedValue = true; - } - - if (!$boolean && $embeddedValue) { - $embeddedValue = true; - } - - if (!$boolean && !$embeddedValue) { - $embeddedValue = false; - } - } - - $defAssign->setIsEmbeddedByChar($embeddedChar, $embeddedValue); - } - } - - public static function computeSanitizedValues($defAssign, $expr) - { - $concatTypesSanitize = []; - foreach ($expr->getDefs() as $def) { - if ($def->isSanitized()) { - foreach ($def->getTypeSanitized() as $typeSanitized) { - $concatTypesSanitize["$typeSanitized"] = true; - } - } - } - - // foreach sanitize types - foreach ($concatTypesSanitize as $typeSanitized => $booleanTrue) { - $typeOk = true; - foreach ($expr->getDefs() as $def) { - // if we find a tainted value that is not sanitized the defassign is not sanitized - if (!$def->isTypeSanitized($typeSanitized) && $def->isTainted()) { - $typeOk = false; - } - } - - if ($typeOk) { - $defAssign->setSanitized(true); - $defAssign->addTypeSanitized($typeSanitized); - } - } - } - - public static function copyValues($def, $defAssign) - { - $defAssign->setIsEmbeddedByChars($def->getIsEmbeddedByChars(), true); - $defAssign->setCast($def->getCast()); - $defAssign->setLabel($def->getLabel()); - - if ($def->getArrayValue() === "PROGPILOT_ALL_INDEX_TAINTED") { - $defAssign->setArrayValue("PROGPILOT_ALL_INDEX_TAINTED"); - $defAssign->setTaintedByExpr($def->getExpr()); - } - - if ($def->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED")) { - $defAssign->property->addProperty("PROGPILOT_ALL_PROPERTIES_TAINTED"); - $defAssign->addType(MyDefinition::TYPE_INSTANCE); - $defAssign->setTaintedByExpr($def->getExpr()); - } - } -} diff --git a/package/src/progpilot/Analysis/VisitorAnalysis.php b/package/src/progpilot/Analysis/VisitorAnalysis.php index 19b5364a..9a246905 100644 --- a/package/src/progpilot/Analysis/VisitorAnalysis.php +++ b/package/src/progpilot/Analysis/VisitorAnalysis.php @@ -10,6 +10,8 @@ namespace progpilot\Analysis; +use function DeepCopy\deep_copy; + use progpilot\Objects\MyFile; use progpilot\Objects\MyOp; use progpilot\Objects\ArrayStatic; @@ -22,6 +24,9 @@ use progpilot\Code\Opcodes; use progpilot\Code\MyInstruction; +use progpilot\Helpers\State as HelpersState; +use progpilot\Helpers\Analysis as HelpersAnalysis; + use progpilot\Lang; use progpilot\Utils; use progpilot\Analyzer; @@ -29,9 +34,6 @@ class VisitorAnalysis { private $context; - private $currentStorageMyBlocks; - private $callStack; - private $myBlockStack; private $defs; private $blocks; @@ -42,10 +44,6 @@ class VisitorAnalysis public function __construct() { - $this->currentStorageMyBlocks = null; - $this->callStack = []; - $this->myBlockStack = []; - $this->currentMyFunc = null; $this->currentContextCall = null; $this->currentMyBlock = null; @@ -53,26 +51,21 @@ public function __construct() $this->defs = null; $this->blocks = null; } - + public function funcCall( - $myCode, $instruction, - $code, - $index, $funcName, - $arrFuncCall, $myFuncCall ) { $hasSources = false; - + $listMyFunc = []; + IncludeAnalysis::funccall( $this->context, - $this->defs, $this->blocks, - $instruction, - $code, - $index + $this->defs, + $instruction ); $stackClass = null; @@ -80,25 +73,28 @@ public function funcCall( $stackClass = ResolveDefs::funccallClass( $this->context, $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - $myFuncCall + $myFuncCall, + $instruction ); - $classOfFuncCallArr = $stackClass[count($stackClass) - 1]; + $classOfFuncCallArr = $stackClass[0]; + foreach ($classOfFuncCallArr as $classOfFuncCall) { $objectId = $classOfFuncCall->getObjectId(); $myClass = $this->context->getObjects()->getMyClassFromObject($objectId); - + if (!is_null($myClass)) { $visibility = true; - $method = $myClass->getMethod($funcName); + if (!ResolveDefs::getVisibilityMethod($myFuncCall->getNameInstance(), $method)) { $method = null; $visibility = false; } if (!is_null($method)) { - $method->getThisDef()->setObjectId($objectId); + // we assign the object of the instance to this-> + $method->getThisDef()->getCurrentState()->setObjectId($objectId); } // twig analysis @@ -109,40 +105,10 @@ public function funcCall( } } } - - $listMyFunc[] = [$objectId, $myClass, $method, $visibility]; - - $hasSources = TaintAnalysis::funccallSpecifyAnalysis( - $method, - $stackClass, - $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - $myClass, - $myFuncCall, - $arrFuncCall, - $instruction, - $myCode, - $index - ); + + $listMyFunc[] = [$objectId, $myClass, $method, $visibility, $classOfFuncCall]; } } - - // we didn't resolve any class so the class of method is unknown (undefined) - // but we authorize to specify method of unknown class during the configuration of sinks ... - if (count($classOfFuncCallArr) === 0) { - $hasSources = TaintAnalysis::funccallSpecifyAnalysis( - null, - $stackClass, - $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - null, - $myFuncCall, - $arrFuncCall, - $instruction, - $myCode, - $index - ); - } } elseif ($myFuncCall->isType(MyFunction::TYPE_FUNC_STATIC)) { $myClassStatic = $this->context->getClasses()->getMyClass( $myFuncCall->getNameInstance() @@ -160,69 +126,42 @@ public function funcCall( $visibility = false; } - $listMyFunc[] = [0, $myClassStatic, $method, $visibility]; + $listMyFunc[] = [0, $myClassStatic, $method, $visibility, null]; + + $myDefStatic = new MyDefinition( + $this->context->getCurrentBlock()->getId(), + $this->context->getCurrentMyFile(), + $myFuncCall->getLine(), + $myFuncCall->getColumn(), + "static" + ); - $myDefStatic = new MyDefinition($myFuncCall->getLine(), $myFuncCall->getColumn(), "static"); $idObjectTmp = $this->context->getObjects()->addObject(); - $myDefStatic->setObjectId($idObjectTmp); + $myDefStatic->getCurrentState()->setObjectId($idObjectTmp); $this->context->getObjects()->addMyclassToObject($idObjectTmp, $myClassStatic); $stackClass[0][0] = $myDefStatic; - - $hasSources = TaintAnalysis::funccallSpecifyAnalysis( - $method, - $stackClass, - $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - $myClassStatic, - $myFuncCall, - $arrFuncCall, - $instruction, - $myCode, - $index - ); } } else { $myFunc = $this->context->getFunctions()->getFunction($funcName); - $hasSources = TaintAnalysis::funccallSpecifyAnalysis( - $myFunc, - null, - $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - null, - $myFuncCall, - $arrFuncCall, - $instruction, - $myCode, - $index - ); - - $listMyFunc[] = [0, null, $myFunc, true]; + $listMyFunc[] = [0, null, $myFunc, true, null]; } - - \progpilot\Analysis\CustomAnalysis::mustVerifyDefinition( - $this->context, - $instruction, - $myFuncCall, - $stackClass - ); foreach ($listMyFunc as $list) { $objectId = $list[0]; $myClass = $list[1]; $myFunc = $list[2]; $visibility = $list[3]; + $instance = $list[4]; - ResolveDefs::instanceBuildThis( + \progpilot\Analysis\CustomAnalysis::mustVerifyDefinition( $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - $objectId, - $myClass, - $myFunc, - $myFuncCall + $instruction, + $myFuncCall, + $myClass ); - if (!is_null($myFunc) && !$this->inCallStack($myFunc)) { + if (!is_null($myFunc) && !$this->context->inCallStack($myFunc)) { // the called function is a method and this method exists in the class if (($myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD) || $myFuncCall->isType(MyFunction::TYPE_FUNC_STATIC)) @@ -230,504 +169,1204 @@ public function funcCall( || ((!$myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD) && !$myFuncCall->isType(MyFunction::TYPE_FUNC_STATIC)) && !$myFunc->isType(MyFunction::TYPE_FUNC_METHOD))) { - FuncAnalysis::funccallBefore( - $this->context, - $this->defs, - $myFunc, - $myFuncCall, - $instruction, - $this->context->getClasses() - ); + // we don't visit twice function with a long execution time + if (HelpersAnalysis::checkIfOneFunctionArgumentIsNew($myFunc, $instruction) + || !$myFunc->isVisited() + || ($myFunc->isType(MyFunction::TYPE_FUNC_METHOD) + && !$myFunc->isType(MyFunction::TYPE_FUNC_STATIC)) + || $myFunc->hasGlobalVariables() + || $myFunc->getName() === "{main}") { + // we clean all the param of the function + $funcCallBack = "Callbacks::cleanTaintedDef"; + HelpersAnalysis::forAllDefsOfFunction($funcCallBack, $myFunc); + + $myFunc->cleanOpInformations(); + + // we propagate the taint to the params + FuncAnalysis::funccallBefore($myFunc, $instruction); + + // we clean all the param of the function + // except return defs see functions21.php test case + $funcCallBack = "Callbacks::addStateDefAsAPastArgument"; + HelpersAnalysis::forAllArgumentsOfFunction($funcCallBack, $myFunc, $instruction); - $myCodefunction = new MyCode; - $myCodefunction->setCodes($myFunc->getMyCode()->getCodes()); - $myCodefunction->setStart(0); - $myCodefunction->setEnd(count($myFunc->getMyCode()->getCodes())); - - $this->analyze($myCodefunction, $myFuncCall); + $myFunc->setIsVisited(true); + $myFunc->reset(); + + $myCodefunction = new MyCode; + $myCodefunction->setCodes($myFunc->getMyCode()->getCodes()); + $myCodefunction->setStart(0); + $myCodefunction->setEnd(count($myFunc->getMyCode()->getCodes())); + + $this->analyzeFunc($myFunc, $myFuncCall); + + if ($myFunc->hasGlobalVariables()) { + // we don't want to visit it a second time, it's an approximation for performance + $myFunc->setHasGlobalVariables(false); + + foreach ($myFunc->getReturnDefs() as $returnDef) { + $returnDefCopy = deep_copy($returnDef); + $myFunc->addInitialReturnDef($returnDefCopy); + } + } + } else { + $funcCallBack = "Callbacks::addAttributesOfInitialReturnDefs"; + HelpersAnalysis::forAllReturnDefsOfFunction($funcCallBack, $myFunc); + } } } - - if (!$hasSources) { - FuncAnalysis::funccallAfter( - $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - $myFuncCall, - $myFunc, - $arrFuncCall, - $instruction, - $code[$index + 3] - ); - } + + FuncAnalysis::funccallAfter( + $myFunc, + $this->context, + $myClass, + $myFuncCall, + $instruction + ); $classOfFuncCall = null; if (is_null($myFunc)) { - ResolveDefs::funccallReturnValues( - $this->context, - $myFuncCall, - $instruction, - $myCode, - $index - ); - // representations start - $this->context->outputs->callgraph->addFuncCall( + $this->context->outputs->callgraphAddFuncCall( + $this->currentMyFunc, $this->currentMyBlock, $myFuncCall, $myClass ); // representations end } else { - $classOfFuncCall = $myFunc->getMyClass(); - // representations start foreach ($myFunc->getBlocks() as $myBlock) { - $this->context->outputs->callgraph->addChild( + $this->context->outputs->callgraphAddChild( + $this->currentMyFunc, $this->currentMyBlock, $myBlock ); - $this->context->outputs->cfg->addEdge( + $this->context->outputs->cfgAddEdge( + $this->currentMyFunc, $this->currentMyBlock, $myBlock ); break; } - $this->context->outputs->callgraph->addFuncCall( + $this->context->outputs->callgraphAddFuncCall( + $this->currentMyFunc, $this->currentMyBlock, $myFuncCall, $myClass ); // representations end } + } + } - ResolveDefs::instanceBuildBack( - $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - $myFunc, - $myClass, - $myFuncCall, - $visibility - ); - - $hasSources = TaintAnalysis::funccallSpecifyAnalysis( - $myFunc, - $stackClass, - $this->context, - $this->defs->getOutMinusKill($myFuncCall->getBlockId()), - $classOfFuncCall, - $myFuncCall, - $arrFuncCall, - $instruction, - $myCode, - $index - ); + public function getMyblock($context) + { + $this->context = $context; + } + + public function setContext($context) + { + $this->context = $context; + } + + public function fetchVariable($variable) + { + $defsFound = ResolveDefs::selectDefinitions( + $this->context, + $this->defs->getOutMinusKill($this->currentMyBlock->getId()), + $variable + ); + + $newDefFounds = []; + foreach ($defsFound as $defFound) { + $newDefFounds[] = $defFound; } + + return $newDefFounds; } - public function inCallStack($curFunc) + public function analyzeFunc($myFunc, $myFuncCalled = null) { - foreach ($this->callStack as $call) { - $callFunc = $call[0]; + if (HelpersAnalysis::checkIfTimeExecutionIsAcceptable($this->context, $myFunc)) { + $this->context->outputs->createRepresentationsForFunction($myFunc); + + $startTime = microtime(true); + + $this->currentContextCall = new \stdClass; + $this->currentContextCall->func_called = $myFuncCalled; + $this->currentContextCall->func_callee = $this->currentMyFunc; + + $this->currentMyFunc = $myFunc; + $this->context->setCurrentFunc($this->currentMyFunc); + + $this->defs = $this->currentMyFunc->getDefs(); + $this->blocks = $this->currentMyFunc->getBlocks(); - if ($callFunc->getName() === $curFunc->getName() - && !$callFunc->isType(MyFunction::TYPE_FUNC_METHOD) - && !$curFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - return true; + $val = [$this->currentMyFunc, + $this->blocks, + $this->defs, + $this->currentContextCall]; + + $this->context->pushToCallStack($val); + + // for the properties data flow + $firstBlockIdCalled = $this->currentMyFunc->getFirstBlockId(); + $firstMyBlockCalled = $this->currentMyFunc->getBlockById($firstBlockIdCalled); + $blockOfCallee = $this->currentMyBlock; + + if (!is_null($firstMyBlockCalled) + && !is_null($blockOfCallee)) { + $firstMyBlockCalled->setVirtualParents([$blockOfCallee]); + $firstMyBlockCalled->setNeedUpdateOfState(true); } - if ($callFunc->getName() === $curFunc->getName() - && $callFunc->isType(MyFunction::TYPE_FUNC_METHOD) - && $curFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - $curClass = $curFunc->getMyClass(); - $callClass = $callFunc->getMyClass(); - if ($curClass->getName() === $callClass->getName()) { - return true; + if ($this->currentMyFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + $myClass = $this->currentMyFunc->getMyClass(); + if (!is_null($myClass)) { + $constructor = $myClass->getMethod("__construct"); + if (!is_null($constructor)) { + $lastBlockIdConstuctor = $constructor->getFirstBlockId(); + $lastMyBlockConstuctor = $constructor->getBlockById($lastBlockIdConstuctor); + + if (!is_null($firstMyBlockCalled) + && !is_null($lastMyBlockConstuctor)) { + $firstMyBlockCalled->addVirtualParent($lastMyBlockConstuctor); + } + } + + $thisDef = $this->currentMyFunc->getThisDef(); + if ($thisDef->getCurrentState()->getObjectId() === -1) { + // we enter in a method with no instance + // to analyze frameworks (see frameworks/codeigniter1.php) + // we need a default "this" + $idObject = $this->context->getObjects()->addObject(); + $thisDef->getCurrentState()->setObjectId($idObject); + $myClass = clone $myClass; + + $this->context->getObjects()->addMyclassToObject($idObject, $myClass); + } } } - } + // end - return false; - } + $this->currentMyFunc->setStartExecutionTime(microtime(true)); + $this->currentMyFunc->setNbExecutions($this->currentMyFunc->getNbExecutions() + 1); - public function getMyblock($context) - { - $this->context = $context; + $error = false; + foreach ($myFunc->getBlocks() as $myBlock) { + if (!$this->analyzeblock($myFunc, $myBlock)) { + $error = true; + break; + } + } + + foreach ($myFunc->getBlocks() as $myBlock) { + $myBlock->setHasBeenAnalyzed(false); + } + + $diffTime = microtime(true) - $myFunc->getStartExecutionTime(); + $myFunc->setLastExecutionTime($diffTime); + + if ($myFunc->getName() === "{main}") { + // free memory + unset($myFunc); + return; + } + + $this->context->popFromCallStack(); + + $callStack = $this->context->getCallStack(); + if (!empty($callStack)) { + $lastElement = $callStack[count($callStack) -1]; + + $this->currentContextCall = $lastElement[3]; + $this->defs = $lastElement[2]; + $this->blocks = $lastElement[1]; + $this->currentMyFunc = $lastElement[0]; + $this->context->setCurrentFunc($this->currentMyFunc); + + + $this->currentMyBlock = $blockOfCallee; + $this->context->setCurrentBlock($this->currentMyBlock); + + // for the states data flow + $lastBlockIdsCalled = $myFunc->getLastBlockIds(); + foreach ($lastBlockIdsCalled as $lastBlockIdCalled) { + $lastMyBlockCalled = $myFunc->getBlockById($lastBlockIdCalled); + // "leave block" has popped the callee block normally + $blockOfCallee = $this->currentMyBlock; + + if (!is_null($lastMyBlockCalled) && !is_null($blockOfCallee)) { + // we add a new parent and remove the old parents + // because the new parent kill the others + // see oop/simple13.php + $blockOfCallee->setVirtualParents([$lastMyBlockCalled]); + $blockOfCallee->setNeedUpdateOfState(true); + } + } + + $returnDefs = $myFunc->getReturnDefs(); + foreach ($returnDefs as $returnDef) { + $state = $returnDef->getCurrentState(); + $currentBlockId = $this->context->getCurrentBlock()->getId(); + $returnDef->assignStateToBlockId($state->getId(), $currentBlockId); + } + + // we enter in a new block this "it's a blockswitching" and we need to update states + // of the previous function we just left + HelpersState::blockSwitching($this->context, $myFunc); + // end + } + + // memory could has been released + if (!$error) { + $tmpCallgraph = $this->context->outputs->callgraph[$myFunc->getId()]; + $tmpCallgraph->computeCallGraph(); + \progpilot\Analysis\CustomAnalysis::mustVerifyCallFlow($this->context, $tmpCallgraph); + } + } } - public function setContext($context) + + public function analyzeblock($myFunc, $myBlock, $blockStack = []) { - $this->context = $context; + $myBlock->setHasBeenAnalyzed(false); + + $myFunc->getMyCode()->setStart($myBlock->getStartAddressBlock()); + $myFunc->getMyCode()->setEnd($myBlock->getEndAddressBlock()); + + return $this->analyzecode($myFunc->getMyCode(), $blockStack, true); } - public function analyze($myCode, $myFuncCalled = null) + public function analyzecode($myCode, $blockStack, $fromParent = false) { $startTime = microtime(true); $index = $myCode->getStart(); $code = $myCode->getCodes(); - - if ($this->context->getCurrentNbDefs() > $this->context->getLimitDefs()) { - Utils::printWarning($this->context, Lang::MAX_DEFS_EXCEEDED); - return; - } + $originalFlow = []; do { if (isset($code[$index])) { $instruction = $code[$index]; - - if ((microtime(true) - $startTime) > $this->context->getLimitTime()) { + + if ((microtime(true) - $startTime) > $this->context->getMaxFileAnalysisDuration()) { Utils::printWarning($this->context, Lang::MAX_TIME_EXCEEDED); - return; + return false; } - switch ($instruction->getOpcode()) { - case Opcodes::ENTER_BLOCK: - $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); + if (HelpersAnalysis::checkCallStackReachMaxTime($this->context)) { + Utils::printWarning($this->context, Lang::MAX_TIME_EXCEEDED); + return false; + } - if ($this->currentStorageMyBlocks->contains($myBlock)) { - array_pop($this->myBlockStack); + if (memory_get_usage() > ($this->context->getMaxMemory() / 2)) { + Utils::printWarning($this->context, Lang::MAX_MEMORY_EXCEEDED); + $this->context->resetDataflow(); + $this->context->outputs->resetRepresentationsForAllFunctions(); + return false; + } - if (count($this->myBlockStack) > 0) { - $this->currentMyBlock = $this->myBlockStack[count($this->myBlockStack) - 1]; - } + $ret = $this->context->getCurrentFunc()->getNbsOpInformations(); + if ($ret > 1000) { + $this->context->getCurrentFunc()->cleanOpInformations(); + return false; + } + + // needed to have a proper opinformation eachtime: + $opInformation["chained_results"] = []; + if ($instruction->getOpcode() !== Opcodes::ARRAYDIM_FETCH + && $instruction->getOpcode() !== Opcodes::PROPERTY_FETCH) { + $originalFlow = []; + } + + switch ($instruction->getOpcode()) { + case Opcodes::ENTER_BLOCK: + $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); + if ($myBlock->hasBeenAnalyzed() || !$fromParent) { $index = $myBlock->getEndAddressBlock(); break; } - $this->currentMyBlock = $myBlock; - - array_push($this->myBlockStack, $this->currentMyBlock); - - $this->currentStorageMyBlocks->attach($myBlock); - - // we remove this parent because it's a loop while(block1) block2 // and block1 must be analysis before block2 - if (!$myBlock->getIsLoop()) { - foreach ($myBlock->parents as $blockParent) { + array_push($blockStack, $myBlock->getId()); + + foreach ($myBlock->parents as $blockParent) { + // if not in the stack (it's not a loop) + if ($blockParent->getId() !== $myBlock->getId() + && !$myBlock->isLoopParent($blockParent) + && !$blockParent->hasBeenAnalyzed() + && !in_array($blockParent->getId(), $blockStack, true)) { $addrStart = $blockParent->getStartAddressBlock(); $addrEnd = $blockParent->getEndAddressBlock(); - - if (!$this->currentStorageMyBlocks->contains($blockParent)) { - $oldIndexStart = $myCode->getStart(); - $oldIndexEnd = $myCode->getEnd(); - $myCode->setStart($addrStart); - $myCode->setEnd($addrEnd); + $oldIndexStart = $myCode->getStart(); + $oldIndexEnd = $myCode->getEnd(); - $this->analyze($myCode); + $myCode->setStart($addrStart); + $myCode->setEnd($addrEnd); - $myCode->setStart($oldIndexStart); - $myCode->setEnd($oldIndexEnd); - } + $this->analyzecode($myCode, $blockStack, $fromParent); + + $myCode->setStart($oldIndexStart); + $myCode->setEnd($oldIndexEnd); } } + + $fromParent = false; + $this->currentMyBlock = $myBlock; + $this->context->setCurrentBlock($myBlock); + + // we enter in a new block this "it's a blockswitching" and we need to update states + $myBlock->setHasBeenAnalyzed(true); + $myBlock->setNeedUpdateOfState(true); + + HelpersState::blockSwitching($this->context, $this->currentMyFunc); break; case Opcodes::LEAVE_BLOCK: - array_pop($this->myBlockStack); + $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); + array_pop($blockStack); - if (count($this->myBlockStack) > 0) { - $this->currentMyBlock = $this->myBlockStack[count($this->myBlockStack) - 1]; + if (!empty($blockStack)) { + $this->currentMyBlock = $blockStack[count($blockStack) - 1]; + $this->context->setCurrentBlock($this->currentMyBlock); } + // a block can "now" be visited twice (loops), so we put virtual parents added when visiting + // functions for instance to the original one + $myBlock->setVirtualParents($myBlock->getParents()); + break; + + case Opcodes::CONCAT_LEFT: + $leftid = $instruction->getProperty(MyInstruction::LEFTID); + $rightids = $instruction->getProperty(MyInstruction::RIGHTID); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + + $chars = ["'", "<", ">"]; - case Opcodes::LEAVE_FUNCTION: - $myFunc = $instruction->getProperty(MyInstruction::MYFUNC); + $leftOpInformation = $this->context->getCurrentFunc()->getOpInformation($leftid); - if ($myFunc->getName() === "{main}") { - return; + $opInformation = []; + $opInformation["chained_results"] = []; + $vars["chained_results"] = []; + + $lastNbChars = HelpersAnalysis::getNbChars("", $chars); + + $leftValues = []; + if (isset($leftOpInformation["chained_results"])) { + foreach ($leftOpInformation["chained_results"] as $chainedResult) { + if (!is_null($chainedResult->getCurrentState())) { + $curLastKnownValues = $chainedResult->getCurrentState()->getLastKnownValues(); + foreach ($curLastKnownValues as $curLastKnownValue) { + $leftValues[] = $curLastKnownValue; + } + + if (isset($curLastKnownValues[0])) { + HelpersAnalysis::updateNbChars($lastNbChars, $curLastKnownValues[0], $chars); + } + + if (!in_array($chainedResult, $vars["chained_results"], true) + && count($vars["chained_results"]) < 20) { + $vars["chained_results"][] = $chainedResult; + } + } + } } + + $rightValuesSets = []; + foreach ($rightids as $rightid) { + $rightOpInformation = $this->context->getCurrentFunc()->getOpInformation($rightid); + $rightValuesSet = []; + if (isset($rightOpInformation["chained_results"]) + && !empty($rightOpInformation["chained_results"])) { + foreach ($rightOpInformation["chained_results"] as $chainedResult) { + $curLastKnownValues = $chainedResult->getCurrentState()->getLastKnownValues(); + $rightValuesSet[] = $curLastKnownValues; + $chainedResult->getCurrentState()->updateIsEmbeddedByChars($lastNbChars); + + if (isset($curLastKnownValues[0])) { + HelpersAnalysis::updateNbChars($lastNbChars, $curLastKnownValues[0], $chars); + } + + if (!in_array($chainedResult, $vars["chained_results"], true) + && count($vars["chained_results"]) < 20) { + $vars["chained_results"][] = $chainedResult; + } + } + } else { + $rightValuesSet[] = ""; + } - $val = array_pop($this->callStack); + $rightValuesSets[] = $rightValuesSet; + } - $this->currentContextCall = $val[4]; - $this->currentStorageMyBlocks = $val[3]; - $this->defs = $val[2]; - $this->blocks = $val[1]; - - break; - + if (empty($leftValues)) { + $leftValues = []; + $leftValues[] = ""; + } - case Opcodes::ENTER_FUNCTION: - $this->currentContextCall = new \stdClass; - $this->currentContextCall->func_called = $myFuncCalled; - $this->currentContextCall->func_callee = $this->currentMyFunc; + $i = 1; + $possibleRightsParts = []; + $possibleRightsParts[0] = $leftValues; + foreach ($rightValuesSets as $rightValueSet) { + $possibleRightsParts[$i] = []; - $this->currentMyFunc = $instruction->getProperty(MyInstruction::MYFUNC); - $this->context->setCurrentFunc($this->currentMyFunc); + foreach ($rightValueSet as $rightValues) { + if (empty($rightValues)) { + $possibleRightsParts[$i][] = ""; + } else { + foreach ($rightValues as $rightValue) { + // $i = 0 + // [0][0] = ./dvwa/ + // [0][1] = ./folder/ + + // $i = 1 + // [1][0] = mid + + // $i = 2 + // [2][0] = low.php + // [2][1] = medium.php + $possibleRightsParts[$i][] = $rightValue; + } + } + } - $val = [ - $this->currentMyFunc, - $this->blocks, - $this->defs, - $this->currentStorageMyBlocks, - $this->currentContextCall]; - array_push($this->callStack, $val); + $i ++; + } - $this->currentStorageMyBlocks = new \SplObjectStorage; - $this->defs = $this->currentMyFunc->getDefs(); - $this->blocks = $this->currentMyFunc->getBlocks(); + /* + big approximation + we took only the first value of the left parts + ./dvwa/mid/low.php + ./dvwa/mid/medium.php + */ + + $concats = []; + $lastPart = count($possibleRightsParts) - 1; + foreach ($possibleRightsParts[$lastPart] as $lastRightPart) { + $stringConcat = ""; + for ($i = 0; $i < ($lastPart); $i ++) { + $stringConcat .= $possibleRightsParts[$i][0]; + } - break; - + $stringConcat .= $lastRightPart; + $concats[] = $stringConcat; + } + + $myTemp = new MyDefinition( + $this->context->getCurrentBlock()->getId(), + $this->context->getCurrentMyFile(), + $this->context->getCurrentLine(), + $this->context->getCurrentColumn(), + "built-in-concatenation" + ); + + $mergedState = HelpersState::mergeDefsBlockIdStates( + $vars["chained_results"], + $concats, + $this->context->getCurrentBlock() + ); + + $myTemp->addState($mergedState); + $currentBlockId = $this->context->getCurrentBlock()->getId(); + $myTemp->assignStateToBlockId($mergedState->getId(), $currentBlockId); + + $opInformation["chained_results"][] = $myTemp; + + //$this->context->getCurrentFunc()->cleanOpInformation($leftid); + foreach ($rightids as $rightid) { + $this->context->getCurrentFunc()->cleanOpInformation($rightid); + } + + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); - case Opcodes::DEFINITION: - $myDef = $instruction->getProperty(MyInstruction::DEF); break; - - case Opcodes::END_EXPRESSION: - $expr = $instruction->getProperty(MyInstruction::EXPR); + case Opcodes::ARRAYDIM_FETCH: + $arrayDim = $instruction->getProperty(MyInstruction::ARRAY_DIM); + $originalDef = $instruction->getProperty(MyInstruction::ORIGINAL_DEF); + + $varid = $instruction->getProperty(MyInstruction::VARID); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + + $opInformation = []; + $opInformation["chained_results"] = []; + $opInformation["array_dim"] = $arrayDim; + + // beginning of the chain: $originalDef[0][1] + if (!is_null($originalDef)) { + $originalFlow = []; + $originalFlow[] = $originalDef; + $originalFlow[] = "["; + $originalFlow[] = $arrayDim; + $originalFlow[] = "]"; + + if ($originalDef->getName() === "GLOBALS") { + $globalDef = new MyDefinition( + $this->context->getCurrentBlock()->getId(), + $this->context->getCurrentMyFile(), + $originalDef->getLine(), + $originalDef->getColumn(), + $arrayDim + ); + + $defGlobals = ResolveDefs::selectGlobals($this->context, $globalDef); + foreach ($defGlobals as $defGlobal) { + $opInformation["chained_results"][] = $defGlobal; + } + } else { + // we get the last definitions + $defsFound = ResolveDefs::selectArrays( + $this->context, + $this->defs->getOutMinusKill($this->currentMyBlock->getId()), + $originalDef, + $arrayDim + ); + + foreach ($defsFound as $defFound) { + // the element has just been created and right side (!expr) + if ($defFound[0]) { + if (HelpersAnalysis::isASource( + $this->context, + $originalDef, + null, + $arrayDim + )) { + foreach ($defFound[1] as $delEle) { + $delEle->getCurrentState()->setTainted(true); + } + } + } + + // just for the flow + foreach ($defFound[1] as $delEle) { + $delEle->original->setDef($originalFlow); + $delEle->original->setArrayIndexAccessor($arrayDim); + $opInformation["chained_results"][] = $delEle; + } + } + + // could be a built-in array/source + if (empty($defsFound)) { + // right side + if (HelpersAnalysis::isASource($this->context, $originalDef, null, $arrayDim)) { + $originalDef->getCurrentState()->setTainted(true); + // just for the flow + $originalDef->original->setDef($originalFlow); + $originalDef->original->setArrayIndexAccessor($arrayDim); - if ($expr->isAssign()) { - $defAssign = $expr->getAssignDef(); + $opInformation["chained_results"][] = $originalDef; + } + } + } - /* - * we have all the resolved defs so maybe when we have two def for one tempdef - * that could lead to abuse the compute of embedded chars for example - * but it's not because all def have the same name (they have been resolved) - * and so same embedded char of tempdef - */ + $opInformation["original_def"] = $originalDef; - ValueAnalysis::computeSanitizedValues($defAssign, $expr); - ValueAnalysis::computeEmbeddedChars($defAssign, $expr); - ValueAnalysis::computeCastValues($defAssign, $expr); - ValueAnalysis::computeKnownValues($defAssign, $expr); + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + } else { + // we are in the middle of the chain thus we can access the previous chained object + $previousOpInformation = $this->context->getCurrentFunc()->getOpInformation($varid); + + if (isset($previousOpInformation["chained_results"])) { + foreach ($previousOpInformation["chained_results"] as $previousChainedResult) { + $state = $previousChainedResult->getState($this->currentMyBlock->getId()); + if (!is_null($state)) { + $newArrs = $state->getOrCreateDefArrayIndex( + $this->currentMyBlock->getId(), + $previousChainedResult, + $arrayDim + )[1]; + + $previousToSlice = 3; + if (str_ends_with($previousChainedResult->getName(), "_return")) { + $previousToSlice = 4; + $originalFlow[] = $previousChainedResult; + } + $originalFlow[] = "["; + $originalFlow[] = $arrayDim; + $originalFlow[] = "]"; + + foreach ($newArrs as $newArr) { + // just for the flow + $newArr->original->setDef($originalFlow); + $opInformation["chained_results"][] = $newArr; + } + + if (count($previousOpInformation["chained_results"]) > 1) { + $endEle = count($originalFlow) - $previousToSlice; + $originalFlow = array_slice($originalFlow, 0, $endEle); + } + } + } + } + + if (isset($previousOpInformation["original_def"])) { + $opInformation["original_def"] = $previousOpInformation["original_def"]; + } + + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); } + $this->context->getCurrentFunc()->cleanOpInformation($varid); + break; + case Opcodes::STATIC_PROPERTY_FETCH: + $propertyName = $instruction->getProperty(MyInstruction::PROPERTY_NAME); + $originalDef = $instruction->getProperty(MyInstruction::ORIGINAL_DEF); + + $varid = $instruction->getProperty(MyInstruction::VARID); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + + // beginning of the chain: $originalDef->foo->bar + if (!is_null($originalDef)) { + $originalFlow = []; + $originalFlow[] = $originalDef; + $originalFlow[] = "::"; + $originalFlow[] = $propertyName; + + $originalDef->setId(0); + $defFound = ResolveDefs::selectStaticProperties( + $this->context, + $originalDef, + $propertyName + ); - case Opcodes::TEMPORARY: - $listOfMyTemp = []; - if ($instruction->isPropertyExist(MyInstruction::PHI)) { - for ($i = 0; $i < $instruction->getProperty(MyInstruction::PHI); $i++) { - $listOfMyTemp[] = $instruction->getProperty("temp_".$i); + if (!is_null($defFound)) { + // just for the flow + $defFound->original->setDef($originalFlow); + $opInformation["chained_results"][] = $defFound; + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); } - } else { - $listOfMyTemp[] = $instruction->getProperty(MyInstruction::TEMPORARY); } - - foreach ($listOfMyTemp as $tempDefa) { - $tempDefaMyExpr = $tempDefa->getExpr(); - $defAssignMyExpr = $tempDefaMyExpr->getAssignDef(); - - $sourceArr = $this->context->inputs->getSourceArrayByName( - $tempDefa, - $tempDefa->getArrayValue() + + $this->context->getCurrentFunc()->cleanOpInformation($varid); + + break; + + + case Opcodes::PROPERTY_FETCH: + $propertyName = $instruction->getProperty(MyInstruction::PROPERTY_NAME); + $originalDef = $instruction->getProperty(MyInstruction::ORIGINAL_DEF); + + $varid = $instruction->getProperty(MyInstruction::VARID); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + + $opInformation = []; + $opInformation["chained_results"] = []; + $opInformation["array_dim"] = null; + + // beginning of the chain: $originalDef->foo->bar + if (!is_null($originalDef)) { + $originalFlow[] = $originalDef; + $originalFlow[] = "->"; + $originalFlow[] = $propertyName; + + $originalDef->setId(0); + $defsFound = ResolveDefs::selectProperties( + $this->context, + $this->defs->getOutMinusKill($this->currentMyBlock->getId()), + $originalDef, + $propertyName ); - // if we use directly echo $_GET["b"]; - if (!is_null($sourceArr)) { - $tempDefa->setArrayValue("PROGPILOT_ALL_INDEX_TAINTED"); - $tempDefa->setLabel($sourceArr->getLabel()); - } - - if ($tempDefaMyExpr->isAssign() && !$tempDefaMyExpr->isAssignIterator()) { - ArrayAnalysis::copyArray( + + foreach ($defsFound as $defFoundArr) { + $defFound = $defFoundArr[0]; + $myClassFound = $defFoundArr[1]; + if (HelpersAnalysis::isASource($this->context, $defFound, $myClassFound, null)) { + $defFound->getCurrentState()->setTainted(true); + } + + \progpilot\Analysis\CustomAnalysis::defineObject( $this->context, - $this->defs->getOutMinusKill($tempDefa->getBlockId()), - $tempDefa, - $tempDefa->getArrayValue(), - $defAssignMyExpr, - $defAssignMyExpr->getArrayValue() + $instruction, + $defFound, + $myClassFound, + null ); + + // just for the flow + $defFound->original->setDef($originalFlow); + $opInformation["chained_results"][] = $defFound; } + + $opInformation["original_def"] = $originalDef; + } else { + $originalFlow[] = "->"; + $originalFlow[] = $propertyName; + // we are in the middle of the chain thus we can access the previous chained object + $previousOpInformation = $this->context->getCurrentFunc()->getOpInformation($varid); - // stackclass is null - // so if we have document a object HTMLDocument is created - $myClassNew = \progpilot\Analysis\CustomAnalysis::defineObject( - $this->context, - $tempDefa, - null - ); - - if (!is_null($myClassNew)) { - $objectId = $this->context->getObjects()->addObject(); - - $tempDefa->addType(MyDefinition::TYPE_INSTANCE); - $tempDefa->setObjectId($objectId); - - $myClass = $this->context->getClasses()->getMyClass($myClassNew->getName()); - - if (is_null($myClass)) { - $myClass = new MyClass( - $tempDefa->getLine(), - $tempDefa->getColumn(), - $myClassNew->getName() - ); + if (isset($previousOpInformation["original_def"])) { + $opInformation["original_def"] = $previousOpInformation["original_def"]; + } + + if (isset($previousOpInformation["chained_results"])) { + foreach ($previousOpInformation["chained_results"] as $previousChainedResult) { + $state = $previousChainedResult->getState($this->currentMyBlock->getId()); + if (!is_null($state)) { + $idObject = $state->getObjectId(); + $tmpMyClass = $this->context->getObjects()->getMyClassFromObject($idObject); + + if (!is_null($tmpMyClass)) { + $property = $tmpMyClass->getProperty( + $this->context, + $this->currentMyBlock->getId(), + $previousChainedResult, + $propertyName + ); + + if (!is_null($property) + && ResolveDefs::getVisibility( + $previousChainedResult, + $property, + $this->context->getCurrentFunc() + )) { + $opInformation["chained_results"][] = $property; + } + } + } } + } + } + + $this->context->getCurrentFunc()->cleanOpInformation($varid); + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + + break; + + + case Opcodes::LITERAL_FETCH: + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $def = $instruction->getProperty(MyInstruction::DEF); + + $opInformation = $this->context->getCurrentFunc()->getOpInformation($resultid); + + $opInformation["chained_results"][] = $def; + + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + + break; + - $this->context->getObjects()->addMyclassToObject($objectId, $myClass); + case Opcodes::CONST_FETCH: + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $def = $instruction->getProperty(MyInstruction::DEF); + + $opInformation = $this->context->getCurrentFunc()->getOpInformation($resultid); + + $defFounds = $this->fetchVariable($def); + if (empty($defFounds)) { + $opInformation["chained_results"][] = $def; + } else { + foreach ($defFounds as $defFound) { + if ($defFound->isType(MyDefinition::TYPE_CONSTANTE)) { + $opInformation["chained_results"][] = $defFound; + } } - ///////////////////////////////////////////////////////////// - - $tainted = false; - $stackClass = null; + } - if ($tempDefa->isType(MyDefinition::TYPE_PROPERTY)) { - $stackClass = ResolveDefs::propertyClass($this->context, $this->defs, $tempDefa); - $classOfTempDefArr = $stackClass[count($stackClass) - 1]; - - foreach ($classOfTempDefArr as $classOfTempDef) { - $objectIdTmp = $classOfTempDef->getObjectId(); - $myClassFromObject = - $this->context->getObjects()->getMyClassFromObject($objectIdTmp); - - if (!is_null($myClassFromObject)) { - $sourceTmp = $this->context->inputs->getSourceByName( - $stackClass, - $tempDefa, - false, - $myClassFromObject->getName(), - $tempDefa->getArrayValue() - ); - - if (!is_null($sourceTmp)) { - $tainted = true; - $tempDefa->setLabel($sourceTmp->getLabel()); + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + + break; + + + case Opcodes::ITERATOR: + $varid = $instruction->getProperty(MyInstruction::VARID); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + + $opDataVar = $this->context->getCurrentFunc()->getOpInformation($varid); + + $opInformation = []; + $opInformation["iterator"][] = true; + + if (!is_null($opDataVar) && isset($opDataVar["chained_results"])) { + foreach ($opDataVar["chained_results"] as $chainedResult) { + $state = $chainedResult->getCurrentState(); + if (!is_null($state)) { + if ($state->isType(MyDefinition::TYPE_ARRAY) + && !$state->isType(MyDefinition::TYPE_INSTANCE)) { + foreach ($state->getArrayIndexes() as $arrayIndex) { + $element = $arrayIndex->def; + $opInformation["chained_results"][] = $element; + } + } elseif (($state->isType(MyDefinition::TYPE_INSTANCE) + && $state->isType(MyDefinition::TYPE_ARRAY)) || + $state->isType(MyDefinition::TYPE_ARRAY_ARRAY)) { + // probably a source arrayofobjects arrayofarrays + if ($state->isType(MyDefinition::ALL_PROPERTIES_TAINTED)) { + $opInformation["chained_results"][] = $chainedResult; + } elseif ($state->isType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED)) { + $opInformation["chained_results"][] = $chainedResult; + } + } + } + } + } + + $this->context->getCurrentFunc()->cleanOpInformation($varid); + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + + break; + + + case Opcodes::VARIABLE_FETCH: + $varid = $instruction->getProperty(MyInstruction::VARID); + $exprid = $instruction->getProperty(MyInstruction::EXPRID); + $variable = $instruction->getProperty(MyInstruction::DEF); + + $id = is_null($varid) ? $exprid : $varid; + + $opInformation = []; + + $newDefFounds = []; + $defFounds = $this->fetchVariable($variable); + + foreach ($defFounds as $defFound) { + if ($defFound->isType(MyDefinition::TYPE_GLOBAL)) { + $defGlobals = ResolveDefs::selectGlobals($this->context, $defFound); + foreach ($defGlobals as $defGlobal) { + $newDefFounds[] = $defGlobal; + } + } elseif ($defFound->isType(MyDefinition::TYPE_REFERENCE)) { + foreach ($defFound->getRefs() as $ref) { + // a classic (not array/property) could have been overwritten + // we should search for the last variables + + if (!$ref->isType(MyDefinition::TYPE_ARRAY_ELEMENT) + && !$ref->isType(MyDefinition::TYPE_PROPERTY)) { + $refbis = clone $ref; + $refbis->setBlockId($variable->getBlockId()); + $refbis->setLine($variable->getLine()); + $refbis->setColumn($variable->getColumn()); + $defFoundsRef = $this->fetchVariable($refbis); + foreach ($defFoundsRef as $refBis) { + $newDefFounds[] = $refBis; } + } else { + $newDefFounds[] = $ref; } } + } elseif ($defFound->isType(MyDefinition::TYPE_ITERATOR)) { + foreach ($defFound->getIteratorValues() as $iteratorValue) { + // just for the flow + $originalFlow = []; + $originalFlow[] = $variable; + $iteratorValue->original->setDef($originalFlow); + $newDefFounds[] = $iteratorValue; + } } else { - $sourceTmp = $this->context->inputs->getSourceByName( - null, - $tempDefa, - false, - false, - $tempDefa->getArrayValue() - ); - - if (!is_null($sourceTmp)) { - $tainted = true; - $tempDefa->setLabel($sourceTmp->getLabel()); + $newDefFounds[] = $defFound; + } + } + + if (empty($defFounds)) { + // could be a built-in sources + // phpwander/test3.php + + $source = $this->context->inputs->getSourceByName($variable, null, null); + if (!is_null($source)) { + if ($source->getIsArray() && empty($source->getArrayValue())) { + $variable->getCurrentState()->addType(MyDefinition::TYPE_ARRAY); + $variable->getCurrentState()->addType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED); + $newDefFounds[] = $variable; } } - - $tempDefa->setTainted($tainted); + } + + $opInformation = []; + $opInformation["chained_results"] = $newDefFounds; + $opInformation["original_def"] = $variable; + $opInformation["array_dim"] = null; + + /* what to clean?? */ + $this->context->getCurrentFunc()->cleanOpInformation($varid); + $this->context->getCurrentFunc()->cleanOpInformation($exprid); + $this->context->getCurrentFunc()->storeOpInformation($id, $opInformation); + + break; + - $defs = ResolveDefs::temporarySimple( - $this->context, - $this->defs, - $tempDefa, - $tempDefaMyExpr->isAssignIterator(), - $tempDefaMyExpr->isAssign(), - $this->callStack + case Opcodes::ARGUMENT: + $varid = $instruction->getProperty(MyInstruction::VARID); + $idparam = $instruction->getProperty("idparam"); + $def = $instruction->getProperty("argdef$idparam"); + + $opDataVar = $this->context->getCurrentFunc()->getOpInformation($varid); + $concatValues = isset($opDataVar["concats_values"]) ? $opDataVar["concats_values"] : []; + + if (isset($opDataVar["chained_results"])) { + $mergedState = HelpersState::mergeDefsBlockIdStates( + $opDataVar["chained_results"], + $concatValues, + $this->context->getCurrentBlock() ); - ValueAnalysis::updateStorageToExpr($tempDefaMyExpr); - $storageCast = ValueAnalysis::$exprsCast[$tempDefaMyExpr]; - $storageKnownValues = ValueAnalysis::$exprsKnownValues[$tempDefaMyExpr]; - - foreach ($defs as $def) { - $safe = AssertionAnalysis::temporarySimple( - $this->context, - $this->defs, - $this->currentMyBlock, - $def, - $tempDefa - ); - - $visibility = ResolveDefs::getVisibilityFromInstances( - $this->context, - $this->defs->getOutMinusKill($def->getBlockId()), - $defAssignMyExpr - ); - - if ($visibility) { - $storageCast[] = $tempDefa->getCast(); - $storageKnownValues["".$tempDefa->getId().""][] = $def->getLastKnownValues(); + $def->addState($mergedState); + $currentBlockId = $this->context->getCurrentBlock()->getId(); + $def->assignStateToBlockId($mergedState->getId(), $currentBlockId); + } - $def->setIsEmbeddedByChars($tempDefa->getIsEmbeddedByChars(), true); - } + $this->context->getCurrentFunc()->cleanOpInformation($varid); - if ($visibility && !$safe) { - TaintAnalysis::setTainted($def->isTainted(), $defAssignMyExpr, $tempDefaMyExpr); - ValueAnalysis::copyValues($def, $defAssignMyExpr); - - if ($def->getLabel() === MyDefinition::SECURITY_HIGH) { - \progpilot\Analysis\CustomAnalysis::disclosureOfInformation( - $this->context, - $this->defs, - $defAssignMyExpr - ); - } - } + break; - // vérifier s'il y a pas de concat - // mise a jour de l'object - if ($def->isType(MyDefinition::TYPE_INSTANCE)) { - $defAssignMyExpr->addType(MyDefinition::TYPE_INSTANCE); - $defAssignMyExpr->setObjectId($def->getObjectId()); - $tmpMyClass = $this->context->getObjects()->getMyClassFromObject( - $def->getObjectId() - ); - if (!is_null($tmpMyClass)) { - foreach ($tmpMyClass->getProperties() as $property) { - $myDefTemp = new MyDefinition( - $tempDefa->getLine(), - $tempDefa->getColumn(), - $tempDefa->getName() - ); - $myDefTemp->addType(MyDefinition::TYPE_PROPERTY); - $myDefTemp->property->setProperties($property->property->getProperties()); - $myDefTemp->setBlockId($tempDefa->getBlockId()); - $myDefTemp->setSourceMyFile($tempDefa->getSourceMyFile()); - $myDefTemp->setId($tempDefa->getId()); + case Opcodes::END_ASSIGN: + $varid = $instruction->getProperty(MyInstruction::VARID); + $exprid = $instruction->getProperty(MyInstruction::EXPRID); + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $def = $instruction->getProperty(MyInstruction::DEF); + $literal = $instruction->getProperty(MyInstruction::LITERAL); + $reference = $instruction->getProperty(MyInstruction::REFERENCE); - $defsFound = ResolveDefs::selectProperties( - $this->context, - $this->defs->getOutMinusKill($tempDefa->getBlockId()), - $myDefTemp, - true + $opVarData = $this->context->getCurrentFunc()->getOpInformation($varid); + $opExprData = $this->context->getCurrentFunc()->getOpInformation($exprid); + $opResultData = $this->context->getCurrentFunc()->getOpInformation($resultid); + + $concatValues = isset($opExprData["concats_values"]) ? $opExprData["concats_values"] : []; + + if (is_null($opExprData) && !is_null($literal)) { + $opExprData["chained_results"] = []; + $opExprData["chained_results"][] = $literal; + } + + // return function $def case for instance + if (is_null($opVarData) && !is_null($def)) { + $opVarData["chained_results"] = []; + $opVarData["chained_results"][] = $def; + } + + // don't need to resolve variable we have already access to it + // ssa = 1) result=var3 2) expr=var3 + if (!is_null($opExprData) && isset($opExprData["chained_results"])) { + $mergedState = HelpersState::mergeDefsBlockIdStates( + $opExprData["chained_results"], + $concatValues, + $this->context->getCurrentBlock() + ); + + if (!is_null($opVarData) && isset($opVarData["chained_results"])) { + foreach ($opVarData["chained_results"] as $chainedResult) { + if (isset($opExprData["iterator"]) && $opExprData["iterator"]) { + $chainedResult->addType(MyDefinition::TYPE_ITERATOR); + $chainedResult->setIteratorValues($opExprData["chained_results"]); + } else { + if ($chainedResult->getNbStates() < 20) { + $chainedResult->addState($mergedState); + $currentBlockId = $this->context->getCurrentBlock()->getId(); + $chainedResult->assignStateToBlockId( + $mergedState->getId(), + $currentBlockId ); - - foreach ($defsFound as $defFound) { - if ($defFound->isType(MyDefinition::TYPE_COPY_ARRAY)) { - $property->setCopyArrays($defFound->getCopyArrays()); - $property->addType(MyDefinition::TYPE_COPY_ARRAY); - } - - TaintAnalysis::setTainted( - $defFound->isTainted(), - $property, - $defFound->getTaintedByExpr() - ); - - if ($defFound->isSanitized()) { - $property->setSanitized(true); - foreach ($defFound->getTypeSanitized() as $typeSanitized) { - $property->addTypeSanitized($typeSanitized); - } - } + + if ($reference) { + $chainedResult->addType(MyDefinition::TYPE_REFERENCE); + $chainedResult->setRefs($opExprData["chained_results"]); } } } + $opResultData["chained_results"][] = $chainedResult; } + + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opResultData); } - - ValueAnalysis::$exprsCast[$tempDefaMyExpr] = $storageCast; - ValueAnalysis::$exprsKnownValues[$tempDefaMyExpr] = $storageKnownValues; } + $this->context->getCurrentFunc()->cleanOpInformation($varid); + $this->context->getCurrentFunc()->cleanOpInformation($exprid); + break; + case Opcodes::CAST: + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $exprid = $instruction->getProperty(MyInstruction::EXPRID); + $typeCast = $instruction->getProperty("type_cast"); + + $rightOpInformation = $this->context->getCurrentFunc()->getOpInformation($exprid); + $leftOpInformation = + $this->context->getCurrentFunc()->getOpInformation($resultid); + + if (isset($rightOpInformation["chained_results"]) + && $typeCast === MyDefinition::CAST_NOT_SAFE) { + foreach ($rightOpInformation["chained_results"] as $chainedResult) { + $leftOpInformation["chained_results"][] = $chainedResult; + } + + $this->context->getCurrentFunc()->storeOpInformation($resultid, $leftOpInformation); + } + + $this->context->getCurrentFunc()->cleanOpInformation($exprid); + + break; + + + + case Opcodes::COND_BOOLEAN_NOT: + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $exprid = $instruction->getProperty(MyInstruction::EXPRID); + + $rightOpInformation = $this->context->getCurrentFunc()->getOpInformation($exprid); + $rightOpInformation["not_boolean"] = true; + + $this->context->getCurrentFunc()->storeOpInformation($resultid, $rightOpInformation); + $this->context->getCurrentFunc()->cleanOpInformation($exprid); + + break; + + + + case Opcodes::BINARYOP: + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $leftid = $instruction->getProperty(MyInstruction::LEFTID); + $rightid = $instruction->getProperty(MyInstruction::RIGHTID); + + $leftOpInformation = $this->context->getCurrentFunc()->getOpInformation($leftid); + $rightOpInformation = $this->context->getCurrentFunc()->getOpInformation($rightid); + + $opInformation = []; + $opInformation["condition_defs"] = []; + + if (isset($leftOpInformation["condition_defs"])) { + $opInformation["condition_defs"] = $leftOpInformation["condition_defs"]; + } + + if (isset($rightOpInformation["condition_defs"])) { + $opInformation["condition_defs"] = + array_merge($rightOpInformation["condition_defs"], $opInformation["condition_defs"]); + } + + $this->context->getCurrentFunc()->cleanOpInformation($leftid); + $this->context->getCurrentFunc()->cleanOpInformation($rightid); + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + + break; + + + case Opcodes::COND_START_IF: + $conds = $instruction->getProperty(MyInstruction::EXPRID); + $myBlockIf = $instruction->getProperty(MyInstruction::MYBLOCK_IF); + $myBlockElse = $instruction->getProperty(MyInstruction::MYBLOCK_ELSE); + $opExprData = $this->context->getCurrentFunc()->getOpInformation($conds); + + $validwhenreturning = true; + if (isset($opExprData["valid_when_returning"])) { + $validwhenreturning = $opExprData["valid_when_returning"]; + } + + $notboolean = false; + if (isset($opExprData["not_boolean"]) + && $opExprData["not_boolean"]) { + $notboolean = true; + $block = $validwhenreturning ? $myBlockElse : $myBlockIf; + } else { + $block = $validwhenreturning ? $myBlockIf : $myBlockElse; + } + + if (!is_null($opExprData) && isset($opExprData["condition_defs"])) { + foreach ($opExprData["condition_defs"] as $chainedResult) { + $callback = "Callbacks::addValidAssertion"; + HelpersAnalysis::forEachTaintedByDefs($chainedResult, $block, $callback); + } + } + + foreach ($block->getReturnDefs() as $defReturn) { + $defReturn->setReturnedFromValidator(true); + $defReturn->setValidWhenReturning($validwhenreturning); + $defReturn->setValidNotBoolean($notboolean); + } + + $this->context->getCurrentFunc()->cleanOpInformation($conds); + + break; + + + case Opcodes::ARRAY_EXPR: + $resultid = $instruction->getProperty(MyInstruction::RESULTID); + $opInformation = $this->context->getCurrentFunc()->getOpInformation($resultid); + $nbkeys = $instruction->getProperty("nbkeys"); + + for ($i = 0; $i < $nbkeys; $i ++) { + $valueid = $instruction->getProperty("value".$i); + $keyid = $instruction->getProperty("key".$i); + + $keyData = $this->context->getCurrentFunc()->getOpInformation($keyid); + $valueData = $this->context->getCurrentFunc()->getOpInformation($valueid); + + $keys = []; + + if (!is_null($keyData) && isset($keyData["chained_results"])) { + foreach ($keyData["chained_results"] as $chainedResult) { + $lastKnownValues = $chainedResult->getCurrentState()->getLastKnownValues(); + foreach ($lastKnownValues as $lastKnownValue) { + $keys[] = $lastKnownValue; + } + } + } else { + $keys[] = $i; + } + + $valuesDef = []; + if (!is_null($valueData) && isset($valueData["chained_results"])) { + foreach ($valueData["chained_results"] as $chainedResult) { + $valuesDef[] = $chainedResult; + } + } + + $myTemp = new MyDefinition( + $this->context->getCurrentBlock()->getId(), + $this->context->getCurrentMyFile(), + $this->context->getCurrentLine(), + $this->context->getCurrentColumn(), + "tmp_array" + ); + + $myTemp->getCurrentState()->addType(MyDefinition::TYPE_ARRAY); + + foreach ($keys as $key) { + $newEle = $myTemp->getCurrentState()->createDefArrayIndex( + $myTemp->getBlockId(), + $myTemp, + $key + )[1][0]; + + $mergedState = HelpersState::mergeDefsBlockIdStates( + $valuesDef, + [], + $this->context->getCurrentBlock() + ); + + $newEle->addState($mergedState); + $currentBlockId = $this->context->getCurrentBlock()->getId(); + $newEle->assignStateToBlockId($mergedState->getId(), $currentBlockId); + } + + $opInformation["chained_results"][] = $myTemp; + + $this->context->getCurrentFunc()->cleanOpInformation($keyid); + $this->context->getCurrentFunc()->cleanOpInformation($valueid); + $this->context->getCurrentFunc()->storeOpInformation($resultid, $opInformation); + } + + break; + + case Opcodes::FUNC_CALL: $funcName = $instruction->getProperty(MyInstruction::FUNCNAME); - $arrFuncCall = $instruction->getProperty(MyInstruction::ARR); $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); - $myExpr = $instruction->getProperty(MyInstruction::EXPR); - + if ($funcName === "call_user_func" || $funcName === "call_user_func_array") { if ($instruction->isPropertyExist("argdef0")) { $defArg = $instruction->getProperty("argdef0"); @@ -742,23 +1381,25 @@ public function analyze($myCode, $myFuncCalled = null) if ($funcName === "call_user_func") { for ($nbParams = 1; $nbParams < $myFuncCall->getNbParams(); $nbParams ++) { $oldDefArg = $instruction->getProperty("argdef$nbParams"); - $oldExprArg = $instruction->getProperty("argexpr$nbParams"); $newNbParams = $nbParams - 1; $newInst->addProperty("argdef$newNbParams", $oldDefArg); - $newInst->addProperty("argexpr$newNbParams", $oldExprArg); } $myFunctionCall->setNbParams($myFuncCall->getNbParams() - 1); } else { if ($instruction->isPropertyExist("argdef1")) { $defArgParam = $instruction->getProperty("argdef1"); - - if ($defArgParam->isType(MyDefinition::TYPE_COPY_ARRAY)) { + if ($defArgParam->getCurrentState()->isType(MyDefinition::TYPE_ARRAY)) { $newNbParams = 0; - foreach ($defArgParam->getCopyArrays() as $copyArray) { - $newInst->addProperty("argdef$newNbParams", $copyArray[1]); - $newInst->addProperty("argexpr$newNbParams", $copyArray[1]->getExpr()); + $arrayIndexes = $defArgParam->getCurrentState()->getArrayIndexes(); + + foreach ($arrayIndexes as $arrayIndex) { + // simulate argument operation + $newdef = clone $arrayIndex->def; + $newdef->removeType(MyDefinition::TYPE_ARRAY_ELEMENT); + + $newInst->addProperty("argdef$newNbParams", $newdef); $newNbParams ++; } @@ -769,32 +1410,22 @@ public function analyze($myCode, $myFuncCalled = null) } $newInst->addProperty(MyInstruction::MYFUNC_CALL, $myFunctionCall); - $newInst->addProperty(MyInstruction::EXPR, $myExpr); - $newInst->addProperty(MyInstruction::ARR, $arrFuncCall); - - foreach ($defArg->getLastKnownValues() as $lastValue) { + + foreach ($defArg->getCurrentState()->getLastKnownValues() as $lastValue) { $myFunctionCall->setName($lastValue); $newInst->addProperty(MyInstruction::FUNCNAME, $lastValue); $this->funcCall( - $myCode, $newInst, - $code, - $index, $lastValue, - $arrFuncCall, $myFunctionCall ); } } } else { $this->funcCall( - $myCode, $instruction, - $code, - $index, $funcName, - $arrFuncCall, $myFuncCall ); } @@ -805,5 +1436,7 @@ public function analyze($myCode, $myFuncCalled = null) $index = $index + 1; } } while (isset($code[$index]) && $index <= $myCode->getEnd()); + + return true; } } diff --git a/package/src/progpilot/Analyzer.php b/package/src/progpilot/Analyzer.php index 0954fe17..70e69bbd 100644 --- a/package/src/progpilot/Analyzer.php +++ b/package/src/progpilot/Analyzer.php @@ -9,9 +9,13 @@ namespace progpilot; +use PHPCfg\Printer; use progpilot\Utils; use progpilot\Code\MyCode; use progpilot\Objects\MyFile; +use progpilot\Helpers\Analysis as HelpersAnalysis; + +use function DeepCopy\deep_copy; class Analyzer { @@ -35,22 +39,21 @@ public function __construct() public function getFilesOfDir($context, $dir, &$files) { - if (is_dir($dir) && !$context->inputs->isExcludedFolder($dir)) { + if (is_dir($dir) && !$context->inputs->isExcludedFile($dir)) { $filesanddirs = @scandir($dir); if ($filesanddirs !== false) { foreach ($filesanddirs as $filedir) { if ($filedir !== '.' && $filedir !== "..") { - $folderorfile = $dir."/".$filedir; + $folderorfile = $dir.DIRECTORY_SEPARATOR.$filedir; if (is_dir($folderorfile)) { - if (!$context->inputs->isExcludedFolder($folderorfile)) { - $this->getFilesOfDir($context, $folderorfile, $files); - } + $this->getFilesOfDir($context, $folderorfile, $files); } else { if (!$context->inputs->isExcludedFile($folderorfile)) { if (!in_array($folderorfile, $files, true) && realpath($folderorfile)) { - $files[] = realpath($folderorfile); + $fileToAdd = realpath($folderorfile); + $files[] = $fileToAdd; } } } @@ -68,7 +71,7 @@ public function parsePhp($context) if (!is_null($context->inputs->getFile()) || !is_null($context->inputs->getCode())) { try { if (is_null($context->inputs->getCode())) { - $fileContent = file_get_contents($context->inputs->getFile()); + $fileContent = @file_get_contents($context->inputs->getFile()); if (Analyzer::getTypeOfLanguage(Analyzer::PHP, $fileContent)) { $context->inputs->setCode($fileContent); $context->setPath(dirname($context->inputs->getFile())); @@ -79,7 +82,8 @@ public function parsePhp($context) $script = $this->parser->parse($context->inputs->getCode(), ""); } } - } catch (\PhpParser\Error $e) { + } catch (\Exception $e) { + Utils::printWarning($context, Lang::PARSER_ERROR.$e->getMessage()); } } @@ -95,138 +99,97 @@ public function transformPhp($context, $script) $transformvisitor->setContext($context); $traverser->addVisitor($transformvisitor); $traverser->traverse($script); - + unset($traverser); unset($transformvisitor); + + /* + $dumper = new \PHPCfg\Printer\Text(); + echo $dumper->printScript($script); + */ } } - public function runInternalFunction($context, $myFunc) + public function runFunctionAnalysis($context, $myFunc, $updatemyfile = true) { - if (!is_null($myFunc) && !$myFunc->isAnalyzed()) { - $myFunc->setIsAnalyzed(true); + if (!is_null($myFunc) && !$myFunc->isVisited()) { + $myFunc->setIsVisited(true); $myFunc->getMyCode()->setStart(0); $myFunc->getMyCode()->setEnd(count($myFunc->getMyCode()->getCodes())); - \progpilot\Analysis\ValueAnalysis::buildStorage(); - $visitoranalyzer = new \progpilot\Analysis\VisitorAnalysis; + + if ($updatemyfile) { + $file = $myFunc->getSourceMyFile()->getName(); + $myFile = new MyFile($file, 0, 0); + $context->inputs->setFile($file); + $context->setCurrentMyfile($myFile); + } + + $params = $myFunc->getParams(); + foreach ($params as $param) { + $param->setParamToArg(null); + } + $visitoranalyzer->setContext($context); - $visitoranalyzer->analyze($myFunc->getMyCode()); + $visitoranalyzer->analyzeFunc($myFunc, null, true); + foreach ($myFunc->getReturnDefs() as $returnDef) { + $returnDefCopy = deep_copy($returnDef); + $myFunc->addInitialReturnDef($returnDefCopy); + } + + $context->resetInternalValues(); unset($visitoranalyzer); } else { // throw main function missing } } - public function runInternalAnalysis($context, $includedDefs = null) + public function visitDataFlow($context) { - $startTime = microtime(true); - // analyze if (!is_null($context)) { - $contextFunctions = []; - if (!is_null($context->getFunctions()->getFunctions())) { - foreach ($context->getFunctions()->getFunctions() as $functionsName) { - if (!is_null($functionsName)) { - foreach ($functionsName as $myFunc) { - $contextFunctions[] = $myFunc; - } - } - } - } - - foreach ($context->getClasses()->getListClasses() as $myClass) { - $contextFunctions = array_merge($contextFunctions, $myClass->getMethods()); - } - $visitordataflow = new \progpilot\Dataflow\VisitorDataflow(); - - foreach ($contextFunctions as $myFunc) { - if (!is_null($myFunc) && !$myFunc->isDataAnalyzed()) { - $myFunc->setIsDataAnalyzed(true); - $visitordataflow->analyze($context, $myFunc, $includedDefs); - } - } - - if ($context->getCurrentNbDefs() > $context->getLimitDefs()) { - Utils::printWarning($context, Lang::MAX_DEFS_EXCEEDED); - return; - } - - if ((microtime(true) - $startTime) > $context->getLimitTime()) { - Utils::printWarning($context, Lang::MAX_TIME_EXCEEDED); - return; - } - - if (!$context->getAnalyzeFunctions()) { - $this->runInternalFunction($context, $context->getFunctions()->getFunction("{main}")); - } else { - foreach ($contextFunctions as $myFunc) { - $this->runInternalFunction($context, $myFunc); - - if ((microtime(true) - $startTime) > $context->getLimitTime()) { - Utils::printWarning($context, Lang::MAX_TIME_EXCEEDED); - return; + + $fileNameHash = hash("sha256", $context->getCurrentMyfile()->getName()); + foreach ($context->getTmpFunctions() as $myFunc) { + if (!is_null($myFunc)) { + $visitordataflow->analyze($context, $myFunc); + + $className = "function"; + if (!is_null($myFunc->getMyclass())) { + $className = $myFunc->getMyclass()->getName(); } + + $context->getFunctions()->addFunction($fileNameHash, $className, $myFunc->getName(), $myFunc); } } - - if ($context->getCurrentNbDefs() > $context->getLimitDefs()) { - Utils::printWarning($context, Lang::MAX_DEFS_EXCEEDED); - return; - } - - $context->outputs->callgraph->computeCallGraph(); - \progpilot\Analysis\CustomAnalysis::mustVerifyCallFlow($context); - unset($visitordataflow); + $context->clearTmpFunctions(); } } - public function runInternalPhp($context, $includedDefs = null, $transform = true) + public function computeDataFlowPhp($context) { - // check if it is PHP language ????? LIKE myJavascriptFile - $startTime = microtime(true); - // free memory if (function_exists('gc_mem_caches')) { gc_mem_caches(); } - - if ($context->getCurrentNbDefs() > $context->getLimitDefs()) { - Utils::printWarning($context, Lang::MAX_DEFS_EXCEEDED); - return; - } - if ($transform) { - $pastResults = &$context->outputs->getResults(); - $context->resetInternalValues(); - $context->outputs->setResults($pastResults); - - $script = $this->parsePhp($context); - - if ((microtime(true) - $startTime) > $context->getLimitTime()) { - Utils::printWarning($context, Lang::MAX_TIME_EXCEEDED); - return; - } + $pastResults = &$context->outputs->getResults(); + $context->resetInternalValues(); + $context->outputs->setResults($pastResults); - $this->transformPhp($context, $script); + $script = $this->parsePhp($context); - unset($script); + $this->transformPhp($context, $script); - if ((microtime(true) - $startTime) > $context->getLimitTime()) { - Utils::printWarning($context, Lang::MAX_TIME_EXCEEDED); - return; - } - - $this->runInternalAnalysis($context, $includedDefs); - } + $this->visitDataFlow($context); } - public function runInternalJs($context, $includedDefs = null, $transform = true) + public function computeDataFlowJs($context) { $startTime = microtime(true); @@ -234,26 +197,21 @@ public function runInternalJs($context, $includedDefs = null, $transform = true) if (function_exists('gc_mem_caches')) { gc_mem_caches(); } - - if (!extension_loaded("v8js")) { - Utils::printWarning($context, Lang::V8JS_NOTLOADED); - return; - } - - if ($context->getCurrentNbDefs() > $context->getLimitDefs()) { - Utils::printWarning($context, Lang::MAX_DEFS_EXCEEDED); - return; - } - if (!is_null($context->inputs->getFile()) && $transform) { - $content = file_get_contents($context->inputs->getFile()); + if (!is_null($context->inputs->getFile())) { + $content = @file_get_contents($context->inputs->getFile()); if (Analyzer::getTypeOfLanguage(Analyzer::JS, $content)) { + if (!extension_loaded("v8js")) { + Utils::printWarning($context, Lang::V8JS_NOTLOADED); + return; + } + $pastResults = &$context->outputs->getResults(); $context->resetInternalValues(); $context->outputs->setResults($pastResults); - if ((microtime(true) - $startTime) > $context->getLimitTime()) { + if ((microtime(true) - $startTime) > $context->getMaxFileAnalysisDuration()) { Utils::printWarning($context, Lang::MAX_TIME_EXCEEDED); return; } @@ -262,41 +220,41 @@ public function runInternalJs($context, $includedDefs = null, $transform = true) $transformvisitor->setContext($context); $transformvisitor->v8jsExecute(); - if ((microtime(true) - $startTime) > $context->getLimitTime()) { + if ((microtime(true) - $startTime) > $context->getMaxFileAnalysisDuration()) { Utils::printWarning($context, Lang::MAX_TIME_EXCEEDED); return; } - $this->runInternalAnalysis($context, $includedDefs); + $this->visitDataFlow($context); } } } - public function runAllInternal($context) + public function computeDataFlow($context) { - if (!file_exists($context->inputs->getFile()) && is_null($context->inputs->getCode())) { + $filename = $context->inputs->getFile(); + + if (!file_exists($filename) && is_null($context->inputs->getCode())) { Utils::printWarning( $context, - Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($context->inputs->getFile()).")" + Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($filename).")" ); - } elseif (is_null($context->inputs->getFile()) && is_null($context->inputs->getCode())) { + } elseif (is_null($filename) && is_null($context->inputs->getCode())) { Utils::printWarning($context, Lang::FILE_AND_CODE_ARE_NULL); } else { if (is_null($context->inputs->getCode()) - && filesize($context->inputs->getFile()) > $context->getLimitSize()) { + && @filesize($filename) > $context->getMaxFileSize()) { Utils::printWarning( $context, - Lang::MAX_SIZE_EXCEEDED." (".Utils::encodeCharacters($context->inputs->getFile()).")" + Lang::MAX_SIZE_EXCEEDED." (".Utils::encodeCharacters($filename).")" ); } else { - if ($context->inputs->isLanguage(Analyzer::PHP)) { - $context->resetDataflow(); - $this->runInternalPhp($context); - } - - if ($context->inputs->isLanguage(Analyzer::JS)) { - $context->resetDataflow(); - $this->runInternalJs($context); + if (!$context->isFileDataAnalyzed($filename)) { + if ($context->inputs->isLanguage(Analyzer::PHP)) { + $this->computeDataFlowPhp($context); + } elseif ($context->inputs->isLanguage(Analyzer::JS)) { + $this->computeDataFlowJs($context); + } } } } @@ -313,38 +271,126 @@ public static function getTypeOfLanguage($lookfor, $content) if ($lookfor === Analyzer::JS && !empty($content)) { if (strpos($content, "var") !== false - || strpos($content, "int") !== false - || strpos($content, "this") !== false - || strpos($content, "let") !== false - || strpos($content, "export") !== false - || strpos($content, "import") !== false) { + || strpos($content, "let") !== false + || strpos($content, "export") !== false + || strpos($content, "import") !== false) { return true; } } return false; } + + public function getNamespace($context, $file) + { + try { + if (@filesize($file) <= $context->getMaxFileSize()) { + $contents = @file_get_contents($file); + preg_match('/namespace (.*);/', $contents, $matches); + if (isset($matches[1])) { + $context->addFileandNamepace($file, $matches[1]); + } + + $script = $this->parser->parse($contents, $file); + + $traverser = new \PHPCfg\Traverser(); + $callvisitor = new \progpilot\CallVisitor(); + $callvisitor->setContext($context); + $traverser->addVisitor($callvisitor); + $traverser->traverse($script); + } + } catch (\Exception $e) { + Utils::printWarning($context, Lang::PARSER_ERROR.$e->getMessage()); + } + } + + public function computeDataFlowOfNamespaces($context, $file) + { + $nsCalls = $context->getCallsToNamespace($file); + if (!is_null($nsCalls)) { + foreach ($nsCalls as $nsCall) { + $fileToInclude = $context->getFileFromNamespace($nsCall); + if (!is_null($fileToInclude) && !$context->isFileDataAnalyzed($fileToInclude)) { + $myFileToInclude = new MyFile($fileToInclude, 0, 0); + $context->inputs->setFile($fileToInclude); + $context->setCurrentMyfile($myFileToInclude); + + $this->computeDataFlow($context); + + // file is now data analyzed + $context->addDataAnalyzedFile($fileToInclude); + // we look for namespaces of this file + $this->computeDataFlowOfNamespaces($context, $fileToInclude); + } + } + } + } + + public function runAnalysisOfCurrentMyFile($context) + { + $functions = $context->getFunctions()->getFunctions(); + $fileNameHash = hash("sha256", $context->getCurrentMyfile()->getName()); + + $myFuncsOfFile = []; + // we take all function except mains + if (isset($functions["$fileNameHash"])) { + $myFuncsToAnalyze = $functions["$fileNameHash"]; + // we put the functions at the top (will be analyzed at first) + // could impact initialreturndef and funccall to be analyzed or not + foreach ($myFuncsToAnalyze as $myFuncsByClass) { + foreach ($myFuncsByClass as $myFunc) { + // func with global variables except to be analyzed/called from a main + if (!$myFunc->hasGlobalVariables() && $myFunc->getName() !== "{main}") { + $myFuncsOfFile[] = $myFunc; + } + } + } + + foreach ($myFuncsToAnalyze as $myFuncsByClass) { + foreach ($myFuncsByClass as $myFunc) { + if ($myFunc->getName() == "{main}") { + $myFuncsOfFile[] = $myFunc; + } + } + } + } + + foreach ($myFuncsOfFile as $myFunc) { + $this->runFunctionAnalysis($context, $myFunc); + $context->resetCallStack(); + } + } public function run($context, $cmdFiles = null) { + $currentMemoryLimit = ini_get("memory_limit"); + if (!$currentMemoryLimit) { + $currentMemoryLimit = 0; + } + + $bytes = HelpersAnalysis::getBytes($currentMemoryLimit); + if ($bytes < $context->getMaxMemory()) { + $ret = ini_set('memory_limit', $context->getMaxMemory()); + if (!$ret) { + Utils::printWarning($context, Lang::CANNOT_SET_MEMORY.$context->getMaxMemory()); + } + } + $files = []; $context->readConfiguration(); - $context->inputs->readExcludes(); - $context->inputs->readIncludes(); + // try to resolve incorrect included/excluded file paths + $context->inputs->resolvePaths(); - $context->inputs->readDev(); + // add all configurations inside frameworks folders except if overwritten $context->inputs->readFrameworks(); - $context->inputs->readSanitizers(); - $context->inputs->readSinks(); - $context->inputs->readSources(); - $context->inputs->readResolvedIncludes(); - $context->inputs->readValidators(); - $context->inputs->readFalsePositives(); - $context->inputs->readCustomRules(); - $includedFiles = $context->inputs->getIncludedFiles(); - $includedFolders = $context->inputs->getIncludedFolders(); + // add common configuration except if overwritten + $context->inputs->readDefaultSanitizers(); + $context->inputs->readDefaultSinks(); + $context->inputs->readDefaultSources(); + $context->inputs->readDefaultValidators(); + $context->inputs->readDefaultCustomRules(); if ($cmdFiles !== null) { foreach ($cmdFiles as $cmdFile) { @@ -359,17 +405,19 @@ public function run($context, $cmdFiles = null) } } + $includedFiles = $context->inputs->getInclusions(); + foreach ($includedFiles as $includedFile) { - if (!in_array($includedFile, $files, true) + if (is_dir($includedFile)) { + $this->getFilesOfDir($context, $includedFile, $files); + } else { + if (!in_array($includedFile, $files, true) && !$context->inputs->isExcludedFile($includedFile)) { - $files[] = $includedFile; + $files[] = $includedFile; + } } } - foreach ($includedFolders as $includedFolder) { - $this->getFilesOfDir($context, $includedFolder, $files); - } - if (!is_null($context->inputs->getFolder())) { $this->getFilesOfDir($context, $context->inputs->getFolder(), $files); } else { @@ -382,29 +430,53 @@ public function run($context, $cmdFiles = null) } } + // a first pass to identify namespaces and calls foreach ($files as $file) { - $context->setCurrentNbDefs(0); + if (is_file($file)) { + $myFile = new MyFile($file, 0, 0); + $context->inputs->setFile($file); + $context->setCurrentMyfile($myFile); - if ($context->getPrintFile()) { - echo "progpilot analyze : ".Utils::encodeCharacters($file)."\n"; + $this->getNamespace($context, $file); } + } + + foreach ($files as $file) { + if (is_file($file)) { + if ($context->isDebugMode()) { + echo "progpilot analyze : ".Utils::encodeCharacters($file)."\n"; + } - $context->outputs->setCountAnalyzedFiles( - $context->outputs->getCountAnalyzedFiles() + 1 - ); + $context->outputs->setCountAnalyzedFiles( + $context->outputs->getCountAnalyzedFiles() + 1 + ); + + // for each file we look for required namespaces to include + $this->computeDataFlowOfNamespaces($context, $file); + + $myFile = new MyFile($file, 0, 0); + $context->inputs->setFile($file); + $context->setCurrentMyfile($myFile); - $myFile = new MyFile($file, 0, 0); - $context->inputs->setFile($file); - $context->setCurrentMyfile($myFile); - $this->runAllInternal($context); - $context->inputs->setCode(null); + $this->computeDataFlow($context); + // the file is now data analyzed + $context->addDataAnalyzedFile($file); + $this->runAnalysisOfCurrentMyFile($context); + + $context->resetIncludedFiles(); + + // needed?? + $context->inputs->setCode(null); + } } - if (count($files) === 0 && !is_null($context->inputs->getCode())) { - $this->runAllInternal($context); + // if no files try to read code + if (empty($files) && !is_null($context->inputs->getCode())) { + $this->computeDataFlow($context); + $this->runAnalysis($context); } - if ($context->outputs->getResolveIncludes()) { + if ($context->outputs->getWriteIncludeFailures()) { $context->outputs->writeIncludesFile(); } } diff --git a/package/src/progpilot/CallVisitor.php b/package/src/progpilot/CallVisitor.php new file mode 100644 index 00000000..812901e6 --- /dev/null +++ b/package/src/progpilot/CallVisitor.php @@ -0,0 +1,73 @@ +context = $context; + } + + public function enterScript(Script $script) + { + } + + public function leaveScript(Script $script) + { + } + + public function leaveFunc(Func $func) + { + } + + public function enterFunc(Func $func) + { + } + + public function enterBlock(Block $block, Block $prior = null) + { + } + + public function skipBlock(Block $block, Block $prior = null) + { + } + + public function leaveOp(Op $op, Block $block) + { + } + + public function leaveBlock(Block $block, Block $prior = null) + { + } + + public function enterOp(Op $op, Block $block) + { + if (isset($op->class->value)) { + if (strpos($op->class->value, "\\") !== false) { + preg_match('/(.*)\\\(.*)/', $op->class->value, $matches); + if (isset($matches[1])) { + // and $matches[2] is the name of the class + $this->context->addCallToNamespace($matches[1]); + } + } + } + } +} diff --git a/package/src/progpilot/Code/MyCode.php b/package/src/progpilot/Code/MyCode.php index b03c5c62..03342f40 100644 --- a/package/src/progpilot/Code/MyCode.php +++ b/package/src/progpilot/Code/MyCode.php @@ -13,7 +13,6 @@ use progpilot\Objects\MyOp; use progpilot\Objects\MyBlock; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyFunction; class MyCode @@ -72,7 +71,7 @@ public static function readCode($context, $codeInput, $myJavascriptFile) { $myFunction = new MyFunction("{main}"); $context->setCurrentMycode($myFunction->getMyCode()); - $context->getFunctions()->addFunction($myFunction->getName(), $myFunction); + $context->getFunctions()->addFunction("file.js", "function", $myFunction->getName(), $myFunction); $instFunc = new MyInstruction(Opcodes::ENTER_FUNCTION); $instFunc->addProperty(MyInstruction::MYFUNC, $myFunction); @@ -97,7 +96,7 @@ public static function readCode($context, $codeInput, $myJavascriptFile) $edges = $codeInput[$nbInst ++]; $nbEdges = (int) $codeInput[$nbInst ++]; - $myBlock = new MyBlock; + $myBlock = new MyBlock($context->getCurrentLine(), $context->getCurrentColumn()); $myBlock->setId($myBlockId); $myBlock->setStartAddressBlock(count($myFunction->getMyCode()->getCodes())); @@ -140,6 +139,7 @@ public static function readCode($context, $codeInput, $myJavascriptFile) $defColumn = (int) $codeInput[$nbInst ++]; $myDef = new MyDefinition($defLine, $defColumn, $defName); + $myDef->setSourceMyFile($myJavascriptFile); $arrayDefinitions[] = $myDef; @@ -181,21 +181,13 @@ public static function readCode($context, $codeInput, $myJavascriptFile) $funcDefIdParam = (int) $codeInput[$nbInst ++]; $funcExprIdParam = (int) $codeInput[$nbInst ++]; $funcDefParam = $arrayDefinitions[$funcDefIdParam]; - $funcExprParam = $arrayExprs[$funcExprIdParam]; $instFuncCallMain->addProperty("argdef$j", $funcDefParam); - $instFuncCallMain->addProperty("argexpr$j", $funcExprParam); } $funcExprString = $codeInput[$nbInst ++]; $funcExprId = (int) $codeInput[$nbInst ++]; - // !!!!???? - //$myExpr = $arrayExprs[$funcExprId]; - $myExpr = null; - $instFuncCallMain->addProperty(MyInstruction::MYFUNC_CALL, $myFunctionCall); - $instFuncCallMain->addProperty(MyInstruction::EXPR, $myExpr); - $instFuncCallMain->addProperty(MyInstruction::ARR, null); $myFunction->getMyCode()->addCode($instFuncCallMain); break; @@ -230,41 +222,6 @@ public static function readCode($context, $codeInput, $myJavascriptFile) case 'end_assign': $myFunction->getMyCode()->addCode(new MyInstruction(Opcodes::END_ASSIGN)); - break; - - case 'start_expression': - $myFunction->getMyCode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - break; - - case 'end_expression': - $exprString = $codeInput[$nbInst ++]; - $exprLine = (int) $codeInput[$nbInst ++]; - $exprColumn = (int) $codeInput[$nbInst ++]; - - $myExpr = new MyExpr($exprLine, $exprColumn); - - $exprIsAssign = $codeInput[$nbInst ++]; - - if ($exprIsAssign === "true") { - $exprDefAssignId = (int) $codeInput[$nbInst ++]; - $myExpr->setAssign(true); - $myExpr->setAssignDef($exprDefAssignId); - } - - $nbExprs = (int) $codeInput[$nbInst ++]; - - $arrayExprs[] = $myExpr; - - for ($i = 0; $i < $nbExprs; $i ++) { - $defId = (int) $codeInput[$nbInst ++]; - $myExpr->addDef($defId); - } - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $myFunction->getMyCode()->addCode($instEndExpr); - break; } } @@ -281,34 +238,6 @@ public static function readCode($context, $codeInput, $myJavascriptFile) } } - foreach ($arrayExprs as $myExpr) { - $defs = $myExpr->getDefs(); - $myExpr->setDefs(array()); - - if ($myExpr->isAssign()) { - $defId = $myExpr->getAssignDef(); - if (isset($arrayDefinitions[$defId])) { - $myDef = $arrayDefinitions[$defId]; - $myExpr->setAssignDef($myDef); - } - } - - foreach ($defs as $defId) { - if (isset($arrayDefinitions[$defId])) { - $myDef = $arrayDefinitions[$defId]; - $myExpr->addDef($myDef); - } - } - } - - foreach ($arrayDefinitions as $myDef) { - $expr = $myDef->getExpr(); - - if (isset($arrayExprs[$expr])) { - $myExpr = $arrayExprs[$expr]; - $myDef->setExpr($myExpr); - } - } $instFunc = new MyInstruction(Opcodes::LEAVE_FUNCTION); $instFunc->addProperty(MyInstruction::MYFUNC, $myFunction); @@ -322,6 +251,7 @@ public static function readCode($context, $codeInput, $myJavascriptFile) public function printStdout() { $index = 0; + $currentFunc = null; do { if (isset($this->code[$index])) { @@ -329,21 +259,22 @@ public function printStdout() echo "[$index] "; switch ($instruction->getOpcode()) { case Opcodes::ENTER_FUNCTION: - echo Opcodes::ENTER_FUNCTION."\n"; + echo "Opcodes::ENTER_FUNCTION\n"; $myFunc = $instruction->getProperty(MyInstruction::MYFUNC); + $currentFunc = $myFunc; echo "name = ".htmlentities($myFunc->getName(), ENT_QUOTES, 'UTF-8')."\n"; break; case Opcodes::CLASSE: - echo Opcodes::CLASSE."\n"; + echo "Opcodes::CLASSE\n"; $myClass = $instruction->getProperty(MyInstruction::MYCLASS); echo "name = ".htmlentities($myClass->getName(), ENT_QUOTES, 'UTF-8')."\n"; break; case Opcodes::ENTER_BLOCK: - echo Opcodes::ENTER_BLOCK."\n"; + echo "Opcodes::ENTER_BLOCK\n"; $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); echo "id = ".$myBlock->getId()."\n"; @@ -351,7 +282,7 @@ public function printStdout() break; case Opcodes::LEAVE_BLOCK: - echo Opcodes::LEAVE_BLOCK."\n"; + echo "Opcodes::LEAVE_BLOCK\n"; $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); echo "id = ".$myBlock->getId()."\n"; @@ -359,13 +290,13 @@ public function printStdout() break; case Opcodes::LEAVE_FUNCTION: - echo Opcodes::LEAVE_FUNCTION."\n"; + echo "Opcodes::LEAVE_FUNCTION\n"; break; case Opcodes::FUNC_CALL: - echo Opcodes::FUNC_CALL."\n"; + echo "Opcodes::FUNC_CALL\n"; $funcname = htmlentities( $instruction->getProperty(MyInstruction::FUNCNAME), @@ -375,70 +306,144 @@ public function printStdout() echo "name = $funcname\n"; break; - case Opcodes::START_EXPRESSION: - echo Opcodes::START_EXPRESSION."\n"; - break; - - case Opcodes::END_EXPRESSION: - echo Opcodes::END_EXPRESSION."\n"; - $myExpr = $instruction->getProperty(MyInstruction::EXPR); - echo "expression et tainted = ".$myExpr->isTainted()."\n"; - break; - case Opcodes::CONCAT_LIST: - echo Opcodes::CONCAT_LIST."\n"; + echo "Opcodes::CONCAT_LIST\n"; break; case Opcodes::CONCAT_LEFT: - echo Opcodes::CONCAT_LEFT."\n"; + echo "Opcodes::CONCAT_LEFT\n"; break; case Opcodes::CONCAT_RIGHT: - echo Opcodes::CONCAT_RIGHT."\n"; + echo "Opcodes::CONCAT_RIGHT\n"; break; case Opcodes::RETURN_FUNCTION: - echo Opcodes::RETURN_FUNCTION."\n"; + echo "Opcodes::RETURN_FUNCTION\n"; break; case Opcodes::START_ASSIGN: - echo Opcodes::START_ASSIGN."\n"; + echo "Opcodes::START_ASSIGN\n"; break; case Opcodes::END_ASSIGN: - echo Opcodes::END_ASSIGN."\n"; + echo "Opcodes::END_ASSIGN\n"; break; case Opcodes::COND_BOOLEAN_NOT: - echo Opcodes::COND_BOOLEAN_NOT."\n"; + echo "Opcodes::COND_BOOLEAN_NOT\n"; break; case Opcodes::COND_START_IF: - echo Opcodes::COND_START_IF."\n"; - break; - - case Opcodes::TEMPORARY: - echo Opcodes::TEMPORARY."\n"; - $listOfMyTemp = []; - if ($instruction->isPropertyExist(MyInstruction::PHI)) { - for ($i = 0; $i < $instruction->getProperty(MyInstruction::PHI); $i++) { - $listOfMyTemp[] = $instruction->getProperty("temp_".$i); - } - } else { - $listOfMyTemp[] = $instruction->getProperty(MyInstruction::TEMPORARY); + echo "Opcodes::COND_START_IF\n"; + break; + + case Opcodes::DEFINITION: + echo "Opcodes::DEFINITION\n"; + $defname = htmlentities( + $instruction->getProperty(MyInstruction::DEF)->getName(), + ENT_QUOTES, + 'UTF-8' + ); + echo "name = $defname\n"; + + $instruction->getProperty(MyInstruction::DEF)->printStdout(); + + break; + + case Opcodes::PROPERTY_FETCH: + echo "Opcodes::PROPERTY_FETCH\n"; + $propertyName = htmlentities( + $instruction->getProperty(MyInstruction::PROPERTY_NAME), + ENT_QUOTES, + 'UTF-8' + ); + echo "property name = $propertyName\n"; + + break; + + case Opcodes::STATIC_PROPERTY_FETCH: + echo "Opcodes::STATIC_PROPERTY_FETCH\n"; + $propertyName = htmlentities( + $instruction->getProperty(MyInstruction::PROPERTY_NAME), + ENT_QUOTES, + 'UTF-8' + ); + echo "property name = $propertyName\n"; + + break; + + case Opcodes::ARRAYDIM_FETCH: + echo "Opcodes::ARRAYDIM_FETCH\n"; + $varid = $instruction->getProperty(MyInstruction::VARID); + $exprid = $instruction->getProperty(MyInstruction::EXPRID); + echo "varid = '$varid'\n"; + echo "exprid = '$exprid'\n"; + $arrayDim = $instruction->getProperty(MyInstruction::ARRAY_DIM); + echo "array dim = $arrayDim\n"; + + break; + + case Opcodes::VARIABLE_FETCH: + echo "Opcodes::VARIABLE_FETCH\n"; + $varid = $instruction->getProperty(MyInstruction::VARID); + $exprid = $instruction->getProperty(MyInstruction::EXPRID); + echo "varid = '$varid'\n"; + echo "exprid = '$exprid'\n"; + $instruction->getProperty(MyInstruction::DEF)->printStdout(); + + break; + + case Opcodes::VARIABLE: + echo "Opcodes::VARIABLE\n"; + $variableName = $instruction->getProperty(MyInstruction::VARIABLE_NAME); + echo "variable name = $variableName\n"; + + break; + + case Opcodes::ASSIGN: + echo "Opcodes::ASSIGN\n"; + + break; + + case Opcodes::ARGUMENT: + echo "Opcodes::ARGUMENT\n"; + + break; + + case Opcodes::CAST: + echo "Opcodes::CAST\n"; + + break; + + case Opcodes::CONST_FETCH: + echo "Opcodes::CONST_FETCH\n"; + + break; + + case Opcodes::LITERAL_FETCH: + echo "Opcodes::LITERAL_FETCH\n"; + $def = $instruction->getProperty(MyInstruction::DEF); + if (isset($def->getCurrentState()->getLastKnownValues()[0])) { + $literal = $def->getCurrentState()->getLastKnownValues()[0]; + echo "literal = $literal\n"; } - foreach ($listOfMyTemp as $def) { - $def->printStdout(); - } + break; + case Opcodes::ITERATOR: + echo "Opcodes::ITERATOR\n"; + break; - case Opcodes::DEFINITION: - echo Opcodes::DEFINITION."\n"; - $def = $instruction->getProperty(MyInstruction::DEF); - $def->printStdout(); + case Opcodes::ARRAY_EXPR: + echo "Opcodes::ARRAY_EXPR\n"; + + break; + case Opcodes::BINARYOP: + echo "Opcodes::BINARYOP\n"; + break; } diff --git a/package/src/progpilot/Code/MyInstruction.php b/package/src/progpilot/Code/MyInstruction.php index 2247b371..e056b898 100644 --- a/package/src/progpilot/Code/MyInstruction.php +++ b/package/src/progpilot/Code/MyInstruction.php @@ -12,38 +12,30 @@ class MyInstruction { - /* - const MYBLOCK = 1; - const MYBLOCK_IF = 2; - const MYBLOCK_ELSE = 3; - const MYFUNC = 4; - const NOT_BOOLEAN = 5; - const EXPR = 6; - const MYCLASS = 7; - const DEF = 8; - const RETURN_DEFS = 9; - const FUNCNAME = 10; - const TYPE_INCLUDE = 11; - const MYFUNC_CALL = 12; - const ARR = 13; - const TEMPORARY = 14; - */ - - const MYBLOCK = "myblock"; - const MYBLOCK_IF = "myblock_if"; - const MYBLOCK_ELSE = "myblock_else"; - const MYFUNC = "myfunc"; - const NOT_BOOLEAN = "not_boolean"; - const EXPR = "expr"; - const MYCLASS = "myclass"; - const DEF = "def"; - const RETURN_DEFS = "return_defs"; - const FUNCNAME = "funcname"; - const TYPE_INCLUDE = "type_include"; - const MYFUNC_CALL = "myfunc_call"; - const ARR = "arr"; - const TEMPORARY = "temporary"; - const PHI = "phi"; + const MYBLOCK = 1; + const MYBLOCK_IF = 2; + const MYBLOCK_ELSE = 3; + const MYFUNC = 4; + const NOT_BOOLEAN = 5; + const MYCLASS = 7; + const DEF = 8; + const RETURN_DEFS = 9; + const FUNCNAME = 10; + const TYPE_INCLUDE = 11; + const MYFUNC_CALL = 12; + const PHI = 15; + const ORIGINAL_DEF = 16; + const ARRAY_DIM = 17; + const PROPERTY_NAME = 18; + const VARID = 19; + const RESULTID = 20; + const VARIABLE_NAME = 21; + const VARIABLE = 22; + const LITERAL = 23; + const EXPRID = 24; + const LEFTID = 25; + const RIGHTID = 26; + const REFERENCE = 27; private $properties; private $opcode; @@ -66,7 +58,11 @@ public function isPropertyExist($index) public function getProperty($index) { - return $this->properties[$index]; + if (isset($this->properties[$index])) { + return $this->properties[$index]; + } + + return null; } public function getOpcode() diff --git a/package/src/progpilot/Code/Opcodes.php b/package/src/progpilot/Code/Opcodes.php index 3ef3a680..42dd3eb9 100644 --- a/package/src/progpilot/Code/Opcodes.php +++ b/package/src/progpilot/Code/Opcodes.php @@ -12,47 +12,34 @@ class Opcodes { - const LEAVE_BLOCK = "leave_block"; - const ENTER_BLOCK = "enter_block"; - const ENTER_FUNCTION = "enter_func"; - const LEAVE_FUNCTION = "leave_func"; - const DEFINITION = "definition"; - const START_ASSIGN = "start_assign"; - const END_ASSIGN = "end_assign"; - const START_EXPRESSION = "start_expression"; - const END_EXPRESSION = "end_expression"; - const TEMPORARY = "temporarySimple"; - const CONCAT_LEFT = "concat_left"; - const CONCAT_RIGHT = "concat_right"; - const CONCAT_LIST = "concat_list"; - const FUNC_CALL = "funccall"; - const RETURN_FUNCTION = "return"; - const CLASSE = "class"; - const ASSERTION = "assertion"; - const COND_START_IF = "condition_start_if"; - const COND_BOOLEAN_NOT = "condition_boolean_not"; - const NEW_INCLUDE = "new_include"; - - /* - const LEAVE_BLOCK = 0; - const ENTER_BLOCK = 1; - const ENTER_FUNCTION = 2; - const LEAVE_FUNCTION = 3; - const DEFINITION = 4; - const START_ASSIGN = 5; - const END_ASSIGN = 6; - const START_EXPRESSION = 7; - const END_EXPRESSION = 8; - const TEMPORARY = 9; - const CONCAT_LEFT = 10; - const CONCAT_RIGHT = 11; - const CONCAT_LIST = 12; - const FUNC_CALL = 13; - const RETURN_FUNCTION = 14; - const CLASSE = 15; - const ASSERTION = 16; - const COND_START_IF = 17; - const COND_BOOLEAN_NOT = 18; - const NEW_INCLUDE = 19; - */ + const LEAVE_BLOCK = 1; + const ENTER_BLOCK = 2; + const ENTER_FUNCTION = 3; + const LEAVE_FUNCTION = 4; + const DEFINITION = 5; + const START_ASSIGN = 6; + const END_ASSIGN = 7; + const CONCAT_LEFT = 11; + const CONCAT_RIGHT = 12; + const CONCAT_LIST = 13; + const FUNC_CALL = 14; + const RETURN_FUNCTION = 15; + const CLASSE = 16; + const ASSERTION = 17; + const COND_START_IF = 18; + const COND_BOOLEAN_NOT = 19; + const NEW_INCLUDE = 20; + const PROPERTY_FETCH = 21; + const ARRAYDIM_FETCH = 22; + const VARIABLE = 23; + const ASSIGN = 24; + const ARGUMENT = 25; + const VARIABLE_FETCH = 26; + const LITERAL_FETCH = 27; + const STATIC_PROPERTY_FETCH = 28; + const CAST = 29; + const CONST_FETCH = 30; + const ITERATOR = 31; + const ARRAY_EXPR = 32; + const BINARYOP = 33; } diff --git a/package/src/progpilot/Command/ProgpilotCommand.php b/package/src/progpilot/Command/ProgpilotCommand.php index 5c8610ff..c8020dc4 100644 --- a/package/src/progpilot/Command/ProgpilotCommand.php +++ b/package/src/progpilot/Command/ProgpilotCommand.php @@ -52,12 +52,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $context = new \progpilot\Context; $analyzer = new \progpilot\Analyzer; - if (!is_null($input->getOption('configuration'))) { - $context->setConfiguration($input->getOption('configuration')); - } - - $cmdFiles = $this->input->getArgument('files'); try { + if (!is_null($input->getOption('configuration'))) { + $context->setConfiguration($input->getOption('configuration')); + } + + $cmdFiles = $this->input->getArgument('files'); + $analyzer->run($context, $cmdFiles); if ($context->getPrettyPrint()) { @@ -69,8 +70,12 @@ protected function execute(InputInterface $input, OutputInterface $output) if (count($context->outputs->getResults()) > 0) { return 1; } - } catch (Exception $e) { - echo Lang::GLOBAL_ERROR.$e->getMessage()." file : ".$e->getFile()." line : ".$e->getLine()."\n"; + } catch (\Exception $e) { + echo $e->getMessage()."\n"; + + if ($context->isDebugMode()) { + echo "Exception raised on file : ".$e->getFile()." line : ".$e->getLine()."\n"; + } } return 0; diff --git a/package/src/progpilot/Console/Application.php b/package/src/progpilot/Console/Application.php index 19108675..f84b8634 100644 --- a/package/src/progpilot/Console/Application.php +++ b/package/src/progpilot/Console/Application.php @@ -26,16 +26,9 @@ public function __construct() public function run(InputInterface $input = null, OutputInterface $output = null) { - $this->setCatchExceptions(false); - - try { - $statusCode = parent::run($input, $output); - } catch (\Exception $e) { - echo "\n\n".$e->getMessage()."\n\n"; - } + parent::run($input, $output); } - public function getDefinition() { $inputDefinition = parent::getDefinition(); diff --git a/package/src/progpilot/Context.php b/package/src/progpilot/Context.php index 8f534e5c..2d746cfc 100644 --- a/package/src/progpilot/Context.php +++ b/package/src/progpilot/Context.php @@ -10,136 +10,8 @@ namespace progpilot; -use Symfony\Component\Yaml\Exception\ParseException; -use Symfony\Component\Yaml\Parser; -use progpilot\Utils; - -class Context +class Context extends ContextInternalApi { - private $arrayIncludes; - private $arrayRequires; - - private $currentMyCode; - private $currentOp; - private $currentBlock; - private $currentLine; - private $currentColumn; - private $currentFunc; - private $currentMyFile; - private $currentNbDefs; - private $myFiles; - private $classes; - private $objects; - private $functions; - private $path; - private $analyzeHardRules; - private $analyzeFunctions; - private $analyzeIncludes; - private $printFileUnderAnalysis; - private $configurationFile; - private $printWarning; - private $prettyPrint; - private $limitTime; - private $limitDefs; - private $limitSize; - private $limitValues; - - public $inputs; - public $outputs; - - public function __construct() - { - $this->configurationFile = null; - $this->analyzeHardRules = false; - $this->analyzeFunctions = true; - $this->analyzeIncludes = true; - $this->printFileUnderAnalysis = false; - $this->printWarning = false; - $this->prettyPrint = true; - $this->limitTime = 10; - $this->limitDefs = 3000; - $this->limitSize = 500000; - $this->currentNbDefs = 0; - - $this->inputs = new \progpilot\Inputs\MyInputs; - $this->outputs = new \progpilot\Outputs\MyOutputs; - - $this->objects = new \progpilot\Dataflow\Objects; - $this->classes = new \progpilot\Dataflow\Classes; - $this->functions = new \progpilot\Dataflow\Functions; - - $this->resetInternalValues(); - - $this->currentMyFile = null; - $this->myfiles = []; - $this->arrayIncludes = []; - $this->arrayRequires = []; - } - - public function getCurrentNbDefs() - { - return $this->currentNbDefs; - } - - public function setCurrentNbDefs($currentNbDefs) - { - $this->currentNbDefs = $currentNbDefs; - } - - public function resetInternalLowvalues() - { - $this->currentOp = null; - $this->currentBlock = null; - $this->currentLine = -1; - $this->currentColumn = -1; - $this->currentFunc = null; - $this->currentMyCode = null; - $this->path = null; - } - - public function resetInternalValues() - { - $this->resetInternalLowvalues(); - - unset($this->currentMyCode); - - $this->inputs->setCode(null); - // representations (cfg, ast ...) are deleted to avoid memory grown - //$this->outputs = new \progpilot\Outputs\MyOutputs; - $this->outputs->resetRepresentations(); - } - - public function resetDataflow() - { - unset($this->objects); - unset($this->classes); - unset($this->functions); - - $this->objects = new \progpilot\Dataflow\Objects; - $this->classes = new \progpilot\Dataflow\Classes; - $this->functions = new \progpilot\Dataflow\Functions; - } - - public function setArrayIncludes($arrayIncludes) - { - $this->arrayIncludes = $arrayIncludes; - } - - public function setArrayRequires($arrayRequires) - { - $this->arrayRequires = $arrayRequires; - } - - public function getArrayIncludes() - { - return $this->arrayIncludes; - } - - public function getArrayRequires() - { - return $this->arrayRequires; - } - public function setPrettyPrint($bool) { $this->prettyPrint = $bool; @@ -150,59 +22,59 @@ public function getPrettyPrint() return $this->prettyPrint; } - public function setPrintWarning($bool) + public function setDebugMode($bool) { - $this->printWarning = $bool; + $this->debugMode = $bool; } - public function getPrintWarning() + public function isDebugMode() { - return $this->printWarning; + return $this->debugMode; } - public function setLimitDefs($limitDefs) + public function setMaxDefinitions($maxDefinitions) { - $this->limitDefs = $limitDefs; + $this->maxDefinitions = $maxDefinitions; } - public function getLimitDefs() + public function getMaxDefinitions() { - return $this->limitDefs; + return $this->maxDefinitions; } - public function setLimitTime($limitTime) + public function setMaxFileAnalysisDuration($maxFileAnalysisDuration) { - $this->limitTime = $limitTime; + $this->maxFileAnalysisDuration = $maxFileAnalysisDuration; } - public function getLimitTime() + public function getMaxFileAnalysisDuration() { - return $this->limitTime; + return $this->maxFileAnalysisDuration; } - public function setLimitSize($limitSize) + public function setMaxFileSize($maxFileSize) { - $this->limitSize = $limitSize; + $this->maxFileSize = $maxFileSize; } - public function getLimitSize() + public function getMaxFileSize() { - return $this->limitSize; + return $this->maxFileSize; } - public function getAnalyzeHardrules() + public function setMaxMemory($maxMemory) { - return $this->analyzeHardRules; + $this->maxMemory = $maxMemory; } - public function getAnalyzeIncludes() + public function getMaxMemory() { - return $this->analyzeIncludes; + return $this->maxMemory; } - public function setAnalyzeHardRules($analyzeHardRules) + public function getAnalyzeIncludes() { - $this->analyzeHardRules = $analyzeHardRules; + return $this->analyzeIncludes; } public function setAnalyzeIncludes($analyzeIncludes) @@ -210,146 +82,6 @@ public function setAnalyzeIncludes($analyzeIncludes) $this->analyzeIncludes = $analyzeIncludes; } - public function setAnalyzeFunctions($analyzeFunctions) - { - $this->analyzeFunctions = $analyzeFunctions; - } - - public function getAnalyzeFunctions() - { - return $this->analyzeFunctions; - } - - public function getCurrentMycode() - { - return $this->currentMyCode; - } - - public function getCurrentOp() - { - return $this->currentOp; - } - - public function getCurrentBlock() - { - return $this->currentBlock; - } - - public function getCurrentLine() - { - return $this->currentLine; - } - - public function getCurrentColumn() - { - return $this->currentColumn; - } - - public function getCurrentFunc() - { - return $this->currentFunc; - } - - public function getObjects() - { - return $this->objects; - } - - public function getClasses() - { - return $this->classes; - } - - public function getFunctions() - { - return $this->functions; - } - - public function getInputs() - { - return $this->inputs; - } - - public function getOutputs() - { - return $this->outputs; - } - - public function getPath() - { - return $this->path; - } - - public function getCurrentMyfile() - { - return $this->currentMyFile; - } - - public function setCurrentMyfile($myFile) - { - $this->currentMyFile = $myFile; - } - - public function setPath($path) - { - $this->path = $path; - } - - public function setCurrentMycode($myCode) - { - $this->currentMyCode = $myCode; - } - - public function setCurrentOp($currentOp) - { - $this->currentOp = $currentOp; - } - - public function setCurrentBlock($currentBlock) - { - $this->currentBlock = $currentBlock; - } - - public function setCurrentLine($currentLine) - { - $this->currentLine = $currentLine; - } - - public function setCurrentColumn($currentColumn) - { - $this->currentColumn = $currentColumn; - } - - public function setCurrentFunc($currentFunc) - { - $this->currentFunc = $currentFunc; - } - - public function setObjects($objects) - { - $this->objects = $objects; - } - - public function setClasses($classes) - { - $this->classes = $classes; - } - - public function setFunctions($functions) - { - $this->functions = $functions; - } - - public function setInputs($inputs) - { - $this->inputs = $inputs; - } - - public function setOutputs($outputs) - { - $this->outputs = $outputs; - } - public function setConfiguration($file) { $this->configurationFile = $file; @@ -359,148 +91,4 @@ public function getConfiguration() { return $this->configurationFile; } - - public function setPrintFile($bool) - { - $this->printFileUnderAnalysis = $bool; - } - - public function getPrintFile() - { - return $this->printFileUnderAnalysis; - } - - public function readConfiguration() - { - if (!is_null($this->configurationFile)) { - try { - if (file_exists($this->configurationFile)) { - $yaml = new Parser(); - $value = $yaml->parse(file_get_contents($this->configurationFile)); - - if (is_array($value)) { - if (isset($value["inputs"])) { - if (isset($value["inputs"]["setCustomRules"])) { - $this->inputs->setCustomRules($value["inputs"]["setCustomRules"]); - } - - if (isset($value["inputs"]["setSources"])) { - $this->inputs->setSources($value["inputs"]["setSources"]); - } - - if (isset($value["inputs"]["setSinks"])) { - $this->inputs->setSinks($value["inputs"]["setSinks"]); - } - - if (isset($value["inputs"]["setValidators"])) { - $this->inputs->setValidators($value["inputs"]["setValidators"]); - } - - if (isset($value["inputs"]["setSanitizers"])) { - $this->inputs->setSanitizers($value["inputs"]["setSanitizers"]); - } - - if (isset($value["inputs"]["setIncludes"])) { - $this->inputs->setIncludes($value["inputs"]["setIncludes"]); - } - - if (isset($value["inputs"]["setExcludes"])) { - $this->inputs->setExcludes($value["inputs"]["setExcludes"]); - } - - if (isset($value["inputs"]["setFolder"])) { - $this->inputs->setFolder($value["inputs"]["setFolder"]); - } - - if (isset($value["inputs"]["setFile"])) { - $this->inputs->setFile($value["inputs"]["setFile"]); - } - - if (isset($value["inputs"]["setCode"])) { - $this->inputs->setCode($value["inputs"]["setCode"]); - } - - if (isset($value["inputs"]["setResolvedIncludes"])) { - $this->inputs->setResolvedIncludes($value["inputs"]["setResolvedIncludes"]); - } - - if (isset($value["inputs"]["setFalsePositives"])) { - $this->inputs->setFalsePositives($value["inputs"]["setFalsePositives"]); - } - - if (isset($value["inputs"]["setLanguages"])) { - $this->inputs->setLanguages($value["inputs"]["setLanguages"]); - } - - if (isset($value["inputs"]["setFrameworks"])) { - $this->inputs->setFrameworks($value["inputs"]["setFrameworks"]); - } - - if (isset($value["inputs"]["setDev"])) { - $this->inputs->setDev($value["inputs"]["setDev"]); - } - } - - if (isset($value["outputs"])) { - if (isset($value["outputs"]["taintedFlow"])) { - $this->outputs->taintedFlow($value["outputs"]["taintedFlow"]); - } - - if (isset($value["outputs"]["resolveIncludes"])) { - $this->outputs->resolveIncludes($value["outputs"]["resolveIncludes"]); - } - - if (isset($value["outputs"]["resolveIncludesFile"])) { - $this->outputs->resolveIncludesFile($value["outputs"]["resolveIncludesFile"]); - } - - if (isset($value["outputs"]["onAddResult"])) { - $this->outputs->setOnAddResult($value["outputs"]["onAddResult"]); - } - } - - if (isset($value["options"])) { - if (isset($value["options"]["setAnalyzeHardRules"])) { - $this->setAnalyzeHardRules($value["options"]["setAnalyzeHardRules"]); - } - - if (isset($value["options"]["setAnalyzeIncludes"])) { - $this->setAnalyzeIncludes($value["options"]["setAnalyzeIncludes"]); - } - - if (isset($value["options"]["setAnalyzeFunctions"])) { - $this->setAnalyzeFunctions($value["options"]["setAnalyzeFunctions"]); - } - - if (isset($value["options"]["setPrintFile"])) { - $this->setPrintFile($value["options"]["setPrintFile"]); - } - - if (isset($value["options"]["setLimitTime"])) { - $this->setLimitTime($value["options"]["setLimitTime"]); - } - - if (isset($value["options"]["setLimitDefs"])) { - $this->setLimitDefs($value["options"]["setLimitDefs"]); - } - - if (isset($value["options"]["setLimitSize"])) { - $this->setLimitSize($value["options"]["setLimitSize"]); - } - - if (isset($value["options"]["setPrintWarning"])) { - $this->setPrintWarning($value["options"]["setPrintWarning"]); - } - - if (isset($value["options"]["setPrettyPrint"])) { - $this->setPrettyPrint($value["options"]["setPrettyPrint"]); - } - } - } - } - } catch (ParseException $e) { - Utils::printError($context, Lang::UNABLE_TO_PARSER_YAML); - } - } - } } diff --git a/package/src/progpilot/ContextInternalApi.php b/package/src/progpilot/ContextInternalApi.php new file mode 100644 index 00000000..3af9f575 --- /dev/null +++ b/package/src/progpilot/ContextInternalApi.php @@ -0,0 +1,561 @@ +configurationFile = null; + $this->analyzeIncludes = true; + $this->debugMode = false; + $this->prettyPrint = true; + $this->maxFileAnalysisDuration = 30; + $this->maxDefinitions = 500; + $this->maxFileSize = 1000000; + $this->maxMemory = 4000000000; // 4GB + + $this->inputs = new \progpilot\Inputs\MyInputs; + $this->outputs = new \progpilot\Outputs\MyOutputs; + + $this->objects = new \progpilot\Dataflow\Objects; + $this->classes = new \progpilot\Dataflow\Classes; + $this->functions = new \progpilot\Dataflow\Functions; + + $this->resetInternalValues(); + + $this->currentFunc = null; + $this->currentMyFile = null; + $this->arrayIncludes = []; + $this->arrayRequires = []; + $this->analyzedDataFiles = []; + + $this->defsMain = []; + $this->tmpfunctions = []; + $this->callStack = []; + } + + public function pushToCallStack($val) + { + array_push($this->callStack, $val); + } + + public function popFromCallStack() + { + return array_pop($this->callStack); + } + + public function setCallStack($callStack) + { + $this->callStack = $callStack; + } + + public function getCallStack() + { + return $this->callStack; + } + + public function inCallStack($curFunc) + { + foreach ($this->callStack as $call) { + $callFunc = $call[0]; + + if ($callFunc->getName() === $curFunc->getName() + && !$callFunc->isType(MyFunction::TYPE_FUNC_METHOD) + && !$curFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + return true; + } + + if ($callFunc->getName() === $curFunc->getName() + && $callFunc->isType(MyFunction::TYPE_FUNC_METHOD) + && $curFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + $curClass = $curFunc->getMyClass(); + $callClass = $callFunc->getMyClass(); + + if ($curClass->getName() === $callClass->getName()) { + return true; + } + + $curClassExtends = $curClass->getExtendsOf(); + $callClassExtends = $callClass->getExtendsOf(); + + if (isset($curClassExtends) && isset($callClassExtends) + && $curClassExtends === $callClassExtends) { + return true; + } + } + } + + return false; + } + + public function addTmpFunctions($myfunc) + { + $this->tmpfunctions[] = $myfunc; + } + + public function clearTmpFunctions() + { + $this->tmpfunctions = []; + } + + public function getTmpFunctions() + { + return $this->tmpfunctions; + } + + public function addDataAnalyzedFile($file) + { + if (!in_array($file, $this->analyzedDataFiles)) { + $this->analyzedDataFiles[] = $file; + } + } + + public function isFileDataAnalyzed($file) + { + return in_array($file, $this->analyzedDataFiles); + } + + public function getDefsMain() + { + return $this->defsMain; + } + + public function setDefsMain($defsMain) + { + $this->defsMain = $defsMain; + } + + public function addFileandNamepace($file, $namespace) + { + $this->namespaces["$namespace"] = $file; + } + + public function getFileFromNamespace($namespace) + { + if (isset($this->namespaces["$namespace"])) { + return $this->namespaces["$namespace"]; + } + + return null; + } + + public function addCallToNamespace($namespace) + { + if (!isset($this->calltonamespaces["".$this->currentMyFile->getName().""])) { + $this->calltonamespaces["".$this->currentMyFile->getName().""] = []; + } + + if (!in_array($namespace, $this->calltonamespaces["".$this->currentMyFile->getName().""], true)) { + $this->calltonamespaces["".$this->currentMyFile->getName().""][] = $namespace; + } + } + + public function getCallsToNamespace($file) + { + if (isset($this->calltonamespaces["$file"])) { + return $this->calltonamespaces["$file"]; + } + + return null; + } + + public function resetInternalLowvalues() + { + $this->currentOp = null; + $this->currentBlock = null; + $this->currentLine = -1; + $this->currentColumn = -1; + $this->currentFunc = null; + $this->currentMyCode = null; + } + + public function resetInternalValues() + { + $this->resetInternalLowvalues(); + + $this->inputs->setCode(null); + } + + public function resetCallStack() + { + $this->callStack = []; + } + + public function resetIncludedFiles() + { + $this->arrayIncludes = []; + $this->arrayRequires = []; + } + + public function resetDataflow() + { + $this->analyzedDataFiles = []; + $this->objects = new \progpilot\Dataflow\Objects; + $this->classes = new \progpilot\Dataflow\Classes; + $this->functions = new \progpilot\Dataflow\Functions; + } + + public function setArrayIncludes($arrayIncludes) + { + $this->arrayIncludes = $arrayIncludes; + } + + public function setArrayRequires($arrayRequires) + { + $this->arrayRequires = $arrayRequires; + } + + public function &getArrayIncludes() + { + return $this->arrayIncludes; + } + + public function &getArrayRequires() + { + return $this->arrayRequires; + } + + public function getCurrentMycode() + { + return $this->currentMyCode; + } + + public function getCurrentOp() + { + return $this->currentOp; + } + + public function getCurrentBlock() + { + return $this->currentBlock; + } + + public function getCurrentLine() + { + return $this->currentLine; + } + + public function getCurrentColumn() + { + return $this->currentColumn; + } + + public function getCurrentFunc() + { + return $this->currentFunc; + } + + public function getObjects() + { + return $this->objects; + } + + public function getClasses() + { + return $this->classes; + } + + public function getFunctions() + { + return $this->functions; + } + + public function getNbsOpInformationsAll() + { + $nb = 0; + foreach ($this->getFunctions()->getFunctions() as $functionclasses) { + foreach ($functionclasses as $functionnames) { + foreach ($functionnames as $function) { + foreach ($function->getOpInformations() as $opInformation) { + if (isset($opInformation["chained_results"])) { + $nb += count($opInformation["chained_results"]); + } + } + } + } + } + + return $nb; + } + + public function getInputs() + { + return $this->inputs; + } + + public function getOutputs() + { + return $this->outputs; + } + + public function getPath() + { + return $this->path; + } + + public function getCurrentMyfile() + { + return $this->currentMyFile; + } + + public function setCurrentMyfile($myFile) + { + $this->currentMyFile = $myFile; + } + + public function setPath($path) + { + $this->path = $path; + } + + public function setCurrentMycode($myCode) + { + $this->currentMyCode = $myCode; + } + + public function setCurrentOp($currentOp) + { + $this->currentOp = $currentOp; + } + + public function setCurrentBlock($currentBlock) + { + $this->currentBlock = $currentBlock; + } + + public function setCurrentLine($currentLine) + { + $this->currentLine = $currentLine; + } + + public function setCurrentColumn($currentColumn) + { + $this->currentColumn = $currentColumn; + } + + public function setCurrentFunc($currentFunc) + { + $this->currentFunc = $currentFunc; + } + + public function setObjects($objects) + { + $this->objects = $objects; + } + + public function setClasses($classes) + { + $this->classes = $classes; + } + + public function setFunctions($functions) + { + $this->functions = $functions; + } + + public function setInputs($inputs) + { + $this->inputs = $inputs; + } + + public function setOutputs($outputs) + { + $this->outputs = $outputs; + } + + public function readConfiguration() + { + if (!is_null($this->configurationFile)) { + try { + if (file_exists($this->configurationFile)) { + $yaml = new Parser(); + $value = $yaml->parse(file_get_contents($this->configurationFile)); + + if (is_array($value)) { + if (isset($value["inputs"])) { + if (isset($value["inputs"]["customrules"])) { + $keep_defaults = true; + if (isset($value["inputs"]["customrules"]["keep_defaults"])) { + $keep_defaults = $value["inputs"]["customrules"]["keep_defaults"]; + } + + if ($keep_defaults) { + $this->inputs->addCustomRules($value["inputs"]["customrules"]["config_files"]); + } else { + $this->inputs->setCustomRules($value["inputs"]["customrules"]["config_files"]); + } + } + + if (isset($value["inputs"]["sources"])) { + $keep_defaults = true; + if (isset($value["inputs"]["sources"]["keep_defaults"])) { + $keep_defaults = $value["inputs"]["sources"]["keep_defaults"]; + } + + if ($keep_defaults) { + $this->inputs->addSources($value["inputs"]["sources"]["config_files"]); + } else { + $this->inputs->setSources($value["inputs"]["sources"]["config_files"]); + } + } + + if (isset($value["inputs"]["sinks"])) { + $keep_defaults = true; + if (isset($value["inputs"]["sinks"]["keep_defaults"])) { + $keep_defaults = $value["inputs"]["sinks"]["keep_defaults"]; + } + + if ($keep_defaults) { + $this->inputs->addSinks($value["inputs"]["sinks"]["config_files"]); + } else { + $this->inputs->setSinks($value["inputs"]["sinks"]["config_files"]); + } + } + + if (isset($value["inputs"]["validators"])) { + $keep_defaults = true; + if (isset($value["inputs"]["validators"]["keep_defaults"])) { + $keep_defaults = $value["inputs"]["validators"]["keep_defaults"]; + } + + if ($keep_defaults) { + $this->inputs->addValidators($value["inputs"]["validators"]["config_files"]); + } else { + $this->inputs->setValidators($value["inputs"]["validators"]["config_files"]); + } + } + + if (isset($value["inputs"]["sanitizers"])) { + $keep_defaults = true; + if (isset($value["inputs"]["sanitizers"]["keep_defaults"])) { + $keep_defaults = $value["inputs"]["sanitizers"]["keep_defaults"]; + } + + if ($keep_defaults) { + $this->inputs->addSanitizers($value["inputs"]["sanitizers"]["config_files"]); + } else { + $this->inputs->setSanitizers($value["inputs"]["sanitizers"]["config_files"]); + } + } + + if (isset($value["inputs"]["inclusions"])) { + $this->inputs->setInclusions($value["inputs"]["inclusions"]); + } + + if (isset($value["inputs"]["exclusions"])) { + $this->inputs->setExclusions($value["inputs"]["exclusions"]); + } + + if (isset($value["inputs"]["resolved_includes_file"])) { + $this->inputs->setResolvedIncludes($value["inputs"]["resolved_includes_file"]); + } + + if (isset($value["inputs"]["false_positives"])) { + $this->inputs->setFalsePositives($value["inputs"]["false_positives"]); + } + + if (isset($value["inputs"]["languages"])) { + $this->inputs->setLanguages($value["inputs"]["languages"]); + } + + if (isset($value["inputs"]["dev_mode"])) { + $this->inputs->setDev($value["inputs"]["dev_mode"]); + } + } + + if (isset($value["outputs"])) { + if (isset($value["outputs"]["tainted_flow"])) { + $this->outputs->taintedFlow($value["outputs"]["tainted_flow"]); + } + + if (isset($value["outputs"]["include_failures_file"])) { + $this->outputs->setIncludeFailuresFile($value["outputs"]["include_failures_file"]); + } + } + + if (isset($value["options"])) { + if (isset($value["options"]["analyze_includes"])) { + $this->setAnalyzeIncludes($value["options"]["analyze_includes"]); + } + + if (isset($value["options"]["max_file_analysis_duration"])) { + $this->setMaxFileAnalysisDuration($value["options"]["max_file_analysis_duration"]); + } + + if (isset($value["options"]["max_definitions"])) { + $this->setMaxDefinitions($value["options"]["max_definitions"]); + } + + if (isset($value["options"]["max_file_size"])) { + $this->setMaxFileSize($value["options"]["max_file_size"]); + } + + if (isset($value["options"]["max_memory"])) { + $this->setMaxFileSize($value["options"]["max_memory"]); + } + + if (isset($value["options"]["debug_mode"])) { + $this->setDebugMode($value["options"]["debug_mode"]); + } + + if (isset($value["options"]["pretty_print"])) { + $this->setPrettyPrint($value["options"]["pretty_print"]); + } + } + } + } else { + Utils::printError( + Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($this->configurationFile).")" + ); + } + } catch (ParseException $e) { + Utils::printError(Lang::UNABLE_TO_PARSER_YAML); + } + } + } +} diff --git a/package/src/progpilot/Dataflow/Classes.php b/package/src/progpilot/Dataflow/Classes.php index 671680d1..c6c1dee9 100644 --- a/package/src/progpilot/Dataflow/Classes.php +++ b/package/src/progpilot/Dataflow/Classes.php @@ -26,32 +26,17 @@ public function getListClasses() public function addMyclass($newMyClass) { - /* - if (!in_array($myClass, $this->listClasses, true)) - $this->listClasses[] = $myClass; - */ - - $continue = true; - foreach ($this->listClasses as $myClass) { - if ($myClass->getName() === $newMyClass->getName()) { - $continue = false; - break; - } - } - - if ($continue) { - $this->listClasses[] = $newMyClass; + if (!isset($this->listClasses[$newMyClass->getName()])) { + $this->listClasses[$newMyClass->getName()] = $newMyClass; } } public function getMyClass($name) { - foreach ($this->listClasses as $myClass) { - if ($myClass->getName() === $name) { - return $myClass; - } + if (isset($this->listClasses[$name])) { + return $this->listClasses[$name]; } - + return null; } } diff --git a/package/src/progpilot/Dataflow/Definitions.php b/package/src/progpilot/Dataflow/Definitions.php index 7156db3b..5e164836 100644 --- a/package/src/progpilot/Dataflow/Definitions.php +++ b/package/src/progpilot/Dataflow/Definitions.php @@ -34,6 +34,7 @@ public function __construct() { $this->currentFunc = null; $this->nbDefs = 0; + $this->defs = []; } public function setNbDefs($nbDefs) @@ -108,9 +109,19 @@ public function getDefs() return $this->defs; } + public function getOriginalDef($id) + { + if (isset($this->originalDefs[$id])) { + return $this->originalDefs[$id]; + } + + return null; + } + public function addDef($name, $def) { $continue = true; + if (isset($this->defs[$name])) { $continue = false; if (!in_array($def, $this->defs[$name], true)) { @@ -121,9 +132,8 @@ public function addDef($name, $def) if ($continue) { $this->nbDefs ++; $this->defs[$name][] = $def; + $this->originalDefs[$def->getId()] = clone $def; } - - return $continue; } public function addIn($block, $def) @@ -256,25 +266,9 @@ public function getDefRefByName($name) } // def1 = def, def2 = defsearch inside ResolveDefs function - public static function defEquality($def1, $def2, $byPassArray = false) + public static function defEquality($def1, $def2) { if ($def1->getName() === $def2->getName()) { - if (($def1->property->getProperties() !== $def2->property->getProperties()) - && !$def1->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED") - && !$def2->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED")) { - return false; - } - - if (($def1->getArrayValue() !== $def2->getArrayValue()) && !$byPassArray) { - if ($def1->isType(MyDefinition::TYPE_ARRAY) && $def2->isType(MyDefinition::TYPE_ARRAY)) { - $extract = BuildArrays::extractArrayFromArr($def1->getArrayValue(), $def2->getArrayValue()); - - if ($extract === false) { - return false; - } - } - } - return true; } @@ -282,7 +276,7 @@ public static function defEquality($def1, $def2, $byPassArray = false) } // $this->data["gen"][$blockId] - public function computeKill($context, $blockId) + public function computeKill($blockId) { foreach ($this->gen[$blockId] as $gen) { $tmpdefs = $this->getDefRefByName($gen->getName()); @@ -311,9 +305,9 @@ public function reachingDefs($myBlocks) while ($change) { $change = false; - foreach ($myBlocks as $id => $block) { + foreach ($myBlocks as $block) { + $idcurrent = $block->getId(); foreach ($block->parents as $idparent => $parent) { - $idcurrent = $block->getId(); $idparent = $parent->getId(); if ($idcurrent !== $idparent) { diff --git a/package/src/progpilot/Dataflow/Functions.php b/package/src/progpilot/Dataflow/Functions.php index c845da44..32d8860c 100644 --- a/package/src/progpilot/Dataflow/Functions.php +++ b/package/src/progpilot/Dataflow/Functions.php @@ -12,38 +12,49 @@ use progpilot\Objects\MyFunction; use progpilot\Objects\MyOp; +use progpilot\Utils; class Functions { private $functions; - public function getFunction($funcname, $className = null) + public function __construct() { - if (isset($this->functions[$funcname])) { - $list_funcs = $this->functions[$funcname]; - foreach ($list_funcs as $myFunc) { - if (!$myFunc->isType(MyFunction::TYPE_FUNC_METHOD) && is_null($className)) { - return $myFunc; - } + $this->functions = []; + } - if ($myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - $myClass = $myFunc->getMyClass(); - if ($className === $myClass->getName()) { - return $myFunc; - } - } + public function getAllFunctions($funcname, $className = "function") + { + $functionsTmp = []; + + foreach ($this->functions as $id => $functionsFile) { + if (isset($functionsFile[$className][$funcname])) { + $functionsTmp[] = $functionsFile[$className][$funcname]; } } - return null; + return $functionsTmp; } - public function getFunctionsByName($name) + + public function getFunction($funcname, $className = null, $specificFile = null) { - if (isset($this->functions[$name])) { - return $this->functions[$name]; + if (is_null($className)) { + $className = "function"; } + if (!is_null($specificFile)) { + if (isset($this->functions[$specificFile][$className][$funcname])) { + return $this->functions[$specificFile][$className][$funcname]; + } + } else { + foreach ($this->functions as $id => $functionsFile) { + if (isset($functionsFile[$className][$funcname])) { + return $functionsFile[$className][$funcname]; + } + } + } + return null; } @@ -52,27 +63,20 @@ public function getFunctions() return $this->functions; } - public function addFunction($funcname, $func) + public function addFunction($filenamehash, $classname, $funcname, $myFunc) { - // we can have many functions/methods with the same name - $continue = true; - if (isset($this->functions[$funcname])) { - $continue = false; - if (!in_array($func, $this->functions[$funcname], true)) { - $continue = true; - } - } - - if ($continue) { - $this->functions[$funcname][] = $func; - } + $this->functions[$filenamehash][$classname][$funcname] = $myFunc; } - public function delFunction($funcname) + public function delFunction($filenamehash, $classname, $funcname) { - $save = $this->functions[$funcname]; - $this->functions[$funcname] = []; - //unset($this->functions[$funcname]); + $save = null; + + if (isset($this->functions[$filenamehash][$classname][$funcname])) { + $save = $this->functions[$filenamehash][$classname][$funcname]; + $this->functions[$filenamehash][$classname][$funcname] = null; + } + return $save; } } diff --git a/package/src/progpilot/Dataflow/VisitorDataflow.php b/package/src/progpilot/Dataflow/VisitorDataflow.php index cfbfcbc9..ffbe262b 100644 --- a/package/src/progpilot/Dataflow/VisitorDataflow.php +++ b/package/src/progpilot/Dataflow/VisitorDataflow.php @@ -24,31 +24,24 @@ use progpilot\Utils; use progpilot\Lang; +use progpilot\Helpers\Analysis as HelpersAnalysis; + class VisitorDataflow { - private $defs; - private $blocks; - private $currentBlockId; - private $currentClass; - - public function __construct() - { - } + private $blocksArrays; - protected function getBlockId($myBlock) + protected function isArrayAlreadyDefined($nameArray) { - if (isset($this->blocks[$myBlock])) { - return $this->blocks[$myBlock]; + if (isset($this->blocksArrays["".$nameArray.""])) { + return true; } - return -1; + return false; } - protected function setBlockId($myBlock) + protected function defineArray($nameArray) { - if (!isset($this->blocks[$myBlock])) { - $this->blocks[$myBlock] = count($this->blocks); - } + $this->blocksArrays["".$nameArray.""] = true; } public function analyze($context, $myFunc, $defsIncluded = null) @@ -59,83 +52,36 @@ public function analyze($context, $myFunc, $defsIncluded = null) $index = 0; $myFunc->getMyCode()->setEnd(count($code)); - $blocksStackId = []; - $lastBlockId = 0; + $blocks = null; + $blocksStack = null; $firstBlock = true; - + $alreadyWarned = false; + $defs = null; + do { if (isset($code[$index])) { $instruction = $code[$index]; switch ($instruction->getOpcode()) { - case Opcodes::START_EXPRESSION: - // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock($idCfg, Opcodes::START_EXPRESSION."\n"); - // representations end - break; - - case Opcodes::END_EXPRESSION: - // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock($idCfg, Opcodes::END_EXPRESSION."\n"); - // representations end - break; - - case Opcodes::CLASSE: - $myClass = $instruction->getProperty(MyInstruction::MYCLASS); - foreach ($myClass->getProperties() as $property) { - if (is_null($property->getSourceMyFile())) { - $property->setSourceMyFile($context->getCurrentMyfile()); - } - } - - $objectId = $context->getObjects()->addObject(); - $myClass->setObjectIdThis($objectId); - - - $this->currentClass = $myClass; - - break; - case Opcodes::ENTER_FUNCTION: - $blockIdZero = hash("sha256", "0-".$context->getCurrentMyfile()->getName()); + $alreadyWarned = false; $myFunc = $instruction->getProperty(MyInstruction::MYFUNC); $myFunc->setSourceMyFile($context->getCurrentMyfile()); - $blocks = new \SplObjectStorage; + $blocks = []; + $blocksStack = []; $defs = new Definitions(); - $defs->createBlock($blockIdZero); + //$defs->createBlock(0); // transform.php: properties have block id 0 $myFunc->setDefs($defs); - $myFunc->setBlocks($blocks); - - $this->defs = $defs; - $this->blocks = $blocks; - $this->currentBlockId = $blockIdZero; - $this->currentFunc = $myFunc; - - if ($myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - $thisdef = $myFunc->getThisDef(); - $thisdef->setSourceMyFile($context->getCurrentMyfile()); - - $thisdef->setObjectId($this->currentClass->getObjectIdThis()); - $thisdef->setBlockId($blockIdZero); - - $this->defs->addDef($thisdef->getName(), $thisdef); - $this->defs->addGen($thisdef->getBlockId(), $thisdef); - - $context->getObjects()->addMyclassToObject( - $this->currentClass->getObjectIdThis(), - $this->currentClass - ); - } + $context->setCurrentFunc($myFunc); // representations start - $hashedValue = $this->currentFunc->getName()."-".$this->currentBlockId; + $hashedValue = $context->getCurrentFunc()->getName()."-".$context->getCurrentBlock()->getId(); $idCfg = hash("sha256", $hashedValue); - $context->outputs->cfg->addTextOfMyBlock( + $context->outputs->cfgAddTextOfMyBlock( + $context->getCurrentFunc(), $idCfg, Opcodes::ENTER_FUNCTION." ".htmlentities($myFunc->getName(), ENT_QUOTES, 'UTF-8')."\n" ); @@ -145,70 +91,84 @@ public function analyze($context, $myFunc, $defsIncluded = null) case Opcodes::ENTER_BLOCK: $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); + $myBlock->setSourceMyFile($context->getCurrentMyfile()); - $this->setBlockId($myBlock); - $blockIdTmp = $this->getBlockId($myBlock); - - $blockId = hash("sha256", "$blockIdTmp-".$context->getCurrentMyfile()->getName()); - $myBlock->setId($blockId); + $context->setCurrentBlock($myBlock); + array_push($blocksStack, $myBlock); + array_push($blocks, $myBlock); - array_push($blocksStackId, $blockId); - $this->currentBlockId = $blockId; - - if ($blockId !== hash("sha256", "0-".$context->getCurrentMyfile()->getName())) { - $this->defs->createBlock($blockId); - } + $context->getCurrentFunc()->getDefs()->createBlock($myBlock->getId()); $assertions = $myBlock->getAssertions(); foreach ($assertions as $assertion) { $myDef = $assertion->getDef(); - $myDef->setBlockId($blockId); + $myDef->setBlockId($myBlock->getId()); } // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock($idCfg, Opcodes::ENTER_BLOCK."\n"); - $context->outputs->cfg->addNode($idCfg, $myBlock); + $idCfg = hash("sha256", $context->getCurrentFunc()->getName()."-".$myBlock->getId()); + $context->outputs->cfgAddTextOfMyBlock( + $context->getCurrentFunc(), + $idCfg, + Opcodes::ENTER_BLOCK."\n" + ); + $context->outputs->cfgAddNode($context->getCurrentFunc(), $idCfg, $myBlock); foreach ($myBlock->parents as $parent) { - $context->outputs->cfg->addEdge($parent, $myBlock); + $context->outputs->cfgAddEdge($context->getCurrentFunc(), $parent, $myBlock); } // representations end - if ($firstBlock && !is_null($defsIncluded) && $this->currentFunc->getName() === "{main}") { - foreach ($defsIncluded as $def_included) { - $this->defs->addDef($def_included->getName(), $def_included); - $this->defs->addGen($blockId, $def_included); - } - + if ($firstBlock) { + $context->getCurrentFunc()->setFirstBlockId($myBlock->getId()); $firstBlock = false; - } - break; + // param are part of the first block of the function + foreach ($context->getCurrentFunc()->getParams() as $param) { + $param->setBlockId($myBlock->getId()); + $param->unsetState(0); + $state = $param->createState(); + $param->assignStateToBlockId($state->getId(), $myBlock->getId()); + } + // end - case Opcodes::LEAVE_BLOCK: - $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); - $blockId = $myBlock->getId(); + if ($context->getCurrentFunc()->isType(MyFunction::TYPE_FUNC_METHOD)) { + $thisdef = $context->getCurrentFunc()->getThisDef(); - $pop = array_pop($blocksStackId); + $thisdef->setBlockId($myBlock->getId()); + $thisdef->unsetState(0); + $state = $thisdef->createState(); + $thisdef->assignStateToBlockId($state->getId(), $myBlock->getId()); - if (count($blocksStackId) > 0) { - $this->currentBlockId = $blocksStackId[count($blocksStackId) - 1]; + $thisdef->getCurrentState()->addType(MyDefinition::TYPE_INSTANCE); + + $context->getCurrentFunc()->getDefs()->addDef($thisdef->getName(), $thisdef); + $context->getCurrentFunc()->getDefs()->addGen($thisdef->getBlockId(), $thisdef); + } } - if (($this->currentFunc->getDefs()->getNbDefs() > $context->getLimitDefs()) || - ($context->getCurrentNbDefs() > $context->getLimitDefs())) { - Utils::printWarning($context, Lang::MAX_DEFS_EXCEEDED); - return; + // just to keep array def uptodate + $blocksArrays["".$myBlock->getId().""] = []; + + break; + + case Opcodes::LEAVE_BLOCK: + $myBlock = $instruction->getProperty(MyInstruction::MYBLOCK); + array_pop($blocksStack); + if (!empty($blocksStack)) { + $context->setCurrentBlock($blocksStack[count($blocksStack) - 1]); } - $this->defs->computeKill($context, $blockId); - $lastBlockId = $blockId; + $context->getCurrentFunc()->getDefs()->computeKill($myBlock->getId()); // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock($idCfg, Opcodes::LEAVE_BLOCK."\n"); + $idCfg = hash("sha256", $context->getCurrentFunc()->getName()."-".$myBlock->getId()); + $context->outputs->cfgAddTextOfMyBlock( + $context->getCurrentFunc(), + $idCfg, + Opcodes::LEAVE_BLOCK."\n" + ); // representations end break; @@ -217,61 +177,47 @@ public function analyze($context, $myFunc, $defsIncluded = null) case Opcodes::LEAVE_FUNCTION: $myFunc = $instruction->getProperty(MyInstruction::MYFUNC); - $context->setCurrentNbDefs($context->getCurrentNbDefs() + $myFunc->getDefs()->getNbDefs()); - - if ($context->getCurrentNbDefs() > $context->getLimitDefs()) { - Utils::printWarning($context, Lang::MAX_DEFS_EXCEEDED); - return; - } + $context->getCurrentFunc()->getDefs()->reachingDefs($blocks); - $this->defs->reachingDefs($this->blocks); + $myFunc->setBlocks($blocks); - $myFunc->setLastBlockId($lastBlockId); + $lastBlockIds = $myFunc->getLastBlockIds(); + + // functions are generally parsed before the main and so declarations of instances + if ($myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + foreach ($context->getObjects()->getObjects() as $idobject => $myClass) { + if (!is_null($myClass)) { + foreach ($myClass->getMethods() as $myMethod) { + if ($myMethod->getName() === $myFunc->getName()) { + $myMethod->setLastBlockIds($lastBlockIds); + } + } + } + } + } // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock($idCfg, Opcodes::LEAVE_FUNCTION."\n"); + if (isset($lastBlockIds[0])) { + $idCfg = hash("sha256", $context->getCurrentFunc()->getName()."-".$lastBlockIds[0]); + $context->outputs->cfgAddTextOfMyBlock( + $context->getCurrentFunc(), + $idCfg, + Opcodes::LEAVE_FUNCTION."\n" + ); + } // representations end break; case Opcodes::FUNC_CALL: $myFuncCall = $instruction->getProperty(MyInstruction::MYFUNC_CALL); - $myFuncCall->setBlockId($this->currentBlockId); + $myFuncCall->setBlockId($context->getCurrentBlock()->getId()); if (is_null($myFuncCall->getSourceMyFile())) { $myFuncCall->setSourceMyFile($context->getCurrentMyfile()); } - if ($myFuncCall->isType(MyFunction::TYPE_FUNC_METHOD)) { - $mybackdef = $myFuncCall->getBackDef(); - $mybackdef->setBlockId($this->currentBlockId); - $mybackdef->addType(MyDefinition::TYPE_INSTANCE); - $mybackdef->setSourceMyFile($context->getCurrentMyfile()); - - $idObject = $context->getObjects()->addObject(); - $mybackdef->setObjectId($idObject); - - if (!empty($mybackdef->getClassName())) { - $className = $mybackdef->getClassName(); - $myClass = $context->getClasses()->getMyClass($className); - - if (is_null($myClass)) { - $myClass = new MyClass( - $mybackdef->getLine(), - $mybackdef->getColumn(), - $className - ); - } - - $context->getObjects()->addMyclassToObject($idObject, $myClass); - } - - $this->defs->addDef($mybackdef->getName(), $mybackdef); - $this->defs->addGen($mybackdef->getBlockId(), $mybackdef); - } - - $mySource = $context->inputs->getSourceByName(null, $myFuncCall, true, false, false); + $mySource = $context->inputs->getSourceByName($myFuncCall, null); if (!is_null($mySource)) { if ($mySource->hasParameters()) { $nbparams = 0; @@ -283,10 +229,16 @@ public function analyze($context, $myFunc, $defsIncluded = null) $defarg = $instruction->getProperty("argdef$nbparams"); if ($mySource->isParameter($nbparams + 1)) { - $deffrom = $defarg->getValueFromDef(); + $deffrom = $instruction->getProperty("argoriginaldef$nbparams"); if (!is_null($deffrom)) { - $this->defs->addDef($deffrom->getName(), $deffrom); - $this->defs->addGen($deffrom->getBlockId(), $deffrom); + $context->getCurrentFunc()->getDefs()->addDef( + $deffrom->getName(), + $deffrom + ); + $context->getCurrentFunc()->getDefs()->addGen( + $deffrom->getBlockId(), + $deffrom + ); } } @@ -296,8 +248,12 @@ public function analyze($context, $myFunc, $defsIncluded = null) } // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock( + $idCfg = hash( + "sha256", + $context->getCurrentFunc()->getName()."-".$context->getCurrentBlock()->getId() + ); + $context->outputs->cfgAddTextOfMyBlock( + $context->getCurrentFunc(), $idCfg, Opcodes::FUNC_CALL." ".htmlentities($myFuncCall->getName(), ENT_QUOTES, 'UTF-8')."\n" ); @@ -305,45 +261,39 @@ public function analyze($context, $myFunc, $defsIncluded = null) break; - case Opcodes::TEMPORARY: - $listOfMyTemp = []; - if ($instruction->isPropertyExist(MyInstruction::PHI)) { - for ($i = 0; $i < $instruction->getProperty(MyInstruction::PHI); $i++) { - $listOfMyTemp[] = $instruction->getProperty("temp_".$i); - } - } else { - $listOfMyTemp[] = $instruction->getProperty(MyInstruction::TEMPORARY); - } - - foreach ($listOfMyTemp as $myDef) { - $myDef->setBlockId($this->currentBlockId); - - if (is_null($myDef->getSourceMyFile())) { - $myDef->setSourceMyFile($context->getCurrentMyfile()); - } - - // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock($idCfg, Opcodes::TEMPORARY."\n"); - // representations end - } - - break; - case Opcodes::DEFINITION: $myDef = $instruction->getProperty(MyInstruction::DEF); - $myDef->setBlockId($this->currentBlockId); - if (is_null($myDef->getSourceMyFile())) { - $myDef->setSourceMyFile($context->getCurrentMyfile()); + if ($context->getCurrentFunc()->getDefs()->getNbDefs() < $context->getMaxDefinitions()) { + if ($myDef->isType(MyDefinition::TYPE_ARRAY)) { + // only one array by name is defined in a function (or blocks see arrays12.php) + if (!$this->isArrayAlreadyDefined($myDef->getName())) { + $context->getCurrentFunc()->getDefs()->addDef($myDef->getName(), $myDef); + $context->getCurrentFunc()->getDefs()->addGen($myDef->getBlockId(), $myDef); + $this->defineArray($myDef->getName()); + } + } elseif (!$myDef->isType(MyDefinition::TYPE_PROPERTY) + && !$myDef->isType(MyDefinition::TYPE_STATIC_PROPERTY)) { + $context->getCurrentFunc()->getDefs()->addDef($myDef->getName(), $myDef); + $context->getCurrentFunc()->getDefs()->addGen($myDef->getBlockId(), $myDef); + } + } else { + if (!$alreadyWarned) { + Utils::printWarning($context, Lang::MAX_DEFS_EXCEEDED); + $alreadyWarned = true; + } } - - $this->defs->addDef($myDef->getName(), $myDef); - $this->defs->addGen($myDef->getBlockId(), $myDef); // representations start - $idCfg = hash("sha256", $this->currentFunc->getName()."-".$this->currentBlockId); - $context->outputs->cfg->addTextOfMyBlock($idCfg, Opcodes::DEFINITION."\n"); + $idCfg = hash( + "sha256", + $context->getCurrentFunc()->getName()."-".$context->getCurrentBlock()->getId() + ); + $context->outputs->cfgAddTextOfMyBlock( + $context->getCurrentFunc(), + $idCfg, + Opcodes::DEFINITION."\n" + ); // representations end break; diff --git a/package/src/progpilot/Helpers/Analysis.php b/package/src/progpilot/Helpers/Analysis.php new file mode 100644 index 00000000..f014a47a --- /dev/null +++ b/package/src/progpilot/Helpers/Analysis.php @@ -0,0 +1,354 @@ +getObjects()->getObjects() as $id => $objectClass) { + $params = array($context, $id, $objectClass, $myClass); + call_user_func_array(__NAMESPACE__ ."\\$func", $params); + } + } + + public static function forDefsInFunctions($context, $func, $myClass) + { + foreach ($context->getFunctions()->getFunctions() as $functionsBlocks) { + foreach ($functionsBlocks as $functionBlock) { + foreach ($functionBlock->getDefs()->getDefs() as $defsBlocks) { + foreach ($defsBlocks as $defBlock) { + $params = array($context, $defBlock, $myClass); + call_user_func_array(__NAMESPACE__ ."\\$func", $params); + } + } + } + } + } + + public static function forAllDefsOfFunctionExceptReturnDefs($func, $myFunc) + { + $returnDefs = $myFunc->getReturnDefs(); + foreach ($myFunc->getDefs()->getDefs() as $defnames) { + if (is_array($defnames)) { + foreach ($defnames as $def) { + if (!in_array($def, $returnDefs, true)) { + $params = array($def, $myFunc->getDefs()); + call_user_func_array(__NAMESPACE__ ."\\$func", $params); + } + } + } + } + } + + public static function forAllReturnDefsOfFunction($func, $myFunc) + { + $initialReturnDefs = $myFunc->getInitialReturnDefs(); + $returnDefs = $myFunc->getReturnDefs(); + $i = 0; + foreach ($returnDefs as $returnDef) { + if (isset($initialReturnDefs[$i])) { + $params = array($returnDef, $initialReturnDefs[$i]); + call_user_func_array(__NAMESPACE__ ."\\$func", $params); + } + + $i ++; + } + } + + public static function forAllDefsOfFunction($func, $myFunc) + { + foreach ($myFunc->getDefs()->getDefs() as $defnames) { + if (is_array($defnames)) { + foreach ($defnames as $def) { + $params = array($def, $myFunc->getDefs()); + call_user_func_array(__NAMESPACE__ ."\\$func", $params); + } + } + } + } + + public static function forAllArgumentsOfFunction($func, $myfunc, $instruction) + { + $params = $myfunc->getParams(); + + for ($i = 0; $i < count($params); $i++) { + if ($instruction->isPropertyExist("argdef$i")) { // myfunccall + $defArg = $instruction->getProperty("argdef$i"); + $params = array($myfunc, $i, $defArg); + call_user_func_array(__NAMESPACE__ ."\\$func", $params); + } + } + } + + public static function checkIfTimeExecutionIsAcceptable($context, $myFunc) + { + $threshold1 = $context->getMaxFileAnalysisDuration() / 4; // 7.5 seconds by default + $threshold2 = $context->getMaxFileAnalysisDuration() / 15; // 2 seconds by defaults + $lastExecutionTime = $myFunc->getLastExecutionTime(); + $nbExecutions = $myFunc->getNbExecutions(); + + if (($nbExecutions >= 1 && $nbExecutions < 5 && $lastExecutionTime >= $threshold1) + || ($nbExecutions >= 4 && $nbExecutions < 10 && $lastExecutionTime >= $threshold2) + || $nbExecutions >= 10) { + return false; + } + + return true; + } + + public static function getBytes($val) + { + $val = trim($val); + $last = strtolower($val[strlen($val)-1]); + if (!is_numeric($last)) { + $val = (int) substr($val, 0, -1); + switch ($last) { + // The 'G' modifier is available since PHP 5.1.0 + case 'g': + $val *= 1024; + // no break + case 'm': + $val *= 1024; + // no break + case 'k': + $val *= 1024; + } + } + + return $val; + } + + public static function isASource($context, $def, $myClass, $arrayDim) + { + $source = $context->inputs->getSourceByName($def, $myClass, $arrayDim); + + if (!is_null($source)) { + return true; + } + + return false; + } + + public static function forEachTaintedByDefs($def, $block, $callback) + { + foreach ($def->getStates() as $state) { + $taintedDefs = $state->getTaintedByDefs(); + foreach ($taintedDefs as $taintedByDef) { + $taintedDef = $taintedByDef[0]; + $taintedState = $taintedByDef[1]; + + $params = array($taintedDef, $block); + call_user_func_array(__NAMESPACE__ ."\\$callback", $params); + } + } + } + + public static function updateNbChars(&$nbChars, $string, $arrayChars) + { + if (is_string($string)) { + foreach ($arrayChars as $char) { + if (!isset($nbChars[$char])) { + $nbChars[$char] = 0; + } + } + + for ($i = 0; $i < strlen($string); $i++) { + foreach ($arrayChars as $char) { + if ($string[$i] === $char) { + $nbChars[$char] ++; + } + } + } + } + } + + public static function getNbChars($string, $arrayChars) + { + $nbChars = []; + + if (is_string($string)) { + foreach ($arrayChars as $char) { + $nbChars[$char] = 0; + } + + for ($i = 0; $i < strlen($string); $i++) { + foreach ($arrayChars as $char) { + if ($string[$i] === $char) { + $nbChars[$char] ++; + } + } + } + } + + return $nbChars; + } + + public static function checkCallStackReachMaxTime($context) + { + $callStack = $context->getCallStack(); + $endTime = microtime(true); + + if (is_array($callStack)) { + foreach ($callStack as $call) { + $myFunc = $call[0]; + $startTime = $myFunc->getStartExecutionTime(); + $diff = $endTime - $startTime; + + if ($diff > $context->getMaxFileAnalysisDuration()) { + return true; + } + } + } + + return false; + } + + public static function checkDeeplyIfDefIsTainted($def) + { + $arrayTotal[] = $def; + $defsToAnalyze[] = $def; + while (!empty($defsToAnalyze)) { + $def = array_pop($defsToAnalyze); + foreach ($def->getStates() as $state) { + if ($state->isTainted()) { + return true; + } + + foreach ($state->getArrayIndexes() as $arrayElement) { + if ($arrayElement->def !== $def + && !in_array($arrayElement->def, $defsToAnalyze, true) + && !in_array($arrayElement->def, $arrayTotal, true) + && count($arrayTotal) < 50) { + $defsToAnalyze[] = $arrayElement->def; + $arrayTotal[] = $arrayElement->def; + } + } + } + } + + return false; + } + + public static function checkIfOneFunctionArgumentIsNew($myFunc, $instruction) + { + if (!is_null($myFunc)) { + $params = $myFunc->getParams(); + + if (empty($params)) { + // if it's not a method and empty params, + // then we can't have additional tainted return + return false; + } + + $statesPastArgs = $myFunc->getStatePastArguments(); + for ($i = 0; $i < count($params); $i++) { + if ($instruction->isPropertyExist("argdef$i")) { + $defArg = $instruction->getProperty("argdef$i"); + + if (Analysis::checkDeeplyIfDefIsTainted($defArg)) { + return true; + } + + if (empty($defArg->getCurrentState()->getLastKnownValues())) { + return false; + } + + if (isset($statesPastArgs[$i]) && is_array($statesPastArgs[$i])) { + foreach ($statesPastArgs[$i] as $statePastArg) { + if ($defArg->getCurrentState()->getLastKnownValues() + === $statePastArg->getLastKnownValues()) { + return false; + } + } + } else { + return true; + } + } + } + } + + return false; + } + + public static function checkIfFuncEqualMySpecify($context, $mySpecify, $myFunc, $myClass = null) + { + if (!$mySpecify->isInstance() + && $mySpecify->getName() === $myFunc->getName()) { + return true; + } + + if ($mySpecify->isInstance() && !is_null($myClass) && + ($myClass->getName() === $mySpecify->getInstanceOfName() + || $myClass->getExtendsOf() === $mySpecify->getInstanceOfName())) { + return true; + } + + return false; + } + + public static function checkIfDefEqualDefRule($context, $defs, $rule, $def, $myClass = null) + { + $definition = $rule->getDefinition(); + + if (!$definition->isInstance() + && $def->getName() === $definition->getName()) { + return true; + } + + if ($definition->isInstance() && !is_null($myClass) && $def->getName() === $definition->getName() && + ($myClass->getName() === $definition->getInstanceOfName() + || $myClass->getExtendsOf() === $definition->getInstanceOfName())) { + return true; + } + + return false; + } + + public static function createObject($context, $myDef) + { + if ($myDef->isType(MyDefinition::TYPE_INSTANCE) + || $myDef->getCurrentState()->isType(MyDefinition::TYPE_INSTANCE)) { + $idObject = $context->getObjects()->addObject(); + + if ($myDef->getCurrentState()->getObjectId() === -1) { + $myDef->getCurrentState()->setObjectId($idObject); + $myDef->getCurrentState()->addType(MyDefinition::TYPE_INSTANCE); + + if (!empty($myDef->getClassName())) { + $myClass = $context->getClasses()->getMyClass($myDef->getClassName()); + if (is_null($myClass)) { + $myClass = new MyClass( + $myDef->getLine(), + $myDef->getColumn(), + $myDef->getClassName() + ); + } else { + $myClass = clone $myClass; + } + + $context->getObjects()->addMyclassToObject($idObject, $myClass); + } + } + } + } +} diff --git a/package/src/progpilot/Helpers/Callbacks.php b/package/src/progpilot/Helpers/Callbacks.php new file mode 100644 index 00000000..7d36dfd5 --- /dev/null +++ b/package/src/progpilot/Helpers/Callbacks.php @@ -0,0 +1,73 @@ +getName() === $newMyclass->getName()) { + $context->getObjects()->addMyclassToObject($idObject, $newMyclass); + } + } + + public static function addStateDefAsAPastArgument($myfunc, $nbparam, $arg) + { + $myfunc->addStatePastArgument($nbparam, $arg->getCurrentState()); + } + + public static function addAttributesOfInitialReturnDefs($returnDef, $initialReturnDef) + { + $returnDef->setStates($initialReturnDef->getStates()); + $returnDef->setStatesToBlocksIds($initialReturnDef->getStatesToBlocksIds()); + } + + public static function addSanitizedTypes($defValid, $sanitizedTypes) + { + $defValid->getCurrentState()->setSanitized(true); + if (is_array($sanitizedTypes)) { + foreach ($sanitizedTypes as $sanitizedType) { + $defValid->getCurrentState()->addTypeSanitized($sanitizedType); + } + } + } + + public static function addValidAssertion($defValid, $block) + { + $myAssertion = new MyAssertion($defValid, "valid"); + $block->addAssertion($myAssertion); + } + + public static function cleanTaintedDef($def, $funcDefs) + { + $originalDef = $funcDefs->getOriginalDef($def->getId()); + + if (!is_null($originalDef)) { + $originalDefCopy = clone $originalDef; + $def->setType($originalDefCopy->getType()); + $def->setSourceMyFile($originalDefCopy->getSourceMyFile()); + $def->setName($originalDefCopy->getName()); + $def->setValidWhenReturning($originalDefCopy->getValidWhenReturning()); + $def->setValidNotBoolean($originalDefCopy->getValidNotBoolean()); + $def->setReturnedFromValidator($originalDefCopy->getReturnedFromValidator()); + $def->setClassName($originalDefCopy->getClassName()); + $def->setRefs($originalDefCopy->getRefs()); + $def->setStates($originalDefCopy->getStates()); + $def->setStatesToBlocksIds($originalDefCopy->getStatesToBlocksIds()); + } + } +} diff --git a/package/src/progpilot/Helpers/State.php b/package/src/progpilot/Helpers/State.php new file mode 100644 index 00000000..72f53738 --- /dev/null +++ b/package/src/progpilot/Helpers/State.php @@ -0,0 +1,306 @@ +getParamToArg())) { + $def = $def->getParamToArg(); + } + + foreach ($def->getStates() as $state) { + if (!in_array($state->getId(), $defStack, true)) { + $defStack[] = $state->getId(); + if ($state->isType(MyDefinition::TYPE_ARRAY)) { + State::updateBlocksOfArrayElements($context, $state, $defStack); + } + + if ($state->isType(MyDefinition::TYPE_INSTANCE)) { + State::updateBlocksOfProperties($context, $state, $defStack); + } + } + } + } + + public static function blockSwitching($context, $myFunc) + { + $defStack = []; + $tmpDefs = $myFunc->getDefs(); + foreach ($tmpDefs->getDefs() as $defs) { + foreach ($defs as $def) { + State::updateBlocksOfDef($context, $def, $defStack); + } + } + + foreach ($context->getCurrentFunc()->getBlocks() as $myBlock) { + $myBlock->setNeedUpdateOfState(false); + } + } + + public static function updateBlocksOfArrayElement($context, $arrayElement, &$defStack) + { + $myBlock = $context->getCurrentBlock(); + $states = []; + $blockParents = array_merge($myBlock->getParents(), $myBlock->getVirtualParents()); + + foreach ($blockParents as $parentMyBlock) { + if ($parentMyBlock->getId() !== $myBlock->getId()) { + $state = $arrayElement->getState($parentMyBlock->getId()); + if (!is_null($state) && !in_array($state, $states, true)) { + $states[] = $state; + } + } + } + + if (count($states) === 1) { + $newstate = $states[0]; + $arrayElement->assignStateToBlockId($newstate->getId(), $myBlock->getId()); + } elseif (count($states) > 1) { + if ($arrayElement->getNbStates() < 20) { + $newstate = State::mergeDefStates($states); + $arrayElement->addState($newstate); + $arrayElement->assignStateToBlockId($newstate->getId(), $myBlock->getId()); + $defStack[] = $newstate->getId(); + } + } + } + + public static function updateBlocksOfArrayElements($context, $state, &$defStack) + { + $arrayIndexes = $state->getArrayIndexes(); + foreach ($arrayIndexes as $arrayIndexArr) { + $arrayDef = $arrayIndexArr->def; + State::updateBlocksOfArrayElement($context, $arrayDef, $defStack); + State::updateBlocksOfDef($context, $arrayDef, $defStack); + } + } + + public static function updateBlocksOfProperty($context, $property, &$defStack) + { + $myBlock = $context->getCurrentBlock(); + $states = []; + + + $blockParents = $myBlock->getVirtualParents(); + $existingState = $property->getState($myBlock->getId()); + + // the state has been already computer, we don't need to update that + // unless there is a new parent that have been added + if (is_null($existingState) || $myBlock->doNeedUpdateOfState()) { + $states = []; + foreach ($blockParents as $parentMyBlock) { + if ($parentMyBlock->getId() !== $myBlock->getId()) { + $state = $property->getState($parentMyBlock->getId()); + if (!is_null($state) && !in_array($state, $states, true)) { + $states[] = $state; + } + } + } + + if (count($states) === 1) { + $newstate = $states[0]; + $property->assignStateToBlockId($newstate->getId(), $myBlock->getId()); + } elseif (count($states) > 1) { + if ($property->getNbStates() < 20) { + $newstate = State::mergeDefStates($states); + $property->addState($newstate); + $property->assignStateToBlockId($newstate->getId(), $myBlock->getId()); + $defStack[] = $newstate->getId(); + } + } + } + } + + public static function updateBlocksOfProperties($context, $state, &$defStack) + { + $idObject = $state->getObjectId(); + $tmpMyClass = $context->getObjects()->getMyClassFromObject($idObject); + + if (!is_null($tmpMyClass)) { + foreach ($tmpMyClass->getProperties() as $property) { + State::updateBlocksOfProperty($context, $property, $defStack); + State::updateBlocksOfDef($context, $property, $defStack); + } + } + } + + public static function mergeDefsBlockIdStates($defs, $concatValues, $block) + { + $blockId = $block->getId(); + + $myState = new MyDefState; + + $oneStateNotSanitizer = false; + + foreach ($defs as $def) { + if ($def->isType(MyDefinition::TYPE_ARRAY_ELEMENT) + || $def->isType(MyDefinition::TYPE_PROPERTY)) { + $state = $def->getState($blockId); + } else { + $state = $def->getCurrentState(); + } + + if (!is_null($state)) { + if ($state->isTainted() && !AssertionAnalysis::checkDefIsAssert($block, $def)) { + $myState->setTainted(true); + + // for the flow we don't want built-in variables + if ($def->getName() === "built-in-concatenation") { + $myState->setTaintedByDefs($state->getTaintedByDefs()); + } else { + $myState->addTaintedByDef([$def, $state]); + } + + if ($state->isSanitized() && !$oneStateNotSanitizer) { + $myState->setSanitized(true); + foreach ($state->getTypeSanitized() as $typeSanitized) { + $myState->addTypeSanitized($typeSanitized); + } + } else { + // if one value is not sanitized it's enough to not sanitize + // see custom/sanitizer4.php + $myState->setSanitized(false); + $myState->setTypeSanitized([]); + $oneStateNotSanitizer = true; + } + } + + if ($state->getObjectId() !== -1) { + $myState->setObjectId($state->getObjectId()); + } + + if ($state->isType(MyDefinition::TYPE_INSTANCE)) { + $myState->addType(MyDefinition::TYPE_INSTANCE); + } + + if ($state->isType(MyDefinition::ALL_PROPERTIES_TAINTED)) { + $myState->addType(MyDefinition::ALL_PROPERTIES_TAINTED); + $myState->addTaintedByDef([$def, $state]); + } + + if ($state->isType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED)) { + $myState->addType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED); + $myState->addTaintedByDef([$def, $state]); + } + + if ($state->isType(MyDefinition::TYPE_ARRAY)) { + foreach ($state->getArrayIndexes() as $oriArrayIndex) { + $retadd = $myState->addArrayIndex($oriArrayIndex->index, $oriArrayIndex->def); + // if tainted we force the potential existing array (could not be added before) + if (!$retadd && $oriArrayIndex->def->getCurrentState()->isTainted()) { + $myState->overwriteArrayIndex($oriArrayIndex->index, $oriArrayIndex->def); + } + } + $myState->addType(MyDefinition::TYPE_ARRAY); + } + + if ($state->isType(MyDefinition::TYPE_ARRAY_ARRAY)) { + $myState->addType(MyDefinition::TYPE_ARRAY_ARRAY); + } + + $myState->setIsEmbeddedByChars($state->getIsEmbeddedByChars(), true); + $myState->setCast($state->getCast()); + $myState->setLabel($state->getLabel()); + + $values = count($concatValues) ? $concatValues : $state->getLastKnownValues(); + foreach ($values as $value) { + $myState->addLastKnownValue($value); + } + } + } + + return $myState; + } + + public static function mergeDefStates($states) + { + $myState = new MyDefState; + + $sanitizedTypes = []; + foreach ($states as $state) { + if ($state->isTainted()) { + $myState->setTainted(true); + foreach ($state->getTaintedByDefs() as $taintedDef) { + $myState->addTaintedByDef($taintedDef); + } + } + + if ($state->isSanitized()) { + foreach ($state->getTypeSanitized() as $typeSanitized) { + $sanitizedTypes["".$typeSanitized.""][] = true; + } + } + + if ($state->getObjectId() !== -1) { + $myState->addType(MyDefinition::TYPE_INSTANCE); + $myState->setObjectId($state->getObjectId()); + } + + if ($state->isType(MyDefinition::TYPE_ARRAY)) { + foreach ($state->getArrayIndexes() as $oriArrayIndex) { + if (!$myState->isArrayIndexExists($oriArrayIndex->index)) { + $tmpDef = clone $oriArrayIndex->def; + $myState->addArrayIndex($oriArrayIndex->index, $tmpDef); + } + } + + $myState->addType(MyDefinition::TYPE_ARRAY); + } + + if ($state->isType(MyDefinition::TYPE_ARRAY_ARRAY)) { + $myState->addType(MyDefinition::TYPE_ARRAY_ARRAY); + } + + if ($state->isType(MyDefinition::ALL_PROPERTIES_TAINTED)) { + $myState->addType(MyDefinition::ALL_PROPERTIES_TAINTED); + foreach ($state->getTaintedByDefs() as $taintedDef) { + $myState->addTaintedByDef($taintedDef); + } + } + + if ($state->isType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED)) { + $myState->addType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED); + foreach ($state->getTaintedByDefs() as $taintedDef) { + $myState->addTaintedByDef($taintedDef); + } + } + + foreach ($state->getLastKnownValues() as $value) { + $myState->addLastKnownValue($value); + } + + $myState->setIsEmbeddedByChars($state->getIsEmbeddedByChars(), true); + $myState->setCast($state->getCast()); + $myState->setLabel($state->getLabel()); + } + + foreach ($sanitizedTypes as $TypeKey => $arrayValue) { + if (count($arrayValue) === count($states)) { + $myState->setSanitized(true); + $myState->addTypeSanitized($TypeKey); + } + } + + return $myState; + } +} diff --git a/package/src/progpilot/Inputs/MyCustomFunction.php b/package/src/progpilot/Inputs/MyCustomFunction.php index b12637dd..4164aad1 100644 --- a/package/src/progpilot/Inputs/MyCustomFunction.php +++ b/package/src/progpilot/Inputs/MyCustomFunction.php @@ -36,9 +36,23 @@ public function __construct($name, $language, $orderNumberExpected = 0) $this->parameters = []; } - public function addParameter($parameter, $validbydefault, $fixed, $values = null) - { - $this->parameters[] = [$parameter, $values, $validbydefault, $fixed]; + public function addParameter( + $parameter, + $validbydefault, + $fixed, + $sufficient, + $failifnotverified, + $notequals, + $values = null + ) { + $this->parameters[] = [ + $parameter, + $values, + $validbydefault, + $fixed, + $sufficient, + $failifnotverified, + $notequals]; } public function getParameters() @@ -56,12 +70,24 @@ public function setHasParameters($hasParameters) $this->hasParameters = $hasParameters; } + public function isParameterSufficient($i) + { + foreach ($this->parameters as $parameter) { + $index = $parameter[0]; + $sufficient = $parameter[4]; + + if ($index === $i) { + return $sufficient; + } + } + + return false; + } + public function isParameterFixed($i) { foreach ($this->parameters as $parameter) { $index = $parameter[0]; - $values = $parameter[1]; - $validbydefault = $parameter[2]; $fixed = $parameter[3]; if ($index === $i) { @@ -76,7 +102,6 @@ public function isParameterValidByDefault($i) { foreach ($this->parameters as $parameter) { $index = $parameter[0]; - $values = $parameter[1]; $validbydefault = $parameter[2]; if ($index === $i) { diff --git a/package/src/progpilot/Inputs/MyInputs.php b/package/src/progpilot/Inputs/MyInputs.php index 83b9eee6..38b13e4b 100644 --- a/package/src/progpilot/Inputs/MyInputs.php +++ b/package/src/progpilot/Inputs/MyInputs.php @@ -12,129 +12,19 @@ use progpilot\Lang; use progpilot\Utils; -use progpilot\Objects\MyFunction; -use progpilot\Objects\MyDefinition; -class MyInputs +class MyInputs extends MyInputsInternalApi { - private $languages; - private $frameworks; - - private $customRules; - - private $sanitizers; - private $sinks; - private $sources; - private $validators; - private $falsePositivesAnalysis; - private $excludesFilesAnalysis; - private $includesFilesAnalysis; - private $excludesFoldersAnalysis; - private $includesFoldersAnalysis; - - private $customFile; - private $resolvedIncludes; - private $falsePositives; - private $sourcesFile; - private $sinksFile; - private $sanitizersFile; - private $validatorsFile; - private $includes; - private $excludes; - - private $file; - private $code; - private $folder; - - public function __construct() - { - $this->dev = false; - $this->languages = ["php"]; - $this->frameworks = ["suitecrm", "codeigniter", "wordpress", "prestashop", "symfony"]; - - $this->customRules = []; - $this->resolvedIncludesAnalysis = []; - $this->sanitizers = []; - $this->sinks = []; - $this->sources = []; - $this->validators = []; - $this->falsePositivesAnalysis = []; - $this->excludesFilesAnalysis = []; - $this->includesFilesAnalysis = []; - $this->excludesFoldersAnalysis = []; - $this->includesFoldersAnalysis = []; - - $this->customFile = null; - $this->falsePositives= null; - $this->resolvedIncludes = null; - $this->sanitizersFile = null; - $this->sinksFile = null; - $this->sourcesFile = null; - $this->validatorsFile = null; - $this->excludes = null; - $this->includes = null; - - $this->file = null; - $this->code = null; - $this->folder = null; - } - - - public function setFrameworks($frameworks) - { - $this->frameworks = $frameworks; - } - public function setLanguages($languages) { $this->languages = $languages; } - - public function isLanguage($lang) - { - return in_array($lang, $this->languages, true); - } public function getLanguages() { return $this->languages; } - public function getFrameworks() - { - return $this->frameworks; - } - - public function getSinksFile() - { - return $this->sinksFile; - } - - public function getSourcesFile() - { - return $this->sourcesFile; - } - - public function getValidatorsFile() - { - return $this->validatorsFile; - } - - public function getSanitizersFile() - { - return $this->sanitizersFile; - } - - public function getIncludedFiles() - { - return $this->includesFilesAnalysis; - } - - public function getIncludedFolders() - { - return $this->includesFoldersAnalysis; - } - public function getFolder() { return $this->folder; @@ -165,358 +55,95 @@ public function setCode($code) $this->code = $code; } - public function isExcludedFolder($name) + public function getDev() { - $name = realpath($name); - foreach ($this->excludesFoldersAnalysis as $excludeName) { - if (strpos($name, realpath($excludeName)) === 0) { - return true; - } - } - - return false; + return $this->dev; } - public function isIncludedFolder($name) + public function setDev($dev) { - $name = realpath($name); - foreach ($this->includesFoldersAnalysis as $includeName) { - if (strpos($name, realpath($includeName)) === 0) { - return true; - } - } - - return false; - } + $this->dev = $dev; - public function isExcludedFile($name) - { - $name = realpath($name); - foreach ($this->excludesFilesAnalysis as $excludeName) { - if (realpath($excludeName) === $name) { - return true; - } - } - - foreach ($this->excludesFoldersAnalysis as $excludeName) { - $realExcludeName = realpath($excludeName); - $folderfirst = substr($name, 0, strlen($realExcludeName)); - if ($realExcludeName === $folderfirst) { - return true; - } + if ($this->dev) { + $sanitizersfile = __DIR__."/../../uptodate_data/php/dev/sanitizers.json"; + $this->readSanitizersFile($sanitizersfile); + + $sinksfile = __DIR__."/../../uptodate_data/php/dev/sinks.json"; + $this->readSinksFile($sinksfile); + + $sourcesfile = __DIR__."/../../uptodate_data/php/dev/sources.json"; + $this->readSourcesFile($sourcesfile); + + $validatorsfile = __DIR__."/../../uptodate_data/php/dev/validators.json"; + $this->readValidatorsFile($validatorsfile); + + $customsfile = __DIR__."/../../uptodate_data/php/dev/rules.json"; + $this->readCustomFile($customsfile); } - - return false; } - public function isIncludedFile($name) + public function addSources($files) { - $name = realpath($name); - foreach ($this->includesFilesAnalysis as $includeName) { - if (realpath($includeName) === $name) { - return true; + if (is_array($files)) { + foreach ($files as $file) { + $this->readSourcesFile($file); } + } else { + $this->readSourcesFile($files); } - - return false; } - public function getIncludeByLocation($line, $column, $sourceFile) + public function setSources($files) { - foreach ($this->resolvedIncludesAnalysis as $myInclude) { - if ($myInclude->getLine() === $line - && $myInclude->getColumn() === $column - && $myInclude->getSourceFile() === $sourceFile) { - return $myInclude; - } - } - - return null; + $this->overwrittenSources = true; + $this->sources = []; + $this->addSources($files); } - public function getValidatorByName($context, $stackClass, $myFunc, $myClass) + public function getSources() { - foreach ($this->validators as $myValidator) { - if ($myValidator->getName() === $myFunc->getName()) { - if (!$myValidator->isInstance() && !$myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - return $myValidator; - } - - if ($myValidator->isInstance() && $myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - if (!is_null($myClass) && - ($myValidator->getInstanceOfName() === $myClass->getName() - || $myValidator->getInstanceOfName() === $myClass->getExtendsOf())) { - return $myValidator; - } - - if ($myValidator->getLanguage() === "php") { - $propertiesValidator = explode("->", $myValidator->getInstanceOfName()); - } elseif ($myValidator->getLanguage() === "js") { - $propertiesValidator = explode(".", $myValidator->getInstanceOfName()); - } - - if (is_array($propertiesValidator)) { - $myValidatorInstanceName = $propertiesValidator[0]; - - $myValidatorNumberOfProperties = count($propertiesValidator); - $stackNumberOfProperties = count($stackClass); - - if ($stackNumberOfProperties >= $myValidatorNumberOfProperties) { - $knownProperties = - $stackClass[$stackNumberOfProperties - $myValidatorNumberOfProperties]; - - foreach ($knownProperties as $propClass) { - $objectId = $propClass->getObjectId(); - $myClass = $context->getObjects()->getMyClassFromObject($objectId); - - if (($myClass->getName() === $myValidatorInstanceName - || $myClass->getExtendsOf() === $myValidatorInstanceName)) { - return $myValidator; - } - } - } - } - } - } - } - - return null; + return $this->sources; } - public function getSanitizerByName($context, $stackClass, $myFunc, $myClass) + public function addSinks($files) { - foreach ($this->sanitizers as $mySanitizer) { - if ($mySanitizer->getName() === $myFunc->getName()) { - if (!$mySanitizer->isInstance() && !$myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - return $mySanitizer; - } - - if ($mySanitizer->isInstance() && $myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - if (!is_null($myClass) && - ($mySanitizer->getInstanceOfName() === $myClass->getName() - || $mySanitizer->getInstanceOfName() === $myClass->getExtendsOf())) { - return $mySanitizer; - } - - if ($mySanitizer->getLanguage() === "php") { - $propertiesSanitizer = explode("->", $mySanitizer->getInstanceOfName()); - } elseif ($mySanitizer->getLanguage() === "js") { - $propertiesSanitizer = explode(".", $mySanitizer->getInstanceOfName()); - } - - if (is_array($propertiesSanitizer)) { - $mySanitizerInstanceName = $propertiesSanitizer[0]; - - $mySanitizerNumberOfProperties = count($propertiesSanitizer); - $stackNumberOfProperties = count($stackClass); - - if ($stackNumberOfProperties >= $mySanitizerNumberOfProperties) { - $knownProperties = - $stackClass[$stackNumberOfProperties - $mySanitizerNumberOfProperties]; - - foreach ($knownProperties as $propClass) { - $objectId = $propClass->getObjectId(); - $myClass = $context->getObjects()->getMyClassFromObject($objectId); - - if (!is_null($myClass) && ($myClass->getName() === $mySanitizerInstanceName - || $myClass->getExtendsOf() === $mySanitizerInstanceName)) { - return $mySanitizer; - } - } - } - } - } + if (is_array($files)) { + foreach ($files as $file) { + $this->readSinksFile($file); } + } else { + $this->readSinksFile($files); } - - return null; } - public function getSinkByName($context, $stackClass, $myFunc, $myClass) + public function getSinks() { - foreach ($this->sinks as $mySink) { - if ($mySink->getName() === $myFunc->getName()) { - if (!$mySink->isInstance() && !$myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - return $mySink; - } - - if ($mySink->isInstance() && $myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { - if (!is_null($myClass) - && ($mySink->getInstanceOfName() === $myClass->getName() - || $mySink->getInstanceOfName() === $myClass->getExtendsOf())) { - return $mySink; - } - - if ($mySink->getLanguage() === "php") { - $propertiesSink = explode("->", $mySink->getInstanceOfName()); - } elseif ($mySink->getLanguage() === "js") { - $propertiesSink = explode(".", $mySink->getInstanceOfName()); - } - - if (is_array($propertiesSink)) { - $mySinkInstanceName = $propertiesSink[0]; - - $mySinkNumberOfProperties = count($propertiesSink); - $stackNumberOfProperties = count($stackClass); - - if ($stackNumberOfProperties >= $mySinkNumberOfProperties) { - $knownProperties = - $stackClass[$stackNumberOfProperties - $mySinkNumberOfProperties]; - - foreach ($knownProperties as $propClass) { - $objectId = $propClass->getObjectId(); - $myClass = $context->getObjects()->getMyClassFromObject($objectId); - - if (!is_null($myClass) - && ($myClass->getName() === $mySinkInstanceName - || $myClass->getExtendsOf() === $mySinkInstanceName)) { - return $mySink; - } - } - } - } - } - } - } - - return null; - } - - public function getSourceArrayByName( - $myFuncOrDef, - $arrValue = false - ) { - foreach ($this->sources as $mySource) { - if ($mySource->getName() === $myFuncOrDef->getName() - && $mySource->getIsArray() - && $arrValue === false) { - return $mySource; - } - } - - return null; + return $this->sinks; } - - public function getSourceByName( - $stackClass, - $myFuncOrDef, - $isFunction = false, - $instanceName = false, - $arrValue = false - ) { - foreach ($this->sources as $mySource) { - $checkName = false; - if (!$isFunction && $myFuncOrDef->isType(MyDefinition::TYPE_PROPERTY)) { - $properties = $myFuncOrDef->property->getProperties(); - if (is_array($properties) && count($properties) > 0) { - $lastproperty = $properties[count($properties) - 1]; - if ($lastproperty === $mySource->getName()) { - $checkName = true; - } - } - } - - if (($mySource->getName() === $myFuncOrDef->getName()) || $checkName) { - $checkFunction = false; - $checkArray = false; - $checkInstance = false; - - if (!$instanceName && !$mySource->isInstance()) { - $checkInstance = true; - } - - if ($instanceName && $mySource->isInstance()) { - if ($mySource->getInstanceOfName() === $instanceName) { - $checkInstance = true; - } - - if ($mySource->getLanguage() === "php") { - $propertiesSource = explode("->", $mySource->getInstanceOfName()); - } elseif ($mySource->getLanguage() === "js") { - $propertiesSource = explode(".", $mySource->getInstanceOfName()); - } - - if (is_array($propertiesSource)) { - $mySourceInstanceName = $propertiesSource[0]; - - $mySourceNumberOfProperties = count($propertiesSource); - $stackNumberOfProperties = count($stackClass); - - if ($stackNumberOfProperties >= $mySourceNumberOfProperties) { - $knownProperties = - $stackClass[$stackNumberOfProperties - $mySourceNumberOfProperties]; - - foreach ($knownProperties as $propClass) { - if ($propClass->getName() === $mySourceInstanceName) { - $checkInstance = true; - } - } - } - } - } - - if ($mySource->isFunction() === $isFunction) { - $checkFunction = true; - } - - // if we request an array the source must be an array - // and array nots equals (like $_GET["p"]) - if (($arrValue !== false && $arrValue !== "PROGPILOT_ALL_INDEX_TAINTED" - && $mySource->getIsArray() - && is_null($mySource->getArrayValue())) - // or we don't request an array - // and the source is not an array (echo $hardcoded_tainted) - || (!$arrValue && !$mySource->getIsArray()) - // or we don't request an array - // if mysource is a function and a array like that : - // $row = mysqli_fetch_assoc() - // echo $row[0] - // we don't want an array ie : $row = mysqli_fetch_assoc()[0] - || (!$arrValue && $mySource->isFunction() && $mySource->getIsArray())) { - $checkArray = true; - } - - // if we request an array the source must be an array and array value equals - if (($arrValue !== false && $arrValue !== "PROGPILOT_ALL_INDEX_TAINTED" - && $mySource->getIsArray() - && !is_null($mySource->getArrayValue()) - && $mySource->getArrayValue() === $arrValue)) { - $checkArray = true; - } - - if ($checkArray && $checkInstance && $checkFunction) { - return $mySource; - } - } - } - return null; + public function setSinks($files) + { + $this->overwrittenSinks = true; + $this->sinks = []; + $this->addSinks($files); } - public function getFalsePositiveById($id) + public function addValidators($files) { - foreach ($this->falsePositivesAnalysis as $falsePositive) { - if ($falsePositive->getId() === $id) { - return $falsePositive; + if (is_array($files)) { + foreach ($files as $file) { + $this->readValidatorsFile($file); } + } else { + $this->readValidatorsFile($files); } - - return null; - } - - public function getSanitizers() - { - return $this->sanitizers; - } - - public function getSinks() - { - return $this->sinks; } - public function getSources() + public function setValidators($files) { - return $this->sources; + $this->overwrittenValidators = true; + $this->validators = []; + $this->addValidators($files); } public function getValidators() @@ -524,34 +151,45 @@ public function getValidators() return $this->validators; } - public function getResolvedIncludes() - { - return $this->resolvedIncludes; - } - - public function getFalsePositives() + public function addSanitizers($files) { - return $this->falsePositives; + if (is_array($files)) { + foreach ($files as $file) { + $this->readSanitizersFile($file); + } + } else { + $this->readSanitizersFile($files); + } } - public function getIncludes() + public function setSanitizers($files) { - return $this->includes; + $this->overwrittenSanitizers = true; + $this->sanitizers = []; + $this->addSanitizers($files); } - public function getExcludes() + public function getSanitizers() { - return $this->excludes; + return $this->sanitizers; } - public function getDev() + public function addCustomRules($files) { - return $this->dev; + if (is_array($files)) { + foreach ($files as $file) { + $this->readCustomFile($file); + } + } else { + $this->readCustomFile($files); + } } - public function setDev($dev) + public function setCustomRules($files) { - $this->dev = $dev; + $this->overwrittenCustomRules = true; + $this->customRules = []; + $this->addCustomRules($files); } public function getCustomRules() @@ -559,1117 +197,102 @@ public function getCustomRules() return $this->customRules; } - public function setCustomRules($file) + public function setResolvedIncludes($resolvedIncludes) { - $this->customFile = $file; - } + if (!is_null($resolvedIncludes)) { + if (!file_exists($resolvedIncludes)) { + Utils::printError( + Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($resolvedIncludes).")" + ); + } - public function setIncludeFiles($file) - { - $this->includesFile = $file; - } + $outputJson = file_get_contents($resolvedIncludes); + $parsedJson = json_decode($outputJson); - public function setExcludes($arr) - { - $this->excludes = $arr; - } + if (isset($parsedJson-> {'includes'})) { + $includes = $parsedJson-> {'includes'}; + foreach ($includes as $include) { + if (!isset($include-> {'line'}) + || !isset($include-> {'column'}) + || !isset($include-> {'source_file'}) + || !isset($include-> {'value'})) { + Utils::printError(Lang::FORMAT_INCLUDES); + } - public function setIncludes($arr) - { - $this->includes = $arr; - } + if (realpath($include-> {'source_file'})) { + $line = $include-> {'line'}; + $column = $include-> {'column'}; + $sourceFile = realpath($include-> {'source_file'}); + $value = $include-> {'value'}; - public function setFalsePositives($arr) + $myInclude = new MyInclude($line, $column, $sourceFile, $value); + if (!in_array($myInclude, $this->resolvedIncludesAnalysis, true)) { + $this->resolvedIncludesAnalysis[] = $myInclude; + } + } + } + } else { + Utils::printError(Lang::FORMAT_INCLUDES); + } + } + } + + public function getFalsePositives() { - $this->falsePositives = $arr; + return $this->falsePositivesAnalysis; } - public function setResolvedIncludes($arr) + public function setFalsePositives($falsePositives) { - $this->resolvedIncludes = $arr; + $this->readFalsePositives($falsePositives); } - public function setSources($file) + public function getInclusions() { - $this->sourcesFile = $file; + return $this->includesFilesAnalysis; } - public function setSinks($file) + public function getExclusions() { - $this->sinksFile = $file; + return $this->excludesFilesAnalysis; } - public function setSanitizers($file) + public function setExclusions($exclusions) { - $this->sanitizersFile = $file; + $this->excludesFilesAnalysis = []; + $this->addExclusions($exclusions); } - public function setValidators($file) + public function setInclusions($inclusions) { - $this->validatorsFile = $file; + $this->includesFilesAnalysis = []; + $this->addInclusions($inclusions); } - public function readDev() + public function addExclusions($exclusions) { - if ($this->dev) { - $sanitizersfile = __DIR__."/../../uptodate_data/php/dev/sanitizers.json"; - $this->readSanitizersFile($sanitizersfile); - - $sinksfile = __DIR__."/../../uptodate_data/php/dev/sinks.json"; - $this->readSinksFile($sinksfile); - - $sourcesfile = __DIR__."/../../uptodate_data/php/dev/sources.json"; - $this->readSourcesFile($sourcesfile); - - $validatorsfile = __DIR__."/../../uptodate_data/php/dev/validators.json"; - $this->readValidatorsFile($validatorsfile); - - $customsfile = __DIR__."/../../uptodate_data/php/dev/rules.json"; - $this->readCustomFile($customsfile); + if (!is_null($exclusions)) { + if (is_string($exclusions)) { + $this->readExcludesFile($exclusions); + } elseif (is_array($exclusions)) { + foreach ($exclusions as $excludeFile) { + if (is_string($excludeFile) && !in_array($excludeFile, $this->excludesFilesAnalysis, true)) { + $this->excludesFilesAnalysis[] = $excludeFile; + } + } + } } } - public function readFrameworks() + public function addInclusions($inclusions) { - if (is_array($this->frameworks) && in_array("suitecrm", $this->frameworks, true)) { - $sanitizersfile = __DIR__."/../../uptodate_data/php/frameworks/suitecrm/sanitizers.json"; - - if (is_array($this->sanitizersFile)) { - if (!in_array($sanitizersfile, $this->sanitizersFile, true)) { - $this->readSanitizersFile($sanitizersfile); - } - } else { - if ($this->sanitizersFile !== $sanitizersfile) { - $this->readSanitizersFile($sanitizersfile); - } - } - - $sinksfile = __DIR__."/../../uptodate_data/php/frameworks/suitecrm/sinks.json"; - - if (is_array($this->sinksFile)) { - if (!in_array($sinksfile, $this->sinksFile, true)) { - $this->readSinksFile($sinksfile); - } - } else { - if ($this->sinksFile !== $sinksfile) { - $this->readSinksFile($sinksfile); - } - } - - $sourcesfile = __DIR__."/../../uptodate_data/php/frameworks/suitecrm/sources.json"; - - if (is_array($this->sourcesFile)) { - if (!in_array($sourcesfile, $this->sourcesFile, true)) { - $this->readSourcesFile($sourcesfile); - } - } else { - if ($this->sourcesFile !== $sourcesfile) { - $this->readSourcesFile($sourcesfile); - } - } - - $validatorsfile = __DIR__."/../../uptodate_data/php/frameworks/suitecrm/validators.json"; - - if (is_array($this->validatorsFile)) { - if (!in_array($validatorsfile, $this->validatorsFile, true)) { - $this->readValidatorsFile($validatorsfile); - } - } else { - if ($this->validatorsFile !== $validatorsfile) { - $this->readValidatorsFile($validatorsfile); - } - } - - $rulesfile = __DIR__."/../../uptodate_data/php/frameworks/suitecrm/rules.json"; - - if (is_array($this->customFile)) { - if (!in_array($rulesfile, $this->customFile, true)) { - $this->readCustomFile($rulesfile); - } - } else { - if ($this->customFile !== $rulesfile) { - $this->readCustomFile($rulesfile); - } - } - } - - if (is_array($this->frameworks) && in_array("codeigniter", $this->frameworks, true)) { - $sanitizersfile = __DIR__."/../../uptodate_data/php/frameworks/codeigniter/sanitizers.json"; - - if (is_array($this->sanitizersFile)) { - if (!in_array($sanitizersfile, $this->sanitizersFile, true)) { - $this->readSanitizersFile($sanitizersfile); - } - } else { - if ($this->sanitizersFile !== $sanitizersfile) { - $this->readSanitizersFile($sanitizersfile); - } - } - - $sinksfile = __DIR__."/../../uptodate_data/php/frameworks/codeigniter/sinks.json"; - - if (is_array($this->sinksFile)) { - if (!in_array($sinksfile, $this->sinksFile, true)) { - $this->readSinksFile($sinksfile); - } - } else { - if ($this->sinksFile !== $sinksfile) { - $this->readSinksFile($sinksfile); - } - } - - $sourcesfile = __DIR__."/../../uptodate_data/php/frameworks/codeigniter/sources.json"; - - if (is_array($this->sourcesFile)) { - if (!in_array($sourcesfile, $this->sourcesFile, true)) { - $this->readSourcesFile($sourcesfile); - } - } else { - if ($this->sourcesFile !== $sourcesfile) { - $this->readSourcesFile($sourcesfile); - } - } - - $validatorsfile = __DIR__."/../../uptodate_data/php/frameworks/codeigniter/validators.json"; - - if (is_array($this->validatorsFile)) { - if (!in_array($validatorsfile, $this->validatorsFile, true)) { - $this->readValidatorsFile($validatorsfile); - } - } else { - if ($this->validatorsFile !== $validatorsfile) { - $this->readValidatorsFile($validatorsfile); - } - } - - $rulesfile = __DIR__."/../../uptodate_data/php/frameworks/codeigniter/rules.json"; - - if (is_array($this->customFile)) { - if (!in_array($rulesfile, $this->customFile, true)) { - $this->readCustomFile($rulesfile); - } - } else { - if ($this->customFile !== $rulesfile) { - $this->readCustomFile($rulesfile); - } - } - } - - - if (is_array($this->frameworks) && in_array("wordpress", $this->frameworks, true)) { - $sanitizersfile = __DIR__."/../../uptodate_data/php/frameworks/wordpress/sanitizers.json"; - - if (is_array($this->sanitizersFile)) { - if (!in_array($sanitizersfile, $this->sanitizersFile, true)) { - $this->readSanitizersFile($sanitizersfile); - } - } else { - if ($this->sanitizersFile !== $sanitizersfile) { - $this->readSanitizersFile($sanitizersfile); - } - } - - $sinksfile = __DIR__."/../../uptodate_data/php/frameworks/wordpress/sinks.json"; - - if (is_array($this->sinksFile)) { - if (!in_array($sinksfile, $this->sinksFile, true)) { - $this->readSinksFile($sinksfile); - } - } else { - if ($this->sinksFile !== $sinksfile) { - $this->readSinksFile($sinksfile); - } - } - - $sourcesfile = __DIR__."/../../uptodate_data/php/frameworks/wordpress/sources.json"; - - if (is_array($this->sourcesFile)) { - if (!in_array($sourcesfile, $this->sourcesFile, true)) { - $this->readSourcesFile($sourcesfile); - } - } else { - if ($this->sourcesFile !== $sourcesfile) { - $this->readSourcesFile($sourcesfile); - } - } - - $validatorsfile = __DIR__."/../../uptodate_data/php/frameworks/wordpress/validators.json"; - - if (is_array($this->validatorsFile)) { - if (!in_array($validatorsfile, $this->validatorsFile, true)) { - $this->readValidatorsFile($validatorsfile); - } - } else { - if ($this->validatorsFile !== $validatorsfile) { - $this->readValidatorsFile($validatorsfile); - } - } - - $rulesfile = __DIR__."/../../uptodate_data/php/frameworks/wordpress/rules.json"; - - if (is_array($this->customFile)) { - if (!in_array($rulesfile, $this->customFile, true)) { - $this->readCustomFile($rulesfile); - } - } else { - if ($this->customFile !== $rulesfile) { - $this->readCustomFile($rulesfile); - } - } - } - - - if (is_array($this->frameworks) && in_array("symfony", $this->frameworks, true)) { - $sanitizersfile = __DIR__."/../../uptodate_data/php/frameworks/symfony/sanitizers.json"; - - if (is_array($this->sanitizersFile)) { - if (!in_array($sanitizersfile, $this->sanitizersFile, true)) { - $this->readSanitizersFile($sanitizersfile); - } - } else { - if ($this->sanitizersFile !== $sanitizersfile) { - $this->readSanitizersFile($sanitizersfile); - } - } - - $sinksfile = __DIR__."/../../uptodate_data/php/frameworks/symfony/sinks.json"; - - if (is_array($this->sinksFile)) { - if (!in_array($sinksfile, $this->sinksFile, true)) { - $this->readSinksFile($sinksfile); - } - } else { - if ($this->sinksFile !== $sinksfile) { - $this->readSinksFile($sinksfile); - } - } - - $sourcesfile = __DIR__."/../../uptodate_data/php/frameworks/symfony/sources.json"; - - if (is_array($this->sourcesFile)) { - if (!in_array($sourcesfile, $this->sourcesFile, true)) { - $this->readSourcesFile($sourcesfile); - } - } else { - if ($this->sourcesFile !== $sourcesfile) { - $this->readSourcesFile($sourcesfile); - } - } - - $validatorsfile = __DIR__."/../../uptodate_data/php/frameworks/symfony/validators.json"; - - if (is_array($this->validatorsFile)) { - if (!in_array($validatorsfile, $this->validatorsFile, true)) { - $this->readValidatorsFile($validatorsfile); - } - } else { - if ($this->validatorsFile !== $validatorsfile) { - $this->readValidatorsFile($validatorsfile); - } - } - - $rulesfile = __DIR__."/../../uptodate_data/php/frameworks/symfony/rules.json"; - - if (is_array($this->customFile)) { - if (!in_array($rulesfile, $this->customFile, true)) { - $this->readCustomFile($rulesfile); - } - } else { - if ($this->customFile !== $rulesfile) { - $this->readCustomFile($rulesfile); - } - } - } - - - if (is_array($this->frameworks) && in_array("prestashop", $this->frameworks, true)) { - $sanitizersfile = __DIR__."/../../uptodate_data/php/frameworks/prestashop/sanitizers.json"; - - if (is_array($this->sanitizersFile)) { - if (!in_array($sanitizersfile, $this->sanitizersFile, true)) { - $this->readSanitizersFile($sanitizersfile); - } - } else { - if ($this->sanitizersFile !== $sanitizersfile) { - $this->readSanitizersFile($sanitizersfile); - } - } - - $sinksfile = __DIR__."/../../uptodate_data/php/frameworks/prestashop/sinks.json"; - - if (is_array($this->sinksFile)) { - if (!in_array($sinksfile, $this->sinksFile, true)) { - $this->readSinksFile($sinksfile); - } - } else { - if ($this->sinksFile !== $sinksfile) { - $this->readSinksFile($sinksfile); - } - } - - $sourcesfile = __DIR__."/../../uptodate_data/php/frameworks/prestashop/sources.json"; - - if (is_array($this->sourcesFile)) { - if (!in_array($sourcesfile, $this->sourcesFile, true)) { - $this->readSourcesFile($sourcesfile); - } - } else { - if ($this->sourcesFile !== $sourcesfile) { - $this->readSourcesFile($sourcesfile); - } - } - - $validatorsfile = __DIR__."/../../uptodate_data/php/frameworks/prestashop/validators.json"; - - if (is_array($this->validatorsFile)) { - if (!in_array($validatorsfile, $this->validatorsFile, true)) { - $this->readValidatorsFile($validatorsfile); - } - } else { - if ($this->validatorsFile !== $validatorsfile) { - $this->readValidatorsFile($validatorsfile); - } - } - - $rulesfile = __DIR__."/../../uptodate_data/php/frameworks/prestashop/rules.json"; - - if (is_array($this->customFile)) { - if (!in_array($rulesfile, $this->customFile, true)) { - $this->readCustomFile($rulesfile); - } - } else { - if ($this->customFile !== $rulesfile) { - $this->readCustomFile($rulesfile); - } - } - } - } - - public function readSanitizers() - { - if (is_null($this->sanitizersFile)) { - $this->readSanitizersFile(__DIR__."/../../uptodate_data/php/sanitizers.json"); - $this->readSanitizersFile(__DIR__."/../../uptodate_data/js/sanitizers.json"); - } else { - if (is_array($this->sanitizersFile)) { - foreach ($this->sanitizersFile as $file) { - $this->readSanitizersFile($file); - } - } else { - $this->readSanitizersFile($this->sanitizersFile); - } - } - } - - public function readSanitizersFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); - } - - $outputJson = file_get_contents($file); - - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'sanitizers'})) { - $sanitizers = $parsedJson-> {'sanitizers'}; - foreach ($sanitizers as $sanitizer) { - if (!isset($sanitizer-> {'name'}) - || !isset($sanitizer-> {'language'})) { - Utils::printError(Lang::FORMAT_SANITIZERS); - } - - $name = $sanitizer-> {'name'}; - $language = $sanitizer-> {'language'}; - - $prevent = []; - if (isset($sanitizer-> {'prevent'})) { - $prevent = $sanitizer-> {'prevent'}; - } - - $mySanitizer = new MySanitizer($name, $language, $prevent); - - if (isset($sanitizer-> {'instanceof'})) { - $mySanitizer->setIsInstance(true); - $mySanitizer->setInstanceOfName($sanitizer-> {'instanceof'}); - } - - if (isset($sanitizer-> {'parameters'})) { - $parameters = $sanitizer-> {'parameters'}; - foreach ($parameters as $parameter) { - if (isset($parameter-> {'id'}) && isset($parameter-> {'conditions'})) { - if (is_int($parameter-> {'id'}) - && ($parameter-> {'conditions'} === "equals" - || $parameter-> {'conditions'} === "taint" - || $parameter-> {'conditions'} === "sanitize")) { - if ($parameter-> {'conditions'} === "equals") { - if (isset($parameter-> {'values'})) { - $mySanitizer->addParameter( - $parameter-> {'id'}, - $parameter-> {'conditions'}, - $parameter-> {'values'} - ); - } - } else { - $mySanitizer->addParameter( - $parameter-> {'id'}, - $parameter-> {'conditions'} - ); - } - } - } - } - - $mySanitizer->setHasParameters(true); - } - - $this->sanitizers[] = $mySanitizer; - } - } else { - Utils::printError(Lang::FORMAT_SANITIZERS); - } - } - } - - public function readSinks() - { - if (is_null($this->sinksFile)) { - $this->readSinksFile(__DIR__."/../../uptodate_data/php/sinks.json"); - $this->readSinksFile(__DIR__."/../../uptodate_data/js/sinks.json"); - } else { - if (is_array($this->sinksFile)) { - foreach ($this->sinksFile as $file) { - $this->readSinksFile($file); - } - } else { - $this->readSinksFile($this->sinksFile); - } - } - } - - public function readSinksFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); - } - - $outputJson = file_get_contents($file); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'sinks'})) { - $sinks = $parsedJson-> {'sinks'}; - foreach ($sinks as $sink) { - if (!isset($sink-> {'name'}) - || !isset($sink-> {'language'}) - || !isset($sink-> {'attack'}) - || !isset($sink-> {'cwe'})) { - Utils::printError(Lang::FORMAT_SINKS); - } - - $name = $sink-> {'name'}; - $language = $sink-> {'language'}; - $attack = $sink-> {'attack'}; - $cwe = $sink-> {'cwe'}; - - $mySink = new MySink($name, $language, $attack, $cwe); - - if (isset($sink-> {'instanceof'})) { - $mySink->setIsInstance(true); - $mySink->setInstanceOfName($sink-> {'instanceof'}); - } - - if (isset($sink-> {'conditions'})) { - $mySink->addGlobalconditions($sink-> {'conditions'}); - } - - if (isset($sink-> {'parameters'})) { - $parameters = $sink-> {'parameters'}; - foreach ($parameters as $parameter) { - if (isset($parameter-> {'id'}) && is_int($parameter-> {'id'})) { - if (isset($parameter-> {'conditions'})) { - $mySink->addParameter($parameter-> {'id'}, $parameter-> {'conditions'}); - } else { - $mySink->addParameter($parameter-> {'id'}); - } - } - } - - $mySink->setHasParameters(true); - } - - $this->sinks[] = $mySink; - } - } else { - Utils::printError(Lang::FORMAT_SINKS); - } - } - } - - public function readSources() - { - if (is_null($this->sourcesFile)) { - $this->readSourcesFile(__DIR__."/../../uptodate_data/php/sources.json"); - $this->readSourcesFile(__DIR__."/../../uptodate_data/js/sources.json"); - } else { - if (is_array($this->sourcesFile)) { - foreach ($this->sourcesFile as $file) { - $this->readSourcesFile($file); - } - } else { - $this->readSourcesFile($this->sourcesFile); - } - } - } - - public function readSourcesFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); - } - - $outputJson = file_get_contents($file); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'sources'})) { - $sources = $parsedJson-> {'sources'}; - foreach ($sources as $source) { - if (!isset($source-> {'name'}) - || !isset($source-> {'language'})) { - Utils::printError(Lang::FORMAT_SOURCES); - } - - $name = $source-> {'name'}; - $language = $source-> {'language'}; - - $mySource = new MySource($name, $language); - - if (isset($source-> {'is_function'}) && $source-> {'is_function'}) { - $mySource->setIsFunction(true); - } - - if (isset($source-> {'is_array'}) && $source-> {'is_array'}) { - $mySource->setIsArray(true); - } - - if (isset($source-> {'is_object'}) && $source-> {'is_object'}) { - $mySource->setIsObject(true); - } - - if (isset($source-> {'array_index'})) { - $arr = array($source-> {'array_index'} => false); - $mySource->setArrayValue($arr); - } - - if (isset($source-> {'instanceof'})) { - $mySource->setIsInstance(true); - $mySource->setInstanceOfName($source-> {'instanceof'}); - } - - if (isset($source-> {'return_array_index'})) { - $mySource->setReturnArray(true); - $mySource->setReturnArrayValue($source-> {'return_array_index'}); - } - - if (isset($source-> {'label'})) { - $label = MyDefinition::SECURITY_LOW; - if ($source-> {'label'} === "high") { - $label = MyDefinition::SECURITY_HIGH; - } - - $mySource->setLabel($label); - } - - if (isset($source-> {'parameters'})) { - $parameters = $source-> {'parameters'}; - foreach ($parameters as $parameter) { - if (is_int($parameter-> {'id'})) { - $mySource->addParameter($parameter-> {'id'}); - - if (isset($parameter-> {'is_array'}) - && $parameter-> {'is_array'} - && isset($parameter-> {'array_index'})) { - $mySource->addconditionsParameter( - $parameter-> {'id'}, - MySource::CONDITION_ARRAY, - $parameter-> {'array_index'} - ); - } - } - } - - $mySource->setHasParameters(true); - } - - $this->sources[] = $mySource; - } - } else { - Utils::printError(Lang::FORMAT_SOURCES); - } - } - } - - public function readValidators() - { - if (is_null($this->validatorsFile)) { - $this->readValidatorsFile(__DIR__."/../../uptodate_data/php/validators.json"); - $this->readValidatorsFile(__DIR__."/../../uptodate_data/js/validators.json"); - } else { - if (is_array($this->validatorsFile)) { - foreach ($this->validatorsFile as $file) { - $this->readValidatorsFile($file); - } - } else { - $this->readValidatorsFile($this->validatorsFile); - } - } - } - - public function readValidatorsFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); - } - - $outputJson = file_get_contents($file); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'validators'})) { - $validators = $parsedJson-> {'validators'}; - foreach ($validators as $validator) { - if (!isset($validator-> {'name'}) - || !isset($validator-> {'language'})) { - Utils::printError(Lang::FORMAT_VALIDATORS); - } - - $name = $validator-> {'name'}; - $language = $validator-> {'language'}; - - $myValidator = new MyValidator($name, $language); - - if (isset($validator-> {'parameters'})) { - $parameters = $validator-> {'parameters'}; - foreach ($parameters as $parameter) { - if (isset($parameter-> {'id'}) && isset($parameter-> {'conditions'})) { - if (is_int($parameter-> {'id'}) - && ($parameter-> {'conditions'} === "not_tainted" - || $parameter-> {'conditions'} === "array_not_tainted" - || $parameter-> {'conditions'} === "valid" - || $parameter-> {'conditions'} === "equals")) { - if ($parameter-> {'conditions'} === "equals") { - if (isset($parameter-> {'values'})) { - $myValidator->addParameter( - $parameter-> {'id'}, - $parameter-> {'conditions'}, - $parameter-> {'values'} - ); - } - } else { - $myValidator->addParameter( - $parameter-> {'id'}, - $parameter-> {'conditions'} - ); - } - } - } - } - - $myValidator->setHasParameters(true); - } - - if (isset($validator-> {'instanceof'})) { - $myValidator->setIsInstance(true); - $myValidator->setInstanceOfName($validator-> {'instanceof'}); - } - - $this->validators[] = $myValidator; - } - } else { - Utils::printError(Lang::FORMAT_VALIDATORS); - } - } - } - - public function readResolvedIncludes() - { - if (!is_null($this->resolvedIncludes)) { - if (!file_exists($this->resolvedIncludes)) { - Utils::printError( - Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($this->resolvedIncludes).")" - ); - } - - $outputJson = file_get_contents($this->resolvedIncludes); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'includes'})) { - $includes = $parsedJson-> {'includes'}; - foreach ($includes as $include) { - if (!isset($include-> {'line'}) - || !isset($include-> {'column'}) - || !isset($include-> {'source_file'}) - || !isset($include-> {'value'})) { - Utils::printError(Lang::FORMAT_INCLUDES); - } - - if (realpath($include-> {'source_file'})) { - $line = $include-> {'line'}; - $column = $include-> {'column'}; - $sourceFile = realpath($include-> {'source_file'}); - $value = $include-> {'value'}; - - $myInclude = new MyInclude($line, $column, $sourceFile, $value); - $this->resolvedIncludesAnalysis[] = $myInclude; - } - } - } else { - Utils::printError(Lang::FORMAT_INCLUDES); - } - } - } - - public function readFalsePositives() - { - if (!is_null($this->falsePositives)) { - if (is_string($this->falsePositives)) { - $this->readFalsePositivesFile($this->falsePositives); - } elseif (is_array($this->falsePositives)) { - foreach ($this->falsePositives as $falsePositive) { - if (!isset($falsePositive["vuln_id"])) { - Utils::printError(Lang::FORMAT_FALSE_POSITIVES); - } - - $vulnId = $falsePositive["vuln_id"]; - - $myVuln = new MyVuln($vulnId); - $this->falsePositivesAnalysis[] = $myVuln; - } - } else { - Utils::printError(Lang::FORMAT_FALSE_POSITIVES); - } - } - } - - public function readFalsePositivesFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError( - Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" - ); - } - - $outputJson = file_get_contents($file); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'false_positives'})) { - $falsePositives = $parsedJson-> {'false_positives'}; - foreach ($falsePositives as $falsePositive) { - if (!isset($falsePositive-> {'vuln_id'})) { - Utils::printError(Lang::FORMAT_FALSE_POSITIVES); - } - - $vulnId = $falsePositive-> {'vuln_id'}; - - $myVuln = new MyVuln($vulnId); - $this->falsePositivesAnalysis[] = $myVuln; - } - } else { - Utils::printError(Lang::FORMAT_FALSE_POSITIVES); - } - } - } - - public function readExcludes() - { - if (!is_null($this->excludes)) { - if (is_string($this->excludes)) { - $this->readExcludesFile($this->excludes); - } elseif (is_array($this->excludes)) { - if (isset($this->excludes["exclude_files"])) { - $excludeFiles = $this->excludes["exclude_files"]; - foreach ($excludeFiles as $excludeFile) { - if (realpath($excludeFile)) { - $this->excludesFilesAnalysis[] = realpath($excludeFile); - } - } - } - - if (isset($this->excludes["exclude_folders"])) { - $excludeFolders = $this->excludes["exclude_folders"]; - foreach ($excludeFolders as $excludeFolder) { - if (realpath($excludeFolder)) { - $this->excludesFoldersAnalysis[] = realpath($excludeFolder); - } - } - } - } - } - } - - public function readExcludesFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError( - Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" - ); - } - - $outputJson = file_get_contents($file); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'exclude_files'})) { - $excludeFiles = $parsedJson-> {'exclude_files'}; - foreach ($excludeFiles as $excludeFile) { - if (realpath($excludeFile)) { - $this->excludesFilesAnalysis[] = realpath($excludeFile); - } - } - } - - if (isset($parsedJson-> {'exclude_folders'})) { - $excludeFolders = $parsedJson-> {'exclude_folders'}; - foreach ($excludeFolders as $excludeFolder) { - if (realpath($excludeFolder)) { - $this->excludesFoldersAnalysis[] = realpath($excludeFolder); - } - } - } - } - } - - public function readIncludesFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError( - Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" - ); - } - - $outputJson = file_get_contents($file); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'include_files'})) { - $includeFiles = $parsedJson-> {'include_files'}; - foreach ($includeFiles as $includeFile) { - if (realpath($includeFile)) { - $this->includesFilesAnalysis[] = realpath($includeFile); - } - } - } - - if (isset($parsedJson-> {'include_folders'})) { - $includeFolders = $parsedJson-> {'include_folders'}; - foreach ($includeFolders as $includeFolder) { - if (realpath($includeFolder)) { - $this->includesFoldersAnalysis[] = realpath($includeFolder); - } - } - } - } - } - - public function readIncludes() - { - if (!is_null($this->includes)) { - if (is_string($this->includes)) { - $this->readIncludesFile($this->includes); - } elseif (is_array($this->includes)) { - if (isset($this->includes["include_files"])) { - $includeFiles = $this->includes["include_files"]; - foreach ($includeFiles as $includeFile) { - if (realpath($includeFile)) { - $this->includesFilesAnalysis[] = realpath($includeFile); - } - } - } - - if (isset($this->includes["include_folders"])) { - $includeFolders = $this->includes["include_folders"]; - foreach ($includeFolders as $includeFolder) { - if (realpath($includeFolder)) { - $this->includesFoldersAnalysis[] = realpath($includeFolder); - } - } - } - } - } - } - - public function readCustomRules() - { - if (is_null($this->customFile)) { - $this->readCustomFile(__DIR__."/../../uptodate_data/php/rules.json"); - $this->readCustomFile(__DIR__."/../../uptodate_data/js/rules.json"); - } else { - if (is_array($this->customFile)) { - foreach ($this->customFile as $file) { - $this->readCustomFile($file); - } - } else { - $this->readCustomFile($this->customFile); - } - } - } - - public function readCustomFile($file) - { - if (!is_null($file)) { - if (!file_exists($file)) { - Utils::printError( - Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" - ); - } - - $outputJson = file_get_contents($file); - $parsedJson = json_decode($outputJson); - - if (isset($parsedJson-> {'custom_rules'})) { - $customRules = $parsedJson-> {'custom_rules'}; - foreach ($customRules as $customRule) { - if (isset($customRule-> {'description'})) { - $myCustom = new MyCustomRule($customRule-> {'description'}); - - if (!isset($customRule-> {'cwe'})) { - $customRule-> {'cwe'} = ""; - } - - if (!isset($customRule-> {'attack'})) { - $customRule-> {'attack'} = ""; - } - - $myCustom->setCwe($customRule-> {'cwe'}); - $myCustom->setAttack($customRule-> {'attack'}); - if (isset($customRule-> {'extra'})) { - $myCustom->setExtra($customRule-> {'extra'}); - } - - if (isset($customRule-> {'sequence'}) && isset($customRule-> {'action'})) { - $myCustom->setType(MyCustomRule::TYPE_SEQUENCE); - $myCustom->setAction($customRule-> {'action'}); - - foreach ($customRule-> {'sequence'} as $seq) { - if (isset($seq-> {'function_name'}) && isset($seq-> {'language'})) { - $myCustomFunction = null; - $myCustomFunction = $myCustom->addToSequence( - $seq-> {'function_name'}, - $seq-> {'language'} - ); - - if (isset($seq-> {'parameters'}) && !is_null($myCustomFunction)) { - $parameters = $seq-> {'parameters'}; - foreach ($parameters as $parameter) { - if (isset($parameter-> {'id'}) && isset($parameter-> {'values'})) { - $validbydefault = false; - if (isset($parameter-> {'valid_by_default'}) - && $parameter-> {'valid_by_default'}) { - $validbydefault = true; - } - - $fixed = false; - if (isset($parameter-> {'fixed'}) - && $parameter-> {'fixed'}) { - $fixed = true; - } - - if (is_int($parameter-> {'id'})) { - $myCustomFunction->addParameter( - $parameter-> {'id'}, - $validbydefault, - $fixed, - $parameter-> {'values'} - ); - } - } - } - - $myCustomFunction->setHasParameters(true); - } - - if (isset($seq-> {'instanceof'})) { - $myCustomFunction->setIsInstance(true); - $myCustomFunction->setInstanceOfName($seq-> {'instanceof'}); - } - } - } - } elseif (isset($customRule-> {'name'}) - && isset($customRule-> {'language'}) - && isset($customRule-> {'action'}) - && isset($customRule-> {'is_function'}) - && $customRule-> {'is_function'} === true) { - $myCustom->setType(MyCustomRule::TYPE_FUNCTION); - $myCustom->setAction($customRule-> {'action'}); - $myCustomFunction = $myCustom->addFunctionDefinition( - $customRule-> {'name'}, - $customRule-> {'language'} - ); - - if (isset($customRule-> {'parameters'})) { - $parameters = $customRule-> {'parameters'}; - foreach ($parameters as $parameter) { - if (isset($parameter-> {'id'}) && isset($parameter-> {'values'})) { - if (is_int($parameter-> {'id'})) { - $fixed = false; - if (isset($parameter-> {'fixed'}) - && $parameter-> {'fixed'}) { - $fixed = true; - } - - $validbydefault = false; - if (isset($parameter-> {'valid_by_default'}) - && $parameter-> {'valid_by_default'}) { - $validbydefault = true; - } - - $myCustomFunction->addParameter( - $parameter-> {'id'}, - $validbydefault, - $fixed, - $parameter-> {'values'} - ); - } - } - } - - $myCustomFunction->setHasParameters(true); - } - - if (isset($customRule-> {'instanceof'})) { - $myCustomFunction->setIsInstance(true); - $myCustomFunction->setInstanceOfName($customRule-> {'instanceof'}); - } - - if (isset($customRule-> {'min_nb_args'})) { - $myCustomFunction->setMinNbArgs($customRule-> {'min_nb_args'}); - } - - if (isset($customRule-> {'max_nb_args'})) { - $myCustomFunction->setMaxNbArgs($customRule-> {'max_nb_args'}); - } - } elseif (isset($customRule-> {'name'}) - && isset($customRule-> {'language'}) - && isset($customRule-> {'action'}) - && (!isset($customRule-> {'is_function'}) - || $customRule-> {'is_function'} !== true)) { - $myCustom->setType(MyCustomRule::TYPE_VARIABLE); - $myCustom->setAction($customRule-> {'action'}); - $myCustomVariable = $myCustom->addVariableDefinition( - $customRule-> {'name'}, - $customRule-> {'language'} - ); - - if (isset($customRule-> {'instanceof'})) { - $myCustomVariable->setIsInstance(true); - $myCustomVariable->setInstanceOfName($customRule-> {'instanceof'}); - } - } - - $this->customRules[] = $myCustom; + if (!is_null($inclusions)) { + if (is_string($inclusions)) { + $this->readIncludesFile($inclusions); + } elseif (is_array($inclusions)) { + foreach ($inclusions as $includeFile) { + if (is_string($includeFile) && !in_array($includeFile, $this->includesFilesAnalysis, true)) { + $this->includesFilesAnalysis[] = $includeFile; } } } diff --git a/package/src/progpilot/Inputs/MyInputsInternalApi.php b/package/src/progpilot/Inputs/MyInputsInternalApi.php new file mode 100644 index 00000000..369ae934 --- /dev/null +++ b/package/src/progpilot/Inputs/MyInputsInternalApi.php @@ -0,0 +1,1050 @@ +dev = false; + $this->languages = ["php"]; + + $this->customRules = []; + $this->sanitizers = []; + $this->sinks = []; + $this->sources = []; + $this->validators = []; + + $this->overwrittenCustomRules = false; + $this->overwrittenSanitizers = false; + $this->overwrittenSinks = false; + $this->overwrittenSources = false; + $this->overwrittenValidators = false; + + $this->falsePositivesAnalysis = []; + $this->excludesFilesAnalysis = ["vendor", "node_modules"]; + $this->includesFilesAnalysis = []; + $this->resolvedIncludesAnalysis = []; + + $this->file = null; + $this->code = null; + $this->folder = null; + + $this->falsePositives= null; + } + + public function resolvePaths() + { + $tmpFiles = $this->excludesFilesAnalysis; + $this->excludesFilesAnalysis = []; + + foreach ($tmpFiles as $excludedFile) { + if (strpos($excludedFile, "/") !== false + || strpos($excludedFile, "\\") !== false) { + // there is a slash, the dev likely wants a path + if (str_starts_with("./", $excludedFile) === 0 + && str_starts_with(".\\", $excludedFile) === 0 + && str_starts_with("/", $excludedFile) === 0 + && preg_match("/^[a-bA-B]*:/", $excludedFile) === 0) { + // it's not a relative or absolute path + $excludedFile = ".".DIRECTORY_SEPARATOR.$excludedFile; + } + + $excludedFile = realpath($excludedFile); + } + + if (!in_array($excludedFile, $this->excludesFilesAnalysis, true)) { + $this->excludesFilesAnalysis[] = $excludedFile; + } + } + + + $tmpFiles = $this->includesFilesAnalysis; + $this->includesFilesAnalysis = []; + + foreach ($tmpFiles as $includedFile) { + if (strpos($includedFile, "/") !== false + || strpos($includedFile, "\\") !== false) { + // there is a slash, the dev likely wants a path + if (str_starts_with("./", $includedFile) === 0 + && str_starts_with(".\\", $includedFile) === 0 + && str_starts_with("/", $includedFile) === 0 + && preg_match("/^[a-bA-B]*:/", $includedFile) === 0) { + // it's not a relative or absolute path + $includedFile = ".".DIRECTORY_SEPARATOR.$includedFile; + } + + $includedFile = realpath($includedFile); + } + + if (!in_array($includedFile, $this->includesFilesAnalysis, true)) { + $this->includesFilesAnalysis[] = $includedFile; + } + } + } + + public function isLanguage($lang) + { + return in_array($lang, $this->languages, true); + } + + public function isExcludedFile($name) + { + $name = realpath($name); + $basename = basename($name); + $folders = explode(DIRECTORY_SEPARATOR, $name); + + foreach ($this->excludesFilesAnalysis as $excludeFileName) { + if (strpos($excludeFileName, "/") === false + && strpos($excludeFileName, "\\") === false) { + // it's not a path + // test if it's a file name + if (basename($excludeFileName) === $basename) { + return true; + } + + // test if it's a folder name + if (!empty($folders)) { + foreach ($folders as $folder) { + if ($folder === $excludeFileName) { + return true; + } + } + } + } else { + // we found if + if (strpos($name, $excludeFileName) === 0) { + return true; + } + } + } + + return false; + } + + public function isIncludedFile($name) + { + $name = realpath($name); + $basename = basename($name); + $folders = explode(DIRECTORY_SEPARATOR, $name); + + foreach ($this->includesFilesAnalysis as $includedFileName) { + if (strpos($includedFileName, "/") === false + && strpos($includedFileName, "\\") === false) { + // it's not a path + // test if it's a file name + if (basename($includedFileName) === $basename) { + return true; + } + + // test if it's a folder name + if (!empty($folders)) { + foreach ($folders as $folder) { + if ($folder === $includedFileName) { + return true; + } + } + } + } else { + // we found if + if (strpos($name, $includedFileName) === 0) { + return true; + } + } + } + + return false; + } + + public function getIncludeByLocation($line, $column, $sourceFile) + { + foreach ($this->resolvedIncludesAnalysis as $myInclude) { + if ($myInclude->getLine() === $line + && $myInclude->getColumn() === $column + && $myInclude->getSourceFile() === $sourceFile) { + return $myInclude; + } + } + + return null; + } + + public function getValidatorByName($myFunc, $myClass) + { + foreach ($this->validators as $myValidator) { + if ($myValidator->getName() === $myFunc->getName()) { + if (!$myValidator->isInstance() && !$myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + return $myValidator; + } + + if ($myValidator->isInstance() && $myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + if (!is_null($myClass) && + ($myValidator->getInstanceOfName() === $myClass->getName() + || $myValidator->getInstanceOfName() === $myClass->getExtendsOf())) { + return $myValidator; + } + } + } + } + + return null; + } + + public function getSanitizerByName($myFunc, $myClass) + { + foreach ($this->sanitizers as $mySanitizer) { + if ($mySanitizer->getName() === $myFunc->getName()) { + if (!$mySanitizer->isInstance() && !$myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + return $mySanitizer; + } + + if ($mySanitizer->isInstance() && $myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + if (!is_null($myClass) && + ($mySanitizer->getInstanceOfName() === $myClass->getName() + || $mySanitizer->getInstanceOfName() === $myClass->getExtendsOf())) { + return $mySanitizer; + } + } + } + } + + return null; + } + + public function getSinkByName($myFunc, $myClass) + { + foreach ($this->sinks as $mySink) { + if ($mySink->getName() === $myFunc->getName()) { + if (!$mySink->isInstance() && !$myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + return $mySink; + } + + if ($mySink->isInstance() && $myFunc->isType(MyFunction::TYPE_FUNC_METHOD)) { + if (!is_null($myClass) + && ($mySink->getInstanceOfName() === $myClass->getName() + || $mySink->getInstanceOfName() === $myClass->getExtendsOf())) { + return $mySink; + } + } + } + } + + return null; + } + + public function getSourceArrayByName( + $myFuncOrDef, + $arrValue = false + ) { + foreach ($this->sources as $mySource) { + if ($mySource->getName() === $myFuncOrDef->getName() + && $mySource->getIsArray() + && $arrValue === false) { + return $mySource; + } + } + + return null; + } + + public function getSourceByName($myFuncOrDef, $myClass, $dimArray = null) + { + foreach ($this->sources as $mySource) { + if ($mySource->getName() === $myFuncOrDef->getName()) { + if (!$mySource->isInstance() + && empty($mySource->getArrayValue()) + && !$myFuncOrDef->isType(MyFunction::TYPE_FUNC_METHOD) + && !$myFuncOrDef->isType(MyDefinition::TYPE_PROPERTY)) { + return $mySource; + } + + if ($mySource->isInstance() + && ($myFuncOrDef->isType(MyFunction::TYPE_FUNC_METHOD) + || $myFuncOrDef->isType(MyDefinition::TYPE_PROPERTY))) { + if (!is_null($myClass) && + ($mySource->getInstanceOfName() === $myClass->getName() + || $mySource->getInstanceOfName() === $myClass->getExtendsOf())) { + return $mySource; + } + } + + if ($mySource->getIsArray() + && !empty($mySource->getArrayValue()) + && $mySource->isAnArrayValue($dimArray)) { + return $mySource; + } + } + } + + return null; + } + + public function getFalsePositiveById($id) + { + foreach ($this->falsePositivesAnalysis as $falsePositive) { + if ($falsePositive->getId() === $id) { + return $falsePositive; + } + } + + return null; + } + + public function readDir($dir, &$files) + { + if (is_dir($dir)) { + $filesanddirs = @scandir($dir); + + if ($filesanddirs !== false) { + foreach ($filesanddirs as $filedir) { + if ($filedir !== '.' && $filedir !== "..") { + $folderorfile = $dir.DIRECTORY_SEPARATOR.$filedir; + + if (is_dir($folderorfile)) { + $this->readDir($folderorfile, $files); + } else { + $files[] = $folderorfile; + } + } + } + } + } + } + + public function readFrameworks() + { + $frameworksConfigurationFiles = []; + $frameworksDir = __DIR__."/../../uptodate_data/php/frameworks/"; + + $this->readDir($frameworksDir, $frameworksConfigurationFiles); + + foreach ($frameworksConfigurationFiles as $configFile) { + $basenameConfigFile = basename($configFile); + switch ($basenameConfigFile) { + case "sanitizers.json": + if (!$this->overwrittenSanitizers) { + $this->readSanitizersFile($configFile); + } + break; + case "validators.json": + if (!$this->overwrittenValidators) { + $this->readValidatorsFile($configFile); + } + break; + case "sinks.json": + if (!$this->overwrittenSinks) { + $this->readSinksFile($configFile); + } + break; + case "sources.json": + if (!$this->overwrittenSources) { + $this->readSourcesFile($configFile); + } + break; + case "rules.json": + if (!$this->overwrittenCustomRules) { + $this->readCustomFile($configFile); + } + break; + default: + break; + } + } + } + + public function readDefaultSanitizers() + { + if (!$this->overwrittenSanitizers) { + $this->readSanitizersFile(__DIR__."/../../uptodate_data/php/sanitizers.json"); + $this->readSanitizersFile(__DIR__."/../../uptodate_data/js/sanitizers.json"); + } + } + + public function readSanitizersFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); + } + + $outputJson = file_get_contents($file); + + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson-> {'sanitizers'})) { + $sanitizers = $parsedJson-> {'sanitizers'}; + foreach ($sanitizers as $sanitizer) { + if (!isset($sanitizer-> {'name'}) + || !isset($sanitizer-> {'language'})) { + Utils::printError(Lang::FORMAT_SANITIZERS); + } + + $name = $sanitizer-> {'name'}; + $language = $sanitizer-> {'language'}; + + $prevent = []; + if (isset($sanitizer-> {'prevent'})) { + $prevent = $sanitizer-> {'prevent'}; + } + + $mySanitizer = new MySanitizer($name, $language, $prevent); + + if (isset($sanitizer-> {'instanceof'})) { + $mySanitizer->setIsInstance(true); + $mySanitizer->setInstanceOfName($sanitizer-> {'instanceof'}); + } + + if (isset($sanitizer-> {'parameters'})) { + $parameters = $sanitizer-> {'parameters'}; + foreach ($parameters as $parameter) { + if (isset($parameter-> {'id'}) && isset($parameter-> {'conditions'})) { + if (is_int($parameter-> {'id'}) + && ($parameter-> {'conditions'} === "equals" + || $parameter-> {'conditions'} === "notequals" + || $parameter-> {'conditions'} === "taint" + || $parameter-> {'conditions'} === "sanitize")) { + if ($parameter-> {'conditions'} === "equals" + || $parameter-> {'conditions'} === "notequals") { + if (isset($parameter-> {'values'})) { + $mySanitizer->addParameter( + $parameter-> {'id'}, + $parameter-> {'conditions'}, + $parameter-> {'values'} + ); + } + } else { + $mySanitizer->addParameter( + $parameter-> {'id'}, + $parameter-> {'conditions'} + ); + } + } + } + } + + $mySanitizer->setHasParameters(true); + } + + if (!in_array($mySanitizer, $this->sanitizers, true)) { + $this->sanitizers[] = $mySanitizer; + } + } + } else { + Utils::printError(Lang::FORMAT_SANITIZERS); + } + } + } + + public function readDefaultSinks() + { + if (!$this->overwrittenSinks) { + $this->readSinksFile(__DIR__."/../../uptodate_data/php/sinks.json"); + $this->readSinksFile(__DIR__."/../../uptodate_data/js/sinks.json"); + } + } + + public function readSinksFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); + } + + $outputJson = file_get_contents($file); + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson-> {'sinks'})) { + $sinks = $parsedJson-> {'sinks'}; + foreach ($sinks as $sink) { + if (!isset($sink-> {'name'}) + || !isset($sink-> {'language'}) + || !isset($sink-> {'attack'}) + || !isset($sink-> {'cwe'})) { + Utils::printError(Lang::FORMAT_SINKS); + } + + $name = $sink-> {'name'}; + $language = $sink-> {'language'}; + $attack = $sink-> {'attack'}; + $cwe = $sink-> {'cwe'}; + + $mySink = new MySink($name, $language, $attack, $cwe); + + if (isset($sink-> {'instanceof'})) { + $mySink->setIsInstance(true); + $mySink->setInstanceOfName($sink-> {'instanceof'}); + } + + if (isset($sink-> {'conditions'})) { + $mySink->addGlobalconditions($sink-> {'conditions'}); + } + + if (isset($sink-> {'parameters'})) { + $parameters = $sink-> {'parameters'}; + foreach ($parameters as $parameter) { + if (isset($parameter-> {'id'}) && is_int($parameter-> {'id'})) { + if (isset($parameter-> {'conditions'})) { + $mySink->addParameter($parameter-> {'id'}, $parameter-> {'conditions'}); + } else { + $mySink->addParameter($parameter-> {'id'}); + } + } + } + + $mySink->setHasParameters(true); + } + + if (!in_array($mySink, $this->sinks, true)) { + $this->sinks[] = $mySink; + } + } + } else { + Utils::printError(Lang::FORMAT_SINKS); + } + } + } + + public function readDefaultSources() + { + if (!$this->overwrittenSources) { + $this->readSourcesFile(__DIR__."/../../uptodate_data/php/sources.json"); + $this->readSourcesFile(__DIR__."/../../uptodate_data/js/sources.json"); + } + } + + public function readSourcesFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); + } + + $outputJson = file_get_contents($file); + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson-> {'sources'})) { + $sources = $parsedJson-> {'sources'}; + foreach ($sources as $source) { + if (!isset($source-> {'name'}) + || !isset($source-> {'language'})) { + Utils::printError(Lang::FORMAT_SOURCES); + } + + $name = $source-> {'name'}; + $language = $source-> {'language'}; + + $mySource = new MySource($name, $language); + + if (isset($source-> {'is_function'}) && $source-> {'is_function'}) { + $mySource->setIsFunction(true); + } + + if (isset($source-> {'is_arrayof_objects'}) && $source-> {'is_arrayof_objects'}) { + $mySource->setIsArrayOfObjects(true); + } + + if (isset($source-> {'is_arrayof_arrays'}) && $source-> {'is_arrayof_arrays'}) { + $mySource->setIsArrayOfArrays(true); + } + + if (isset($source-> {'is_array'}) && $source-> {'is_array'}) { + $mySource->setIsArray(true); + } + + if (isset($source-> {'is_object'}) && $source-> {'is_object'}) { + $mySource->setIsObject(true); + } + + if (isset($source-> {'array_index'}) && is_array($source-> {'array_index'})) { + $arr = []; + foreach ($source-> {'array_index'} as $array_index) { + $arr[] = $array_index; + } + + $mySource->setArrayValue($arr); + } + + if (isset($source-> {'instanceof'})) { + $mySource->setIsInstance(true); + $mySource->setInstanceOfName($source-> {'instanceof'}); + } + + if (isset($source-> {'return_array_index'})) { + $mySource->setReturnArray(true); + $mySource->setReturnArrayValue($source-> {'return_array_index'}); + } + + if (isset($source-> {'label'})) { + $label = MyDefinition::SECURITY_LOW; + if ($source-> {'label'} === "high") { + $label = MyDefinition::SECURITY_HIGH; + } + + $mySource->setLabel($label); + } + + if (isset($source-> {'parameters'})) { + $parameters = $source-> {'parameters'}; + foreach ($parameters as $parameter) { + if (is_int($parameter-> {'id'})) { + $mySource->addParameter($parameter-> {'id'}); + + if (isset($parameter-> {'is_array'}) + && $parameter-> {'is_array'} + && isset($parameter-> {'array_index'})) { + $mySource->addconditionsParameter( + $parameter-> {'id'}, + MySource::CONDITION_ARRAY, + $parameter-> {'array_index'} + ); + } + } + } + + $mySource->setHasParameters(true); + } + + if (!in_array($mySource, $this->sources, true)) { + $this->sources[] = $mySource; + } + } + } else { + Utils::printError(Lang::FORMAT_SOURCES); + } + } + } + + public function readDefaultValidators() + { + if (!$this->overwrittenValidators) { + $this->readValidatorsFile(__DIR__."/../../uptodate_data/php/validators.json"); + $this->readValidatorsFile(__DIR__."/../../uptodate_data/js/validators.json"); + } + } + + public function readValidatorsFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError(Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")"); + } + + $outputJson = file_get_contents($file); + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson-> {'validators'})) { + $validators = $parsedJson-> {'validators'}; + foreach ($validators as $validator) { + if (!isset($validator-> {'name'}) + || !isset($validator-> {'language'})) { + Utils::printError(Lang::FORMAT_VALIDATORS); + } + + $name = $validator-> {'name'}; + $language = $validator-> {'language'}; + + $validWhenReturning = true; + if (isset($validator-> {'valid_when_returning'})) { + $validWhenReturning = $validator-> {'valid_when_returning'}; + } + + $myValidator = new MyValidator($name, $language, $validWhenReturning); + + if (isset($validator-> {'parameters'})) { + $parameters = $validator-> {'parameters'}; + foreach ($parameters as $parameter) { + if (isset($parameter-> {'id'}) && isset($parameter-> {'conditions'})) { + if (is_int($parameter-> {'id'}) + && ($parameter-> {'conditions'} === "not_tainted" + || $parameter-> {'conditions'} === "array_not_tainted" + || $parameter-> {'conditions'} === "valid" + || $parameter-> {'conditions'} === "equals" + || $parameter-> {'conditions'} === "notequals")) { + if ($parameter-> {'conditions'} === "equals" + || $parameter-> {'conditions'} === "notequals") { + if (isset($parameter-> {'values'})) { + $myValidator->addParameter( + $parameter-> {'id'}, + $parameter-> {'conditions'}, + $parameter-> {'values'} + ); + } + } else { + $myValidator->addParameter( + $parameter-> {'id'}, + $parameter-> {'conditions'} + ); + } + } + } + } + + $myValidator->setHasParameters(true); + } + + if (isset($validator-> {'instanceof'})) { + $myValidator->setIsInstance(true); + $myValidator->setInstanceOfName($validator-> {'instanceof'}); + } + + if (!in_array($myValidator, $this->validators, true)) { + $this->validators[] = $myValidator; + } + } + } else { + Utils::printError(Lang::FORMAT_VALIDATORS); + } + } + } + + public function readDefaultCustomRules() + { + if (!$this->overwrittenCustomRules) { + $this->readCustomFile(__DIR__."/../../uptodate_data/php/rules.json"); + $this->readCustomFile(__DIR__."/../../uptodate_data/js/rules.json"); + } + } + + public function readCustomFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError( + Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" + ); + } + + $outputJson = file_get_contents($file); + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson-> {'custom_rules'})) { + $customRules = $parsedJson-> {'custom_rules'}; + foreach ($customRules as $customRule) { + if (isset($customRule-> {'description'})) { + $myCustom = new MyCustomRule($customRule-> {'description'}); + + if (!isset($customRule-> {'cwe'})) { + $customRule-> {'cwe'} = ""; + } + + if (!isset($customRule-> {'attack'})) { + $customRule-> {'attack'} = ""; + } + + $myCustom->setCwe($customRule-> {'cwe'}); + $myCustom->setAttack($customRule-> {'attack'}); + if (isset($customRule-> {'extra'})) { + $myCustom->setExtra($customRule-> {'extra'}); + } + + if (isset($customRule-> {'sequence'}) && isset($customRule-> {'action'})) { + $myCustom->setType(MyCustomRule::TYPE_SEQUENCE); + $myCustom->setAction($customRule-> {'action'}); + + foreach ($customRule-> {'sequence'} as $seq) { + if (isset($seq-> {'function_name'}) && isset($seq-> {'language'})) { + $myCustomFunction = null; + $myCustomFunction = $myCustom->addToSequence( + $seq-> {'function_name'}, + $seq-> {'language'} + ); + + if (isset($seq-> {'parameters'}) && !is_null($myCustomFunction)) { + $parameters = $seq-> {'parameters'}; + foreach ($parameters as $parameter) { + if (isset($parameter-> {'id'}) && isset($parameter-> {'values'})) { + $validbydefault = false; + if (isset($parameter-> {'valid_by_default'}) + && $parameter-> {'valid_by_default'}) { + $validbydefault = true; + } + + $fixed = false; + if (isset($parameter-> {'fixed'}) + && $parameter-> {'fixed'}) { + $fixed = true; + } + + $sufficient = false; + if (isset($parameter-> {'sufficient'}) + && $parameter-> {'sufficient'}) { + $sufficient = true; + } + + $fail_if_not_verified = true; + if (isset($parameter-> {'fail_if_not_verified'}) + && !$parameter-> {'fail_if_not_verified'}) { + $fail_if_not_verified = false; + } + + $notequals = false; + if (isset($parameter-> {'notequals'}) + && $parameter-> {'notequals'}) { + $notequals = true; + } + + if (is_int($parameter-> {'id'})) { + $myCustomFunction->addParameter( + $parameter-> {'id'}, + $validbydefault, + $fixed, + $sufficient, + $fail_if_not_verified, + $notequals, + $parameter-> {'values'} + ); + } + } + } + + $myCustomFunction->setHasParameters(true); + } + + if (isset($seq-> {'instanceof'})) { + $myCustomFunction->setIsInstance(true); + $myCustomFunction->setInstanceOfName($seq-> {'instanceof'}); + } + } + } + } elseif (isset($customRule-> {'name'}) + && isset($customRule-> {'language'}) + && isset($customRule-> {'action'}) + && isset($customRule-> {'is_function'}) + && $customRule-> {'is_function'} === true) { + $myCustom->setType(MyCustomRule::TYPE_FUNCTION); + $myCustom->setAction($customRule-> {'action'}); + $myCustomFunction = $myCustom->addFunctionDefinition( + $customRule-> {'name'}, + $customRule-> {'language'} + ); + + if (isset($customRule-> {'parameters'})) { + $parameters = $customRule-> {'parameters'}; + foreach ($parameters as $parameter) { + if (isset($parameter-> {'id'}) && isset($parameter-> {'values'})) { + if (is_int($parameter-> {'id'})) { + $fixed = false; + if (isset($parameter-> {'fixed'}) + && $parameter-> {'fixed'}) { + $fixed = true; + } + + $validbydefault = false; + if (isset($parameter-> {'valid_by_default'}) + && $parameter-> {'valid_by_default'}) { + $validbydefault = true; + } + + $sufficient = false; + if (isset($parameter-> {'sufficient'}) + && $parameter-> {'sufficient'}) { + $sufficient = true; + } + + $fail_if_not_verified = true; + if (isset($parameter-> {'fail_if_not_verified'}) + && !$parameter-> {'fail_if_not_verified'}) { + $fail_if_not_verified = false; + } + + $notequals = false; + if (isset($parameter-> {'notequals'}) + && $parameter-> {'notequals'}) { + $notequals = true; + } + + $myCustomFunction->addParameter( + $parameter-> {'id'}, + $validbydefault, + $fixed, + $sufficient, + $fail_if_not_verified, + $notequals, + $parameter-> {'values'} + ); + } + } + } + + $myCustomFunction->setHasParameters(true); + } + + if (isset($customRule-> {'instanceof'})) { + $myCustomFunction->setIsInstance(true); + $myCustomFunction->setInstanceOfName($customRule-> {'instanceof'}); + } + + if (isset($customRule-> {'min_nb_args'})) { + $myCustomFunction->setMinNbArgs($customRule-> {'min_nb_args'}); + } + + if (isset($customRule-> {'max_nb_args'})) { + $myCustomFunction->setMaxNbArgs($customRule-> {'max_nb_args'}); + } + } elseif (isset($customRule-> {'name'}) + && isset($customRule-> {'language'}) + && isset($customRule-> {'action'}) + && (!isset($customRule-> {'is_function'}) + || $customRule-> {'is_function'} !== true)) { + $myCustom->setType(MyCustomRule::TYPE_VARIABLE); + $myCustom->setAction($customRule-> {'action'}); + $myCustomVariable = $myCustom->addVariableDefinition( + $customRule-> {'name'}, + $customRule-> {'language'} + ); + + if (isset($customRule-> {'instanceof'})) { + $myCustomVariable->setIsInstance(true); + $myCustomVariable->setInstanceOfName($customRule-> {'instanceof'}); + } + } + + if (!in_array($myCustom, $this->customRules, true)) { + $this->customRules[] = $myCustom; + } + } + } + } + } + } + + public function readFalsePositives($falsePositives) + { + if (!is_null($falsePositives)) { + if (is_string($falsePositives)) { + $this->readFalsePositivesFile($falsePositives); + } elseif (is_array($falsePositives)) { + foreach ($falsePositives as $falsePositive) { + if (!isset($falsePositive["vuln_id"])) { + Utils::printError(Lang::FORMAT_FALSE_POSITIVES); + } + + $vulnId = $falsePositive["vuln_id"]; + + $myVuln = new MyVuln($vulnId); + + if (!in_array($myVuln, $this->falsePositivesAnalysis, true)) { + $this->falsePositivesAnalysis[] = $myVuln; + } + } + } else { + Utils::printError(Lang::FORMAT_FALSE_POSITIVES); + } + } + } + + public function readFalsePositivesFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError( + Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" + ); + } + + $outputJson = file_get_contents($file); + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson-> {'false_positives'})) { + $falsePositives = $parsedJson-> {'false_positives'}; + foreach ($falsePositives as $falsePositive) { + if (!isset($falsePositive-> {'vuln_id'})) { + Utils::printError(Lang::FORMAT_FALSE_POSITIVES); + } + + $vulnId = $falsePositive-> {'vuln_id'}; + + $myVuln = new MyVuln($vulnId); + if (!in_array($myVuln, $this->falsePositivesAnalysis, true)) { + $this->falsePositivesAnalysis[] = $myVuln; + } + } + } else { + Utils::printError(Lang::FORMAT_FALSE_POSITIVES); + } + } + } + + public function readExcludesFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError( + Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" + ); + } + + $outputJson = file_get_contents($file); + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson->{'exclusions'}) && is_array($parsedJson->{'exclusions'})) { + foreach ($parsedJson->{'exclusions'} as $excludeFile) { + if (is_string($excludeFile) && !in_array($excludeFile, $this->excludesFilesAnalysis, true)) { + $this->excludesFilesAnalysis[] = $excludeFile; + } + } + } + } + } + + public function readIncludesFile($file) + { + if (!is_null($file)) { + if (!file_exists($file)) { + Utils::printError( + Lang::FILE_DOESNT_EXIST." (".Utils::encodeCharacters($file).")" + ); + } + + $outputJson = file_get_contents($file); + $parsedJson = json_decode($outputJson); + + if (isset($parsedJson->{'inclusions'}) && is_array($parsedJson->{'inclusions'})) { + foreach ($parsedJson->{'inclusions'} as $includeFile) { + if (is_string($includeFile) && !in_array($includeFile, $this->includesFilesAnalysis, true)) { + $this->includesFilesAnalysis[] = $includeFile; + } + } + } + } + } +} diff --git a/package/src/progpilot/Inputs/MySource.php b/package/src/progpilot/Inputs/MySource.php index 38ecd5f3..04570831 100644 --- a/package/src/progpilot/Inputs/MySource.php +++ b/package/src/progpilot/Inputs/MySource.php @@ -15,6 +15,8 @@ class MySource extends MySpecify { private $isObject; + private $isArrayOfObjects; + private $isArrayOfArrays; private $isArray; private $isFunction; private $arrayValue; @@ -32,10 +34,12 @@ public function __construct($name, $language) parent::__construct($name, $language); $this->isObject = false; + $this->isArrayOfObjects = false; + $this->isArrayOfArrays = false; $this->isFunction = false; $this->returnArrayValue = null; $this->isReturnArray = false; - $this->arrayValue = null; + $this->arrayValue = []; $this->isArray = false; $this->hasParameters = false; $this->parameters = []; @@ -83,6 +87,26 @@ public function setIsObject($isObject) $this->isObject = $isObject; } + public function getIsArrayOfObjects() + { + return $this->isArrayOfObjects; + } + + public function setIsArrayOfObjects($isArrayOfObjects) + { + $this->isArrayOfObjects = $isArrayOfObjects; + } + + public function getIsArrayOfArrays() + { + return $this->isArrayOfArrays; + } + + public function setIsArrayOfArrays($isArrayOfArrays) + { + $this->isArrayOfArrays = $isArrayOfArrays; + } + public function getIsArray() { return $this->isArray; @@ -93,6 +117,15 @@ public function setIsArray($isArray) $this->isArray = $isArray; } + public function isAnArrayValue($arr) + { + if (in_array($arr, $this->arrayValue, true)) { + return true; + } + + return false; + } + public function getArrayValue() { return $this->arrayValue; diff --git a/package/src/progpilot/Inputs/MyValidator.php b/package/src/progpilot/Inputs/MyValidator.php index d3163c41..0a82766b 100644 --- a/package/src/progpilot/Inputs/MyValidator.php +++ b/package/src/progpilot/Inputs/MyValidator.php @@ -14,13 +14,25 @@ class MyValidator extends MySpecify { private $parameters; private $hasParameters; + private $validWhenReturning; - public function __construct($name, $language) + public function __construct($name, $language, $validWhenReturning) { parent::__construct($name, $language); $this->hasParameters = false; $this->parameters = []; + $this->validWhenReturning = $validWhenReturning; + } + + public function getValidWhenReturning() + { + return $this->validWhenReturning; + } + + public function setValidWhenReturning($validWhenReturning, $validwhenreturning, $notboolean) + { + $this->validWhenReturning = $validWhenReturning; } public function addParameter($parameter, $conditions, $values = null) @@ -51,7 +63,6 @@ public function getParameterValues($i) { foreach ($this->parameters as $parameter) { $index = $parameter[0]; - $conditions = $parameter[1]; $values = $parameter[2]; if ($index === $i) { @@ -66,7 +77,6 @@ public function isParameter($i) { foreach ($this->parameters as $parameter) { $index = $parameter[0]; - $conditions = $parameter[1]; if ($index === $i) { return true; diff --git a/package/src/progpilot/Lang.php b/package/src/progpilot/Lang.php index b4520c66..bd5c2e2e 100644 --- a/package/src/progpilot/Lang.php +++ b/package/src/progpilot/Lang.php @@ -70,19 +70,27 @@ class Lang Lang::GLOBAL_ERROR. Lang::GLOBAL_CHECK_CONFIG. "Unable to parse the YAML file configuration"; + + const UNABLE_TO_CREATE_WORKSPACE = + Lang::GLOBAL_ERROR. + "Unable to create the workspace directory, do you have the correct permissions on filesystem?"; const COMMAND_LINE_ARG = "Usage of progpilot : php progpilot.phar [--configuration path_to_config_file.yml] ". "[files and folders (file1.php file2.php ...)]"; + const PARSER_ERROR = "Exception raised during parsing.\nMessage: "; const PROGPILOT_ARG_DESC = "Progpilot a static analysis tool for security purposes"; const FILES_ARG_DESC = "Files to be analyzed"; const CONFIG_ARG_DESC = "Read configuration from config file"; const MAX_TIME_EXCEEDED = "Max time execution exceeded (you can increase the value with ". - "\$context->setLimitTime())"; + "\$context->setMaxFileAnalysisDuration())"; const MAX_DEFS_EXCEEDED = "Max definitions exceeded (you can increase the value with ". - "\$context->setLimitDefs())"; + "\$context->setMaxDefinitions())"; const MAX_SIZE_EXCEEDED = "Max size of file exceeded (you can increase the value with ". - "\$context->setLimitSize())"; + "\$context->setMaxFileSize())"; + const MAX_MEMORY_EXCEEDED = "Memory threshold reached, some references will be released ". + "(increasing execution time)"; + const CANNOT_SET_MEMORY = "Cannot set php memory_limit to "; const V8JS_NOTLOADED = "extension v8js must be loaded to analyze js files"; } diff --git a/package/src/progpilot/Objects/MyAssertion.php b/package/src/progpilot/Objects/MyAssertion.php index bc151f9f..ba4e42cb 100644 --- a/package/src/progpilot/Objects/MyAssertion.php +++ b/package/src/progpilot/Objects/MyAssertion.php @@ -12,10 +12,8 @@ class MyAssertion { - private $arr; private $typeAssertion; - - private $myDef; + private $mydef; public function __construct($myDef, $typeAssertion) { diff --git a/package/src/progpilot/Objects/MyBlock.php b/package/src/progpilot/Objects/MyBlock.php index 5fa785b9..ec0f898b 100644 --- a/package/src/progpilot/Objects/MyBlock.php +++ b/package/src/progpilot/Objects/MyBlock.php @@ -12,77 +12,148 @@ class MyBlock extends MyOp { + private $returnDefs; private $startAddressBlock; private $endAddressBlock; - private $id; - private $loop; + private $needUpdateOfState; + private $hasBeenAnalyzed; + private $loopParents; + public $children; public $parents; + public $virtualParents; public $assertions; - public function __construct() + public function __construct($startLine, $startColumn) { - parent::__construct("", 0, 0); + parent::__construct("", $startLine, $startColumn); + $this->returnDefs = []; $this->startAddressBlock = -1; $this->endAddressBlock = -1; $this->assertions = []; $this->parents = []; - $this->loop = false; + $this->loopParents = []; + $this->virtualParents = []; + $this->children = []; + $this->needUpdateOfState = true; + $this->hasBeenAnalyzed = false; + } + + public function hasBeenAnalyzed() + { + return $this->hasBeenAnalyzed; + } + + public function setHasBeenAnalyzed($hasBeenAnalyzed) + { + $this->hasBeenAnalyzed = $hasBeenAnalyzed; + } + + public function setNeedUpdateOfState($update) + { + $this->needUpdateOfState = $update; + } + + public function doNeedUpdateOfState() + { + return $this->needUpdateOfState; + } + + public function addLoopParent($parent) + { + if (!in_array($parent, $this->loopParents, true)) { + $this->loopParents[] = $parent; + } + } + + public function isLoopParent($parent) + { + return in_array($parent, $this->loopParents, true); + } + + public function addVirtualParent($parent) + { + if (!in_array($parent, $this->virtualParents, true)) { + $this->virtualParents[] = $parent; + } + } + + public function setVirtualParents($parents) + { + $this->virtualParents = $parents; + } + + public function getVirtualParents() + { + return $this->virtualParents; } public function addParent($parent) { - $this->parents[] = $parent; + if (!in_array($parent, $this->parents, true)) { + $this->parents[] = $parent; + } } - public function addAssertion($myassertion) + public function getParents() { - $this->assertions[] = $myassertion; + return $this->parents; } - public function getAssertions() + public function addChild($child) { - return $this->assertions; + if (!in_array($child, $this->children, true)) { + $this->children[] = $child; + } } - public function setStartAddressBlock($address) + public function getChildren() { - $this->startAddressBlock = $address; + return $this->children; } - public function setEndAddressBlock($address) + public function addReturnDef($def) { - $this->endAddressBlock = $address; + if (!in_array($def, $this->returnDefs, true)) { + $this->returnDefs[] = $def; + } } - public function getStartAddressBlock() + public function getReturnDefs() { - return $this->startAddressBlock; + return $this->returnDefs; } - public function getEndAddressBlock() + public function addAssertion($myassertion) { - return $this->endAddressBlock; + if (!in_array($myassertion, $this->assertions, true)) { + $this->assertions[] = $myassertion; + } } - public function getId() + public function getAssertions() { - return $this->id; + return $this->assertions; } - public function setId($id) + public function setStartAddressBlock($address) { - $this->id = $id; + $this->startAddressBlock = $address; } - public function setIsLoop($loop) + public function setEndAddressBlock($address) { - $this->loop = $loop; + $this->endAddressBlock = $address; } - public function getIsLoop() + public function getStartAddressBlock() { - return $this->loop; + return $this->startAddressBlock; + } + + public function getEndAddressBlock() + { + return $this->endAddressBlock; } } diff --git a/package/src/progpilot/Objects/MyClass.php b/package/src/progpilot/Objects/MyClass.php index bc31b3be..ca658472 100644 --- a/package/src/progpilot/Objects/MyClass.php +++ b/package/src/progpilot/Objects/MyClass.php @@ -12,9 +12,8 @@ class MyClass extends MyOp { - private $properties; private $methods; - private $objectIdThis; + private $properties; private $extendsof; public function __construct($varLine, $varColumn, $varName) @@ -23,7 +22,6 @@ public function __construct($varLine, $varColumn, $varName) $this->properties = []; $this->methods = []; - $this->objectIdThis = null; $this->extendsof = null; } @@ -77,8 +75,8 @@ public function getProperties() public function addProperty($property) { $exist = false; - foreach ($this->properties as $propertyClass) { - if ($propertyClass->property->getProperties() === $property->property->getProperties()) { + foreach ($this->properties as $defClass) { + if ($defClass->getName() === $property->getName()) { $exist = true; break; } @@ -89,28 +87,36 @@ public function addProperty($property) } } - public function getProperty($name) + public function getProperty($context, $blockid, $instance, $name) { - foreach ($this->properties as $property) { + foreach ($this->properties as $def) { // we don't check if it's STATIC property or NOT - if ($property->property->getProperties()[0] === $name) { - return $property; + if ($def->getName() === $name) { + return $def; } } - return null; - } - - public function setObjectIdThis($objectId) - { - $this->objectIdThis = $objectId; - } + // we didn't find any propery but in this case php create automatically the property + // same code than in selectproperties resolvedef + $myProperty = new MyProperty( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $instance->getLine(), + $instance->getColumn(), + $name + ); + $myProperty->setVisibility("public"); + $this->addProperty($myProperty); + + $state = $instance->getState($blockid); + if (!is_null($state) && $state->isType(MyDefinition::ALL_PROPERTIES_TAINTED)) { + $myProperty->getCurrentState()->setTainted(true); + $myProperty->getCurrentState()->addTaintedByDef([$instance, $state]); + } - public function getObjectIdThis() - { - return $this->objectIdThis; + return $myProperty; } - + public function getExtendsOf() { return $this->extendsof; diff --git a/package/src/progpilot/Objects/MyDefOriginal.php b/package/src/progpilot/Objects/MyDefOriginal.php new file mode 100644 index 00000000..06959b3e --- /dev/null +++ b/package/src/progpilot/Objects/MyDefOriginal.php @@ -0,0 +1,43 @@ +def = null; + $this->arrayIndexAccessor = null; + } + + public function getDef() + { + return $this->def; + } + + public function setDef($myDef) + { + $this->def = $myDef; + } + + public function getArrayIndexAccessor() + { + return $this->arrayIndexAccessor; + } + + public function setArrayIndexAccessor($arrayIndexAccessor) + { + $this->arrayIndexAccessor = $arrayIndexAccessor; + } +} diff --git a/package/src/progpilot/Objects/MyDefState.php b/package/src/progpilot/Objects/MyDefState.php new file mode 100644 index 00000000..1f128f1f --- /dev/null +++ b/package/src/progpilot/Objects/MyDefState.php @@ -0,0 +1,320 @@ +isEmbeddedByChar = []; + $this->isTainted = false; + $this->taintedByDefs = []; + $this->label = MyDefinition::SECURITY_LOW; + $this->isSanitized = false; + $this->typeSanitized = []; + $this->lastKnownValue = []; + $this->cast = MyDefinition::CAST_NOT_SAFE; + $this->objectId = -1; + $this->arrayIndexes = []; + } + + public function printStdout() + { + echo "cast = ".$this->getCast()."\n"; + + if ($this->isTainted()) { + echo "tainted = 1\n"; + + foreach ($this->taintedByDefs as $taintedByDef) { + echo "taintedByDef = '".$taintedByDef[0]->getId()."'\n"; + } + } + + echo "last_known_value :\n"; + var_dump($this->lastKnownValue); + echo "is_embeddedbychar :\n"; + var_dump($this->isEmbeddedByChar); + echo "isSanitized :\n"; + var_dump($this->isSanitized); + echo "type_sanitized :\n"; + var_dump($this->typeSanitized); + echo "objectid :\n"; + var_dump($this->objectId); + echo "istypeinstance : '".$this->isType(MyDefinition::TYPE_INSTANCE)."'\n"; + echo "allpropertiestainted : '".$this->isType(MyDefinition::ALL_PROPERTIES_TAINTED)."'\n"; + echo "allarrayelementstainted : '".$this->isType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED)."'\n"; + echo "istypearray : '".$this->isType(MyDefinition::TYPE_ARRAY)."'\n"; + echo "istypearrayarray : '".$this->isType(MyDefinition::TYPE_ARRAY_ARRAY)."'\n"; + + foreach ($this->arrayIndexes as $arrayIndex) { + echo "index :\n"; + var_dump($arrayIndex->index); + echo "def :\n"; + $arrayIndex->def->printStdout(); + } + } + + public function addTaintedByDef($taintedByDef) + { + if (!in_array($taintedByDef, $this->taintedByDefs, true)) { + $this->taintedByDefs[] = $taintedByDef; + } + } + + public function getTaintedByDefs() + { + return $this->taintedByDefs; + } + + public function setTaintedByDefs($defs) + { + $this->taintedByDefs = $defs; + } + + public function updateIsEmbeddedByChars($chars) + { + foreach ($chars as $char => $value) { + $curChar = isset($this->isEmbeddedByChar[$char]) ? $this->isEmbeddedByChar[$char] : 0; + $this->isEmbeddedByChar[$char] = ($value + $curChar) % 2; + } + } + + public function setIsEmbeddedByChars($chars) + { + foreach ($chars as $char => $value) { + $this->isEmbeddedByChar[$char] = $value % 2; + } + } + + public function getIsEmbeddedByChars() + { + return $this->isEmbeddedByChar; + } + + public function setEmbeddedByChar($value) + { + $this->isEmbeddedByChar = $value; + } + + public function setIsEmbeddedByChar($char, $bool) + { + $this->isEmbeddedByChar[$char] = $bool; + } + + public function getIsEmbeddedByChar($char) + { + if (isset($this->isEmbeddedByChar[$char])) { + return $this->isEmbeddedByChar[$char]; + } + + return false; + } + + public function setLabel($label) + { + $this->label = $label; + } + + public function getLabel() + { + return $this->label; + } + + public function setCast($cast) + { + $this->cast = $cast; + } + + public function getCast() + { + return $this->cast; + } + + public function addLastKnownValue($value) + { + $value = rtrim(ltrim($value)); + + if (Common::validLastKnownValue($value) + && !in_array($value, $this->lastKnownValue, true) + && count($this->lastKnownValue) < 10) { + $this->lastKnownValue[] = $value; + } + } + + public function getLastKnownValues() + { + return $this->lastKnownValue; + } + + public function setSanitized($isSanitized) + { + $this->isSanitized = $isSanitized; + } + + public function isSanitized() + { + return $this->isSanitized; + } + + public function setTypeSanitized($typeSanitized) + { + $this->typeSanitized = $typeSanitized; + } + + public function getTypeSanitized() + { + return $this->typeSanitized; + } + + public function addTypeSanitized($typeSanitized) + { + if (!in_array($typeSanitized, $this->typeSanitized, true)) { + $this->typeSanitized[] = $typeSanitized; + } + } + + public function isTypeSanitized($typeSanitized) + { + if (in_array($typeSanitized, $this->typeSanitized, true)) { + return true; + } + + return false; + } + + public function isTainted() + { + return $this->isTainted; + } + + public function setTainted($tainted) + { + $this->isTainted = $tainted; + } + + public function createDefArrayIndex($blockId, $myArrayDef, $arrayIndex) + { + $myDef = new MyDefinition( + $blockId, + $myArrayDef->getSourceMyFile(), + $myArrayDef->getLine(), + $myArrayDef->getColumn(), + "built-in-index-array" + ); + + $myDef->addType(MyDefinition::TYPE_ARRAY_ELEMENT); + $this->addArrayIndex($arrayIndex, $myDef); + $this->addType(MyDefinition::TYPE_ARRAY); + $ret[] = $myDef; + + $existingState = $myArrayDef->getState($blockId); + + if (!is_null($existingState) && $existingState->isType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED)) { + $myDef->getCurrentState()->setTainted(true); + $myDef->getCurrentState()->addTaintedByDef([$myArrayDef, $myArrayDef->getCurrentState()]); + } elseif ($myArrayDef->getCurrentState()->isType(MyDefinition::ALL_ARRAY_ELEMENTS_TAINTED)) { + // data/source17.php + $myDef->getCurrentState()->setTainted(true); + $myDef->getCurrentState()->addTaintedByDef([$myArrayDef, $myArrayDef->getCurrentState()]); + } + + return [true, $ret]; + } + + public function getOrCreateDefArrayIndex($blockId, $myArrayDef, $arrayIndex) + { + $ret = []; + $found = false; + for ($i = 0; $i < count($this->arrayIndexes); $i ++) { + if ($this->arrayIndexes[$i]->index === $arrayIndex) { + $found = true; + $ret[] = $this->arrayIndexes[$i]->def; + } + } + + if ($found) { + return [false, $ret]; + } + + return $this->createDefArrayIndex($blockId, $myArrayDef, $arrayIndex); + } + + public function isArrayIndexExists($arrayIndex) + { + for ($i = 0; $i < count($this->arrayIndexes); $i ++) { + if ($this->arrayIndexes[$i]->index === $arrayIndex) { + return true; + } + } + + return false; + } + + public function addArrayIndex($arrayIndex, $def) + { + if (!$this->isArrayIndexExists($arrayIndex) + && count($this->arrayIndexes) < 100) { + $ele = new \stdClass; + $ele->index = $arrayIndex; + $ele->def = $def; + $this->arrayIndexes[] = $ele; + + return true; + } + + return false; + } + + public function overwriteArrayIndex($arrayIndex, $def) + { + for ($i = 0; $i < count($this->arrayIndexes); $i ++) { + if ($this->arrayIndexes[$i]->index === $arrayIndex) { + $this->arrayIndexes[$i]->def = $def; + break; + } + } + } + + public function setArrayIndexes($arrayIndexes) + { + $this->arrayIndexes = $arrayIndexes; + } + + public function getArrayIndexes() + { + return $this->arrayIndexes; + } + + public function getObjectId() + { + return $this->objectId; + } + + public function setObjectId($objectId) + { + $this->objectId = $objectId; + } +} diff --git a/package/src/progpilot/Objects/MyDefinition.php b/package/src/progpilot/Objects/MyDefinition.php index bc39f125..66f6a4e5 100644 --- a/package/src/progpilot/Objects/MyDefinition.php +++ b/package/src/progpilot/Objects/MyDefinition.php @@ -15,378 +15,292 @@ use progpilot\Utils; use progpilot\Transformations\Php\Common; +use progpilot\Helpers\State as HelpersState; + class MyDefinition extends MyOp { - const CAST_SAFE = "cast_int"; - const CAST_NOT_SAFE = "cast_string"; + const CAST_SAFE = 1; + const CAST_NOT_SAFE = 2; const TYPE_PROPERTY = 0x0001; const TYPE_ARRAY = 0x0002; const TYPE_CONSTANTE = 0x0004; const TYPE_REFERENCE = 0x0008; const TYPE_ARRAY_REFERENCE = 0x0010; - const TYPE_COPY_ARRAY = 0x0020; + const TYPE_ARRAY_ELEMENT = 0x0020; const TYPE_INSTANCE = 0x0040; const TYPE_GLOBAL = 0x0080; const TYPE_STATIC_PROPERTY = 0x0100; + const ALL_PROPERTIES_TAINTED = 0x0200; + const ALL_ARRAY_ELEMENTS_TAINTED = 0x0400; + const TYPE_LITERAL = 0x0800; + const TYPE_ITERATOR = 0x1000; + const TYPE_ARRAY_ARRAY = 0x2000; const SECURITY_HIGH = 1; const SECURITY_LOW = 2; - private $isCopyArray; - private $objectId; private $blockId; - private $isTainted; - private $isConst; - private $isRef; - private $refName; - private $isRefArr; - private $refArrValue; - private $theArrays; - private $theExpr; - private $theExprs; - private $taintedByExpr; - private $instance; private $className; - private $isSanitized; - private $typeSanitized; - private $valueFromDef; - private $cast; - private $isProperty; - private $isInstance; - private $isEmbeddedByChar; - private $label; + private $returnedFromValidator; + private $validWhenReturning; + private $validNotBoolean; + private $paramToArg; + private $argToParam; + private $isReturnDef; + private $refs; + private $iteratorValues; - public $property; + public $original; + public $states; - public function __construct($varLine, $varColumn, $varName) + public function __construct($blockId, $myFile, $varLine, $varColumn, $varName) { parent::__construct($varName, $varLine, $varColumn); - $this->isEmbeddedByChar = []; - - $this->isCopyArray = false; - $this->valueFromDef = null; - - $this->objectId = -1; - $this->blockId = -1; - $this->isTainted = false; - $this->isConst = false; - $this->isRef = false; - $this->isRefArr = false; - $this->refArrValue = null; - $this->instance = false; - $this->theArrays = []; - $this->theExpr = null; - $this->taintedByExpr = null; + $this->setSourceMyFile($myFile); + $this->blockId = $blockId; $this->className = ""; - $this->label = MyDefinition::SECURITY_LOW; - - $this->isSanitized = false; - $this->typeSanitized = []; - - $this->lastKnownValue = []; - - $this->property = new MyProperty; - $this->cast = MyDefinition::CAST_NOT_SAFE; + $this->returnedFromValidator = false; + $this->validWhenReturning = false; + $this->validNotBoolean = false; + $this->paramToArg = null; + $this->argToParam = null; + $this->isReturnDef = null; + $this->refs = []; + $this->iteratorValues = []; - $this->isProperty = false; - $this->isInstance = false; - } + $this->original = new MyDefOriginal; + $this->states = []; + $this->blocksIdsStates = []; - public function __clone() - { - $this->property = clone $this->property; + // original state + $state = $this->createState(); + $this->assignStateToBlockId($state->getId(), $blockId); } public function printStdout($context = null) { + echo "_____________________ start def _____________________\n"; echo "def id ".$this->varId." :: \ name = ".htmlentities($this->getName(), ENT_QUOTES, 'UTF-8')." :: \ line = ".$this->getLine()." :: column = ".$this->getColumn()." :: \ - tainted = ".$this->isTainted()." :: \ - label = ".$this->getLabel()." :: \ ref = ".$this->isType(MyDefinition::TYPE_REFERENCE)." :: \ is_property = ".$this->isType(MyDefinition::TYPE_PROPERTY)." :: \ is_static_property = ".$this->isType(MyDefinition::TYPE_STATIC_PROPERTY)." :: \ - isInstance = ".$this->isType(MyDefinition::TYPE_INSTANCE)." :: \ + is_type_array_element = ".$this->isType(MyDefinition::TYPE_ARRAY_ELEMENT)." :: \ + isArray = ".$this->isType(MyDefinition::TYPE_ARRAY)." :: \ is_const = ".$this->isType(MyDefinition::TYPE_CONSTANTE)." :: \ - blockid = ".$this->getBlockId()." :: \ - cast = ".$this->getCast()."\n"; - - echo "last_known_value :\n"; - var_dump($this->lastKnownValue); - - echo "is_embeddedbychar :\n"; - var_dump($this->isEmbeddedByChar); - echo "type_sanitized :\n"; - var_dump($this->typeSanitized); - - if ($this->isType(MyDefinition::TYPE_ARRAY)) { - echo "array index value :\n"; - var_dump($this->getArrayValue()); - } - - if ($this->getArrayValue() === "PROGPILOT_ALL_INDEX_TAINTED") { - echo "array index value : PROGPILOT_ALL_INDEX_TAINTED\n"; - } - - if ($this->property->hasProperty("PROGPILOT_ALL_PROPERTIES_TAINTED")) { - echo "property value : PROGPILOT_ALL_PROPERTIES_TAINTED\n"; + is_iterator = ".$this->isType(MyDefinition::TYPE_ITERATOR)." :: \ + is_return_def = ".$this->isReturnDef." :: \ + blockid = ".$this->getBlockId()."\n"; + + if (!is_null($this->getParamToArg())) { + echo "it's a param (to arg possibility) start ===\n"; + $this->getParamToArg()->printStdout(); + echo "it's a param (to arg possibility) end ===\n"; } - if ($this->isType(MyDefinition::TYPE_PROPERTY) || $this->isType(MyDefinition::TYPE_STATIC_PROPERTY)) { - echo "property : ".Utils::printProperties("php", $this->property->getProperties())."\n"; - echo "class_name : ".htmlentities($this->getClassName(), ENT_QUOTES, 'UTF-8')."\n"; - echo "visibility : ".htmlentities($this->property->getVisibility(), ENT_QUOTES, 'UTF-8')."\n"; - } + $this->getSourceMyFile()->printStdout(); - if ($this->isType(MyDefinition::TYPE_INSTANCE)) { - echo "instance : ".htmlentities($this->getClassName(), ENT_QUOTES, 'UTF-8')."\n"; - echo "object id : ".$this->getObjectId()."\n"; - - if (!is_null($context)) { - $tmpMyClass = $context->getObjects()->getMyClassFromObject($this->getObjectId()); - if (!is_null($tmpMyClass)) { - echo "class of object : ".htmlentities($tmpMyClass->getName(), ENT_QUOTES, 'UTF-8')."\n"; - } - } + foreach ($this->states as $id => $state) { + echo "state id '$id'\n"; + echo "__________________ start state ________________________\n"; + $state->printStdout(); + echo "__________________ end state________________________\n"; } - if ($this->isType(MyDefinition::TYPE_COPY_ARRAY)) { - echo "copyarray start ================= count = ".count($this->getCopyArrays())."\n"; - foreach ($this->getCopyArrays() as $copyArray) { - var_dump($copyArray[0]); - } - echo "copyarray end =================\n"; - } - echo "__________________________________________\n\n\n"; + echo "_____________________ states blocks associations _____________________\n\n\n"; + var_dump($this->blocksIdsStates); + echo "_____________________ end def _____________________\n\n\n"; } - public function setIsEmbeddedByChars($chars, $control) + public function unsetState($blockId) { - foreach ($chars as $char => $value) { - if (!isset($this->isEmbeddedByChar[$char])) { - $this->isEmbeddedByChar[$char] = $value; - } else { - if (!$value && !$control) { - $this->isEmbeddedByChar[$char] = false; - } elseif ($value) { - $this->isEmbeddedByChar[$char] = true; - } - } - } - } - - public function getIsEmbeddedByChars() - { - return $this->isEmbeddedByChar; - } - - public function setIsEmbeddedByChar($char, $bool) - { - $this->isEmbeddedByChar[$char] = $bool; - } - - public function getIsEmbeddedByChar($char) - { - if (isset($this->isEmbeddedByChar[$char])) { - return $this->isEmbeddedByChar[$char]; - } - - return false; + unset($this->blocksIdsStates[$blockId]); } - public function setLabel($label) + public function setStatesToBlocksIds($statesToBlocksIds) { - $this->label = $label; + $this->blocksIdsStates = $statesToBlocksIds; } - public function getLabel() + public function getStatesToBlocksIds() { - return $this->label; + return $this->blocksIdsStates; } - public function setCast($cast) + public function setStates($states) { - $this->cast = $cast; + $this->states = $states; } - public function getCast() + public function assignStateToBlockId($stateId, $blockId) { - return $this->cast; - } + if (isset($this->blocksIdsStates[$blockId]) + && $blockId !== $this->getBlockId()) { + $toRemove = true; + $overWrittentStateId = $this->blocksIdsStates[$blockId]; + foreach ($this->blocksIdsStates as $keyBlockId => $valueStateId) { + if ($valueStateId === $overWrittentStateId + && $keyBlockId !== $blockId) { + $toRemove = false; + break; + } + } - public function setValueFromDef($def) - { - $this->valueFromDef = $def; - } + if ($toRemove) { + unset($this->states[$overWrittentStateId]); + } + } - public function getValueFromDef() - { - return $this->valueFromDef; + $this->blocksIdsStates[$blockId] = $stateId; } - public function resetLastKnownValues() + public function createState() { - $this->lastKnownValue = []; - } + $state = new MyDefState; + $this->states[$state->getId()] = $state; - public function setLastKnownValues($values) - { - $this->lastKnownValue = $values; + return $state; } - public function setLastKnownValue($id, $value) + public function addState($newstate) { - $this->lastKnownValue[$id] = $value; + $this->states[$newstate->getId()] = $newstate; } - public function addLastKnownValue($value) + public function getCurrentState() { - $value = rtrim(ltrim($value)); - - if (Common::validLastKnownValue($value) && !in_array($value, $this->lastKnownValue, true)) { - $this->lastKnownValue[] = $value; + if (isset($this->blocksIdsStates[$this->blockId])) { + $stateId = $this->blocksIdsStates[$this->blockId]; + if (isset($this->states[$stateId])) { + return $this->states[$stateId]; + } } - } - public function getLastKnownValues() - { - return $this->lastKnownValue; + return null; } - public function getClassName() + public function getNbStates() { - return $this->className; + return count($this->states); } - public function setClassName($className) - { - $this->className = $className; - } - - public function getRefName() + public function getStates() { - return $this->refName; + return $this->states; } - public function setRefName($refname) + public function getState($blockId) { - $this->refName = $refname; - } + if (isset($this->blocksIdsStates[$blockId])) { + $stateId = $this->blocksIdsStates[$blockId]; + if (isset($this->states[$stateId])) { + return $this->states[$stateId]; + } + } - public function isTainted() - { - return $this->isTainted; + return null; } - - public function setTainted($tainted) + + public function setArgToParam($def) { - $this->isTainted = $tainted; + $this->argToParam = $def; } - public function setTaintedByExpr($expr) + public function getArgToParam() { - $this->taintedByExpr = $expr; + return $this->argToParam; } - public function getTaintedByExpr() + public function setParamToArg($def) { - return $this->taintedByExpr; + $this->paramToArg = $def; } - public function getRefArrValue() + public function getParamToArg() { - return $this->refArrValue; + return $this->paramToArg; } - public function setRefArrValue($arr) + public function setValidWhenReturning($value) { - $this->refArrValue = $arr; + $this->validWhenReturning = $value; } - public function getObjectId() + public function getValidWhenReturning() { - return $this->objectId; + return $this->validWhenReturning; } - public function setObjectId($objectId) + public function setValidNotBoolean($value) { - $this->objectId = $objectId; + $this->validNotBoolean = $value; } - public function getBlockId() + public function getValidNotBoolean() { - return $this->blockId; + return $this->validNotBoolean; } - public function setBlockId($blockId) + public function setReturnedFromValidator($value) { - $this->blockId = $blockId; + $this->returnedFromValidator = $value; } - public function addCopyArray($arr, $def) + public function getReturnedFromValidator() { - $val = [$arr, $def]; - if (!in_array($val, $this->theArrays, true)) { - $this->theArrays[] = $val; - } + return $this->returnedFromValidator; } - public function setCopyArrays($theArrays) + public function getClassName() { - $this->theArrays = $theArrays; + return $this->className; } - public function getCopyArrays() + public function setClassName($className) { - return $this->theArrays; + $this->className = $className; } - public function setExpr($myExpr) + public function getRefs() { - $this->theExpr = $myExpr; + return $this->refs; } - public function getExpr() + public function setRefs($refs) { - return $this->theExpr; + $this->refs = $refs; } - public function setSanitized($isSanitized) + public function getIteratorValues() { - $this->isSanitized = $isSanitized; + return $this->iteratorValues; } - public function isSanitized() + public function setIteratorValues($iteratorValues) { - return $this->isSanitized; + $this->iteratorValues = $iteratorValues; } - public function setTypeSanitized($typeSanitized) + public function getBlockId() { - $this->typeSanitized = $typeSanitized; + return $this->blockId; } - public function getTypeSanitized() + public function setBlockId($blockId) { - return $this->typeSanitized; + $this->blockId = $blockId; } - public function addTypeSanitized($typeSanitized) + public function isReturnDef() { - if (!in_array($typeSanitized, $this->typeSanitized, true)) { - $this->typeSanitized[] = $typeSanitized; - } + return $this->returnDef; } - public function isTypeSanitized($typeSanitized) + public function setReturnDef($returnDef) { - if (in_array($typeSanitized, $this->typeSanitized, true)) { - return true; - } - - return false; + $this->returnDef = $returnDef; } } diff --git a/package/src/progpilot/Objects/MyExpr.php b/package/src/progpilot/Objects/MyExpr.php deleted file mode 100644 index 7574c27a..00000000 --- a/package/src/progpilot/Objects/MyExpr.php +++ /dev/null @@ -1,122 +0,0 @@ -nbChars = []; - $this->isConcat = false; - $this->tainted = false; - $this->assign = false; - $this->assignIterator = false; - $this->assignDef = null; - $this->theDefs = []; - } - - public function setNbChars($char, $nbChars) - { - $this->nbChars[$char] = $nbChars; - } - - public function getNbChars($char) - { - if (isset($this->nbChars[$char])) { - return $this->nbChars[$char]; - } - - return 0; - } - - public function setIsConcat($concat) - { - $this->isConcat = $concat; - } - - public function getIsConcat() - { - return $this->isConcat; - } - - public function setTainted($tainted) - { - $this->tainted = $tainted; - } - - public function isTainted() - { - foreach ($this->theDefs as $theDef) { - if ($theDef->isTainted()) { - return true; - } - } - return false; - } - - /* assignement utilisant cette expression */ - public function setAssignDef($def) - { - $this->assignDef = $def; - } - - public function getAssignDef() - { - return $this->assignDef; - } - - public function setAssignIterator($assignIterator) - { - $this->assignIterator = $assignIterator; - } - - public function setAssign($assign) - { - $this->assign = $assign; - } - - public function isAssign() - { - return $this->assign; - } - - public function isAssignIterator() - { - return $this->assignIterator; - } - - public function setDefs($defs) - { - $this->theDefs = $defs; - } - - public function addDef($myDef) - { - if (!in_array($myDef, $this->theDefs, true)) { - $this->theDefs[] = $myDef; - } - } - - public function getDefs() - { - return $this->theDefs; - } -} diff --git a/package/src/progpilot/Objects/MyFile.php b/package/src/progpilot/Objects/MyFile.php index a29b53f5..3ab9cc82 100644 --- a/package/src/progpilot/Objects/MyFile.php +++ b/package/src/progpilot/Objects/MyFile.php @@ -13,10 +13,16 @@ class MyFile extends MyOp { private $includedFromMyFile; + private $includedToMyFile; - public function __construct($varName, $varLine, $varColumn) + public function setIncludedToMyfile($myFileTo) { - parent::__construct($varName, $varLine, $varColumn); + $this->includedToMyFile = $myFileTo; + } + + public function getIncludedToMyfile() + { + return $this->includedToMyFile; } public function setIncludedFromMyfile($myFileFrom) @@ -28,4 +34,19 @@ public function getIncludedFromMyfile() { return $this->includedFromMyFile; } + + public function printStdout($context = null) + { + echo "file ".$this->getName()."\n"; + $includeFile = $this->getIncludedFromMyfile(); + while (!is_null($includeFile)) { + echo "included from ".$includeFile->getName()."\n"; + $includeFile = $includeFile->getIncludedFromMyfile(); + } + $includeFile = $this->getIncludedToMyfile(); + while (!is_null($includeFile)) { + echo "included to ".$includeFile->getName()."\n"; + $includeFile = $includeFile->getIncludedToMyfile(); + } + } } diff --git a/package/src/progpilot/Objects/MyFunction.php b/package/src/progpilot/Objects/MyFunction.php index 3746cad3..7a80da2a 100644 --- a/package/src/progpilot/Objects/MyFunction.php +++ b/package/src/progpilot/Objects/MyFunction.php @@ -21,86 +21,141 @@ class MyFunction extends MyOp const TYPE_FUNC_STATIC = 0x0002; const TYPE_FUNC_METHOD = 0x0004; + private $lastLine; + private $lastColumn; + private $firstBlockId; + private $lastBlockIds; + private $nbParams; private $params; private $returnDefs; + private $initialReturnDefs; private $defs; + private $opInformations; private $blocks; private $visibility; private $myClass; - private $instance; private $blockId; private $nameInstance; private $thisDef; - private $backDef; - - private $lastLine; - private $lastColumn; - private $lastBlockId; + private $instanceClassName; - private $isAnalyzed; - private $isDataAnalyzed; + private $thisHasBeenUpdated; + private $isVisited; + private $isVisitedFromInclude; private $myCode; - private $castReturn; - public $property; - public function __construct($name) { parent::__construct($name, 0, 0); + $this->args = []; $this->params = []; + $this->initialReturnDefs = []; $this->returnDefs = []; $this->visibility = "public"; - $this->myclass = null; + $this->myClass = null; $this->nameInstance = null; $this->thisDef = null; - $this->backDef = null; + $this->instanceClassName = ""; $this->blockId = 0; + $this->firstBlockId = 0; $this->nbParams = 0; $this->lastLine = 0; $this->lastColumn = 0; - $this->lastBlockId = 0; + $this->lastBlockIds = []; + $this->lastExecutionTime = 0; + $this->startExecutionTime = 0; + $this->nbExecutions = 0; - $this->isAnalyzed = false; - $this->isDataAnalyzed = false; + $this->thisHasBeenUpdated = false; + $this->isVisited = false; + $this->isVisitedFromInclude = false; + $this->hasGlobalVariables = false; - $this->property = new MyProperty; $this->defs = new Definitions; - $this->blocks = new \SplObjectStorage; + $this->opInformations = []; + $this->blocks = []; $this->myCode = new \progpilot\Code\MyCode; - $this->castReturn = MyDefinition::CAST_NOT_SAFE; } - public function __clone() + public function reset() + { + $this->opInformations = []; + } + + public function setNbExecutions($nbExecutions) + { + $this->nbExecutions = $nbExecutions; + } + + public function getNbExecutions() + { + return $this->nbExecutions; + } + + public function setStartExecutionTime($startExecutionTime) + { + $this->startExecutionTime = $startExecutionTime; + } + + public function getStartExecutionTime() { - $this->property = clone $this->property; - $this->blocks = clone $this->blocks; - $this->defs = clone $this->defs; + return $this->startExecutionTime; } - public function setIsDataAnalyzed($isDataAnalyzed) + public function setLastExecutionTime($lastExecutionTime) { - $this->isDataAnalyzed = $isDataAnalyzed; + $this->lastExecutionTime = $lastExecutionTime; } - public function isDataAnalyzed() + public function getLastExecutionTime() { - return $this->isDataAnalyzed; + return $this->lastExecutionTime; } - public function setIsAnalyzed($isAnalyzed) + public function setThisHasBeenUpdated($thisHasBeenUpdated) { - $this->isAnalyzed = $isAnalyzed; + $this->thisHasBeenUpdated = $thisHasBeenUpdated; } - public function isAnalyzed() + public function thisHasBeenUpdated() { - return $this->isAnalyzed; + return $this->thisHasBeenUpdated; + } + + public function setHasGlobalVariables($hasGlobalVariables) + { + $this->hasGlobalVariables = $hasGlobalVariables; + } + + public function hasGlobalVariables() + { + return $this->hasGlobalVariables; + } + + public function setIsVisitedFromInclude($isVisited) + { + $this->isVisitedFromInclude = $isVisited; + } + + public function isVisitedFromInclude() + { + return $this->isVisitedFromInclude; + } + + public function setIsVisited($isVisited) + { + $this->isVisited = $isVisited; + } + + public function isVisited() + { + return $this->isVisited; } public function setMyCode($myCode) @@ -123,9 +178,21 @@ public function setLastColumn($lastColumn) $this->lastColumn = $lastColumn; } - public function setLastBlockId($lastBlockId) + public function setLastBlockIds($lastBlockIds) { - $this->lastBlockId = $lastBlockId; + $this->lastBlockIds = $lastBlockIds; + } + + public function getLastBlockIds() + { + return $this->lastBlockIds; + } + + public function addLastBlockId($id) + { + if (!in_array($id, $this->lastBlockIds, true)) { + $this->lastBlockIds[] = $id; + } } public function getLastLine() @@ -138,19 +205,14 @@ public function getLastColumn() return $this->lastColumn; } - public function getLastBlockId() - { - return $this->lastBlockId; - } - public function getMyClass() { - return $this->myclass; + return $this->myClass; } public function setMyClass($myClass) { - $this->myclass = $myClass; + $this->myClass = $myClass; } public function getThisDef() @@ -163,14 +225,14 @@ public function setThisDef($thisDef) $this->thisDef = $thisDef; } - public function getBackDef() + public function getInstanceClassName() { - return $this->backDef; + return $this->instanceClassName; } - public function setBackDef($backDef) + public function setInstanceClassName($instanceClassName) { - $this->backDef = $backDef; + $this->instanceClassName = $instanceClassName; } public function getNameInstance() @@ -203,6 +265,86 @@ public function getBlocks() return $this->blocks; } + public function getBlockById($id) + { + foreach ($this->blocks as $block) { + if ($block->getId() === $id) { + return $block; + } + } + + return null; + } + + public function setOpInformations($opInformations) + { + $this->opInformations = $opInformations; + } + + public function getOpInformations() + { + return $this->opInformations; + } + + public function getOpId($op) + { + if (is_object($op)) { + return spl_object_hash($op); + } + + return null; + } + + public function getNbsOpInformations() + { + $nb = 0; + if (is_array($this->opInformations)) { + foreach ($this->opInformations as $opInformation) { + if (isset($opInformation["chained_results"])) { + $nb += count($opInformation["chained_results"]); + } + + if (isset($opInformation["valid_when_returning"])) { + $nb += 1; + } + + if (isset($opInformation["condition_defs"])) { + $nb += count($opInformation["condition_defs"]); + } + } + } + + return $nb; + } + + public function cleanOpInformations() + { + $this->opInformations = null; + } + + public function cleanOpInformation($id) + { + if (isset($this->opInformations[$id])) { + unset($this->opInformations[$id]); + } + } + + public function getOpInformation($id) + { + if (isset($this->opInformations[$id])) { + return $this->opInformations[$id]; + } + + return null; + } + + public function storeOpInformation($id, $infos) + { + if (!empty($id)) { + $this->opInformations[$id] = $infos; + } + } + public function setDefs($defs) { $this->defs = $defs; @@ -213,6 +355,16 @@ public function getDefs() return $this->defs; } + public function getStatePastArguments() + { + return $this->args; + } + + public function addStatePastArgument($nbparam, $state) + { + $this->args[$nbparam][] = $state; + } + public function addParam($param) { $this->params[] = $param; @@ -252,23 +404,43 @@ public function addReturnDef($return_def) $this->returnDefs[] = $return_def; } - public function getBlockId() + public function setReturnDefs($returndefs) { - return $this->blockId; + $this->returnDefs = $returndefs; } - public function setBlockId($blockId) + public function getInitialReturnDefs() { - $this->blockId = $blockId; + return $this->initialReturnDefs; } - - public function setCastReturn($cast) + + public function addInitialReturnDef($returnDef) { - $this->castReturn = $cast; + $this->initialReturnDefs[] = $returnDef; } - - public function getCastReturn() + + public function setInitialReturnDefs($returndefs) { - return $this->castReturn; + $this->initialReturnDefs = $returndefs; + } + + public function getFirstBlockId() + { + return $this->firstBlockId; + } + + public function setFirstBlockId($blockId) + { + $this->firstBlockId = $blockId; + } + + public function getBlockId() + { + return $this->blockId; + } + + public function setBlockId($blockId) + { + $this->blockId = $blockId; } } diff --git a/package/src/progpilot/Objects/MyOp.php b/package/src/progpilot/Objects/MyOp.php index ca8ba703..09634b27 100644 --- a/package/src/progpilot/Objects/MyOp.php +++ b/package/src/progpilot/Objects/MyOp.php @@ -19,7 +19,6 @@ class MyOp private $varLine; private $varColumn; private $sourceMyFile; - private $arrayValue; private $flags; @@ -27,7 +26,7 @@ class MyOp const TYPE_LITERAL = "type_literal"; const TYPE_ARRAY = "type_array"; const TYPE_ARRAY_EXPR = "type_array_expr"; - const TYPE_COPY_ARRAY = "type_copy_array"; + const TYPE_ARRAY_ELEMENT = "type_array_element"; const TYPE_METHOD = "type_method"; const TYPE_INSTANCE = "type_instance"; const TYPE_PROPERTY = "type_property"; @@ -44,17 +43,11 @@ public function __construct($varName, $varLine, $varColumn) $this->varLine = $varLine; $this->varColumn = $varColumn; $this->sourceMyFile = null; - $this->arrayValue = false; } - public function setId($varId) - { - $this->varId = $varId; - } - - public function getId() + public function __clone() { - return $this->varId; + $this->varId = MyOp::$nbObjects ++; } public function setType($flags) @@ -84,14 +77,14 @@ public function removeType($type) } } - public function setArrayValue($arrayValue) + public function setId($varId) { - $this->arrayValue = $arrayValue; + $this->varId = $varId; } - public function getArrayValue() + public function getId() { - return $this->arrayValue; + return $this->varId; } public function getLine() diff --git a/package/src/progpilot/Objects/MyProperty.php b/package/src/progpilot/Objects/MyProperty.php index dda82dc3..6831c2c3 100644 --- a/package/src/progpilot/Objects/MyProperty.php +++ b/package/src/progpilot/Objects/MyProperty.php @@ -10,16 +10,16 @@ namespace progpilot\Objects; -class MyProperty extends MyOp +class MyProperty extends MyDefinition { private $visibility; - private $properties; - public function __construct() + public function __construct($blockId, $myFile, $varLine, $varColumn, $varName) { - parent::__construct("", 0, 0); + parent::__construct($blockId, $myFile, $varLine, $varColumn, $varName); + + $this->addType(MyDefinition::TYPE_PROPERTY); $this->visibility = "public"; - $this->properties = []; } public function setVisibility($visibility) @@ -31,37 +31,4 @@ public function getVisibility() { return $this->visibility; } - - public function setProperties($properties) - { - $this->properties = $properties; - } - - public function getProperties() - { - return $this->properties; - } - - public function addProperty($property) - { - $this->properties[] = $property; - } - - public function hasProperty($name) - { - if (is_array($this->properties)) { - foreach ($this->properties as $property) { - if ($property === $name) { - return true; - } - } - } - - return false; - } - - public function popProperty() - { - return array_pop($this->properties); - } } diff --git a/package/src/progpilot/Outputs/MyOutputs.php b/package/src/progpilot/Outputs/MyOutputs.php index 55f9a3ec..140c6eef 100644 --- a/package/src/progpilot/Outputs/MyOutputs.php +++ b/package/src/progpilot/Outputs/MyOutputs.php @@ -12,126 +12,96 @@ use progpilot\Lang; use progpilot\Representations\Callgraph; -use progpilot\Representations\ControlFlowGraph; -use progpilot\Representations\AbstractSyntaxTree; -use PhpParser\NodeTraverser; - -class MyOutputs +class MyOutputs extends MyOutputsInternalApi { - private $results; - private $resolveIncludes; - private $resolveIncludesFile; - private $onAddResult; - private $taintedFlow; - private $countfilesanalyzed; - - public $currentIncludesFile; - public $cfg; - public $callgraph; - public $ast; - - public function __construct() - { - $this->resolveIncludes = false; - $this->resolveIncludesFile = null; - $this->currentIncludesFile = []; - $this->results = []; - $this->taintedFlow = false; - $this->onAddResult = null; - $this->countfilesanalyzed = 0; - - $this->resetRepresentations(); - } - - public function resetRepresentations() - { - //$this->results = []; - $this->cfg = new ControlFlowGraph; - $this->callgraph = new Callgraph; - $this->ast = new AbstractSyntaxTree; - } - - public function getAst() + public function getAst($myFunc) { $nodesjson = []; $linksjson = []; - foreach ($this->ast->getNodes() as $node) { - $hash = spl_object_hash($node); + if (!is_null($myFunc) && isset($this->ast[$myFunc->getId()])) { + $tmpAst = $this->ast[$myFunc->getId()]; - $nodesjson[] = array('name' => get_class($node), 'id' => $hash); - } + foreach ($tmpAst->getNodes() as $node) { + $hash = spl_object_hash($node); + + $nodesjson[] = array('name' => get_class($node), 'id' => $hash); + } - foreach ($this->ast->getEdges() as $edge) { - $caller = $edge[0]; - $callee = $edge[1]; + foreach ($tmpAst->getEdges() as $edge) { + $caller = $edge[0]; + $callee = $edge[1]; - $hashcaller = spl_object_hash($caller); - $hashcallee = spl_object_hash($callee); + $hashcaller = spl_object_hash($caller); + $hashcallee = spl_object_hash($callee); - if ($hashcaller !== $hashcallee) { - $linksjson[] = array('target' => $hashcallee, 'source' => $hashcaller); + if ($hashcaller !== $hashcallee) { + $linksjson[] = array('target' => $hashcallee, 'source' => $hashcaller); + } } } - $outputjson = array('nodes' => $nodesjson, 'links' => $linksjson); - - return $outputjson; + return array('nodes' => $nodesjson, 'links' => $linksjson); } - public function getCfg() + public function getCfg($myFunc) { $nodesjson = []; $linksjson = []; $realNodes = []; - foreach ($this->cfg->getNodes() as $id => $node) { - $realNodes[] = $id; - $nodesjson[] = array('name' => $this->cfg->getTextOfMyBlock($id), 'id' => $id); - } + if (!is_null($myFunc) && isset($this->cfg[$myFunc->getId()])) { + $tmpCfg = $this->cfg[$myFunc->getId()]; + + foreach ($tmpCfg->getNodes() as $id => $node) { + $realNodes[] = $id; + $nodesjson[] = array('name' => $tmpCfg->getTextOfMyBlock($id), 'id' => $id); + } - foreach ($this->cfg->getEdges() as $edge) { - $callerId = $this->cfg->getIdOfNode($edge[0]); - $calleeId = $this->cfg->getIdOfNode($edge[1]); + foreach ($tmpCfg->getEdges() as $edge) { + $callerId = $tmpCfg->getIdOfNode($edge[0]); + $calleeId = $tmpCfg->getIdOfNode($edge[1]); - if ($callerId !== $calleeId - && in_array($callerId, $realNodes, true) - && in_array($calleeId, $realNodes, true)) { - $linksjson[] = array('source' => $callerId, 'target' => $calleeId); + if ($callerId !== $calleeId + && in_array($callerId, $realNodes, true) + && in_array($calleeId, $realNodes, true)) { + $linksjson[] = array('source' => $callerId, 'target' => $calleeId); + } } } - $outputjson = array('nodes' => $nodesjson, 'links' => $linksjson); - return $outputjson; + return array('nodes' => $nodesjson, 'links' => $linksjson); } - public function getCallGraph() + public function getCallGraph($myFunc) { $nodesjson = []; $linksjson = []; $realNodes = []; - $nodes = $this->callgraph->getNodes(); + if (!is_null($myFunc) && isset($this->callgraph[$myFunc->getId()])) { + $tmpCallgraph = $this->callgraph[$myFunc->getId()]; + $nodes = $tmpCallgraph->getNodes(); - foreach ($nodes as $nodeCaller) { - $realNodes[] = $nodeCaller->getId(); - $nodesjson[] = array('name' => $nodeCaller->getName(), 'id' => $nodeCaller->getId()); - } + foreach ($nodes as $nodeCaller) { + $realNodes[] = $nodeCaller->getId(); + $nodesjson[] = array('name' => $nodeCaller->getName(), 'id' => $nodeCaller->getId()); + } - foreach ($nodes as $key => $nodeCaller) { - foreach ($nodeCaller->getChildren() as $key => $nodeCalleeId) { - $nodeCallee = $nodes[$nodeCalleeId]; - if ($nodeCaller->getId() !== $nodeCallee->getId() - && in_array($nodeCaller->getId(), $realNodes, true) - && in_array($nodeCallee->getId(), $realNodes, true)) { - $linksjson[] = array('source' => $nodeCaller->getId(), 'target' => $nodeCallee->getId()); + foreach ($nodes as $key => $nodeCaller) { + foreach ($nodeCaller->getChildren() as $key => $nodeCalleeId) { + $nodeCallee = $nodes[$nodeCalleeId]; + if ($nodeCaller->getId() !== $nodeCallee->getId() + && in_array($nodeCaller->getId(), $realNodes, true) + && in_array($nodeCallee->getId(), $realNodes, true)) { + $linksjson[] = array('source' => $nodeCaller->getId(), 'target' => $nodeCallee->getId()); + } } } } - $outputjson = array('nodes' => $nodesjson, 'links' => $linksjson); - return $outputjson; + return array('nodes' => $nodesjson, 'links' => $linksjson); } public function setOnAddResult($func) @@ -144,56 +114,25 @@ public function getOnAddResult() return $this->onAddResult; } - public function addResult($temp) - { - if (!in_array($temp, $this->results, true)) { - if (!is_null($this->onAddResult)) { - $params = array($temp); - call_user_func($this->onAddResult, $params); - } - - $this->results[] = $temp; - } - } - - public function setResults(&$results) - { - $this->results = &$results; - } - public function &getResults() { return $this->results; } - public function getCountAnalyzedFiles() - { - return $this->countfilesanalyzed; - } - - public function setCountAnalyzedFiles($nb) - { - $this->countfilesanalyzed = $nb; - } - - public function getResolveIncludes() - { - return $this->resolveIncludes; - } - - public function resolveIncludes($option) + public function setIncludeFailuresFile($file) { - $this->resolveIncludes = $option; - } + $log = true; + if (is_null($file) || empty($file)) { + $log = false; + } - public function resolveIncludesFile($file) - { - $this->resolveIncludesFile = $file; + $this->writeIncludeFailures = $log; + $this->includeFailuresFile = $file; } - public function getresolveIncludesFile() + public function getIncludeFailuresFile() { - return $this->resolveIncludesFile; + return $this->includeFailuresFile; } public function taintedFlow($bool) @@ -205,24 +144,4 @@ public function getTaintedFlow() { return $this->taintedFlow; } - - public function writeIncludesFile() - { - if ($this->resolveIncludes) { - $fp = fopen($this->resolveIncludesFile, "w"); - if ($fp) { - $myArray = ""; - if (count($this->currentIncludesFile) > 0) { - $myArray = []; - foreach ($this->currentIncludesFile as $includeFile) { - $myArray[] = [$includeFile->getName(), $includeFile->getLine(), $includeFile->getColumn()]; - } - } - - $outputjson = array('includes_not_resolved' => $myArray); - fwrite($fp, json_encode($outputjson, JSON_UNESCAPED_SLASHES)); - fclose($fp); - } - } - } } diff --git a/package/src/progpilot/Outputs/MyOutputsInternalApi.php b/package/src/progpilot/Outputs/MyOutputsInternalApi.php new file mode 100644 index 00000000..cc385d82 --- /dev/null +++ b/package/src/progpilot/Outputs/MyOutputsInternalApi.php @@ -0,0 +1,180 @@ +writeIncludeFailures = false; + $this->includeFailuresFile = null; + $this->currentIncludesFile = []; + $this->results = []; + $this->taintedFlow = false; + $this->onAddResult = null; + $this->countfilesanalyzed = 0; + + $this->cfg = []; + $this->callgraph = []; + $this->ast = []; + } + + public function resetRepresentationsForAllFunctions() + { + $this->cfg = []; + $this->callgraph = []; + $this->ast = []; + } + + public function createRepresentationsForFunction($myFunc) + { + if (!is_null($myFunc)) { + $this->cfg[$myFunc->getId()] = new ControlFlowGraph; + $this->callgraph[$myFunc->getId()] = new Callgraph; + $this->ast[$myFunc->getId()] = new AbstractSyntaxTree; + } + } + + // cfg accessors + public function cfgAddTextOfMyBlock($myFunc, $id, $text) + { + if (!is_null($myFunc) && isset($this->cfg[$myFunc->getId()])) { + $tmpCfg = $this->cfg[$myFunc->getId()]; + $tmpCfg->addTextOfMyBlock($id, $text); + } + } + + public function cfgAddNode($myFunc, $id, $block) + { + if (!is_null($myFunc) && isset($this->cfg[$myFunc->getId()])) { + $tmpCfg = $this->cfg[$myFunc->getId()]; + $tmpCfg->addNode($id, $text); + } + } + + public function cfgAddEdge($myFunc, $caller, $callee) + { + if (!is_null($myFunc) && isset($this->cfg[$myFunc->getId()])) { + $tmpCfg = $this->cfg[$myFunc->getId()]; + $tmpCfg->addEdge($caller, $callee); + } + } + + // callgraph accessors + public function callgraphAddNode($myFunc, $myFunccall, $myClass) + { + if (!is_null($myFunc) && isset($this->callgraph[$myFunc->getId()])) { + $tmpCallgraph = $this->callgraph[$myFunc->getId()]; + $tmpCallgraph->addNode($myFunccall, $myClass); + } + } + + public function callgraphAddEdge($myFunc, $myFuncCaller, $myClassCaller, $myFuncCallee, $myClassCallee) + { + if (!is_null($myFunc) && isset($this->callgraph[$myFunc->getId()])) { + $tmpCallgraph = $this->callgraph[$myFunc->getId()]; + $tmpCallgraph->addEdge($myFuncCaller, $myClassCaller, $myFuncCallee, $myClassCallee); + } + } + + public function callgraphAddFuncCall($myFunc, $myBlock, $myFunccall, $myClass) + { + if (!is_null($myFunc) + && isset($this->callgraph[$myFunc->getId()])) { + $tmpCallgraph = $this->callgraph[$myFunc->getId()]; + $tmpCallgraph->addFuncCall($myBlock, $myFunccall, $myClass); + } + } + + public function callgraphAddChild($myFunc, $myBlockParent, $myBlockChild) + { + if (!is_null($myFunc) && isset($this->callgraph[$myFunc->getId()])) { + $tmpCallgraph = $this->callgraph[$myFunc->getId()]; + $tmpCallgraph->addChild($myBlockParent, $myBlockChild); + } + } + + public function addResult($temp) + { + if (!in_array($temp, $this->results, true)) { + if (!is_null($this->onAddResult)) { + $params = array($temp); + call_user_func($this->onAddResult, $params); + } + + $this->results[] = $temp; + } + } + + public function setResults(&$results) + { + $this->results = &$results; + } + + public function getCountAnalyzedFiles() + { + return $this->countfilesanalyzed; + } + + public function setCountAnalyzedFiles($nb) + { + $this->countfilesanalyzed = $nb; + } + + public function getWriteIncludeFailures() + { + return $this->writeIncludeFailures; + } + + public function setWriteIncludeFailures($writeIncludeFailures) + { + $this->writeIncludeFailures = $writeIncludeFailures; + } + + public function writeIncludesFile() + { + if ($this->writeIncludeFailures) { + $fp = fopen($this->includeFailuresFile, "w"); + if ($fp) { + $myArray = ""; + if (count($this->currentIncludesFile) > 0) { + $myArray = []; + foreach ($this->currentIncludesFile as $includeFile) { + $myArray[] = [$includeFile->getName(), $includeFile->getLine(), $includeFile->getColumn()]; + } + } + + $outputjson = array('include_failures' => $myArray); + fwrite($fp, json_encode($outputjson, JSON_UNESCAPED_SLASHES)); + fclose($fp); + } + } + } +} diff --git a/package/src/progpilot/Representations/Callgraph.php b/package/src/progpilot/Representations/Callgraph.php index 4ea7c930..26049230 100644 --- a/package/src/progpilot/Representations/Callgraph.php +++ b/package/src/progpilot/Representations/Callgraph.php @@ -70,8 +70,8 @@ public function addEdge($myFuncCaller, $myClassCaller, $myFuncCallee, $myClassCa ); if (array_key_exists($NodeCGCaller->getId(), $this->nodes) - && array_key_exists($NodeCGCallee->getId(), $this->nodes) - && $NodeCGCaller->getId() !== $NodeCGCallee->getId()) { + && array_key_exists($NodeCGCallee->getId(), $this->nodes) + && $NodeCGCaller->getId() !== $NodeCGCallee->getId()) { if (!in_array($NodeCGCallee->getId(), $this->nodes[$NodeCGCaller->getId()]->getChildren(), true)) { $storage = $this->nodes[$NodeCGCaller->getId()]->getChildren(); $storage[] = $NodeCGCallee->getId(); @@ -186,12 +186,12 @@ public function computeCallGraph() } // calculate edges second case : calls from parents - if (count($calls) > 0) { + if (!empty($calls)) { $lastCallParent = $calls[count($calls) - 1][0]; $lastMyClassParent = $calls[count($calls) - 1][1]; foreach ($this->blocks[$myBlock]->children as $child) { $calls = $this->getCalls($child); - if (!is_null($calls) && count($calls) > 0) { + if (!is_null($calls) && !empty($calls)) { $firstCallChild = $calls[0][0]; $firstMyClassChild = $calls[0][1]; $this->addEdge( diff --git a/package/src/progpilot/Transformations/Js/Assign.php b/package/src/progpilot/Transformations/Js/Assign.php index 17f9e288..ff531675 100644 --- a/package/src/progpilot/Transformations/Js/Assign.php +++ b/package/src/progpilot/Transformations/Js/Assign.php @@ -15,7 +15,6 @@ use progpilot\Objects\MyFunction; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyOp; use progpilot\Code\MyInstruction; @@ -30,19 +29,7 @@ public static function instruction($context, $opExpr, $opDef) $context->getCurrentMycode()->addCode( new MyInstruction(Opcodes::START_ASSIGN) ); - $context->getCurrentMycode()->addCode( - new MyInstruction(Opcodes::START_EXPRESSION) - ); - $myExpr = new MyExpr( - $opExpr->loc->start->line, - $opExpr->loc->start->column - ); - - $backDef = Expr::instruction($opExpr, $context, $myExpr); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); $context->getCurrentMycode()->addCode($instEndExpr); $context->getCurrentMycode()->addCode( @@ -76,9 +63,6 @@ public static function instruction($context, $opExpr, $opDef) } } - $myDef->setExpr($myExpr); - $myExpr->setAssign(true); - $myExpr->setAssignDef($myDef); $instDef = new MyInstruction(Opcodes::DEFINITION); $instDef->addProperty(MyInstruction::DEF, $myDef); diff --git a/package/src/progpilot/Transformations/Js/Expr.php b/package/src/progpilot/Transformations/Js/Expr.php index 894d6351..8345d738 100644 --- a/package/src/progpilot/Transformations/Js/Expr.php +++ b/package/src/progpilot/Transformations/Js/Expr.php @@ -15,7 +15,6 @@ use progpilot\Objects\MyFunction; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyOp; use progpilot\Code\MyInstruction; @@ -24,13 +23,12 @@ class Expr { - - public static function instruction($op, $context, $myExpr) + public static function instruction($op, $context) { - return Expr::instructionInternal($op, $context, $myExpr); + return Expr::instructionInternal($op, $context); } - public static function instructionInternal($op, $context, $myExpr) + public static function instructionInternal($op, $context) { $myTempDef = null; @@ -39,7 +37,6 @@ public static function instructionInternal($op, $context, $myExpr) if ($typeright === "Identifier" || $typeright === "Literal" || $typeright === "MemberExpression") { $myright = new MyDefinition($op->loc->start->line, $op->loc->start->column, $nameright); - $myright->setExpr($myExpr); if ($typeright === "MemberExpression") { $propertyName = Common::getNameProperty($op); @@ -59,17 +56,9 @@ public static function instructionInternal($op, $context, $myExpr) } if ($typeright === "BinaryExpression") { - $myExpr->setIsConcat(true); - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::CONCAT_LEFT)); - Expr::instructionInternal($op->left, $context, $myExpr); $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::CONCAT_RIGHT)); - Expr::instructionInternal($op->right, $context, $myExpr); - } elseif ($typeright === "CallExpression") { - $myTempDef = FuncCall::instruction($context, $myExpr, $op); - } elseif ($typeright === "NewExpression") { - $myTempDef = FuncCall::instruction($context, $myExpr, $op); } return $myTempDef; diff --git a/package/src/progpilot/Transformations/Js/FuncCall.php b/package/src/progpilot/Transformations/Js/FuncCall.php index 3c1f847c..bb5801a3 100644 --- a/package/src/progpilot/Transformations/Js/FuncCall.php +++ b/package/src/progpilot/Transformations/Js/FuncCall.php @@ -15,7 +15,6 @@ use progpilot\Objects\MyFunction; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyOp; use progpilot\Code\MyInstruction; @@ -41,22 +40,9 @@ public static function argument($arg, $op, $context, $instFuncCallMain, $funcCal $myDef = new MyDefinition($op->loc->start->line, $op->loc->start->column, $defName); $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_ASSIGN)); - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - $myExprparam = new MyExpr($op->loc->start->line, $op->loc->start->column); - $myExprparam->setAssign(true); - $myExprparam->setAssignDef($myDef); $instFuncCallMain->addProperty("argdef$numParam", $myDef); - $instFuncCallMain->addProperty("argexpr$numParam", $myExprparam); - - /* - $myTemp = Expr::instruction($arg, $context, $myExprparam); - if (!is_null($myTemp)) { - $myDef->setValueFromDef($myTemp); - } - */ $myargumenttype = $arg->type; $myargumentvalue = ""; @@ -75,7 +61,6 @@ public static function argument($arg, $op, $context, $instFuncCallMain, $funcCal } $mytemp = new MyDefinition($op->loc->start->line, $op->loc->start->column, $myargumentvalue); - $mytemp->setExpr($myExprparam); if ($myargumenttype === 'MemberExpression') { $propertyName = Common::getNameProperty($arg); @@ -87,10 +72,6 @@ public static function argument($arg, $op, $context, $instFuncCallMain, $funcCal $instTemporarySimple->addProperty(MyInstruction::TEMPORARY, $mytemp); $context->getCurrentMycode()->addCode($instTemporarySimple); - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExprparam); - $context->getCurrentMycode()->addCode($instEndExpr); - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::END_ASSIGN)); $instDef = new MyInstruction(Opcodes::DEFINITION); @@ -104,7 +85,6 @@ public static function argument($arg, $op, $context, $instFuncCallMain, $funcCal */ public static function instruction( $context, - $myExpr, $op ) { @@ -163,8 +143,6 @@ public static function instruction( $myFunctionCall->setNbParams($nbparams); $instFuncCallMain->addProperty(MyInstruction::MYFUNC_CALL, $myFunctionCall); - $instFuncCallMain->addProperty(MyInstruction::EXPR, $myExpr); - $instFuncCallMain->addProperty(MyInstruction::ARR, false); $context->getCurrentMycode()->addCode($instFuncCallMain); return $mybackdef; diff --git a/package/src/progpilot/Transformations/Js/Transform.php b/package/src/progpilot/Transformations/Js/Transform.php index 4c0c3552..268589b8 100644 --- a/package/src/progpilot/Transformations/Js/Transform.php +++ b/package/src/progpilot/Transformations/Js/Transform.php @@ -13,7 +13,6 @@ use progpilot\Objects\MyFunction; use progpilot\Objects\MyBlock; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyClass; use progpilot\Objects\MyOp; use progpilot\Objects\MyFile; @@ -77,7 +76,7 @@ public function v8jsExecute() $this->context->getCurrentMycode()->addCode($instFunc); // because when we call (funccall) a function by name, it can be undefined - $this->context->getFunctions()->addFunction($myFunction->getName(), $myFunction); + $this->context->getFunctions()->addFunction("file.js", "function", $myFunction->getName(), $myFunction); $this->context->setCurrentFunc($myFunction); foreach ($cfg[2] as $FlowNode) { @@ -85,7 +84,10 @@ public function v8jsExecute() if (isset($astNode->type)) { if ($astNode->type !== "exit") { - $myBlock = new MyBlock; + $myBlock = new MyBlock( + $this->context->getCurrentLine(), + $this->context->getCurrentColumn() + ); $myBlock->setStartAddressBlock(count($this->context->getCurrentMycode()->getCodes())); //$this->context->setCurrentBlock($FlowNode); @@ -119,20 +121,6 @@ public function v8jsExecute() break; case 'CallExpression': - $myExpr = new MyExpr( - $astNode->loc->start->line, - $astNode->loc->start->column - ); - $this->context->getCurrentMycode()->addCode( - new MyInstruction(Opcodes::START_EXPRESSION) - ); - - FuncCall::instruction($this->context, $myExpr, $astNode); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - break; case 'ReturnStatement': diff --git a/package/src/progpilot/Transformations/Php/ArrayExpr.php b/package/src/progpilot/Transformations/Php/ArrayExpr.php deleted file mode 100644 index 8f27c38d..00000000 --- a/package/src/progpilot/Transformations/Php/ArrayExpr.php +++ /dev/null @@ -1,97 +0,0 @@ -ops[0]->values)) { - $nbArrayExpr = 0; - foreach ($op->ops[0]->values as $value) { - $name = Common::getNameDefinition($value); - $type = Common::getTypeDefinition($value); - $typeArray = Common::getTypeIsArray($value); - - // we create an element for each value of array expr - // name_arr = [expr1, expr2] => name_arr[0] = expr1, name_arr[1] = expr2 - if (isset($op->ops[0]->keys[$nbArrayExpr]->value)) { - $indexValue = $op->ops[0]->keys[$nbArrayExpr]->value; - $buildingArr = array($indexValue => $arr); - } elseif (isset($op->ops[0]->keys[$nbArrayExpr]->ops[0]->name->value)) { - // const arr(CONST => "value") - $indexValue = $op->ops[0]->keys[$nbArrayExpr]->ops[0]->name->value; - $buildingArr = array($indexValue => $arr); - } else { - $buildingArr = array($nbArrayExpr => $arr); - } - - if ($typeArray === MyOp::TYPE_ARRAY_EXPR) { - $buildingArr = ArrayExpr::instruction($value, $context, $buildingArr, $defName, $isReturnDef); - } else { - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_ASSIGN)); - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - $myExpr = new MyExpr($context->getCurrentLine(), $context->getCurrentColumn()); - $myExpr->setAssign(true); - - Expr::instruction($value, $context, $myExpr, null); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $context->getCurrentMycode()->addCode($instEndExpr); - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::END_ASSIGN)); - - // mydef after expr because expr is executed before (and his id lower than mydef id) - $myDef = new MyDefinition($context->getCurrentLine(), $context->getCurrentColumn(), $defName); - - if ($isReturnDef) { - $context->getCurrentFunc()->addReturnDef($myDef); - } - - $myExpr->setAssignDef($myDef); - - // we reverse the arr - $arrtrans = BuildArrays::buildArrayFromArr($buildingArr, false); - - $myDef->addType(MyDefinition::TYPE_ARRAY); - $myDef->setArrayValue($arrtrans); - - $instDef = new MyInstruction(Opcodes::DEFINITION); - $instDef->addProperty(MyInstruction::DEF, $myDef); - $context->getCurrentMycode()->addCode($instDef); - - unset($myExpr); - unset($myDef); - } - - $nbArrayExpr ++; - } - } - - return $buildingArr; - } -} diff --git a/package/src/progpilot/Transformations/Php/Assign.php b/package/src/progpilot/Transformations/Php/Assign.php index 74ce94b9..4bb5eb83 100644 --- a/package/src/progpilot/Transformations/Php/Assign.php +++ b/package/src/progpilot/Transformations/Php/Assign.php @@ -12,46 +12,49 @@ use PHPCfg\Block; use PHPCfg\Op; +use PHPCfg\Operand; use progpilot\Objects\MyFunction; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyOp; use progpilot\Code\MyInstruction; use progpilot\Code\Opcodes; use progpilot\Transformations\Php\Transform; use progpilot\Transformations\Php\OpTr; +use progpilot\Transformations\Php\Common; +use progpilot\Transformations\Php\Exprs\PropertyFetch; +use progpilot\Transformations\Php\Exprs\DimFetch; +use progpilot\Transformations\Php\Exprs\VariableFetch; +use progpilot\Transformations\Php\Exprs\ArrayFetch; class Assign { - public static function instruction($context, $isReturnDef = false, $isDefine = false) - { + public static function instruction( + $context, + $op, + $expr, + $var, + $isReturnDef = false, + $isDefine = false + ) { + $backDef = null; + if ($isDefine) { $name = "const_".rand(); if (isset($context->getCurrentOp()->args[0]->value)) { $name = $context->getCurrentOp()->args[0]->value; + $var = $context->getCurrentOp()->args[0]; } - $type = MyOp::TYPE_CONST; - $typeArray = null; - $typeInstance = null; - - $exprOp = $context->getCurrentOp(); if (isset($context->getCurrentOp()->args[1])) { - $exprOp = $context->getCurrentOp()->args[1]; + $expr = $context->getCurrentOp()->args[1]; } } else { $name = Common::getNameDefinition($context->getCurrentOp()); - $type = Common::getTypeDefinition($context->getCurrentOp()); - $typeArray = Common::getTypeIsArray($context->getCurrentOp()); - $typeInstance = Common::getTypeIsInstance($context->getCurrentOp()); - if (empty($name)) { $name = "empty_".rand(); } - - $exprOp = $context->getCurrentOp()->expr; } // name of function return @@ -59,109 +62,84 @@ public static function instruction($context, $isReturnDef = false, $isDefine = f $name = $context->getCurrentFunc()->getName()."_return"; } - // $array = [expr, expr, expr] - if ($typeArray === MyOp::TYPE_ARRAY_EXPR) { - $arr = false; - if (isset($context->getCurrentOp()->var)) { - $arr = BuildArrays::buildArrayFromOps($context->getCurrentOp()->var, false); - } - - ArrayExpr::instruction($context->getCurrentOp()->expr, $context, $arr, $name, $isReturnDef); - } else { - $isRef = false; - if ($context->getCurrentOp() instanceof Op\Expr\AssignRef) { - $isRef = true; - } - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_ASSIGN)); - - // it's an expression which will define a definition - $myExpr = new MyExpr($context->getCurrentLine(), $context->getCurrentColumn()); - $myExpr->setAssign(true); + $instAssign = new MyInstruction(Opcodes::END_ASSIGN); - if (isset($context->getCurrentOp()->expr->ops[0]) - && $context->getCurrentOp()->expr->ops[0] instanceof Op\Iterator\Value) { - $myExpr->setAssignIterator(true); - } - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - $backDef = Expr::instruction($exprOp, $context, $myExpr); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $context->getCurrentMycode()->addCode($instEndExpr); - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::END_ASSIGN)); - - $myDef = new MyDefinition($context->getCurrentLine(), $context->getCurrentColumn(), $name); + // extra = properties, arrays + // $left(extra) = $right(extra) <= expr right - if ($isRef) { - $myDef->addType(MyDefinition::TYPE_REFERENCE); - } + // let's start by the right part + // in case of the expr has not been catched naturally in the transform + if (isset($expr)) { + Expr::implicitfetch($context, $expr, null); + } - if ($type === MyOp::TYPE_CONST) { + // let's continue by the left part + $instDefinition = new MyInstruction(Opcodes::DEFINITION); + + $myDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + $name + ); + + switch (Common::getTypeDef($op)) { + case MyOp::TYPE_ARRAY: + $myDef->addType(MyDefinition::TYPE_ARRAY); + $myDef->getCurrentState()->addType(MyDefinition::TYPE_ARRAY); + break; + case MyOp::TYPE_PROPERTY: + $myDef->addType(MyDefinition::TYPE_PROPERTY); + break; + case MyOp::TYPE_STATIC_PROPERTY: + $myDef->addType(MyDefinition::TYPE_STATIC_PROPERTY); + break; + case MyOp::TYPE_CONST: $myDef->addType(MyDefinition::TYPE_CONSTANTE); - } - - if ($isReturnDef) { - $context->getCurrentFunc()->addReturnDef($myDef); - } + break; + case MyOp::TYPE_VARIABLE: + break; + default: + break; + } - $myExpr->setAssignDef($myDef); + $instDefinition->addProperty(MyInstruction::DEF, $myDef); + $context->getCurrentMycode()->addCode($instDefinition); - $instDef = new MyInstruction(Opcodes::DEFINITION); - $instDef->addProperty(MyInstruction::DEF, $myDef); - $context->getCurrentMycode()->addCode($instDef); + $instAssign->addProperty(MyInstruction::DEF, $myDef); - // $array[09][098] = expr; - if ($typeArray === MyOp::TYPE_ARRAY) { - $arr = BuildArrays::buildArrayFromOps($context->getCurrentOp()->var, false); - $myDef->addType(MyDefinition::TYPE_ARRAY); - $myDef->setArrayValue($arr); - } + if ($op instanceof Op\Expr\AssignRef) { + $instAssign->addProperty(MyInstruction::REFERENCE, true); + } - // a variable, property - if ($type === MyOp::TYPE_PROPERTY) { - $myDef->addType(MyDefinition::TYPE_PROPERTY); - $propertyName = Common::getNameProperty($context->getCurrentOp()->var->ops[0]); - $myDef->property->setProperties($propertyName); - } + if ($isReturnDef) { + $context->getCurrentFunc()->addReturnDef($myDef); + $context->getCurrentBlock()->addReturnDef($myDef); + $myDef->setReturnDef(true); + } - // a variable, property - if ($type === MyOp::TYPE_STATIC_PROPERTY) { - $myDef->addType(MyDefinition::TYPE_STATIC_PROPERTY); - $propertyName = Common::getNameProperty($context->getCurrentOp()->var->ops[0]); - $myDef->property->setProperties($propertyName); - } + $instAssign->addProperty( + MyInstruction::EXPRID, + $context->getCurrentFunc()->getOpId($expr) + ); - // an object (created by new) - if ($typeInstance === MyOp::TYPE_INSTANCE) { - // it's the class name not instance name - if (isset($context->getCurrentOp()->expr->ops[0]->class->value)) { - $nameClass = $context->getCurrentOp()->expr->ops[0]->class->value; - $myDef->addType(MyDefinition::TYPE_INSTANCE); - $myDef->setClassName($nameClass); - - // ou bien créer backdef ici - if (!is_null($backDef)) { - $backDef->setId($myDef->getId() + 1); - } - } - } + if (!$isReturnDef) { + $instAssign->addProperty( + MyInstruction::VARID, + $context->getCurrentFunc()->getOpId($var) + ); + } - if ($isRef) { - $refName = Common::getNameDefinition($context->getCurrentOp()->expr); - $refType = Common::getTypeDefinition($context->getCurrentOp()->expr); - $refTypeArray = Common::getTypeIsArray($context->getCurrentOp()->expr); - $myDef->setRefName($refName); - - if ($refTypeArray === MyOp::TYPE_ARRAY) { - $arr = BuildArrays::buildArrayFromOps($context->getCurrentOp()->expr, false); - $myDef->addType(MyDefinition::TYPE_ARRAY_REFERENCE); - $myDef->setRefArrValue($arr); - } - } + if (isset($op->result)) { + $instAssign->addProperty( + MyInstruction::RESULTID, + $context->getCurrentFunc()->getOpId($op->result) + ); } + + $context->getCurrentMycode()->addCode($instAssign); + + return $backDef; } } diff --git a/package/src/progpilot/Transformations/Php/BuildArrays.php b/package/src/progpilot/Transformations/Php/BuildArrays.php deleted file mode 100644 index 2a8aed73..00000000 --- a/package/src/progpilot/Transformations/Php/BuildArrays.php +++ /dev/null @@ -1,94 +0,0 @@ -ops)) { - foreach ($initops->ops as $op) { - if ($op instanceof Op\Expr\ArrayDimFetch) { - return BuildArrays::functionStartOps($op->var); - } - } - } - - if ($initops instanceof Op\Iterator\Value) { - return BuildArrays::functionStartOps($initops->var); - } - - return $initops; - } - - public static function buildArrayFromArr($start, $end) - { - if (is_array($start)) { - foreach ($start as $ind => $value) { - $end = array($ind => $end); - $end = BuildArrays::buildArrayFromArr($value, $end); - } - } - - return $end; - } - - public static function extractArrayFromArr($originalarr, $indarr) - { - if ($originalarr === $indarr) { - return false; - } - - $arr = $originalarr; - - if (is_array($indarr)) { - foreach ($indarr as $ind => $value) { - if (isset($originalarr[$ind])) { - if ($originalarr[$ind] === $indarr[$ind]) { - return $originalarr[$ind]; - } - - $arr = BuildArrays::extractArrayFromArr($originalarr[$ind], $indarr[$ind]); - } else { - $arr = false; - } - } - } - - return $arr; - } - - public static function buildArrayFromOps($initops, $arr) - { - if (isset($initops->ops)) { - foreach ($initops->ops as $op) { - if ($op instanceof Op\Expr\ArrayDimFetch) { - $ind = 0; - if (isset($op->dim->value)) { - $ind = $op->dim->value; - } elseif (isset($op->dim->ops[0]->name->value)) { - $ind = $op->dim->ops[0]->name->value; - } - - $arr = array($ind => $arr); - $arr = BuildArrays::buildArrayFromOps($op->var, $arr); - } - } - } - - return $arr; - } -} diff --git a/package/src/progpilot/Transformations/Php/Common.php b/package/src/progpilot/Transformations/Php/Common.php index f9392a38..31c1d001 100644 --- a/package/src/progpilot/Transformations/Php/Common.php +++ b/package/src/progpilot/Transformations/Php/Common.php @@ -14,6 +14,7 @@ use PHPCfg\Op; use PHPCfg\Operand; +use progpilot\Objects\MyDefinition; use progpilot\Objects\MyOp; use progpilot\Code\MyInstruction; use progpilot\Code\Opcodes; @@ -30,45 +31,69 @@ public static function validLastKnownValue($value) return false; } - public static function getNameProperty($op) + public static function isChainedKnownType($op) { - $propertyNameArray = []; + if ($op instanceof Op\Expr\PropertyFetch + || $op instanceof Op\Expr\StaticPropertyFetch + || $op instanceof Op\Expr\ArrayDimFetch) { + return true; + } - if (isset($op->ops[0])) { - if ($op->ops[0] instanceof Op\Expr\ArrayDimFetch) { - $propertyNameArray = Common::getNameProperty($op->ops[0]); - } + return false; + } - if ($op instanceof Op\Expr\PropertyFetch) { - $propertyNameArray = Common::getNameProperty($op->ops[0]); - } + public static function getTypeDef($op) + { + // the order is important (current it breaks array14.php) + // first we look for the left side + // this->foo + + if (isset($op->var->ops[0])) { + if (isset($op->var->ops[0]->var->ops[0]) + && Common::isChainedKnownType($op->var->ops[0]->var->ops[0]) + && $op->var->ops[0]->var->ops[0] !== $op->var->ops[0]) { + return Common::getTypeDef($op->var->ops[0]); + } else { + if ($op->var->ops[0] instanceof Op\Expr\PropertyFetch) { + return MyOp::TYPE_PROPERTY; + } - if ($op instanceof Op\Expr\StaticPropertyFetch) { - $propertyNameArray = Common::getNameProperty($op->ops[0]); - } - } + if ($op->var->ops[0] instanceof Op\Expr\StaticPropertyFetch) { + return MyOp::TYPE_STATIC_PROPERTY; + } - if (isset($op->var->ops)) { - foreach ($op->var->ops as $opeach) { - if ($opeach instanceof Op\Expr\ArrayDimFetch) { - $propertyNameArray = Common::getNameProperty($opeach); + if ($op->var->ops[0] instanceof Op\Expr\ArrayDimFetch) { + return MyOp::TYPE_ARRAY; } - if ($opeach instanceof Op\Expr\PropertyFetch) { - $propertyNameArray = Common::getNameProperty($opeach); + if ($op->var->ops[0] instanceof Op\Expr\Array_) { + return MyOp::TYPE_ARRAY_EXPR; } - if ($opeach instanceof Op\Expr\StaticPropertyFetch) { - $propertyNameArray = Common::getNameProperty($opeach); + if (isset($op->var->original->name->value)) { + return MyOp::TYPE_VARIABLE; } } } - - if (isset($op->name->value)) { - $propertyNameArray[] = $op->name->value; + + // then for the right + // foo = array() + // foo = define("FOO") + if (isset($op) && isset($op->name)) { + if ($op instanceof Op\Expr\FuncCall + && $op->name instanceof Operand\Literal + && $op->name->value === "define") { + return MyOp::TYPE_CONST; + } + } + + if (isset($op->expr->ops[0])) { + if ($op->expr->ops[0] instanceof Op\Expr\Array_) { + return MyOp::TYPE_ARRAY_EXPR; + } } - return $propertyNameArray; + return null; } public static function getNameDefinition($ops, $lookingForProperty = false) @@ -146,7 +171,7 @@ public static function getNameDefinition($ops, $lookingForProperty = false) if (isset($ops->name->value)) { return $ops->name->value; } - + // arrayexpr if (isset($ops->value)) { return $ops->value; @@ -179,225 +204,4 @@ public static function getTypeVisibility($visibility) return "public"; } } - - public static function isFuncCallWithoutReturn($op) - { - if (!(isset($op->result->usages[0])) || ( - // funccall()[0] - !(isset($op->result->usages[0]) && $op->result->usages[0] instanceof Op\Expr\ArrayDimFetch) && - // test = funccall() // funcccall(funccall()) - !(isset($op->result->usages[0]) - && ( - $op->result->usages[0] instanceof Op\Terminal\Echo_ - || $op->result->usages[0] instanceof Op\Terminal\Return_ - || $op->result->usages[0] instanceof Op\Expr\Print_ - || $op->result->usages[0] instanceof Op\Expr\StaticCall - || $op->result->usages[0] instanceof Op\Expr\MethodCall - || $op->result->usages[0] instanceof Op\Expr\NsFuncCall - || $op->result->usages[0] instanceof Op\Expr\FuncCall - || $op->result->usages[0] instanceof Op\Expr\Assign - || $op->result->usages[0] instanceof Op\Expr\BinaryOp\Concat - || $op->result->usages[0] instanceof Op\Expr\Array_ - || $op->result->usages[0] instanceof Op\Expr\Include_ - || $op->result->usages[0] instanceof Op\Expr\Eval_ - - || $op->result->usages[0] instanceof Op\Expr\Cast\Int_ - || $op->result->usages[0] instanceof Op\Expr\Cast\Array_ - || $op->result->usages[0] instanceof Op\Expr\Cast\Bool_ - || $op->result->usages[0] instanceof Op\Expr\Cast\Double_ - || $op->result->usages[0] instanceof Op\Expr\Cast\Object_ - || $op->result->usages[0] instanceof Op\Expr\Cast\String_ - - || $op->result->usages[0] instanceof Op\Iterator\Reset - )) - )) { - return true; - } - - return false; - } - - public static function getTypeIsArray($ops) - { - if (isset($ops->ops[0])) { - if ($ops->ops[0] instanceof Op\Expr\FuncCall - || $ops->ops[0] instanceof Op\Expr\NsFuncCall - || $ops->ops[0] instanceof Op\Expr\MethodCall - || $ops->ops[0] instanceof Op\Expr\StaticCall - || $ops->ops[0] instanceof Op\Expr\New_) { - return MyOp::TYPE_FUNCCALL_ARRAY; - } - - if ($ops->ops[0] instanceof Op\Expr\ArrayDimFetch) { - $ret = Common::getTypeDefinition($ops->ops[0]); - - if ($ret === MyOp::TYPE_FUNCCALL_ARRAY) { - return MyOp::TYPE_FUNCCALL_ARRAY; - } - - return MyOp::TYPE_ARRAY; - } - - if ($ops->ops[0] instanceof Op\Expr\Array_) { - return MyOp::TYPE_ARRAY_EXPR; - } - } - - if (isset($ops->expr->ops[0])) { - if ($ops->expr->ops[0] instanceof Op\Expr\Array_) { - return MyOp::TYPE_ARRAY_EXPR; - } - } - - if (isset($ops->var->ops[0])) { - if ($ops->var->ops[0] instanceof Op\Expr\ArrayDimFetch) { - $ret = Common::getTypeDefinition($ops->var->ops[0]); - - if ($ret == MyOp::TYPE_FUNCCALL_ARRAY) { - return MyOp::TYPE_FUNCCALL_ARRAY; - } - - return MyOp::TYPE_ARRAY; - } - - if ($ops->var->ops[0] instanceof Op\Expr\FuncCall - || $ops->var->ops[0] instanceof Op\Expr\NsFuncCall - || $ops->var->ops[0] instanceof Op\Expr\MethodCall - || $ops->var->ops[0] instanceof Op\Expr\StaticCall - || $ops->var->ops[0] instanceof Op\Expr\New_) { - return MyOp::TYPE_FUNCCALL_ARRAY; - } - } - - if (isset($ops->var->original->name)) { - if ($ops->var->original->name instanceof Operand\Literal) { - return MyOp::TYPE_LITERAL; - } - } - - if (isset($ops->expr->original->name)) { // return - if ($ops->expr->original->name instanceof Operand\Literal) { - return MyOp::TYPE_LITERAL; - } - } - - if (isset($ops->original->name)) { - if ($ops->original->name instanceof Operand\Literal) { - return MyOp::TYPE_LITERAL; - } - } - - if ($ops instanceof Op\Expr\ArrayDimFetch) { - return MyOp::TYPE_ARRAY; - } - - if ($ops instanceof Operand\Literal) { - return MyOp::TYPE_LITERAL; - } - - return null; - } - - - public static function getTypeIsInstance($ops) - { - if (isset($ops->expr->ops[0])) { - if ($ops->expr->ops[0] instanceof Op\Expr\New_) { - return MyOp::TYPE_INSTANCE; - } - } - - return null; - } - - public static function getTypeDefinition($ops) - { - if (isset($ops->ops[0])) { - if ($ops->ops[0] instanceof Op\Expr\ConstFetch) { - return MyOp::TYPE_CONST; - } - - if ($ops->ops[0] instanceof Op\Expr\FuncCall - || $ops->ops[0] instanceof Op\Expr\NsFuncCall - || $ops->ops[0] instanceof Op\Expr\MethodCall - || $ops->ops[0] instanceof Op\Expr\StaticCall - || $ops->ops[0] instanceof Op\Expr\New_) { - return MyOp::TYPE_FUNCCALL_ARRAY; - } - - if ($ops->ops[0] instanceof Op\Expr\PropertyFetch) { - return MyOp::TYPE_PROPERTY; - } - - if ($ops->ops[0] instanceof Op\Expr\StaticPropertyFetch) { - return MyOp::TYPE_STATIC_PROPERTY; - } - - if ($ops->ops[0] instanceof Op\Expr\ArrayDimFetch) { - return Common::getTypeDefinition($ops->ops[0]); - } - } - - if (isset($ops->var->ops[0])) { - if ($ops->var->ops[0] instanceof Op\Expr\ArrayDimFetch) { - return Common::getTypeDefinition($ops->var->ops[0]); - } - - if ($ops->var->ops[0] instanceof Op\Expr\PropertyFetch) { - return MyOp::TYPE_PROPERTY; - } - - if ($ops->var->ops[0] instanceof Op\Expr\StaticPropertyFetch) { - return MyOp::TYPE_STATIC_PROPERTY; - } - - if ($ops->var->ops[0] instanceof Op\Expr\FuncCall - || $ops->var->ops[0] instanceof Op\Expr\NsFuncCall - || $ops->var->ops[0] instanceof Op\Expr\MethodCall - || $ops->var->ops[0] instanceof Op\Expr\StaticCall - || $ops->var->ops[0] instanceof Op\Expr\New_) { - return MyOp::TYPE_FUNCCALL_ARRAY; - } - } - - if (isset($ops->var->original->name)) { - if ($ops->var->original->name instanceof Operand\Literal) { - return MyOp::TYPE_VARIABLE; - } - } - - if (isset($ops->expr->original->name)) { // return - if ($ops->expr->original->name instanceof Operand\Literal) { - return MyOp::TYPE_LITERAL; - } - } - - if (isset($ops->original->name)) { - if ($ops->original instanceof Operand\Variable) { - return MyOp::TYPE_VARIABLE; - } - - if ($ops->original->name instanceof Operand\Literal) { - return MyOp::TYPE_LITERAL; - } - } - - if ($ops instanceof Operand\Literal) { - return MyOp::TYPE_LITERAL; - } - - if ($ops instanceof Op\Expr\PropertyFetch) { - return MyOp::TYPE_PROPERTY; - } - - if ($ops instanceof Op\Expr\StaticPropertyFetch) { - return MyOp::TYPE_STATIC_PROPERTY; - } - - if ($ops instanceof Op\Iterator\Value) { - return MyOp::TYPE_VARIABLE; - } - - return null; - } } diff --git a/package/src/progpilot/Transformations/Php/Expr.php b/package/src/progpilot/Transformations/Php/Expr.php index b87aacb4..121e302f 100644 --- a/package/src/progpilot/Transformations/Php/Expr.php +++ b/package/src/progpilot/Transformations/Php/Expr.php @@ -15,260 +15,40 @@ use progpilot\Objects\MyFunction; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyOp; use progpilot\Code\MyInstruction; use progpilot\Code\Opcodes; use progpilot\Transformations\Php\Transform; +use progpilot\Transformations\Php\Exprs\PropertyFetch; +use progpilot\Transformations\Php\Exprs\StaticPropertyFetch; +use progpilot\Transformations\Php\Exprs\DimFetch; +use progpilot\Transformations\Php\Exprs\VariableFetch; +use progpilot\Transformations\Php\Exprs\FunccallFetch; +use progpilot\Transformations\Php\Exprs\ConcatFetch; +use progpilot\Transformations\Php\Exprs\ArrayFetch; +use progpilot\Transformations\Php\Exprs\CastFetch; +use progpilot\Transformations\Php\Exprs\ConstFetch; +use progpilot\Transformations\Php\Exprs\LiteralFetch; class Expr { - public static function setChars($myExpr, $myTemp, $string, $arrayChars) + public static function explicitfetch($context, $op, $expr) { - if (is_string($string)) { - $nbChars = []; - foreach ($arrayChars as $char) { - $nbChars[$char] = 0; - } - - for ($i = 0; $i < strlen($string); $i++) { - foreach ($arrayChars as $char) { - if ($string[$i] === $char) { - $nbChars[(string) $char] ++; - } - } - } - - foreach ($arrayChars as $char) { - $myExpr->setNbChars($char, $myExpr->getNbChars($char) + $nbChars[$char]); - $myTemp->setIsEmbeddedByChar($char, $myExpr->getNbChars($char)); - } - } + CastFetch::castFetch($context, $op, $expr); + ConcatFetch::concatFetch($context, $op, $expr); + DimFetch::dimFetch($context, $op); + PropertyFetch::propertyFetch($context, $op); + StaticPropertyFetch::staticPropertyFetch($context, $op); + FunccallFetch::funccallFetch($context, $op); + VariableFetch::variableFetch($context, $op, $expr); + ArrayFetch::arrayFetch($context, $op); + ConstFetch::constFetch($context, $op); } - public static function setCharsDefsOfMyExpr( - &$defsOfExpr, - $myExpr, - $arrayChars - ) { - $nbChars = []; - - foreach ($defsOfExpr as $oneDef) { - if ($oneDef->getIsEmbeddedByChar("<") > - $oneDef->getIsEmbeddedByChar(">")) { - $oneDef->setIsEmbeddedByChar("<", true); - } else { - $oneDef->setIsEmbeddedByChar("<", false); - } - - if ((($oneDef->getIsEmbeddedByChar("'") % 2) === 1) - && $myExpr->getNbChars("'") > $oneDef->getIsEmbeddedByChar("'")) { - $oneDef->setIsEmbeddedByChar("'", true); - } else { - $oneDef->setIsEmbeddedByChar("'", false); - } - } - } - - public static function instruction($op, $context, $myExpr, $cast = MyDefinition::CAST_NOT_SAFE) + public static function implicitfetch($context, $op, $expr) { - $defsOfExpr = []; - $ret = Expr::instructionInternal($defsOfExpr, $op, $context, $myExpr, $cast); - Expr::setCharsDefsOfMyExpr($defsOfExpr, $myExpr, ["'", "<", ">"]); - - return $ret; - } - - public static function instructionInternal( - &$defsOfExpr, - $op, - $context, - $myExpr, - $cast = MyDefinition::CAST_NOT_SAFE, - $phi = false - ) { - $myTempDef = null; - $arrFuncCall = false; - $name = Common::getNameDefinition($op); - $type = Common::getTypeDefinition($op); - $typeArray = Common::getTypeIsArray($op); - - if ($op instanceof Op\Phi) { - $instTemporarySimple = new MyInstruction(Opcodes::TEMPORARY); - $instTemporarySimple->addProperty(MyInstruction::PHI, count($op->vars)); - - $nbvars = 0; - foreach ($op->vars as $var) { - $myTemp = Expr::instructionInternal($defsOfExpr, $var, $context, $myExpr, $cast, true); - - if (!is_null($myTemp)) { - $instTemporarySimple->addProperty("temp_".$nbvars, $myTemp); - $nbvars ++; - } - } - - $instTemporarySimple->addProperty(MyInstruction::PHI, $nbvars); - $context->getCurrentMycode()->addCode($instTemporarySimple); - // end of expression - } elseif (!is_null($type) && $type !== MyOp::TYPE_FUNCCALL_ARRAY) { - if (is_null($name)) { - $name = mt_rand(); - } - - $arr = BuildArrays::buildArrayFromOps($op, false); - - $column = $context->getCurrentColumn(); - if ($op instanceof Op\Expr\Assign) { - $column = $op->getAttribute("startFilePos", -1); - } - - $myTemp = new MyDefinition($context->getCurrentLine(), $column, $name); - if ($type === MyOp::TYPE_CONST || $type === MyOp::TYPE_LITERAL) { - $myTemp->addLastKnownValue($name); - } - $myTemp->setCast($cast); - - Expr::setChars($myExpr, $myTemp, $name, ["'", "<", ">"]); - - if ($arr != false) { - $myTemp->addType(MyDefinition::TYPE_ARRAY); - $myTemp->setArrayValue($arr); - } - - $myTemp->setExpr($myExpr); - $defsOfExpr[] = $myTemp; - - if ($type === MyOp::TYPE_CONST) { - $myTemp->addType(MyDefinition::TYPE_CONSTANTE); - } - - if ($type === MyOp::TYPE_PROPERTY) { - $propertyName = ""; - if (isset($op->ops[0])) { - $propertyName = Common::getNameProperty($op->ops[0]); - } - - $myTemp->addType(MyDefinition::TYPE_PROPERTY); - $myTemp->property->setProperties($propertyName); - } - - if ($type === MyOp::TYPE_STATIC_PROPERTY) { - $propertyName = ""; - if (isset($op->ops[0])) { - $propertyName = Common::getNameProperty($op->ops[0]); - } - - $myTemp->addType(MyDefinition::TYPE_STATIC_PROPERTY); - $myTemp->property->setProperties($propertyName); - } - - if (!$phi) { - $instTemporarySimple = new MyInstruction(Opcodes::TEMPORARY); - $instTemporarySimple->addProperty(MyInstruction::TEMPORARY, $myTemp); - $context->getCurrentMycode()->addCode($instTemporarySimple); - } - - return $myTemp; - } elseif ($type === MyOp::TYPE_FUNCCALL_ARRAY) { - // func()[0][1] - $arrFuncCall = BuildArrays::buildArrayFromOps($op, false); - $startOps = BuildArrays::functionStartOps($op); - $op = $startOps; - } - - if (isset($op->ops)) { - foreach ($op->ops as $ops) { - if ($ops instanceof Op\Expr\Cast\Int_ - || $ops instanceof Op\Expr\Cast\Array_ - || $ops instanceof Op\Expr\Cast\Bool_ - || $ops instanceof Op\Expr\Cast\Double_ - || $ops instanceof Op\Expr\Cast\Object_) { - Expr::instructionInternal( - $defsOfExpr, - $ops->expr, - $context, - $myExpr, - MyDefinition::CAST_SAFE, - $phi - ); - } elseif ($ops instanceof Op\Expr\Cast\String_) { - Expr::instructionInternal( - $defsOfExpr, - $ops->expr, - $context, - $myExpr, - MyDefinition::CAST_NOT_SAFE, - $phi - ); - } elseif ($ops instanceof Op\Expr\BinaryOp\Concat) { - $myExpr->setIsConcat(true); - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::CONCAT_LEFT)); - Expr::instructionInternal($defsOfExpr, $ops->left, $context, $myExpr, $cast, $phi); - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::CONCAT_RIGHT)); - Expr::instructionInternal($defsOfExpr, $ops->right, $context, $myExpr, $cast, $phi); - } elseif ($ops instanceof Op\Expr\ConcatList) { - $myExpr->setIsConcat(true); - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::CONCAT_LIST)); - - foreach ($ops->list as $opsbis) { - Expr::instructionInternal($defsOfExpr, $opsbis, $context, $myExpr, $cast, $phi); - } - } elseif ($ops instanceof Op\Expr\Include_) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - FuncCall::instruction($context, $myExpr, $arrFuncCall, false, false, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Expr\Print_) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - FuncCall::instruction($context, $myExpr, $arrFuncCall, false, false, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Terminal\Echo_) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - FuncCall::instruction($context, $myExpr, $arrFuncCall, false, false, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Expr\Eval_) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - FuncCall::instruction($context, $myExpr, $arrFuncCall, false, false, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Expr\Exit_) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - FuncCall::instruction($context, $myExpr, $arrFuncCall, false, false, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Expr\FuncCall || $ops instanceof Op\Expr\NsFuncCall) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - $myTempDef = FuncCall::instruction($context, $myExpr, $arrFuncCall, false, false, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Expr\MethodCall) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - $myTempDef = FuncCall::instruction($context, $myExpr, $arrFuncCall, true, false, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Expr\StaticCall) { - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - $myTempDef = FuncCall::instruction($context, $myExpr, $arrFuncCall, false, true, $cast); - $context->setCurrentOp($oldOp); - } elseif ($ops instanceof Op\Expr\New_) { - // funccall for the constructor - $oldOp = $context->getCurrentOp(); - $context->setCurrentOp($ops); - $myTempDef = FuncCall::instruction($context, $myExpr, $arrFuncCall, false, false, $cast); - $context->setCurrentOp($oldOp); - } else { - $myTempDef = Expr::instructionInternal($defsOfExpr, $ops, $context, $myExpr, $cast, $phi); - } - } - } - - return $myTempDef; + VariableFetch::variableFetch($context, $op, $expr); + LiteralFetch::literalFetch($context, $op); } } diff --git a/package/src/progpilot/Transformations/Php/Exprs/ArrayFetch.php b/package/src/progpilot/Transformations/Php/Exprs/ArrayFetch.php new file mode 100644 index 00000000..4277d0a9 --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/ArrayFetch.php @@ -0,0 +1,65 @@ +values) && !empty($op->values)) { + $instVariableFetch = new MyInstruction(Opcodes::ARRAY_EXPR); + $nbArrayExpr = 0; + foreach ($op->values as $value) { + if (isset($op->keys[$nbArrayExpr])) { + $instVariableFetch->addProperty( + "key".$nbArrayExpr, + $context->getCurrentFunc()->getOpId($op->keys[$nbArrayExpr]) + ); + + Expr::implicitfetch($context, $op->keys[$nbArrayExpr], ""); + } + + Expr::implicitfetch($context, $value, ""); + $instVariableFetch->addProperty( + "value".$nbArrayExpr, + $context->getCurrentFunc()->getOpId($value) + ); + + $nbArrayExpr ++; + } + + + $instVariableFetch->addProperty( + MyInstruction::RESULTID, + $context->getCurrentFunc()->getOpId($op->result) + ); + + $instVariableFetch->addProperty( + "nbkeys", + $nbArrayExpr + ); + + $context->getCurrentMycode()->addCode($instVariableFetch); + } + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/CastFetch.php b/package/src/progpilot/Transformations/Php/Exprs/CastFetch.php new file mode 100644 index 00000000..dd231e1f --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/CastFetch.php @@ -0,0 +1,46 @@ +expr, $expr); + $instDefChained = new MyInstruction(Opcodes::CAST); + $instDefChained->addProperty("type_cast", MyDefinition::CAST_SAFE); + $instDefChained->addProperty(MyInstruction::EXPRID, $context->getCurrentFunc()->getOpId($op->expr)); + $instDefChained->addProperty(MyInstruction::RESULTID, $context->getCurrentFunc()->getOpId($op->result)); + $context->getCurrentMycode()->addCode($instDefChained); + } elseif ($op instanceof Op\Expr\Cast\String_) { + Expr::implicitfetch($context, $op->expr, $expr); + $instDefChained = new MyInstruction(Opcodes::CAST); + $instDefChained->addProperty("type_cast", MyDefinition::CAST_NOT_SAFE); + $instDefChained->addProperty(MyInstruction::EXPRID, $context->getCurrentFunc()->getOpId($op->expr)); + $instDefChained->addProperty(MyInstruction::RESULTID, $context->getCurrentFunc()->getOpId($op->result)); + $context->getCurrentMycode()->addCode($instDefChained); + } + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/ConcatFetch.php b/package/src/progpilot/Transformations/Php/Exprs/ConcatFetch.php new file mode 100644 index 00000000..cec2f9b7 --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/ConcatFetch.php @@ -0,0 +1,50 @@ +left, $expr); + $instDefChained->addProperty(MyInstruction::LEFTID, $context->getCurrentFunc()->getOpId($op->left)); + Expr::implicitfetch($context, $op->right, $expr); + $opIds = []; + $opIds[] = $context->getCurrentFunc()->getOpId($op->right); + $instDefChained->addProperty(MyInstruction::RIGHTID, $opIds); + $instDefChained->addProperty(MyInstruction::RESULTID, $context->getCurrentFunc()->getOpId($op->result)); + + $context->getCurrentMycode()->addCode($instDefChained); + } elseif ($op instanceof Op\Expr\ConcatList) { + $instDefChained = new MyInstruction(Opcodes::CONCAT_LEFT); + + $opIds = []; + foreach ($op->list as $opsbis) { + $opIds[] = $context->getCurrentFunc()->getOpId($opsbis); + Expr::implicitfetch($context, $opsbis, $expr); + } + $instDefChained->addProperty(MyInstruction::RIGHTID, $opIds); + $instDefChained->addProperty(MyInstruction::RESULTID, $context->getCurrentFunc()->getOpId($op->result)); + $context->getCurrentMycode()->addCode($instDefChained); + } + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/ConstFetch.php b/package/src/progpilot/Transformations/Php/Exprs/ConstFetch.php new file mode 100644 index 00000000..bda1c3a6 --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/ConstFetch.php @@ -0,0 +1,48 @@ +name) + && $op->name instanceof Operand\Literal) { + $myTemp = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + $op->name->value + ); + $myTemp->getCurrentState()->addType(MyDefinition::TYPE_CONSTANTE); + if ($op->name->value === "DIRECTORY_SEPARATOR") { + $myTemp->getCurrentState()->addLastKnownValue("/"); + } else { + $myTemp->getCurrentState()->addLastKnownValue($op->name->value); + } + + $instDefChained = new MyInstruction(Opcodes::CONST_FETCH); + $instDefChained->addProperty(MyInstruction::DEF, $myTemp); + $instDefChained->addProperty(MyInstruction::RESULTID, $context->getCurrentFunc()->getOpId($op->result)); + $context->getCurrentMycode()->addCode($instDefChained); + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/DimFetch.php b/package/src/progpilot/Transformations/Php/Exprs/DimFetch.php new file mode 100644 index 00000000..36285204 --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/DimFetch.php @@ -0,0 +1,67 @@ +dim->value)) { + $instDefChained->addProperty(MyInstruction::ARRAY_DIM, $op->dim->value); + } else { + // null when for pushing elements $arr[] = $ele; + $instDefChained->addProperty(MyInstruction::ARRAY_DIM, 0); + } + + if (isset($op->result)) { + $instDefChained->addProperty( + MyInstruction::RESULTID, + $context->getCurrentFunc()->getOpId($op->result) + ); + } + + if (isset($op->var)) { + $instDefChained->addProperty( + MyInstruction::VARID, + $context->getCurrentFunc()->getOpId($op->var) + ); + } + + // beginning of the chain + if (isset($op->var->original)) { + $originalDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + $op->var->original->name->value + ); + + $instDefChained->addProperty(MyInstruction::ORIGINAL_DEF, $originalDef); + + if ($originalDef->getName() === "GLOBALS") { + $context->getCurrentFunc()->setHasGlobalVariables(true); + } + } + + $context->getCurrentMycode()->addCode($instDefChained); + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/FunccallFetch.php b/package/src/progpilot/Transformations/Php/Exprs/FunccallFetch.php new file mode 100644 index 00000000..4ac201c4 --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/FunccallFetch.php @@ -0,0 +1,43 @@ +getCurrentOp(); + $context->setCurrentOp($op); + FuncCall::instruction($context); + $context->setCurrentOp($oldOp); + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/LiteralFetch.php b/package/src/progpilot/Transformations/Php/Exprs/LiteralFetch.php new file mode 100644 index 00000000..e2d22025 --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/LiteralFetch.php @@ -0,0 +1,43 @@ +getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + "literal" + ); + + $myTemp->getCurrentState()->addType(MyDefinition::TYPE_LITERAL); + $myTemp->getCurrentState()->addLastKnownValue($op->value); + + $instDefChained = new MyInstruction(Opcodes::LITERAL_FETCH); + $instDefChained->addProperty(MyInstruction::DEF, $myTemp); + $instDefChained->addProperty(MyInstruction::RESULTID, $context->getCurrentFunc()->getOpId($op)); + $context->getCurrentMycode()->addCode($instDefChained); + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/PropertyFetch.php b/package/src/progpilot/Transformations/Php/Exprs/PropertyFetch.php new file mode 100644 index 00000000..815284bd --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/PropertyFetch.php @@ -0,0 +1,71 @@ +name->value)) { + $instDefChained = new MyInstruction(Opcodes::PROPERTY_FETCH); + $instDefChained->addProperty(MyInstruction::PROPERTY_NAME, $op->name->value); + + // beginning of the chain + if (isset($op->var->original)) { + $originalDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + $op->var->original->name->value + ); + $originalDef->addType(MyDefinition::TYPE_PROPERTY); + + $instDefChained->addProperty(MyInstruction::ORIGINAL_DEF, $originalDef); + } + + if (isset($op->var) && $op->var instanceof Operand\BoundVariable) { + $originalDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + "this" + ); + $instDefChained->addProperty(MyInstruction::ORIGINAL_DEF, $originalDef); + } + + if (isset($op->result)) { + $instDefChained->addProperty( + MyInstruction::RESULTID, + $context->getCurrentFunc()->getOpId($op->result) + ); + } + if (isset($op->var)) { + $instDefChained->addProperty( + MyInstruction::VARID, + $context->getCurrentFunc()->getOpId($op->var) + ); + } + + $context->getCurrentMycode()->addCode($instDefChained); + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/StaticPropertyFetch.php b/package/src/progpilot/Transformations/Php/Exprs/StaticPropertyFetch.php new file mode 100644 index 00000000..ac64aa9a --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/StaticPropertyFetch.php @@ -0,0 +1,70 @@ +addProperty(MyInstruction::PROPERTY_NAME, $op->name->value); + + // static property + if (isset($op->class->value)) { + $originalDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + $op->class->value + ); + $originalDef->addType(MyDefinition::TYPE_STATIC_PROPERTY); + + $instDefChained->addProperty(MyInstruction::ORIGINAL_DEF, $originalDef); + } + + if (isset($op->var) && $op->var instanceof Operand\BoundVariable) { + $originalDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + "this" + ); + $instDefChained->addProperty(MyInstruction::ORIGINAL_DEF, $originalDef); + } + + if (isset($op->result)) { + $instDefChained->addProperty( + MyInstruction::RESULTID, + $context->getCurrentFunc()->getOpId($op->result) + ); + } + if (isset($op->var)) { + $instDefChained->addProperty( + MyInstruction::VARID, + $context->getCurrentFunc()->getOpId($op->var) + ); + } + + $context->getCurrentMycode()->addCode($instDefChained); + } + } +} diff --git a/package/src/progpilot/Transformations/Php/Exprs/VariableFetch.php b/package/src/progpilot/Transformations/Php/Exprs/VariableFetch.php new file mode 100644 index 00000000..7ec92522 --- /dev/null +++ b/package/src/progpilot/Transformations/Php/Exprs/VariableFetch.php @@ -0,0 +1,47 @@ +original) && $op->original instanceof Operand\Variable + && isset($op->original->name) && $op->original->name instanceof Operand\Literal) { + $myTemp = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + $op->original->name->value + ); + + $instVariableFetch = new MyInstruction(Opcodes::VARIABLE_FETCH); + + $property = is_null($expr) ? MyInstruction::VARID : MyInstruction::EXPRID; + + $instVariableFetch->addProperty( + $property, + $context->getCurrentFunc()->getOpId($op) + ); + + $instVariableFetch->addProperty(MyInstruction::DEF, $myTemp); + $context->getCurrentMycode()->addCode($instVariableFetch); + } + } +} diff --git a/package/src/progpilot/Transformations/Php/FuncCall.php b/package/src/progpilot/Transformations/Php/FuncCall.php index ef2bcc16..1a570a1c 100644 --- a/package/src/progpilot/Transformations/Php/FuncCall.php +++ b/package/src/progpilot/Transformations/Php/FuncCall.php @@ -15,19 +15,28 @@ use progpilot\Objects\MyFunction; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; use progpilot\Objects\MyOp; use progpilot\Code\MyInstruction; use progpilot\Code\Opcodes; use progpilot\Transformations\Php\Transform; use progpilot\Transformations\Php\Expr; +use progpilot\Transformations\Php\Exprs\PropertyFetch; +use progpilot\Transformations\Php\Exprs\DimFetch; +use progpilot\Transformations\Php\Exprs\VariableFetch; class FuncCall { public static function argument($context, $arg, $instFuncCallMain, $funcCallName, $numParam) { - // each argument will be a definition defined by an expression + Expr::implicitfetch($context, $arg, null); + + $instVariable = $context->getCurrentMycode()->getLastCode(); + if ($instVariable->getOpcode() === Opcodes::VARIABLE_FETCH) { + $defArg = $instVariable->getProperty(MyInstruction::DEF); + $instFuncCallMain->addProperty("argoriginaldef$numParam", $defArg); + } + $defName = $funcCallName. "_param". @@ -37,92 +46,54 @@ public static function argument($context, $arg, $instFuncCallMain, $funcCallName "_column". $context->getCurrentColumn(). "_progpilot"; - $typeArray = Common::getTypeIsArray($arg); - - $myDef = new MyDefinition($context->getCurrentLine(), $context->getCurrentColumn(), $defName); - - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_ASSIGN)); - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - $myExprparam = new MyExpr($context->getCurrentLine(), $context->getCurrentColumn()); - $myExprparam->setAssign(true); - $myExprparam->setAssignDef($myDef); - - $instFuncCallMain->addProperty("argdef$numParam", $myDef); - $instFuncCallMain->addProperty("argexpr$numParam", $myExprparam); - // if we have funccall($arg, array("test"=>false)); for example - if ($typeArray === MyOp::TYPE_ARRAY_EXPR) { - ArrayExpr::instruction($arg, $context, false, $defName, false); + $myDef = new MyDefinition( + $context->getCurrentBlock()->getId(), + $context->getCurrentMyFile(), + $context->getCurrentLine(), + $context->getCurrentColumn(), + $defName + ); - $myTemp = new MyDefinition($context->getCurrentLine(), $context->getCurrentColumn(), $defName); - //$myTemp->addLastKnownValue($defName); - $myTemp->setExpr($myExprparam); - $instTemporarySimple = new MyInstruction(Opcodes::TEMPORARY); - $instTemporarySimple->addProperty(MyInstruction::TEMPORARY, $myTemp); - $context->getCurrentMycode()->addCode($instTemporarySimple); - } else { - $myTemp = Expr::instruction($arg, $context, $myExprparam); - - if (!is_null($myTemp)) { - $myDef->setValueFromDef($myTemp); - } - } + $instArg = new MyInstruction(Opcodes::ARGUMENT); + $instArg->addProperty(MyInstruction::VARID, $context->getCurrentFunc()->getOpId($arg)); + $context->getCurrentMycode()->addCode($instArg); - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExprparam); - $context->getCurrentMycode()->addCode($instEndExpr); + $instArg->addProperty("argdef$numParam", $myDef); + $instArg->addProperty("idparam", $numParam); - $context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::END_ASSIGN)); - - $instDef = new MyInstruction(Opcodes::DEFINITION); - $instDef->addProperty(MyInstruction::DEF, $myDef); - $context->getCurrentMycode()->addCode($instDef); - - unset($myExprparam); - unset($myDef); + $instFuncCallMain->addProperty("argdef$numParam", $myDef); } /* arg2 : expr for the return arg3 : arr for the return : function_call()[0] (arr = [0]) */ - public static function instruction( - $context, - $myExpr, - $funcCallArr, - $isMethod = false, - $isStatic = false, - $cast = MyDefinition::CAST_NOT_SAFE - ) { - $mybackdef = null; + public static function instruction($context) + { + $isMethod = false; $nbparams = 0; - $propertyName = ""; $className = ""; // instance_name = new obj; instance_name->method_name() - if ($isMethod) { + + if ($context->getCurrentOp() instanceof Op\Expr\MethodCall) { + $isMethod = true; $instanceName = Common::getNameDefinition($context->getCurrentOp()->var); - if (isset($context->getCurrentOp()->var->ops[0])) { - $propertyName = Common::getNameProperty($context->getCurrentOp()->var->ops[0]); - } } $funcCallName = ""; if ($context->getCurrentOp() instanceof Op\Expr\New_) { $funcCallName = "__construct"; - // we have the class name if (isset($context->getCurrentOp()->class->value)) { $className = $context->getCurrentOp()->class->value; } - $isMethod = true; - - $instanceName = Common::getNameDefinition($context->getCurrentOp()->result->usages[0]); - if (isset($context->getCurrentOp()->result->usages[0]->var->ops[0])) { - $propertyName = Common::getNameProperty($context->getCurrentOp()->result->usages[0]->var->ops[0]); + if (isset($context->getCurrentOp()->result->usages[0])) { + $isMethod = true; + $instanceName = Common::getNameDefinition($context->getCurrentOp()->result->usages[0]); } } elseif (isset($context->getCurrentOp()->nsName->value)) { $funcCallName = $context->getCurrentOp()->nsName->value; @@ -131,7 +102,7 @@ public static function instruction( } if ($funcCallName === "define") { - Assign::instruction($context, false, true); + Assign::instruction($context, $context->getCurrentOp(), null, null, false, true); } if ($context->getCurrentOp() instanceof Op\Expr\Include_) { @@ -142,20 +113,39 @@ public static function instruction( $funcCallName = "echo"; } elseif ($context->getCurrentOp() instanceof Op\Expr\Eval_) { $funcCallName = "eval"; - } elseif ($context->getCurrentOp() instanceof Op\Expr\Exit_) { + } elseif ($context->getCurrentOp() instanceof Op\Expr\Exit_ + || $context->getCurrentOp() instanceof Op\Terminal\Exit_) { $funcCallName = "exit"; } $instFuncCallMain = new MyInstruction(Opcodes::FUNC_CALL); + + if (isset($context->getCurrentOp()->result)) { + $instFuncCallMain->addProperty( + MyInstruction::RESULTID, + $context->getCurrentFunc()->getOpId($context->getCurrentOp()->result) + ); + } + if (isset($context->getCurrentOp()->var)) { + $instFuncCallMain->addProperty( + MyInstruction::VARID, + $context->getCurrentFunc()->getOpId($context->getCurrentOp()->var) + ); + } elseif (isset($context->getCurrentOp()->class)) { + $instFuncCallMain->addProperty( + MyInstruction::VARID, + $context->getCurrentFunc()->getOpId($context->getCurrentOp()->class) + ); + } $instFuncCallMain->addProperty(MyInstruction::FUNCNAME, $funcCallName); $myFunctionCall = new MyFunction($funcCallName); - $myFunctionCall->setCastReturn($cast); $myFunctionCall->setLine($context->getCurrentLine()); $myFunctionCall->setColumn($context->getCurrentColumn()); + $myFunctionCall->setInstanceClassName($className); - - if ($isStatic && isset($context->getCurrentOp()->class->value)) { + if ($context->getCurrentOp() instanceof Op\Expr\StaticCall && + isset($context->getCurrentOp()->class->value)) { $nameClass = $context->getCurrentOp()->class->value; $myFunctionCall->addType(MyFunction::TYPE_FUNC_STATIC); $myFunctionCall->setNameInstance($nameClass); @@ -164,17 +154,6 @@ public static function instruction( if ($isMethod) { $myFunctionCall->addType(MyFunction::TYPE_FUNC_METHOD); $myFunctionCall->setNameInstance($instanceName); - - $mybackdef = new MyDefinition($context->getCurrentLine(), $context->getCurrentColumn(), $instanceName); - $mybackdef->addType(MyDefinition::TYPE_INSTANCE); - $mybackdef->setClassName($className); - - if ($propertyName !== "" && count($propertyName) > 0) { - $mybackdef->addType(MyDefinition::TYPE_PROPERTY); - $mybackdef->property->setProperties($propertyName); - } - - $myFunctionCall->setBackDef($mybackdef); } $listArgs = []; @@ -183,8 +162,11 @@ public static function instruction( || $context->getCurrentOp() instanceof Op\Expr\Print_ || $context->getCurrentOp() instanceof Op\Terminal\Echo_ || $context->getCurrentOp() instanceof Op\Expr\Eval_ - || $context->getCurrentOp() instanceof Op\Expr\Exit_) { - $listArgs[] = $context->getCurrentOp()->expr; + || $context->getCurrentOp() instanceof Op\Expr\Exit_ + || $context->getCurrentOp() instanceof Op\Terminal\Exit_) { + if (isset($context->getCurrentOp()->expr)) { + $listArgs[] = $context->getCurrentOp()->expr; + } if ($context->getCurrentOp() instanceof Op\Expr\Include_) { $instFuncCallMain->addProperty(MyInstruction::TYPE_INCLUDE, $context->getCurrentOp()->type); @@ -200,10 +182,6 @@ public static function instruction( $myFunctionCall->setNbParams($nbparams); $instFuncCallMain->addProperty(MyInstruction::MYFUNC_CALL, $myFunctionCall); - $instFuncCallMain->addProperty(MyInstruction::EXPR, $myExpr); - $instFuncCallMain->addProperty(MyInstruction::ARR, $funcCallArr); $context->getCurrentMycode()->addCode($instFuncCallMain); - - return $mybackdef; } } diff --git a/package/src/progpilot/Transformations/Php/Transform.php b/package/src/progpilot/Transformations/Php/Transform.php index 2d8b30a3..8e52a55a 100644 --- a/package/src/progpilot/Transformations/Php/Transform.php +++ b/package/src/progpilot/Transformations/Php/Transform.php @@ -15,12 +15,12 @@ use PHPCfg\Op; use PHPCfg\Script; use PHPCfg\Visitor; -use PHPCfg\Printer; +use PHPCfg\Operand; use progpilot\Objects\MyFunction; use progpilot\Objects\MyBlock; use progpilot\Objects\MyDefinition; -use progpilot\Objects\MyExpr; +use progpilot\Objects\MyProperty; use progpilot\Objects\MyClass; use progpilot\Objects\MyOp; use progpilot\Objects\MyFile; @@ -28,23 +28,37 @@ use progpilot\Code\MyInstruction; use progpilot\Code\Opcodes; -use progpilot\Transformations\Php\FuncCall; use progpilot\Transformations\Php\Expr; use progpilot\Transformations\Php\Assign; use progpilot\Transformations\Php\Common; use progpilot\Transformations\Php\Context; +use function DeepCopy\deep_copy; + class Transform implements Visitor { + private $blocksStack; private $sBlocks; private $context; private $blockIfToBeResolved; private $insideInclude; + private $varIds; + + protected function getVarId(Operand $var) + { + if (isset($this->varIds[$var])) { + return $this->varIds[$var]; + } + + return $this->varIds[$var] = $this->varIds->count() + 1; + } public function __construct() { + $this->varIds = new \SplObjectStorage; $this->sBlocks = new \SplObjectStorage; $this->blockIfToBeResolved = []; + $this->blocksStack = []; } public function setContext($context) @@ -57,32 +71,76 @@ public function enterScript(Script $script) $this->context->outputs->currentIncludesFile = []; } - public function leaveScript(Script $script) + public function checkIsALoop($block, $blockToLook) { - // creating edges for myblocks structure as block structure - foreach ($this->sBlocks as $block) { - $myBlock = $this->sBlocks[$block]; - foreach ($block->parents as $block_parent) { - if ($this->sBlocks->contains($block_parent)) { - $myBlockParent = $this->sBlocks[$block_parent]; - $myBlock->addParent($myBlockParent); + $allBlocks = []; + $allBlocks[] = $block; + $parentsBlocks = []; + $parentsBlocks[] = $block; + $myBlockIf = $this->sBlocks[$blockToLook]; + + while (!empty($parentsBlocks)) { + $blockP = array_pop($parentsBlocks); + if ($this->sBlocks->contains($blockP)) { + $myBlockP = $this->sBlocks[$blockP]; + foreach ($blockP->parents as $block_parent) { + if ($block_parent !== $blockP) { + if ($block_parent === $blockToLook) { + $myBlockP->addLoopParent($myBlockIf); + return true; + } + + if (!in_array($block_parent, $parentsBlocks, true) + && !in_array($block_parent, $allBlocks, true)) { + $parentsBlocks[] = $block_parent; + $allBlocks[] = $block_parent; + } + } } } } + return false; + } + + public function leaveScript(Script $script) + { foreach ($this->blockIfToBeResolved as $blockResolved) { $instructionIf = $blockResolved[0]; - $blockIf = $blockResolved[1]; - $blockElse = $blockResolved[2]; + $blockInit = $blockResolved[1]; + $blockIf = $blockResolved[2]; + $blockElse = $blockResolved[3]; - if ($this->sBlocks->contains($blockIf) && $this->sBlocks->contains($blockElse)) { + if ($this->sBlocks->contains($blockIf) + && $this->sBlocks->contains($blockElse) + && $this->sBlocks->contains($blockInit)) { $myBlockIf = $this->sBlocks[$blockIf]; $myBlockElse = $this->sBlocks[$blockElse]; + $myBlockInit = $this->sBlocks[$blockInit]; + + if ($this->checkIsALoop($blockInit, $blockIf)) { + $myBlockElse->addParent($myBlockIf); + $myBlockElse->addVirtualParent($myBlockIf); + } $instructionIf->addProperty(MyInstruction::MYBLOCK_IF, $myBlockIf); $instructionIf->addProperty(MyInstruction::MYBLOCK_ELSE, $myBlockElse); } } + + // creating edges for myblocks structure as block structure + foreach ($this->sBlocks as $block) { + $myBlock = $this->sBlocks[$block]; + foreach ($block->parents as $block_parent) { + if ($this->sBlocks->contains($block_parent)) { + $myBlockParent = $this->sBlocks[$block_parent]; + $myBlock->addParent($myBlockParent); + $myBlock->addVirtualParent($myBlockParent); + + $myBlockParent->addChild($myBlock); + } + } + } // extends class foreach ($this->context->getClasses()->getListClasses() as $myClass) { @@ -90,7 +148,9 @@ public function leaveScript(Script $script) $myClassFather = $this->context->getClasses()->getMyClass($myClass->getExtendsOf()); if (!is_null($myClassFather)) { foreach ($myClassFather->getMethods() as $methodFather) { - $myClass->addMethod(clone $methodFather); + $cloneMethodFather = $methodFather; + + $myClass->addMethod($cloneMethodFather); $methodFather->setMyClass($myClass); } @@ -100,20 +160,17 @@ public function leaveScript(Script $script) } } } - - /* - $printer = new Printer\Text(); - var_dump($printer->printScript($script)); - */ } public function enterBlock(Block $block, Block $prior = null) { $this->insideInclude = false; if (!($this->context->getCurrentOp() instanceof Op\Expr\Include_)) { - $myBlock = new MyBlock; + $myBlock = new MyBlock($this->context->getCurrentLine(), $this->context->getCurrentColumn()); $myBlock->setStartAddressBlock(count($this->context->getCurrentMycode()->getCodes())); - $this->context->setCurrentBlock($block); + $this->context->setCurrentBlock($myBlock); + + array_push($this->blocksStack, $myBlock); $this->sBlocks[$block] = $myBlock; @@ -124,7 +181,13 @@ public function enterBlock(Block $block, Block $prior = null) // block is for himself a parent block (handle dataflow for first block) $block->addParent($block); - $myBlock->setId(rand()); + // params are part of the first block + foreach ($this->context->getCurrentFunc()->getParams() as $param) { + $instDef = new MyInstruction(Opcodes::DEFINITION); + $instDef->addProperty(MyInstruction::DEF, $param); + $this->context->getCurrentMycode()->addCode($instDef); + } + // end } else { $this->insideInclude = true; } @@ -150,15 +213,18 @@ public function leaveBlock(Block $block, Block $prior = null) $instBlock = new MyInstruction(Opcodes::LEAVE_BLOCK); $instBlock->addProperty(MyInstruction::MYBLOCK, $myBlock); $this->context->getCurrentMycode()->addCode($instBlock); + + array_pop($this->blocksStack); + + if (!empty($this->blocksStack)) { + $this->context->setCurrentBlock($this->blocksStack[count($this->blocksStack) - 1]); + } } } } public function enterFunc(Func $func) { - // blocks are set back to zero when entering new function - $this->context->setCurrentBlock(null); - $myFunction = new MyFunction($func->name); $this->context->setCurrentMycode($myFunction->getMyCode()); @@ -182,9 +248,17 @@ public function enterFunc(Func $func) $myFunction->addType(MyFunction::TYPE_FUNC_STATIC); } - $mythisdef = new MyDefinition(0, 0, "this"); - $mythisdef->setBlockId(0); + $mythisdef = new MyDefinition( + 0, + $this->context->getCurrentMyFile(), + 0, + 0, + "this" + ); + $mythisdef->addType(MyDefinition::TYPE_INSTANCE); + $mythisdef->getCurrentState()->addType(MyDefinition::TYPE_INSTANCE); + $myFunction->setThisDef($mythisdef); } } @@ -193,23 +267,21 @@ public function enterFunc(Func $func) $paramName = $param->name->value; $byref = $param->byRef; - $myDef = new MyDefinition($param->getLine(), $param->getAttribute("startFilePos", -1), $paramName); + $myDef = new MyDefinition( + 0, + $this->context->getCurrentMyFile(), + $param->getLine(), + $param->getAttribute("startFilePos", -1), + $paramName + ); if ($byref) { $myDef->addType(MyDefinition::TYPE_REFERENCE); } $myFunction->addParam($myDef); - - $instDef = new MyInstruction(Opcodes::DEFINITION); - $instDef->addProperty(MyInstruction::DEF, $myDef); - $this->context->getCurrentMycode()->addCode($instDef); - - unset($myDef); } - // because when we call (funccall) a function by name, it can be undefined - $this->context->getFunctions()->addFunction($myFunction->getName(), $myFunction); $this->context->setCurrentFunc($myFunction); } @@ -221,7 +293,8 @@ public function leaveFunc(Func $func) // we can have a block opened and we need to leave it if ($inst->getOpcode() !== Opcodes::LEAVE_BLOCK) { - if (!is_null($this->context->getCurrentBlock())) { + if (!is_null($this->context->getCurrentBlock()) + && $this->sBlocks->contains($this->context->getCurrentBlock())) { $myBlock = $this->sBlocks[$this->context->getCurrentBlock()]; $myBlock->setEndAddressBlock(count($this->context->getCurrentMycode()->getCodes())); @@ -231,42 +304,25 @@ public function leaveFunc(Func $func) } } - $className = null; - if (!is_null($func->class)) { - $className = $func->class->value; - } - - $myFunction = $this->context->getFunctions()->getFunction($func->name, $className); - - if (!is_null($myFunction)) { - $myFunction->setLastLine($this->context->getCurrentLine()); - $myFunction->setLastColumn($this->context->getCurrentColumn()); - $myFunction->setLastBlockId(-1); + $myFunction = $this->context->getCurrentFunc(); + + $myFunction->setLastLine($this->context->getCurrentLine()); + $myFunction->setLastColumn($this->context->getCurrentColumn()); - $instFunc = new MyInstruction(Opcodes::LEAVE_FUNCTION); - $instFunc->addProperty(MyInstruction::MYFUNC, $myFunction); - $this->context->getCurrentMycode()->addCode($instFunc); - } - } + $instFunc = new MyInstruction(Opcodes::LEAVE_FUNCTION); + $instFunc->addProperty(MyInstruction::MYFUNC, $myFunction); + $this->context->getCurrentMycode()->addCode($instFunc); - public function parseconditions($instStartIf, $cond) - { - foreach ($cond as $ops) { - if ($ops instanceof Op\Expr\BooleanNot) { - $instStartIf->addProperty(MyInstruction::NOT_BOOLEAN, true); - $this->parseconditions($instStartIf, $ops->expr->ops); - } - } + $this->context->addTmpFunctions($myFunction); } public function enterOp(Op $op, Block $block) { $this->context->setCurrentOp($op); - $this->context->setCurrentBlock($block); // for theses objects getline et getcolumn methods exists except for assertion if ($op instanceof Op\Stmt || - ($op instanceof Op\Expr && !($op instanceof Op\Expr\Assertion)) || + ($op instanceof Op\Expr && !($op instanceof Op\Expr\Assertion)) || $op instanceof Op\Terminal) { if ($op->getLine() !== -1 && $op->getAttribute("startFilePos", -1) !== -1) { $this->context->setCurrentLine($op->getLine()); @@ -274,143 +330,67 @@ public function enterOp(Op $op, Block $block) } } - if ($op instanceof Op\Stmt\JumpIf) { + if ($op instanceof Op\Expr\BinaryOp + && !($op instanceof Op\Expr\BinaryOp\Concat)) { + $instBinary = new MyInstruction(Opcodes::BINARYOP); + $instBinary->addProperty(MyInstruction::LEFTID, $this->context->getCurrentFunc()->getOpId($op->left)); + $instBinary->addProperty(MyInstruction::RIGHTID, $this->context->getCurrentFunc()->getOpId($op->right)); + $instBinary->addProperty(MyInstruction::RESULTID, $this->context->getCurrentFunc()->getOpId($op->result)); + $this->context->getCurrentMycode()->addCode($instBinary); + } elseif ($op instanceof Op\Expr\BooleanNot) { + $instNotBoolean = new MyInstruction(Opcodes::COND_BOOLEAN_NOT); + $instNotBoolean->addProperty(MyInstruction::EXPRID, $this->context->getCurrentFunc()->getOpId($op->expr)); + $instNotBoolean->addProperty( + MyInstruction::RESULTID, + $this->context->getCurrentFunc()->getOpId($op->result) + ); + $this->context->getCurrentMycode()->addCode($instNotBoolean); + } elseif ($op instanceof Op\Stmt\JumpIf) { $instStartIf = new MyInstruction(Opcodes::COND_START_IF); + $instStartIf->addProperty(MyInstruction::EXPRID, $this->context->getCurrentFunc()->getOpId($op->cond)); $this->context->getCurrentMycode()->addCode($instStartIf); - $this->blockIfToBeResolved[] = [$instStartIf, $op->if, $op->else]; - - $myblock = $this->sBlocks[$block]; - $myblock->setIsLoop(true); - - $this->parseconditions($instStartIf, $op->cond->ops); - } - /* - const TYPE_INCLUDE = 1; - const TYPE_INCLUDE_OPNCE = 2; - const TYPE_REQUIRE = 3; - const TYPE_REQUIRE_ONCE = 4; - */ - - if ($op instanceof Op\Expr\Include_) { - if (Common::isFuncCallWithoutReturn($op)) { - // expr of type "assign" to have a defined return - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - FuncCall::instruction($this->context, $myExpr, false); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - } + $this->blockIfToBeResolved[] = [$instStartIf, $block, $op->if, $op->else]; + } elseif ($op instanceof Op\Iterator\Value) { + $instIterator = new MyInstruction(Opcodes::ITERATOR); + Expr::implicitfetch($this->context, $op->var, null); + $instIterator->addProperty(MyInstruction::VARID, $this->context->getCurrentFunc()->getOpId($op->var)); + $instIterator->addProperty(MyInstruction::RESULTID, $this->context->getCurrentFunc()->getOpId($op->result)); + $this->context->getCurrentMycode()->addCode($instIterator); } elseif ($op instanceof Op\Terminal\GlobalVar) { - $nameGlobal = Common::getNameDefinition($this->context->getCurrentOp()->var); - $myDefGlobal = new MyDefinition( + $this->context->getCurrentBlock()->getId(), + $this->context->getCurrentMyFile(), $this->context->getCurrentLine(), $this->context->getCurrentColumn(), - $nameGlobal + $op->var->value ); $myDefGlobal->setType(MyDefinition::TYPE_GLOBAL); $instDef = new MyInstruction(Opcodes::DEFINITION); $instDef->addProperty(MyInstruction::DEF, $myDefGlobal); $this->context->getCurrentMycode()->addCode($instDef); - } elseif ($op instanceof Op\Terminal\Return_) { - Assign::instruction($this->context, true); - - $inst = new MyInstruction(Opcodes::RETURN_FUNCTION); - $inst->addProperty(MyInstruction::RETURN_DEFS, $this->context->getCurrentFunc()->getReturnDefs()); - $this->context->getCurrentMycode()->addCode($inst); - } elseif ($op instanceof Op\Expr\Eval_) { - if (Common::isFuncCallWithoutReturn($op)) { - // expr of type "assign" to have a defined return - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - FuncCall::instruction($this->context, $myExpr, false); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - } - } elseif ($op instanceof Op\Terminal\Echo_) { - if (Common::isFuncCallWithoutReturn($op)) { - // expr of type "assign" to have a defined return - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - FuncCall::instruction($this->context, $myExpr, false); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - } - } elseif ($op instanceof Op\Expr\Print_) { - if (Common::isFuncCallWithoutReturn($op)) { - // expr of type "assign" to have a defined return - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - FuncCall::instruction($this->context, $myExpr, false); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - } - } elseif ($op instanceof Op\Expr\Exit_) { - if (Common::isFuncCallWithoutReturn($op)) { - // expr of type "assign" to have a defined return - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - FuncCall::instruction($this->context, $myExpr, false); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - } - } elseif ($op instanceof Op\Expr\StaticCall) { - if (Common::isFuncCallWithoutReturn($op)) { - // expr of type "assign" to have a defined return - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - FuncCall::instruction($this->context, $myExpr, false, false, true); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - } - } elseif ($op instanceof Op\Expr\FuncCall || $op instanceof Op\Expr\NsFuncCall) { - if (Common::isFuncCallWithoutReturn($op)) { - // expr of type "assign" to have a defined return - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); - - FuncCall::instruction($this->context, $myExpr, false); - - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); - } - } elseif ($op instanceof Op\Expr\MethodCall) { - if (Common::isFuncCallWithoutReturn($op)) { - $className = Common::getNameDefinition($op->var); - - $myExpr = new MyExpr($this->context->getCurrentLine(), $this->context->getCurrentColumn()); + $this->context->getCurrentFunc()->setHasGlobalVariables(true); + } elseif ($op instanceof Op\Terminal\Return_) { + // we put the ids of return as last block id + $this->context->getCurrentFunc()->addLastBlockId($this->context->getCurrentBlock()->getId()); - $this->context->getCurrentMycode()->addCode(new MyInstruction(Opcodes::START_EXPRESSION)); + if (isset($op->expr)) { + if (isset($op->expr->original)) { + Expr::implicitfetch($this->context, $op->expr, "right"); + } - FuncCall::instruction($this->context, $myExpr, false, true); + Assign::instruction($this->context, $op, $op->expr, null, true, false); - $instEndExpr = new MyInstruction(Opcodes::END_EXPRESSION); - $instEndExpr->addProperty(MyInstruction::EXPR, $myExpr); - $this->context->getCurrentMycode()->addCode($instEndExpr); + $inst = new MyInstruction(Opcodes::RETURN_FUNCTION); + $inst->addProperty(MyInstruction::RETURN_DEFS, $this->context->getCurrentFunc()->getReturnDefs()); + $this->context->getCurrentMycode()->addCode($inst); } } elseif ($op instanceof Op\Expr\Assign || $op instanceof Op\Expr\AssignRef) { - Assign::instruction($this->context); + if (isset($op->expr) && isset($op->var)) { + Assign::instruction($this->context, $op, $op->expr, $op->var); + } } elseif ($op instanceof Op\Stmt\Class_) { $className = Common::getNameDefinition($op); @@ -427,35 +407,32 @@ public function enterOp(Op $op, Block $block) $this->context->getClasses()->addMyclass($myClass); foreach ($op->stmts->children as $property) { - // if($property instanceof Op\Stmt\ClassMethod) if ($property instanceof Op\Stmt\Property) { $propertyName = Common::getNameDefinition($property); - $visibility = Common::getTypeVisibility($property->visiblity); + $visibility = Common::getTypeVisibility($property->visibility); - $myDef = new MyDefinition( + $myProperty = new MyProperty( + $this->context->getCurrentBlock()->getId(), + $this->context->getCurrentMyFile(), $property->getLine(), $property->getAttribute("startFilePos", -1), - "this" + $propertyName ); - $myDef->property->setVisibility($visibility); - $myDef->property->addProperty($propertyName); - $myDef->setClassName($className); - - // it's necessary for SecurityAnalysis (visibility) - - if ($property->static === 8) { - $myDef->addType(MyDefinition::TYPE_STATIC_PROPERTY); - } else { - $myDef->addType(MyDefinition::TYPE_PROPERTY); + $myProperty->setVisibility($visibility); + $myClass->addProperty($myProperty); + + if ($property->static) { + $myProperty->addType(MyDefinition::TYPE_STATIC_PROPERTY); + $myProperty->getCurrentState()->addType(MyDefinition::TYPE_STATIC_PROPERTY); } - - $myClass->addProperty($myDef); } } $instClass = new MyInstruction(Opcodes::CLASSE); $instClass->addProperty(MyInstruction::MYCLASS, $myClass); $this->context->getCurrentMycode()->addCode($instClass); + } else { + Expr::explicitfetch($this->context, $op, null); } } } diff --git a/package/src/progpilot/Utils.php b/package/src/progpilot/Utils.php index 404ccc98..c4311b57 100644 --- a/package/src/progpilot/Utils.php +++ b/package/src/progpilot/Utils.php @@ -36,8 +36,14 @@ public static function encodeCharacters($string) public static function printWarning($context, $message) { - if ($context->getPrintWarning()) { - fwrite(STDERR, "progpilot warning : $message\n"); + if ($context->isDebugMode()) { + fwrite(STDERR, "progpilot warning: $message\n"); + fwrite(STDERR, "file: '".$context->getCurrentMyFile()->getName()."'\n"); + + // sometimes it's called outside of function context + if (!is_null($context->getCurrentFunc())) { + fwrite(STDERR, "function: '".$context->getCurrentFunc()->getName()."'\n"); + } } } @@ -48,15 +54,7 @@ public static function printError($message) public static function printStaticProperties($language, $props) { - $propertyName = ""; - - if (is_array($props)) { - foreach ($props as $prop) { - $propertyName .= "::"."\$".Utils::encodeCharacters($prop); - } - } - - return $propertyName; + return "::"."\$".Utils::encodeCharacters($props); } public static function printProperties($language, $props) @@ -65,41 +63,69 @@ public static function printProperties($language, $props) if ($language === "js") { $separator = "."; } - - $propertyName = ""; - if (is_array($props)) { - foreach ($props as $prop) { - $propertyName .= "$separator".Utils::encodeCharacters($prop); - } - } - - return $propertyName; + return $separator.Utils::encodeCharacters($props); } - public static function printDefinition($language, $def) + public static function printDefinition($language, $def, $original = null) { + $prefix = "\$"; - if ($language === "js") { - $prefix = ""; + if ($def->isType(MyDefinition::TYPE_STATIC_PROPERTY) + || $language === "js") { + // oop/simple31.php + if (!is_null($original) + && !empty($original) + && $original[0]->isType(MyDefinition::TYPE_STATIC_PROPERTY)) { + $prefix = ""; + } } - - if ($def->isType(MyDefinition::TYPE_PROPERTY)) { - $defName = "$prefix".Utils::encodeCharacters($def->getName()). - Utils::printProperties($language, $def->property->getProperties()); - } elseif ($def->isType(MyDefinition::TYPE_STATIC_PROPERTY)) { - $defName = Utils::encodeCharacters($def->getName()). - Utils::printStaticProperties($language, $def->property->getProperties()); + + $printableDef = ""; + if (!is_null($original) && !empty($original)) { + for ($i = 0; $i < count($original); $i ++) { + if ($original[$i] instanceof MyDefinition) { + $printableDef .= "$prefix".Utils::encodeCharacters($original[$i]->getName()); + } elseif (is_numeric($original[$i]) + || $original[$i] === ']' + || $original[$i] === '[' + || $original[$i] === '->' + || $original[$i] === '::') { + // numeric element + $printableDef .= $original[$i]; + + $wasArray = false; + if ($original[$i] === '[') { + $wasArray = true; + } + + $wasStatic = false; + if ($original[$i] === '::') { + $wasStatic = true; + } + } else { + // string element (array, property name) + if ($wasArray) { + $printableDef .= "\""; + } + + if ($wasStatic) { + $printableDef .= "\$"; + } + + $printableDef .= Utils::encodeCharacters($original[$i]); + + if ($wasArray) { + $printableDef .= "\""; + } + } + } } else { - $defName = "$prefix".Utils::encodeCharacters($def->getName()); + $printableDef = "$prefix".Utils::encodeCharacters($def->getName()); } - $nameArray = ""; - if ($def->isType(MyDefinition::TYPE_ARRAY)) { - Utils::printArray($def->getArrayValue(), $nameArray); - } - return $defName.$nameArray; + return $printableDef; } public static function printFunction($function) diff --git a/package/src/uptodate_data/php/dev/rules.json b/package/src/uptodate_data/php/dev/rules.json index 9221ae82..4ddb0ef4 100644 --- a/package/src/uptodate_data/php/dev/rules.json +++ b/package/src/uptodate_data/php/dev/rules.json @@ -1,16 +1,5 @@ { "custom_rules": [ - {"sequence": - [ - {"function_name": "dev_iam_authenticated", "language": "php"}, - {"function_name": "dev_iam_rights", "language": "php"}, - {"function_name": "dev_retrieve_secret", "language": "php"} - ], - "description": "the user must be authentified and have rights before retrieving secrets", - "action": "MUST_VERIFY_CALL_FLOW", - "attack": "bypass access control", - "cwe": "CWE_285" - }, {"sequence": [ {"function_name": "secret", "language": "php"}, @@ -38,6 +27,30 @@ "language": "php", "action": "MUST_VERIFY_DEFINITION", "attack": "security misconfiguration", "cwe": "CWE_1004" + }, + { + "name": "db", + "instanceof": "SomeClass", + "description": "Access to SomeClass->db as new sinks", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "SomeClassDb" + }, + { + "name": "middle_object_unknown", + "instanceof": "known_class1", + "description": "new sinks", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "SomeClassUnknown" + }, + { + "name": "middle_object", + "instanceof": "known_class1_undefined", + "description": "new sinks", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "SomeClassUnknown" } ] } diff --git a/package/src/uptodate_data/php/dev/sinks.json b/package/src/uptodate_data/php/dev/sinks.json index 8e1bec18..121fea7a 100644 --- a/package/src/uptodate_data/php/dev/sinks.json +++ b/package/src/uptodate_data/php/dev/sinks.json @@ -1,10 +1,10 @@ { "sinks": [ {"name": "mysink", "instanceof": "known_middle_class2", "language": "php", "attack": "xss", "cwe": "CWE_79"}, - {"name": "mysink", "instanceof": "known_class1->middle_object", "language": "php", "attack": "xss", "cwe": "CWE_79"}, + {"name": "mysink", "instanceof": "SomeClassUnknown", "language": "php", "attack": "xss", "cwe": "CWE_79"}, {"name": "mysink", "instanceof": "known_class1_undefined", "language": "php", "attack": "xss", "cwe": "CWE_79"}, - {"name": "mysink", "instanceof": "known_class1_undefined->middle_object", "language": "php", "attack": "xss", "cwe": "CWE_79"}, - {"name": "query", "instanceof": "SomeClass->db", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, + {"name": "mysink", "instanceof": "unknown_middle_class2", "language": "php", "attack": "xss", "cwe": "CWE_79"}, + {"name": "query", "instanceof": "SomeClassDb", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, {"name": "query", "instanceof": "SomeClass1", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, {"name": "query", "instanceof": "VulnerableClass", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"} ] diff --git a/package/src/uptodate_data/php/dev/sources.json b/package/src/uptodate_data/php/dev/sources.json index 38825516..31fe6e36 100644 --- a/package/src/uptodate_data/php/dev/sources.json +++ b/package/src/uptodate_data/php/dev/sources.json @@ -5,6 +5,6 @@ {"name": "methodc1arr", "is_function": true, "instanceof": "testc1", "return_array_index": "tainted", "language": "php", "type": "for dev purposes"}, {"name": "func1arr", "is_function": true, "return_array_index": "tainted", "language": "php", "type": "for dev purposes"}, {"name": "member1", "instanceof": "testc1", "language": "php", "type": "for dev purposes"}, - {"name": "vardev", "is_array": true, "array_index": "tainted", "language": "php", "type": "for dev purposes"} + {"name": "vardev", "is_array": true, "array_index": ["tainted"], "language": "php", "type": "for dev purposes"} ] } diff --git a/package/src/uptodate_data/php/dev/validators.json b/package/src/uptodate_data/php/dev/validators.json index 0695c601..8356535e 100644 --- a/package/src/uptodate_data/php/dev/validators.json +++ b/package/src/uptodate_data/php/dev/validators.json @@ -1,4 +1,16 @@ { - "validators": [ - ] -} + "validators": [ + {"name": "in_array_false", "language": "php", "valid_when_returning": false, "parameters": + [ + {"id": 1, "conditions": "valid"}, + {"id": 2, "conditions": "array_not_tainted"} + ] + }, + {"name": "in_array_true", "language": "php", "valid_when_returning": true, "parameters": + [ + {"id": 1, "conditions": "valid"}, + {"id": 2, "conditions": "array_not_tainted"} + ] + } + ] +} \ No newline at end of file diff --git a/package/src/uptodate_data/php/frameworks/codeigniter/rules.json b/package/src/uptodate_data/php/frameworks/codeigniter/rules.json index 911bd671..812ae324 100644 --- a/package/src/uptodate_data/php/frameworks/codeigniter/rules.json +++ b/package/src/uptodate_data/php/frameworks/codeigniter/rules.json @@ -1,9 +1,41 @@ { "custom_rules": [ + { + "name": "input", + "instanceof": "CI_Controller", + "description": "load security helper in the controller", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "InputCodeIgniter" + }, + { + "name": "security", + "instanceof": "CI_Controller", + "description": "load security helper in the controller", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "SecurityHelperCodeIgniter" + }, + { + "name": "load", + "instanceof": "CI_Controller", + "description": "load templates in the controller", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "ViewCodeIgniter" + }, + { + "name": "db", + "instanceof": "CI_Model", + "description": "db interface to perform queries", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "DBCodeIgniter" + }, { "name": "query", "is_function": true, - "instanceof": "CI_Model->db", + "instanceof": "DBCodeIgniter", "description": "Result of db queries as new sources", "language": "php", "action": "DEFINE_OBJECT", @@ -12,7 +44,7 @@ { "name": "get", "is_function": true, - "instanceof": "CI_Model->db", + "instanceof": "DBCodeIgniter", "description": "Result of db queries as new sources", "language": "php", "action": "DEFINE_OBJECT", diff --git a/package/src/uptodate_data/php/frameworks/codeigniter/sanitizers.json b/package/src/uptodate_data/php/frameworks/codeigniter/sanitizers.json index 48989781..74ed86f4 100644 --- a/package/src/uptodate_data/php/frameworks/codeigniter/sanitizers.json +++ b/package/src/uptodate_data/php/frameworks/codeigniter/sanitizers.json @@ -3,9 +3,9 @@ {"name": "is_php", "language": "php", "prevent": ["ALL"]}, {"name": "is_really_writable", "language": "php", "prevent": ["ALL"]}, {"name": "html_escape", "language": "php", "prevent": ["xss", "QUOTES"]}, - {"name": "xss_clean", "instanceof": "CI_Controller->security", "language": "php", "prevent": ["xss", "QUOTES"]}, - {"name": "escape", "instanceof": "CI_Model->db", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, - {"name": "escape_str", "instanceof": "CI_Model->db", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, - {"name": "escape_like_str", "instanceof": "CI_Model->db", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]} + {"name": "xss_clean", "instanceof": "SecurityHelperCodeIgniter", "language": "php", "prevent": ["xss", "QUOTES"]}, + {"name": "escape", "instanceof": "DBCodeIgniter", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, + {"name": "escape_str", "instanceof": "DBCodeIgniter", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, + {"name": "escape_like_str", "instanceof": "DBCodeIgniter", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]} ] } diff --git a/package/src/uptodate_data/php/frameworks/codeigniter/sinks.json b/package/src/uptodate_data/php/frameworks/codeigniter/sinks.json index 7c8ed8cd..b9584b8a 100644 --- a/package/src/uptodate_data/php/frameworks/codeigniter/sinks.json +++ b/package/src/uptodate_data/php/frameworks/codeigniter/sinks.json @@ -1,10 +1,10 @@ { "sinks": [ - {"name": "view", "parameters": [{"id": 2, "conditions": ["QUOTES_HTML", "array_tainted"]}], "instanceof": "CI_Controller->load", "language": "php", "attack": "xss", "cwe": "CWE_79"}, - {"name": "query", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "CI_Model->db", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "simple_query", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "CI_Model->db", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "select", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "CI_Model->db", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "from", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "CI_Model->db", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "where", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "CI_Model->db", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"} + {"name": "view", "parameters": [{"id": 2, "conditions": ["QUOTES_HTML", "array_tainted"]}], "instanceof": "ViewCodeIgniter", "language": "php", "attack": "xss", "cwe": "CWE_79"}, + {"name": "query", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "DBCodeIgniter", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, + {"name": "simple_query", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "DBCodeIgniter", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, + {"name": "select", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "DBCodeIgniter", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, + {"name": "from", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "DBCodeIgniter", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, + {"name": "where", "parameters": [{"id": 1, "conditions": "QUOTES"}], "instanceof": "DBCodeIgniter", "language": "php", "attack": "sql_injection", "cwe": "CWE_89"} ] } diff --git a/package/src/uptodate_data/php/frameworks/codeigniter/sources.json b/package/src/uptodate_data/php/frameworks/codeigniter/sources.json index f9e81cff..9c302637 100644 --- a/package/src/uptodate_data/php/frameworks/codeigniter/sources.json +++ b/package/src/uptodate_data/php/frameworks/codeigniter/sources.json @@ -1,11 +1,11 @@ { "sources": [ - {"name": "get", "is_function": true, "instanceof": "CI_Controller->input", "language": "php"}, - {"name": "post", "is_function": true, "instanceof": "CI_Controller->input", "language": "php"}, - {"name": "cookie", "is_function": true, "instanceof": "CI_Controller->input", "language": "php"}, - {"name": "server", "is_function": true, "instanceof": "CI_Controller->input", "language": "php"}, - {"name": "result", "is_object": true, "is_function": true, "instanceof": "DBQueryCodeIgniter", "language": "php"}, - {"name": "result_array", "is_array": true, "is_function": true, "instanceof": "DBQueryCodeIgniter", "language": "php"}, + {"name": "get", "is_function": true, "instanceof": "InputCodeIgniter", "language": "php"}, + {"name": "post", "is_function": true, "instanceof": "InputCodeIgniter", "language": "php"}, + {"name": "cookie", "is_function": true, "instanceof": "InputCodeIgniter", "language": "php"}, + {"name": "server", "is_function": true, "instanceof": "InputCodeIgniter", "language": "php"}, + {"name": "result", "is_arrayof_objects": true, "is_function": true, "instanceof": "DBQueryCodeIgniter", "language": "php"}, + {"name": "result_array", "is_arrayof_arrays": true, "is_function": true, "instanceof": "DBQueryCodeIgniter", "language": "php"}, {"name": "row", "is_object": true, "is_function": true, "instanceof": "DBQueryCodeIgniter", "language": "php"}, {"name": "row_array", "is_array": true, "is_function": true, "instanceof": "DBQueryCodeIgniter", "language": "php"}, {"name": "last_query", "is_function": true, "instanceof": "DBQueryCodeIgniter", "language": "php"} diff --git a/package/src/uptodate_data/php/frameworks/codeigniter/validators.json b/package/src/uptodate_data/php/frameworks/codeigniter/validators.json index 3bcaddfd..d5bccf71 100644 --- a/package/src/uptodate_data/php/frameworks/codeigniter/validators.json +++ b/package/src/uptodate_data/php/frameworks/codeigniter/validators.json @@ -1,6 +1,6 @@ { "validators": [ - {"name": "xss_clean", "instanceof": "CI_Controller->security", "language": "php", "parameters": + {"name": "xss_clean", "instanceof": "SecurityHelperCodeIgniter", "language": "php", "parameters": [ {"id": 1, "conditions": "valid"}, {"id": 2, "conditions": "equals", "values": diff --git a/package/src/uptodate_data/php/frameworks/suitecrm/rules.json b/package/src/uptodate_data/php/frameworks/laravel/rules.json similarity index 64% rename from package/src/uptodate_data/php/frameworks/suitecrm/rules.json rename to package/src/uptodate_data/php/frameworks/laravel/rules.json index 2971e14a..4c845de6 100644 --- a/package/src/uptodate_data/php/frameworks/suitecrm/rules.json +++ b/package/src/uptodate_data/php/frameworks/laravel/rules.json @@ -1,4 +1,5 @@ { "custom_rules": [ - ] -} + ] +} + diff --git a/package/src/uptodate_data/php/frameworks/laravel/sanitizers.json b/package/src/uptodate_data/php/frameworks/laravel/sanitizers.json new file mode 100644 index 00000000..0ff6171b --- /dev/null +++ b/package/src/uptodate_data/php/frameworks/laravel/sanitizers.json @@ -0,0 +1,4 @@ +{ + "sanitizers": [ + ] +} diff --git a/package/src/uptodate_data/php/frameworks/laravel/sinks.json b/package/src/uptodate_data/php/frameworks/laravel/sinks.json new file mode 100644 index 00000000..3f54f9ba --- /dev/null +++ b/package/src/uptodate_data/php/frameworks/laravel/sinks.json @@ -0,0 +1,5 @@ +{ + "sinks": [ + {"name": "redirect", "parameters": [{"id": 1}], "instanceof": "App\\Http\\Controllers\\OpenRedirect", "language": "php", "attack": "open_redirect", "cwe": "CWE_601"} + ] +} diff --git a/package/src/uptodate_data/php/frameworks/laravel/sources.json b/package/src/uptodate_data/php/frameworks/laravel/sources.json new file mode 100644 index 00000000..c6f6fb79 --- /dev/null +++ b/package/src/uptodate_data/php/frameworks/laravel/sources.json @@ -0,0 +1,5 @@ +{ + "sources": [ + {"name": "get", "is_function": true, "instanceof": "Illuminate\\Http\\Request->input", "language": "php"} + ] +} diff --git a/package/src/uptodate_data/php/frameworks/suitecrm/validators.json b/package/src/uptodate_data/php/frameworks/laravel/validators.json similarity index 62% rename from package/src/uptodate_data/php/frameworks/suitecrm/validators.json rename to package/src/uptodate_data/php/frameworks/laravel/validators.json index f479de18..0695c601 100644 --- a/package/src/uptodate_data/php/frameworks/suitecrm/validators.json +++ b/package/src/uptodate_data/php/frameworks/laravel/validators.json @@ -1,4 +1,4 @@ { "validators": [ - ] -} + ] +} diff --git a/package/src/uptodate_data/php/frameworks/prestashop/rules.json b/package/src/uptodate_data/php/frameworks/prestashop/rules.json deleted file mode 100644 index 8b137891..00000000 --- a/package/src/uptodate_data/php/frameworks/prestashop/rules.json +++ /dev/null @@ -1 +0,0 @@ - diff --git a/package/src/uptodate_data/php/frameworks/prestashop/sanitizers.json b/package/src/uptodate_data/php/frameworks/prestashop/sanitizers.json deleted file mode 100644 index 0d645816..00000000 --- a/package/src/uptodate_data/php/frameworks/prestashop/sanitizers.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "sanitizers": [ - {"name": "safeOutput", "instanceof": "ToolsCore", "language": "php", "prevent": ["xss"]}, - {"name": "htmlentitiesUTF8", "instanceof": "ToolsCore", "language": "php", "prevent": ["xss"]}, - {"name": "encrypt", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "hash", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "encryptIV", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "hashIV", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "floorf", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "ps_round", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "math_round", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "round_helper", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]}, - {"name": "ceilf", "instanceof": "ToolsCore", "language": "php", "prevent": ["ALL"]} - ] -} diff --git a/package/src/uptodate_data/php/frameworks/prestashop/sinks.json b/package/src/uptodate_data/php/frameworks/prestashop/sinks.json deleted file mode 100644 index 46c0fc16..00000000 --- a/package/src/uptodate_data/php/frameworks/prestashop/sinks.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "sinks": [ - ] -} diff --git a/package/src/uptodate_data/php/frameworks/prestashop/sources.json b/package/src/uptodate_data/php/frameworks/prestashop/sources.json deleted file mode 100644 index 60a387a0..00000000 --- a/package/src/uptodate_data/php/frameworks/prestashop/sources.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "sources": [ - ] -} diff --git a/package/src/uptodate_data/php/frameworks/prestashop/validators.json b/package/src/uptodate_data/php/frameworks/prestashop/validators.json deleted file mode 100644 index 59925b69..00000000 --- a/package/src/uptodate_data/php/frameworks/prestashop/validators.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "validators": [ - {"name": "isIp2Long", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isMd5", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isSha1", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isFloat", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isUnsignedFloat", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isOptFloat", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isImageSize", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isPrice", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isNegativePrice", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isNumericIsoCode", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isDateFormat", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isDate", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isDateOrNull", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isBirthDate", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isBool", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isPhoneNumber", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isEan13", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isIsbn", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isUpc", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isZipCodeFormat", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isOrderWay", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isProductVisibility", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isInt", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isUnsignedInt", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isPercentage", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isUnsignedId", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isNullOrUnsignedId", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isLoadedObject", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isTrackingNumber", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isMySQLEngine", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isSortDirection", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isPriceDisplayMethod", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isCookie", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isReductionType", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isBoolId", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isLocalizationPackSelection", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isCoordinate", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isStockManagement", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "isPrestaShopVersion", "instanceof": "ValidateCore", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]} - ] -} diff --git a/package/src/uptodate_data/php/frameworks/suitecrm/sanitizers.json b/package/src/uptodate_data/php/frameworks/suitecrm/sanitizers.json deleted file mode 100644 index 2f35707a..00000000 --- a/package/src/uptodate_data/php/frameworks/suitecrm/sanitizers.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "sanitizers": [ - {"name": "quote", "instanceof": "DBManager", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, - {"name": "quoted", "instanceof": "DBManager", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]} - ] -} diff --git a/package/src/uptodate_data/php/frameworks/suitecrm/sinks.json b/package/src/uptodate_data/php/frameworks/suitecrm/sinks.json deleted file mode 100644 index 0454fc15..00000000 --- a/package/src/uptodate_data/php/frameworks/suitecrm/sinks.json +++ /dev/null @@ -1,78 +0,0 @@ -{ - "sinks": [ - {"name": "query", "instanceof": "DBManager", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "update", "instanceof": "DBManager", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "delete", "instanceof": "DBManager", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "retrieve", "instanceof": "DBManager", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "retrieveView", "instanceof": "DBManager", "parameters": [{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "generateInsertSQL", "instanceof": "DBManager", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getOne", "instanceof": "DBManager", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "fetchOne", "instanceof": "DBManager", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "pQuery", "instanceof": "DBManager", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "queryArray", "instanceof": "DBManager", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "limitQuery", "instanceof": "DBManager", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "validateQuery", "instanceof": "DBManager", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "delete", "instanceof": "Link", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "relationship_exists", "instanceof": "Link", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getQuery", "instanceof": "Link2", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "query", "instanceof": "Link2", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getBeans", "instanceof": "Link2", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getQuery", "instanceof": "SugarRelationship", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "load", "instanceof": "SugarRelationship", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_linked_beans", "instanceof": "SugarBean", "parameters": [{"id": 3, "conditions": "QUOTES"},{"id": 7, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "process_union_list_query", "instanceof": "SugarBean", "parameters": [{"id": 2, "conditions": "QUOTES"},{"id": 8, "conditions": "QUOTES"},{"id": 9, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "_get_num_rows_in_query", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "process_list_query", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "process_detail_query", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getRelatedFields", "instanceof": "SugarBean", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "process_full_list_query", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "create_index", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "build_related_list", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "build_related_list_where", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"},{"id": 4, "conditions": "QUOTES"},{"id": 5, "conditions": "QUOTES"},{"id": 6, "conditions": "QUOTES"},{"id": 7, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "build_related_in", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "build_related_list2", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "set_relationship", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"},{"id": 5, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "retrieve_relationships", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_union_related_list", "instanceof": "SugarBean", "parameters": [{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_list", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_related_list", "instanceof": "SugarBean", "parameters": [{"id": 3, "conditions": "QUOTES"},{"id": 4, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_detail", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_full_list", "instanceof": "SugarBean", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getTotalCount", "instanceof": "ListViewData", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getListViewData", "instanceof": "ListViewData", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getUsersMailerForSystemOverride", "instanceof": "OutboundEmail", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getUserMailers", "instanceof": "OutboundEmail", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getUserMailerSettings", "instanceof": "OutboundEmail", "parameters": [{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getInboundMailerSettings", "instanceof": "OutboundEmail", "parameters": [{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "retrieve", "instanceof": "OutboundEmail", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "search", "instanceof": "SugarSpot", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getData", "instanceof": "SugarChart", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "deleteEmailFromAllFolder", "instanceof": "SugarFolder", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "deleteEmailFromFolder", "instanceof": "SugarFolder", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "checkEmailExistForFolder", "instanceof": "SugarFolder", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getAllHTML", "instanceof": "SuiteMozaik", "parameters": [{"id": 5, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "addActions", "instanceof": "ACLAction", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "removeActions", "instanceof": "ACLAction", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getDefaultActions", "instanceof": "ACLAction", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getUserActions", "instanceof": "ACLAction", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"},{"id": 4, "conditions": "QUOTES"},{"id": 5, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getUserRoles", "instanceof": "ACLRole", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getUserRoleNames", "instanceof": "ACLRole", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getRoleActions", "instanceof": "ACLRole", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "mark_relationships_deleted", "instanceof": "ACLRole", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getRecipients", "instanceof": "EmailReminder", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "retrieveSettings", "instanceof": "Administration", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "saveSetting", "instanceof": "Administration", "parameters": [{"id": 1, "conditions": "QUOTES"},{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "determineIfUpgrade", "instanceof": "UpgradeHistory", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "retrieveTargetList", "instanceof": "Prospect", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getModuleTemplates", "instanceof": "formLetter", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_user_name", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_user_array", "parameters": [{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"},{"id": 5, "conditions": "QUOTES"},{"id": 6, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "getUserArrayFromFullName", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "clone_history", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "clone_relationship", "parameters": [{"id": 2, "conditions": "QUOTES"},{"id": 4, "conditions": "QUOTES"},{"id": 5, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_bean_select_array", "parameters": [{"id": 4, "conditions": "QUOTES"},{"id": 5, "conditions": "QUOTES"},{"id": 6, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "get_campaign_urls", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "check_enabled", "parameters": [{"id": 2, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"}, - {"name": "duplicate_check", "parameters": [{"id": 2, "conditions": "QUOTES"},{"id": 3, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"} - ] -} diff --git a/package/src/uptodate_data/php/frameworks/suitecrm/sources.json b/package/src/uptodate_data/php/frameworks/suitecrm/sources.json deleted file mode 100644 index 1b938e3b..00000000 --- a/package/src/uptodate_data/php/frameworks/suitecrm/sources.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "sources": [ - ] -} diff --git a/package/src/uptodate_data/php/frameworks/symfony/rules.json b/package/src/uptodate_data/php/frameworks/symfony/rules.json index 4b799bea..c211e7c2 100644 --- a/package/src/uptodate_data/php/frameworks/symfony/rules.json +++ b/package/src/uptodate_data/php/frameworks/symfony/rules.json @@ -1,8 +1,17 @@ { - "custom_rules": [{ + "custom_rules": [ + { + "name": "headers", + "instanceof": "Symfony\\Component\\HttpFoundation\\Response", + "description": "access headers of the symfony response", + "language": "php", + "action": "DEFINE_OBJECT", + "extra": "ResponseHeadersSymfony" + }, + { "name": "set", "is_function": true, - "instanceof": "Symfony\\Component\\HttpFoundation\\Response->headers", + "instanceof": "ResponseHeadersSymfony", "parameters": [ {"id": 1, "fixed": true, "valid_by_default": true, "values": diff --git a/package/src/uptodate_data/php/frameworks/wordpress/rules.json b/package/src/uptodate_data/php/frameworks/wordpress/rules.json deleted file mode 100644 index 8b137891..00000000 --- a/package/src/uptodate_data/php/frameworks/wordpress/rules.json +++ /dev/null @@ -1 +0,0 @@ - diff --git a/package/src/uptodate_data/php/frameworks/wordpress/sanitizers.json b/package/src/uptodate_data/php/frameworks/wordpress/sanitizers.json deleted file mode 100644 index 3982e466..00000000 --- a/package/src/uptodate_data/php/frameworks/wordpress/sanitizers.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "sanitizers": [ - {"name": "addslashes_gpc", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_attr", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_attr__", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_attr_x", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_html", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_html__", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_html_x", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_js", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_sql", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, - {"name": "esc_textarea", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "format_to_post", "language": "php", "prevent": ["sql_injection", "command_injection", "QUOTES"]}, - {"name": "htmlentities2", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "sanitize_email", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_file_name", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_html_class", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_key", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_mime_type", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_option", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_sql_orderby", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_text_field", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_title", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_title_for_query", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_trackback_urls", "language": "php", "prevent": ["ALL"]}, - {"name": "sanitize_user", "language": "php", "prevent": ["ALL"]}, - {"name": "tag_escape", "language": "php", "prevent": ["ALL"]}, - {"name": "wp_htmledit_pre", "language": "php", "prevent": ["xss"]}, - {"name": "wp_html_excerpt", "language": "php", "prevent": ["xss"]}, - {"name": "_wp_specialchars", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "wp_specialchars", "language": "php", "prevent": ["xss", "sql_injection", "command_injection", "QUOTES"]}, - {"name": "zeroise", "language": "php","prevent": ["ALL"]} - ] -} diff --git a/package/src/uptodate_data/php/frameworks/wordpress/sinks.json b/package/src/uptodate_data/php/frameworks/wordpress/sinks.json deleted file mode 100644 index 959177f9..00000000 --- a/package/src/uptodate_data/php/frameworks/wordpress/sinks.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "sinks": [ - {"name": "query", "instanceof": "wpdb", "parameters": [{"id": 1, "conditions": "QUOTES"}], "language": "php", "attack": "sql_injection", "cwe": "CWE_89"} - ] -} diff --git a/package/src/uptodate_data/php/frameworks/wordpress/sources.json b/package/src/uptodate_data/php/frameworks/wordpress/sources.json deleted file mode 100644 index db583d03..00000000 --- a/package/src/uptodate_data/php/frameworks/wordpress/sources.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "sources": [ - {"name": "get_col", "is_function": true, "instanceof": "wpdb", "language": "php"}, - {"name": "get_results", "is_object": true, "is_function": true, "instanceof": "wpdb", "language": "php"}, - {"name": "get_row", "is_function": true, "instanceof": "wpdb", "language": "php"}, - {"name": "get_var", "is_function": true, "instanceof": "wpdb", "language": "php"}, - {"name": "query", "is_function": true, "instanceof": "wpdb", "language": "php"} - ] -} diff --git a/package/src/uptodate_data/php/frameworks/wordpress/validators.json b/package/src/uptodate_data/php/frameworks/wordpress/validators.json deleted file mode 100644 index ccfae9bd..00000000 --- a/package/src/uptodate_data/php/frameworks/wordpress/validators.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "validators": [ - {"name": "is_email", "language": "php", "parameters": - [ - {"id": 1, "conditions": "valid"} - ] - } - ] -} diff --git a/package/src/uptodate_data/php/rules.json b/package/src/uptodate_data/php/rules.json index b4ed7aee..0c74f686 100644 --- a/package/src/uptodate_data/php/rules.json +++ b/package/src/uptodate_data/php/rules.json @@ -79,6 +79,13 @@ "max_nb_args": 3, "parameters": [ + {"id": 2, "sufficient": true, "fail_if_not_verified": false, "notequals": true, "values": + [ + {"value" : ""}, + {"value" : "null"}, + {"value" : "NULL"} + ] + }, {"id": 3, "valid_by_default": false, "values": [ {"value" : "FALSE", "is_array": true, "array_index": "secure"}, @@ -98,6 +105,13 @@ "max_nb_args": 3, "parameters": [ + {"id": 2, "sufficient": true, "fail_if_not_verified": false, "notequals": true, "values": + [ + {"value" : ""}, + {"value" : "null"}, + {"value" : "NULL"} + ] + }, {"id": 3, "valid_by_default": false, "values": [ {"value" : "FALSE", "is_array": true, "array_index": "httponly"}, @@ -117,6 +131,12 @@ "min_nb_args": 4, "parameters": [ + {"id": 2, "sufficient": true, "fail_if_not_verified": false, "values": + [ + {"value" : ""}, + {"value" : "null"} + ] + }, {"id": 7, "values": [ {"value" : "true"}, @@ -136,6 +156,12 @@ "min_nb_args": 4, "parameters": [ + {"id": 2, "sufficient": true, "fail_if_not_verified": false, "values": + [ + {"value" : ""}, + {"value" : "null"} + ] + }, {"id": 6, "values": [ {"value" : "true"}, diff --git a/package/src/uptodate_data/php/sanitizers.json b/package/src/uptodate_data/php/sanitizers.json index 9f40e657..58a29eed 100644 --- a/package/src/uptodate_data/php/sanitizers.json +++ b/package/src/uptodate_data/php/sanitizers.json @@ -103,6 +103,7 @@ {"name": "decoct", "language": "php", "prevent": ["ALL"]}, {"name": "hexdec", "language": "php", "prevent": ["ALL"]}, {"name": "rand", "language": "php", "prevent": ["ALL"]}, + {"name": "realpath", "language": "php", "prevent": ["ALL"]}, {"name": "max", "language": "php", "prevent": ["ALL"]}, {"name": "min", "language": "php", "prevent": ["ALL"]}, {"name": "metaphone", "language": "php", "prevent": ["ALL"]}, @@ -143,7 +144,8 @@ [ {"value" : "/'/", "prevent" : ["command_injection", "ldap_injection", "sql_injection", "QUOTES"]}, {"value" : "/\\W/si", "prevent" : ["command_injection", "ldap_injection", "sql_injection", "QUOTES"]}, - {"value" : "/[^a-zA-Z0-9_\\ -]/", "prevent" : ["command_injection", "ldap_injection", "sql_injection", "QUOTES"]} + {"value" : "/[^a-zA-Z0-9_\\ -]/", "prevent" : ["command_injection", "ldap_injection", "sql_injection", "QUOTES"]}, + {"value" : "|a-z0-d9 _.-|i", "prevent" : ["ALL"]} ]} ] } diff --git a/package/src/uptodate_data/php/sinks.json b/package/src/uptodate_data/php/sinks.json index 773c4b07..29a81b60 100644 --- a/package/src/uptodate_data/php/sinks.json +++ b/package/src/uptodate_data/php/sinks.json @@ -1,7 +1,7 @@ { "sinks": [ - {"name": "setcookie", "parameters": [{"id": 2}], "language": "php", "attack": "session_fixation", "cwe": "CWE_384"}, - {"name": "setrawcookie", "parameters": [{"id": 2}], "language": "php", "attack": "session_fixation", "cwe": "CWE_384"}, + {"name": "setcookie", "parameters": [{"id": 1}], "language": "php", "attack": "session_fixation", "cwe": "CWE_384"}, + {"name": "setrawcookie", "parameters": [{"id": 1}], "language": "php", "attack": "session_fixation", "cwe": "CWE_384"}, {"name": "session_id", "parameters": [{"id": 1}], "language": "php", "attack": "session_fixation", "cwe": "CWE_384"}, {"name": "echo", "conditions": "QUOTES_HTML", "language": "php", "attack": "xss", "cwe": "CWE_79"}, @@ -24,60 +24,6 @@ {"name": "set_include_path", "parameters": [{"id": 1}], "language": "php", "attack": "file_inclusion", "cwe": "CWE_98"}, {"name": "virtual", "parameters": [{"id": 1}], "language": "php", "attack": "file_inclusion", "cwe": "CWE_98"}, - {"name": "bzread", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "bzflush", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "dio_read", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "eio_readdir", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fdf_open", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "file", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "file_get_contents", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "finfo_file", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fflush", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fgetc", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fgetcsv", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fgets", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fgetss", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fread", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fpassthru", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "fscanf", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "ftok", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "get_meta_tags", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "glob", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "gzfile", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "gzgetc", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "gzgets", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "gzgetss", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "gzread", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "gzpassthru", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "highlight_file", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "imagecreatefrompng", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "imagecreatefromjpg", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "imagecreatefromgif", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "imagecreatefromgd2", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "imagecreatefromgd2part", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "imagecreatefromgd", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "opendir", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "parse_ini_file", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "php_strip_whitespace", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "readfile", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "readgzfile", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "readlink", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "scandir", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "show_source", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "simplexml_load_file", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "stream_get_contents", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "stream_get_line", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_bdiff", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_bpatch", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_diff_binary", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_diff", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_merge3", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_patch_binary", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_patch", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "xdiff_file_rabdiff", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "yaml_parse_file", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "zip_open", "parameters": [{"id": 1}], "language": "php", "attack": "file_disclosure", "cwe": "CWE_200"}, - {"name": "w32api_register_function", "parameters": [{"id": 1}], "language": "php", "attack": "w32api command_injection", "cwe": "CWE_78"}, {"name": "w32api_invoke_function", "parameters": [{"id": 1}], "language": "php", "attack": "w32api command_injection", "cwe": "CWE_78"}, {"name": "mb_send_mail", "parameters": [{"id": 5}], "language": "php", "attack": "mail command_injection", "cwe": "CWE_78"}, @@ -170,62 +116,82 @@ {"name": "xptr_eval", "parameters": [{"id": 2}], "language": "php", "attack": "xml_injection", "cwe": "CWE_91"}, {"name": "xpath", "instanceof": "simplexml_load_file", "parameters": [{"id": 1}], "language": "php", "attack": "xml_injection", "cwe": "CWE_91"}, - {"name": "fopen", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "bzwrite", "parameters": [{"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "chmod", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "chgrp", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "chown", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "copy", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "dio_write", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "eio_chmod", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "eio_chown", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "eio_mkdir", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "eio_mknod", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "eio_rmdir", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "eio_write", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "eio_unlink", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "error_log", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "event_buffer_write", "parameters": [{"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "file_put_contents", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "fputcsv", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "fputs", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "fprintf", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "ftruncate", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "fwrite", "parameters": [{"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "gzwrite", "parameters": [{"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "gzputs", "parameters": [{"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "loadXML", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "mkdir", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "move_uploaded_file", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "posix_mknod", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "recode_file", "parameters": [{"id": 2}, {"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "rename", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "rmdir", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "shmop_write", "parameters": [{"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "touch", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "unlink", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "vfprintf", "parameters": [{"id": 1}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_bdiff", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_bpatch", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_diff_binary", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_diff", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_merge3", "parameters": [{"id": 4}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_patch_binary", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_patch", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "xdiff_file_rabdiff", "parameters": [{"id": 3}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, - {"name": "yaml_emit_file", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "idor", "cwe": "CWE_862"}, + {"name": "fopen", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "bzopen", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "chmod", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "chgrp", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "chown", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "copy", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "dio_open", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_chmod", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_chown", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_mkdir", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_mknod", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_readdir", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_rmdir", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_write", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "eio_unlink", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "error_log", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "event_buffer_write", "parameters": [{"id": 2}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "fdf_open", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "file", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "file_get_contents", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "finfo_file", "parameters": [{"id": 2}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "ftok", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "file_put_contents", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "gzopen", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "get_meta_tags", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "glob", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "gzfile", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "highlight_file", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "imagecreatefrompng", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "imagecreatefromjpg", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "imagecreatefromgif", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "imagecreatefromgd2", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "imagecreatefromgd2part", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "imagecreatefromgd", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "mkdir", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "move_uploaded_file", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "opendir", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "parse_ini_file", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "php_strip_whitespace", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "posix_mknod", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "readfile", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "readgzfile", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "readlink", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "rename", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "rmdir", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "scandir", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "show_source", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "simplexml_load_file", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "stream_get_contents", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "stream_get_line", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "touch", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "unlink", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_bdiff", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_bpatch", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_diff_binary", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_diff", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_merge3", "parameters": [{"id": 4}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_patch_binary", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_patch", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "xdiff_file_rabdiff", "parameters": [{"id": 3}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "yaml_emit_file", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "yaml_parse_file", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, + {"name": "zip_open", "parameters": [{"id": 1}], "language": "php", "attack": "path_traversal", "cwe": "CWE_22"}, {"name": "header", "parameters": [{"id": 1}], "language": "php", "attack": "header_injection", "cwe": "CWE_601"}, {"name": "yaml_parse", "parameters": [{"id": 1}], "language": "php", "attack": "code_injection", "cwe": "CWE_95"}, {"name": "unserialize", "parameters": [{"id": 1}], "language": "php", "attack": "code_injection", "cwe": "CWE_95"}, - {"name": "preg_replace", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "code_injection", "cwe": "CWE_95"}, - {"name": "mb_ereg_replace", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "code_injection", "cwe": "CWE_95"}, - {"name": "mb_eregi_replace", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "code_injection", "cwe": "CWE_95"}, - {"name": "preg_filter", "parameters": [{"id": 1}, {"id": 2}], "language": "php", "attack": "code_injection", "cwe": "CWE_95"}, {"name": "preg_replace_callback", "parameters": [{"id": 2}], "language": "php", "attack": "code_injection", "cwe": "CWE_95"}, {"name": "eval", "parameters": [{"id": 1}], "language": "php", "attack": "eval code_injection", "cwe": "CWE_95"}, {"name": "assert", "parameters": [{"id": 1}], "language": "php", "attack": "assert code_injection", "cwe": "CWE_95"}, - {"name": "create_function", "language": "php", "attack": "create_function code_injection", "cwe": "CWE_95"} + {"name": "create_function", "language": "php", "attack": "create_function code_injection", "cwe": "CWE_95"}, + + {"name": "preg_replace", "parameters": [{"id": 1}], "language": "php", "attack": "redos", "cwe": "CWE_1333"}, + {"name": "mb_ereg_replace", "parameters": [{"id": 1}], "language": "php", "attack": "redos", "cwe": "CWE_1333"}, + {"name": "mb_eregi_replace", "parameters": [{"id": 1}], "language": "php", "attack": "redos", "cwe": "CWE_1333"}, + {"name": "preg_filter", "parameters": [{"id": 1}], "language": "php", "attack": "redos", "cwe": "CWE_1333"} ] } diff --git a/package/src/uptodate_data/php/sources.json b/package/src/uptodate_data/php/sources.json index 91c2a5f2..3d538143 100644 --- a/package/src/uptodate_data/php/sources.json +++ b/package/src/uptodate_data/php/sources.json @@ -5,7 +5,7 @@ {"name": "_COOKIE", "is_array": true, "language": "php"}, {"name": "_REQUEST", "is_array": true, "language": "php"}, {"name": "_FILES", "is_array": true, "language": "php"}, - {"name": "_SERVER", "is_array": true, "language": "php"}, + {"name": "_SERVER", "is_array": true, "array_index": ["QUERY_STRING", "HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET", "HTTP_ACCEPT_ENCODING", "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION", "HTTP_HOST", "HTTP_REFERER", "HTTP_USER_AGENT"], "language": "php"}, {"name": "HTTP_GET_VARS", "is_array": true, "language": "php"}, {"name": "HTTP_POST_VARS", "is_array": true, "language": "php"}, {"name": "HTTP_COOKIE_VARS", "is_array": true, "language": "php"}, @@ -15,83 +15,82 @@ {"name": "HTTP_RAW_POST_DATA", "is_array": true, "language": "php"}, {"name": "argc", "language": "php"}, {"name": "argv", "is_array": true, "language": "php"}, - {"name": "shell_exec", "is_function": true, "language": "php"}, - {"name": "exec", "is_function": true, "parameters": [{"id": 2, "is_array": true, "array_index": 0}], "language": "php"}, - {"name": "fgets", "is_function": true, "language": "php"}, - {"name": "fread", "is_function": true, "language": "php"}, - {"name": "bzread", "is_function": true, "language": "php"}, - {"name": "dio_read", "is_function": true, "language": "php"}, - {"name": "exif_imagetype", "is_function": true, "language": "php"}, - {"name": "exif_read_data", "is_function": true, "language": "php"}, - {"name": "exif_thumbnail", "is_function": true, "language": "php"}, - {"name": "fgetss", "is_function": true, "language": "php"}, - {"name": "file", "is_function": true, "language": "php"}, - {"name": "file_get_contents", "is_function": true, "language": "php"}, - {"name": "get_meta_tags", "is_function": true, "language": "php"}, - {"name": "glob", "is_function": true, "language": "php"}, - {"name": "gzread", "is_function": true, "language": "php"}, - {"name": "readdir", "is_function": true, "language": "php"}, - {"name": "read_exif_data", "is_function": true, "language": "php"}, - {"name": "scandir", "is_function": true, "language": "php"}, - {"name": "zip_read", "is_function": true, "language": "php"}, - {"name": "mysql_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "mysql_fetch_assoc", "is_array": true, "is_function": true, "language": "php"}, - {"name": "mysql_fetch_object", "is_object": true, "is_function": true, "language": "php", "type": "TODO"}, - {"name": "mysql_fetch_row", "is_array": true, "is_function": true, "language": "php"}, - {"name": "mysqli_fetch_all", "is_array": true, "is_function": true, "language": "php"}, - {"name": "mysqli_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "mysqli_fetch_assoc", "is_array": true, "is_function": true, "language": "php"}, - {"name": "mysqli_fetch_object", "is_object": true, "is_function": true, "language": "php", "type": "TODO"}, - {"name": "mysqli_fetch_row", "is_array": true, "is_function": true, "language": "php"}, - {"name": "fetch_all", "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, - {"name": "fetch_array", "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, - {"name": "fetch_assoc", "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, - {"name": "fetch_object", "is_object": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, - {"name": "fetch_row", "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, - {"name": "pg_fetch_all", "is_array": true, "is_function": true, "language": "php"}, - {"name": "pg_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "pg_fetch_assoc", "is_array": true, "is_function": true, "language": "php"}, - {"name": "pg_fetch_object", "is_object": true, "is_function": true, "language": "php", "type": "TODO"}, - {"name": "pg_fetch_result", "is_function": true, "language": "php"}, - {"name": "pg_fetch_row", "is_array": true, "is_function": true, "language": "php"}, - {"name": "sqlite_fetch_all", "is_array": true, "is_function": true, "language": "php"}, - {"name": "sqlite_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "sqlite_fetch_object", "is_object": true, "is_function": true, "language": "php", "type": "TODO"}, - {"name": "sqlite_fetch_single", "is_function": true, "language": "php"}, - {"name": "sqlite_fetch_string", "is_function": true, "language": "php"}, - {"name": "fetchArray", "instanceof": "SQLite3Result", "is_array": true, "is_function": true, "language": "php"}, - {"name": "cubrid_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "cubrid_fetch", "is_array": true, "is_function": true, "language": "php"}, - {"name": "db2_fetch_assoc", "is_array": true, "is_function": true, "language": "php"}, - {"name": "db2_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "db2_fetch_both", "is_array": true, "is_function": true, "language": "php"}, - {"name": "db2_fetch_object", "is_object": true, "is_function": true, "language": "php"}, - {"name": "db2_fetch_row ", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_assoc", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_row", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_object ", "is_object": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_assoc", "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_array", "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_row", "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch", "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, - {"name": "maxdb_fetch_object ", "instanceof": "maxdb_result", "is_object": true, "is_function": true, "language": "php"}, - {"name": "ingres_fetch_assoc", "is_array": true, "is_function": true, "language": "php"}, - {"name": "ingres_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "ingres_fetch_object", "is_object": true, "is_function": true, "language": "php"}, - {"name": "ingres_fetch_row ", "is_array": true, "is_function": true, "language": "php"}, - {"name": "fbsql_fetch_assoc", "is_array": true, "is_function": true, "language": "php"}, - {"name": "fbsql_fetch_array", "is_array": true, "is_function": true, "language": "php"}, - {"name": "fbsql_fetch_object", "is_object": true, "is_function": true, "language": "php"}, - {"name": "fbsql_fetch_row", "is_array": true, "is_function": true, "language": "php"}, - {"name": "stream_get_contents", "is_function": true, "language": "php"}, - {"name": "system", "is_function": true, "language": "php"}, + {"name": "shell_exec", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "exec", "stored_payload": true, "is_function": true, "parameters": [{"id": 2, "is_array": true, "array_index": 0}], "language": "php"}, + {"name": "fgets", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "fread", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "bzread", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "dio_read", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "exif_imagetype", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "exif_read_data", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "exif_thumbnail", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "fgetss", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "file", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "file_get_contents", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "get_meta_tags", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "glob", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "gzread", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "readdir", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "read_exif_data", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "scandir", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "zip_read", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "mysql_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "mysql_fetch_assoc", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "mysql_fetch_object", "stored_payload": true, "is_arrayof_objects": true, "is_function": true, "language": "php", "type": "TODO"}, + {"name": "mysql_fetch_row", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "mysqli_fetch_all", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "mysqli_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "mysqli_fetch_assoc", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "mysqli_fetch_object", "stored_payload": true, "is_object": true, "is_function": true, "language": "php", "type": "TODO"}, + {"name": "mysqli_fetch_row", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "fetch_all", "stored_payload": true, "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, + {"name": "fetch_array", "stored_payload": true, "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, + {"name": "fetch_assoc", "stored_payload": true, "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, + {"name": "fetch_object", "stored_payload": true, "is_object": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, + {"name": "fetch_row", "stored_payload": true, "is_array": true, "instanceof": "mysqli_result", "is_function": true, "language": "php"}, + {"name": "pg_fetch_all", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "pg_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "pg_fetch_assoc", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "pg_fetch_object", "stored_payload": true, "is_object": true, "is_function": true, "language": "php", "type": "TODO"}, + {"name": "pg_fetch_result", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "pg_fetch_row", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "sqlite_fetch_all", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "sqlite_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "sqlite_fetch_object", "stored_payload": true, "is_object": true, "is_function": true, "language": "php", "type": "TODO"}, + {"name": "sqlite_fetch_single", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "sqlite_fetch_string", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "fetchArray", "stored_payload": true, "instanceof": "SQLite3Result", "is_array": true, "is_function": true, "language": "php"}, + {"name": "cubrid_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "cubrid_fetch", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "db2_fetch_assoc", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "db2_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "db2_fetch_both", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "db2_fetch_object", "stored_payload": true, "is_object": true, "is_function": true, "language": "php"}, + {"name": "db2_fetch_row ", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_assoc", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_row", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_object", "stored_payload": true, "is_object": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_assoc", "stored_payload": true, "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_array", "stored_payload": true, "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_row", "stored_payload": true, "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch", "stored_payload": true, "instanceof": "maxdb_result", "is_array": true, "is_function": true, "language": "php"}, + {"name": "maxdb_fetch_object", "stored_payload": true, "instanceof": "maxdb_result", "is_object": true, "is_function": true, "language": "php"}, + {"name": "ingres_fetch_assoc", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "ingres_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "ingres_fetch_object", "stored_payload": true, "is_object": true, "is_function": true, "language": "php"}, + {"name": "ingres_fetch_row", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "fbsql_fetch_assoc", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "fbsql_fetch_array", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "fbsql_fetch_object", "stored_payload": true, "is_object": true, "is_function": true, "language": "php"}, + {"name": "fbsql_fetch_row", "stored_payload": true, "is_array": true, "is_function": true, "language": "php"}, + {"name": "stream_get_contents", "stored_payload": true, "is_function": true, "language": "php"}, + {"name": "system", "stored_payload": true, "is_function": true, "language": "php"}, {"name": "get_headers", "is_function": true, "language": "php"}, {"name": "getallheaders", "is_function": true, "language": "php"}, {"name": "get_browser", "is_function": true, "language": "php"}, {"name": "getenv", "is_function": true, "language": "php"}, - {"name": "gethostbyaddr", "is_function": true, "language": "php"}, {"name": "runkit_superglobals", "is_function": true, "language": "php"}, {"name": "import_request_variables", "is_function": true, "language": "php"} ] diff --git a/package/src/uptodate_data/php/validators.json b/package/src/uptodate_data/php/validators.json index 2d690fb7..8553db4b 100644 --- a/package/src/uptodate_data/php/validators.json +++ b/package/src/uptodate_data/php/validators.json @@ -50,6 +50,12 @@ {"name": "is_callable", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, {"name": "is_iterable", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, {"name": "is_resource", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, - {"name": "empty", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]} + {"name": "empty", "language": "php", "parameters": [{"id": 1, "conditions": "valid"}]}, + {"name": "strstr", "language": "php", "parameters": + [ + {"id": 1, "conditions": "valid"}, + {"id": 2, "conditions": "valid"} + ] + } ] } diff --git a/progpilot.yml b/progpilot.yml index 59d1411a..13be8dc3 100644 --- a/progpilot.yml +++ b/progpilot.yml @@ -1,40 +1,41 @@ inputs: - setDev: false - setLanguages: + dev_mode: false + languages: - php - setFrameworks: - - suitecrm - - codeigniter - - wordpress - setSources: ~ - setSinks: ~ - setValidators: ~ - setSanitizers: ~ - setCustomRules: ~ - setIncludes: ~ - setExcludes: - exclude_folders: - - "./projects/" - - ".git/" - - "./package/src/progpilot/Transformations/Js/" - setFolder: ~ - setFile: ~ - setCode: ~ - setResolvedIncludes: ~ - setFalsePositives: ~ + sources: + keep_defaults: true + config_files: ~ + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folder5/" + exclusions: + - "vendor" + - "node_modules" + - ".git/" + - "./projects/" + - "./package/src/progpilot/Transformations/Js/" + resolved_includes_file: ~ + false_positives: ~ outputs: - taintedFlow: false - resolveIncludes: false - resolveIncludesFile: ~ + tainted_flow: true + include_failures_file: ~ options: - setAnalyzeIncludes: true - setAnalyzeFunctions: true - setAnalyzeHardRules: true - setPrintFile: false - setPrintWarning: false - setPrettyPrint: true - setLimitTime: 10 - setLimitDefs: 3000 - setLimitSize: 500000 + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/example/composer.json b/projects/example/composer.json index 62f44111..1b47b6dd 100644 --- a/projects/example/composer.json +++ b/projects/example/composer.json @@ -2,8 +2,8 @@ "name": "progpilot/example", "description": "Example of use of Progpilot", "require": { - "php": "^7.0.25", - "designsecurity/progpilot": "@dev", - "ircmaxell/php-cfg": "@dev" + "php": ">=7.4", + "designsecurity/progpilot": "^0.8.0", + "ircmaxell/php-cfg": "^0.6.0" } } diff --git a/projects/example_config/composer.json b/projects/example_config/composer.json index 9d620688..eb4f1ff1 100644 --- a/projects/example_config/composer.json +++ b/projects/example_config/composer.json @@ -8,8 +8,8 @@ } ], "require": { - "php": "^7.0.25", - "progpilot/package": "@dev", - "ircmaxell/php-cfg": "@dev" + "php": ">=7.4", + "progpilot/package": "^0.8.0", + "ircmaxell/php-cfg": "^0.6.0" } } diff --git a/projects/example_config/configuration.yml b/projects/example_config/configuration.yml index 61682c42..88924ae6 100644 --- a/projects/example_config/configuration.yml +++ b/projects/example_config/configuration.yml @@ -1,44 +1,39 @@ inputs: - setDev: false - setLanguages: + dev_mode: false + languages: - php - setFrameworks: - - suitecrm - - codeigniter - - wordpress - - prestashop - - symfony - setSources: ~ - setSinks: ~ - setValidators: ~ - setSanitizers: ~ - setCustomRules: ~ - setIncludes: ~ - setExcludes: - exclude_files: - - "/srv/www/htdocs/phpMyAdmin/libraries/tcpdf/fonts/dejavusans.php" - exclude_folders: - - "/srv/www/htdocs/phpMyAdmin/themes/original/" - - "/home/eric/Téléchargements/prestashop/vendor/" - - "/home/eric/Téléchargements/prestashop/localization/" - setFolder: "/home/eric/Téléchargements/drupal-8.6.1/" - setFile: ~ - setCode: ~ - setResolvedIncludes: ~ - setFalsePositives: ~ + sources: + keep_defaults: true + config_files: ~ + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "/srv/www/htdocs/website/" + exclusions: + - "vendor" + - "node_modules" + - ".git" + resolved_includes_file: ~ + false_positives: ~ outputs: - taintedFlow: false - resolveIncludes: false - resolveIncludesFile: ~ + tainted_flow: true + include_failures_file: ~ options: - setAnalyzeIncludes: true - setAnalyzeFunctions: true - setAnalyzeHardRules: true - setPrintFile: true - setPrintWarning: false - setPrettyPrint: true - setLimitTime: 10 - setLimitDefs: 3000 - setLimitSize: 500000 + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/phar/composer.json b/projects/phar/composer.json index a0f2e7d1..1bb7c392 100644 --- a/projects/phar/composer.json +++ b/projects/phar/composer.json @@ -9,11 +9,12 @@ } ], "require": { - "php": "^7.0.25", + "php": ">=7.4", "progpilot/package": "@dev", - "ircmaxell/php-cfg": "@dev", - "symfony/yaml": "3.3.6", - "symfony/console": "3.3.5" + "ircmaxell/php-cfg": "^0.6.0", + "symfony/yaml": ">=3.3.6", + "symfony/console": ">=3.3.5", + "myclabs/deep-copy": "^1.10.2" }, "bin": ["progpilot"] } diff --git a/projects/phar/progpilot b/projects/phar/progpilot old mode 100644 new mode 100755 diff --git a/projects/tests/.phpunit.result.cache b/projects/tests/.phpunit.result.cache deleted file mode 100644 index 24b0b976..00000000 --- a/projects/tests/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":83587:{a:2:{s:7:"defects";a:1:{s:43:"RunAllTest::testSecurity with data set #176";i:3;}s:5:"times";a:1412:{s:41:"RunAllTest::testSecurity with data set #0";d:0.086;s:41:"RunAllTest::testSecurity with data set #1";d:0.021;s:41:"RunAllTest::testSecurity with data set #2";d:0.015;s:41:"RunAllTest::testSecurity with data set #3";d:0.017;s:41:"RunAllTest::testSecurity with data set #4";d:0.025;s:41:"RunAllTest::testSecurity with data set #5";d:0.019;s:41:"RunAllTest::testSecurity with data set #6";d:0.018;s:41:"RunAllTest::testSecurity with data set #7";d:0.018;s:41:"RunAllTest::testSecurity with data set #8";d:0.02;s:41:"RunAllTest::testSecurity with data set #9";d:0.019;s:42:"RunAllTest::testSecurity with data set #10";d:0.023;s:42:"RunAllTest::testSecurity with data set #11";d:0.042;s:42:"RunAllTest::testSecurity with data set #12";d:0.031;s:42:"RunAllTest::testSecurity with data set #13";d:0.025;s:42:"RunAllTest::testSecurity with data set #14";d:0.033;s:42:"RunAllTest::testSecurity with data set #15";d:0.022;s:42:"RunAllTest::testSecurity with data set #16";d:0.022;s:42:"RunAllTest::testSecurity with data set #17";d:0.024;s:42:"RunAllTest::testSecurity with data set #18";d:0.035;s:42:"RunAllTest::testSecurity with data set #19";d:0.035;s:42:"RunAllTest::testSecurity with data set #20";d:0.034;s:42:"RunAllTest::testSecurity with data set #21";d:0.023;s:42:"RunAllTest::testSecurity with data set #22";d:0.032;s:42:"RunAllTest::testSecurity with data set #23";d:0.027;s:42:"RunAllTest::testSecurity with data set #24";d:0.017;s:42:"RunAllTest::testSecurity with data set #25";d:0.019;s:42:"RunAllTest::testSecurity with data set #26";d:0.022;s:42:"RunAllTest::testSecurity with data set #27";d:0.015;s:42:"RunAllTest::testSecurity with data set #28";d:0.032;s:42:"RunAllTest::testSecurity with data set #29";d:0.017;s:42:"RunAllTest::testSecurity with data set #30";d:0.018;s:42:"RunAllTest::testSecurity with data set #31";d:0.02;s:42:"RunAllTest::testSecurity with data set #32";d:0.101;s:42:"RunAllTest::testSecurity with data set #33";d:0.028;s:42:"RunAllTest::testSecurity with data set #34";d:0.138;s:42:"RunAllTest::testSecurity with data set #35";d:0.015;s:42:"RunAllTest::testSecurity with data set #36";d:0.788;s:42:"RunAllTest::testSecurity with data set #37";d:0.019;s:42:"RunAllTest::testSecurity with data set #38";d:0.02;s:42:"RunAllTest::testSecurity with data set #39";d:0.019;s:42:"RunAllTest::testSecurity with data set #40";d:0.02;s:42:"RunAllTest::testSecurity with data set #41";d:0.024;s:42:"RunAllTest::testSecurity with data set #42";d:0.02;s:42:"RunAllTest::testSecurity with data set #43";d:0.02;s:42:"RunAllTest::testSecurity with data set #44";d:0.02;s:42:"RunAllTest::testSecurity with data set #45";d:0.066;s:42:"RunAllTest::testSecurity with data set #46";d:0.017;s:42:"RunAllTest::testSecurity with data set #47";d:0.016;s:42:"RunAllTest::testSecurity with data set #48";d:0.021;s:42:"RunAllTest::testSecurity with data set #49";d:0.019;s:42:"RunAllTest::testSecurity with data set #50";d:0.019;s:42:"RunAllTest::testSecurity with data set #51";d:0.019;s:42:"RunAllTest::testSecurity with data set #52";d:0.019;s:42:"RunAllTest::testSecurity with data set #53";d:0.026;s:42:"RunAllTest::testSecurity with data set #54";d:0.016;s:42:"RunAllTest::testSecurity with data set #55";d:0.026;s:42:"RunAllTest::testSecurity with data set #56";d:0.044;s:42:"RunAllTest::testSecurity with data set #57";d:0.016;s:42:"RunAllTest::testSecurity with data set #58";d:0.015;s:42:"RunAllTest::testSecurity with data set #59";d:0.014;s:42:"RunAllTest::testSecurity with data set #60";d:0.018;s:42:"RunAllTest::testSecurity with data set #61";d:0.024;s:42:"RunAllTest::testSecurity with data set #62";d:0.025;s:42:"RunAllTest::testSecurity with data set #63";d:0.019;s:42:"RunAllTest::testSecurity with data set #64";d:0.021;s:42:"RunAllTest::testSecurity with data set #65";d:0.033;s:42:"RunAllTest::testSecurity with data set #66";d:0.025;s:42:"RunAllTest::testSecurity with data set #67";d:0.017;s:42:"RunAllTest::testSecurity with data set #68";d:0.024;s:42:"RunAllTest::testSecurity with data set #69";d:0.024;s:42:"RunAllTest::testSecurity with data set #70";d:0.02;s:42:"RunAllTest::testSecurity with data set #71";d:0.021;s:42:"RunAllTest::testSecurity with data set #72";d:0.023;s:42:"RunAllTest::testSecurity with data set #73";d:0.021;s:42:"RunAllTest::testSecurity with data set #74";d:0.018;s:42:"RunAllTest::testSecurity with data set #75";d:0.132;s:42:"RunAllTest::testSecurity with data set #76";d:0.07;s:42:"RunAllTest::testSecurity with data set #77";d:0.038;s:42:"RunAllTest::testSecurity with data set #78";d:0.033;s:42:"RunAllTest::testSecurity with data set #79";d:0.029;s:42:"RunAllTest::testSecurity with data set #80";d:0.035;s:42:"RunAllTest::testSecurity with data set #81";d:0.039;s:42:"RunAllTest::testSecurity with data set #82";d:0.031;s:42:"RunAllTest::testSecurity with data set #83";d:0.031;s:42:"RunAllTest::testSecurity with data set #84";d:0.029;s:42:"RunAllTest::testSecurity with data set #85";d:0.026;s:42:"RunAllTest::testSecurity with data set #86";d:0.028;s:42:"RunAllTest::testSecurity with data set #87";d:0.034;s:42:"RunAllTest::testSecurity with data set #88";d:0.029;s:42:"RunAllTest::testSecurity with data set #89";d:0.031;s:42:"RunAllTest::testSecurity with data set #90";d:0.026;s:42:"RunAllTest::testSecurity with data set #91";d:0.026;s:42:"RunAllTest::testSecurity with data set #92";d:0.021;s:42:"RunAllTest::testSecurity with data set #93";d:0.024;s:42:"RunAllTest::testSecurity with data set #94";d:0.029;s:42:"RunAllTest::testSecurity with data set #95";d:0.023;s:42:"RunAllTest::testSecurity with data set #96";d:0.02;s:42:"RunAllTest::testSecurity with data set #97";d:0.036;s:42:"RunAllTest::testSecurity with data set #98";d:0.021;s:42:"RunAllTest::testSecurity with data set #99";d:0.196;s:43:"RunAllTest::testSecurity with data set #100";d:0.031;s:43:"RunAllTest::testSecurity with data set #101";d:0.032;s:43:"RunAllTest::testSecurity with data set #102";d:0.029;s:43:"RunAllTest::testSecurity with data set #103";d:0.03;s:43:"RunAllTest::testSecurity with data set #104";d:0.027;s:43:"RunAllTest::testSecurity with data set #105";d:0.027;s:43:"RunAllTest::testSecurity with data set #106";d:0.028;s:43:"RunAllTest::testSecurity with data set #107";d:0.025;s:43:"RunAllTest::testSecurity with data set #108";d:0.026;s:43:"RunAllTest::testSecurity with data set #109";d:0.027;s:43:"RunAllTest::testSecurity with data set #110";d:0.028;s:43:"RunAllTest::testSecurity with data set #111";d:0.027;s:43:"RunAllTest::testSecurity with data set #112";d:0.031;s:43:"RunAllTest::testSecurity with data set #113";d:0.031;s:43:"RunAllTest::testSecurity with data set #114";d:0.03;s:43:"RunAllTest::testSecurity with data set #115";d:0.023;s:43:"RunAllTest::testSecurity with data set #116";d:0.031;s:43:"RunAllTest::testSecurity with data set #117";d:0.029;s:43:"RunAllTest::testSecurity with data set #118";d:0.786;s:43:"RunAllTest::testSecurity with data set #119";d:0.031;s:43:"RunAllTest::testSecurity with data set #120";d:0.036;s:43:"RunAllTest::testSecurity with data set #121";d:0.031;s:43:"RunAllTest::testSecurity with data set #122";d:0.033;s:43:"RunAllTest::testSecurity with data set #123";d:0.038;s:43:"RunAllTest::testSecurity with data set #124";d:0.029;s:43:"RunAllTest::testSecurity with data set #125";d:0.027;s:43:"RunAllTest::testSecurity with data set #126";d:0.033;s:43:"RunAllTest::testSecurity with data set #127";d:0.027;s:43:"RunAllTest::testSecurity with data set #128";d:0.03;s:43:"RunAllTest::testSecurity with data set #129";d:0.027;s:43:"RunAllTest::testSecurity with data set #130";d:0.025;s:43:"RunAllTest::testSecurity with data set #131";d:0.029;s:43:"RunAllTest::testSecurity with data set #132";d:0.06;s:43:"RunAllTest::testSecurity with data set #133";d:0.029;s:43:"RunAllTest::testSecurity with data set #134";d:0.104;s:43:"RunAllTest::testSecurity with data set #135";d:0.032;s:43:"RunAllTest::testSecurity with data set #136";d:0.041;s:43:"RunAllTest::testSecurity with data set #137";d:0.029;s:43:"RunAllTest::testSecurity with data set #138";d:0.028;s:43:"RunAllTest::testSecurity with data set #139";d:0.038;s:43:"RunAllTest::testSecurity with data set #140";d:0.049;s:43:"RunAllTest::testSecurity with data set #141";d:0.028;s:43:"RunAllTest::testSecurity with data set #142";d:0.027;s:43:"RunAllTest::testSecurity with data set #143";d:0.026;s:43:"RunAllTest::testSecurity with data set #144";d:0.023;s:43:"RunAllTest::testSecurity with data set #145";d:0.028;s:43:"RunAllTest::testSecurity with data set #146";d:0.024;s:43:"RunAllTest::testSecurity with data set #147";d:0.025;s:43:"RunAllTest::testSecurity with data set #148";d:0.026;s:43:"RunAllTest::testSecurity with data set #149";d:0.035;s:43:"RunAllTest::testSecurity with data set #150";d:0.03;s:43:"RunAllTest::testSecurity with data set #151";d:0.057;s:43:"RunAllTest::testSecurity with data set #152";d:0.035;s:43:"RunAllTest::testSecurity with data set #153";d:0.035;s:43:"RunAllTest::testSecurity with data set #154";d:0.028;s:43:"RunAllTest::testSecurity with data set #155";d:0.021;s:43:"RunAllTest::testSecurity with data set #156";d:0.03;s:43:"RunAllTest::testSecurity with data set #157";d:0.027;s:43:"RunAllTest::testSecurity with data set #158";d:0.018;s:43:"RunAllTest::testSecurity with data set #159";d:0.034;s:43:"RunAllTest::testSecurity with data set #160";d:0.076;s:43:"RunAllTest::testSecurity with data set #161";d:0.033;s:43:"RunAllTest::testSecurity with data set #162";d:0.034;s:43:"RunAllTest::testSecurity with data set #163";d:0.048;s:43:"RunAllTest::testSecurity with data set #164";d:0.049;s:43:"RunAllTest::testSecurity with data set #165";d:0.038;s:43:"RunAllTest::testSecurity with data set #166";d:0.025;s:43:"RunAllTest::testSecurity with data set #167";d:0.026;s:43:"RunAllTest::testSecurity with data set #168";d:0.033;s:43:"RunAllTest::testSecurity with data set #169";d:0.033;s:43:"RunAllTest::testSecurity with data set #170";d:0.023;s:43:"RunAllTest::testSecurity with data set #171";d:0.062;s:43:"RunAllTest::testSecurity with data set #172";d:0.01;s:43:"RunAllTest::testSecurity with data set #173";d:0.025;s:43:"RunAllTest::testSecurity with data set #174";d:0.052;s:43:"RunAllTest::testSecurity with data set #175";d:0.025;s:43:"RunAllTest::testSecurity with data set #176";d:0.032;s:43:"RunAllTest::testSecurity with data set #177";d:0.061;s:43:"RunAllTest::testSecurity with data set #178";d:0.033;s:43:"RunAllTest::testSecurity with data set #179";d:0.033;s:43:"RunAllTest::testSecurity with data set #180";d:0.043;s:43:"RunAllTest::testSecurity with data set #181";d:0.053;s:43:"RunAllTest::testSecurity with data set #182";d:0.04;s:43:"RunAllTest::testSecurity with data set #183";d:0.055;s:43:"RunAllTest::testSecurity with data set #184";d:0.047;s:43:"RunAllTest::testSecurity with data set #185";d:0.043;s:43:"RunAllTest::testSecurity with data set #186";d:0.058;s:43:"RunAllTest::testSecurity with data set #187";d:0.032;s:43:"RunAllTest::testSecurity with data set #188";d:0.094;s:43:"RunAllTest::testSecurity with data set #189";d:0.044;s:43:"RunAllTest::testSecurity with data set #190";d:0.05;s:43:"RunAllTest::testSecurity with data set #191";d:0.053;s:43:"RunAllTest::testSecurity with data set #192";d:0.052;s:43:"RunAllTest::testSecurity with data set #193";d:0.041;s:43:"RunAllTest::testSecurity with data set #194";d:0.103;s:43:"RunAllTest::testSecurity with data set #195";d:0.035;s:43:"RunAllTest::testSecurity with data set #196";d:0.039;s:43:"RunAllTest::testSecurity with data set #197";d:0.037;s:43:"RunAllTest::testSecurity with data set #198";d:0.041;s:43:"RunAllTest::testSecurity with data set #199";d:0.034;s:43:"RunAllTest::testSecurity with data set #200";d:0.042;s:43:"RunAllTest::testSecurity with data set #201";d:0.035;s:43:"RunAllTest::testSecurity with data set #202";d:0.035;s:43:"RunAllTest::testSecurity with data set #203";d:0.027;s:43:"RunAllTest::testSecurity with data set #204";d:0.028;s:43:"RunAllTest::testSecurity with data set #205";d:0.028;s:43:"RunAllTest::testSecurity with data set #206";d:0.039;s:43:"RunAllTest::testSecurity with data set #207";d:0.027;s:43:"RunAllTest::testSecurity with data set #208";d:0.073;s:43:"RunAllTest::testSecurity with data set #209";d:0.043;s:43:"RunAllTest::testSecurity with data set #210";d:0.052;s:43:"RunAllTest::testSecurity with data set #211";d:0.029;s:43:"RunAllTest::testSecurity with data set #212";d:0.036;s:43:"RunAllTest::testSecurity with data set #213";d:0.026;s:43:"RunAllTest::testSecurity with data set #214";d:0.03;s:43:"RunAllTest::testSecurity with data set #215";d:0.03;s:43:"RunAllTest::testSecurity with data set #216";d:0.026;s:43:"RunAllTest::testSecurity with data set #217";d:0.027;s:43:"RunAllTest::testSecurity with data set #218";d:0.028;s:43:"RunAllTest::testSecurity with data set #219";d:0.028;s:43:"RunAllTest::testSecurity with data set #220";d:0.023;s:43:"RunAllTest::testSecurity with data set #221";d:0.029;s:43:"RunAllTest::testSecurity with data set #222";d:0.022;s:43:"RunAllTest::testSecurity with data set #223";d:0.03;s:43:"RunAllTest::testSecurity with data set #224";d:0.039;s:43:"RunAllTest::testSecurity with data set #225";d:0.032;s:43:"RunAllTest::testSecurity with data set #226";d:0.037;s:43:"RunAllTest::testSecurity with data set #227";d:0.07;s:43:"RunAllTest::testSecurity with data set #228";d:0.032;s:43:"RunAllTest::testSecurity with data set #229";d:0.035;s:43:"RunAllTest::testSecurity with data set #230";d:0.032;s:43:"RunAllTest::testSecurity with data set #231";d:0.043;s:43:"RunAllTest::testSecurity with data set #232";d:0.034;s:43:"RunAllTest::testSecurity with data set #233";d:0.032;s:43:"RunAllTest::testSecurity with data set #234";d:0.028;s:43:"RunAllTest::testSecurity with data set #235";d:0.05;s:43:"RunAllTest::testSecurity with data set #236";d:0.046;s:43:"RunAllTest::testSecurity with data set #237";d:0.03;s:43:"RunAllTest::testSecurity with data set #238";d:0.026;s:43:"RunAllTest::testSecurity with data set #239";d:0.03;s:43:"RunAllTest::testSecurity with data set #240";d:0.028;s:43:"RunAllTest::testSecurity with data set #241";d:0.033;s:43:"RunAllTest::testSecurity with data set #242";d:0.029;s:43:"RunAllTest::testSecurity with data set #243";d:0.039;s:43:"RunAllTest::testSecurity with data set #244";d:0.035;s:43:"RunAllTest::testSecurity with data set #245";d:0.067;s:43:"RunAllTest::testSecurity with data set #246";d:0.059;s:43:"RunAllTest::testSecurity with data set #247";d:0.05;s:43:"RunAllTest::testSecurity with data set #248";d:0.042;s:43:"RunAllTest::testSecurity with data set #249";d:0.049;s:43:"RunAllTest::testSecurity with data set #250";d:0.034;s:43:"RunAllTest::testSecurity with data set #251";d:0.038;s:43:"RunAllTest::testSecurity with data set #252";d:0.05;s:43:"RunAllTest::testSecurity with data set #253";d:0.027;s:43:"RunAllTest::testSecurity with data set #254";d:0.032;s:43:"RunAllTest::testSecurity with data set #255";d:0.031;s:43:"RunAllTest::testSecurity with data set #256";d:0.061;s:43:"RunAllTest::testSecurity with data set #257";d:0.034;s:43:"RunAllTest::testSecurity with data set #258";d:0.032;s:43:"RunAllTest::testSecurity with data set #259";d:0.036;s:43:"RunAllTest::testSecurity with data set #260";d:0.035;s:43:"RunAllTest::testSecurity with data set #261";d:0.033;s:43:"RunAllTest::testSecurity with data set #262";d:0.038;s:43:"RunAllTest::testSecurity with data set #263";d:0.037;s:43:"RunAllTest::testSecurity with data set #264";d:0.034;s:43:"RunAllTest::testSecurity with data set #265";d:0.042;s:43:"RunAllTest::testSecurity with data set #266";d:0.044;s:43:"RunAllTest::testSecurity with data set #267";d:0.034;s:43:"RunAllTest::testSecurity with data set #268";d:0.07;s:43:"RunAllTest::testSecurity with data set #269";d:0.039;s:43:"RunAllTest::testSecurity with data set #270";d:0.04;s:43:"RunAllTest::testSecurity with data set #271";d:0.028;s:43:"RunAllTest::testSecurity with data set #272";d:0.024;s:43:"RunAllTest::testSecurity with data set #273";d:0.028;s:43:"RunAllTest::testSecurity with data set #274";d:0.024;s:43:"RunAllTest::testSecurity with data set #275";d:0.034;s:43:"RunAllTest::testSecurity with data set #276";d:0.027;s:43:"RunAllTest::testSecurity with data set #277";d:0.029;s:43:"RunAllTest::testSecurity with data set #278";d:0.029;s:43:"RunAllTest::testSecurity with data set #279";d:0.026;s:43:"RunAllTest::testSecurity with data set #280";d:0.045;s:43:"RunAllTest::testSecurity with data set #281";d:0.033;s:43:"RunAllTest::testSecurity with data set #282";d:0.033;s:43:"RunAllTest::testSecurity with data set #283";d:0.034;s:43:"RunAllTest::testSecurity with data set #284";d:0.034;s:43:"RunAllTest::testSecurity with data set #285";d:0.046;s:43:"RunAllTest::testSecurity with data set #286";d:0.044;s:43:"RunAllTest::testSecurity with data set #287";d:0.037;s:43:"RunAllTest::testSecurity with data set #288";d:0.041;s:43:"RunAllTest::testSecurity with data set #289";d:0.027;s:43:"RunAllTest::testSecurity with data set #290";d:0.066;s:43:"RunAllTest::testSecurity with data set #291";d:0.036;s:43:"RunAllTest::testSecurity with data set #292";d:0.037;s:43:"RunAllTest::testSecurity with data set #293";d:0.04;s:43:"RunAllTest::testSecurity with data set #294";d:0.037;s:43:"RunAllTest::testSecurity with data set #295";d:0.035;s:43:"RunAllTest::testSecurity with data set #296";d:0.031;s:43:"RunAllTest::testSecurity with data set #297";d:0.031;s:43:"RunAllTest::testSecurity with data set #298";d:0.033;s:43:"RunAllTest::testSecurity with data set #299";d:0.04;s:43:"RunAllTest::testSecurity with data set #300";d:0.033;s:43:"RunAllTest::testSecurity with data set #301";d:0.036;s:43:"RunAllTest::testSecurity with data set #302";d:0.058;s:43:"RunAllTest::testSecurity with data set #303";d:0.039;s:43:"RunAllTest::testSecurity with data set #304";d:0.034;s:43:"RunAllTest::testSecurity with data set #305";d:0.043;s:43:"RunAllTest::testSecurity with data set #306";d:0.04;s:43:"RunAllTest::testSecurity with data set #307";d:0.027;s:43:"RunAllTest::testSecurity with data set #308";d:0.026;s:43:"RunAllTest::testSecurity with data set #309";d:0.033;s:43:"RunAllTest::testSecurity with data set #310";d:0.03;s:43:"RunAllTest::testSecurity with data set #311";d:0.054;s:43:"RunAllTest::testSecurity with data set #312";d:0.031;s:43:"RunAllTest::testSecurity with data set #313";d:0.035;s:43:"RunAllTest::testSecurity with data set #314";d:0.034;s:43:"RunAllTest::testSecurity with data set #315";d:0.044;s:43:"RunAllTest::testSecurity with data set #316";d:0.036;s:43:"RunAllTest::testSecurity with data set #317";d:0.037;s:43:"RunAllTest::testSecurity with data set #318";d:0.038;s:43:"RunAllTest::testSecurity with data set #319";d:0.045;s:43:"RunAllTest::testSecurity with data set #320";d:0.041;s:43:"RunAllTest::testSecurity with data set #321";d:0.037;s:43:"RunAllTest::testSecurity with data set #322";d:0.068;s:43:"RunAllTest::testSecurity with data set #323";d:0.053;s:43:"RunAllTest::testSecurity with data set #324";d:0.051;s:43:"RunAllTest::testSecurity with data set #325";d:0.041;s:43:"RunAllTest::testSecurity with data set #326";d:0.033;s:43:"RunAllTest::testSecurity with data set #327";d:0.032;s:43:"RunAllTest::testSecurity with data set #328";d:0.032;s:43:"RunAllTest::testSecurity with data set #329";d:0.032;s:43:"RunAllTest::testSecurity with data set #330";d:0.032;s:43:"RunAllTest::testSecurity with data set #331";d:0.039;s:43:"RunAllTest::testSecurity with data set #332";d:0.044;s:43:"RunAllTest::testSecurity with data set #333";d:0.068;s:43:"RunAllTest::testSecurity with data set #334";d:0.038;s:43:"RunAllTest::testSecurity with data set #335";d:0.053;s:43:"RunAllTest::testSecurity with data set #336";d:0.045;s:43:"RunAllTest::testSecurity with data set #337";d:0.058;s:43:"RunAllTest::testSecurity with data set #338";d:0.073;s:43:"RunAllTest::testSecurity with data set #339";d:0.055;s:43:"RunAllTest::testSecurity with data set #340";d:0.054;s:43:"RunAllTest::testSecurity with data set #341";d:0.055;s:43:"RunAllTest::testSecurity with data set #342";d:0.071;s:43:"RunAllTest::testSecurity with data set #343";d:0.039;s:43:"RunAllTest::testSecurity with data set #344";d:0.034;s:43:"RunAllTest::testSecurity with data set #345";d:0.036;s:43:"RunAllTest::testSecurity with data set #346";d:0.035;s:43:"RunAllTest::testSecurity with data set #347";d:0.033;s:43:"RunAllTest::testSecurity with data set #348";d:0.033;s:43:"RunAllTest::testSecurity with data set #349";d:0.032;s:43:"RunAllTest::testSecurity with data set #350";d:0.039;s:43:"RunAllTest::testSecurity with data set #351";d:0.032;s:43:"RunAllTest::testSecurity with data set #352";d:0.033;s:43:"RunAllTest::testSecurity with data set #353";d:0.067;s:43:"RunAllTest::testSecurity with data set #354";d:0.051;s:43:"RunAllTest::testSecurity with data set #355";d:0.065;s:43:"RunAllTest::testSecurity with data set #356";d:0.066;s:43:"RunAllTest::testSecurity with data set #357";d:0.052;s:43:"RunAllTest::testSecurity with data set #358";d:0.045;s:43:"RunAllTest::testSecurity with data set #359";d:0.064;s:43:"RunAllTest::testSecurity with data set #360";d:0.041;s:43:"RunAllTest::testSecurity with data set #361";d:0.033;s:43:"RunAllTest::testSecurity with data set #362";d:0.034;s:43:"RunAllTest::testSecurity with data set #363";d:0.035;s:43:"RunAllTest::testSecurity with data set #364";d:0.034;s:43:"RunAllTest::testSecurity with data set #365";d:0.035;s:43:"RunAllTest::testSecurity with data set #366";d:0.035;s:43:"RunAllTest::testSecurity with data set #367";d:0.033;s:43:"RunAllTest::testSecurity with data set #368";d:0.05;s:43:"RunAllTest::testSecurity with data set #369";d:0.033;s:43:"RunAllTest::testSecurity with data set #370";d:0.03;s:43:"RunAllTest::testSecurity with data set #371";d:0.04;s:43:"RunAllTest::testSecurity with data set #372";d:0.034;s:43:"RunAllTest::testSecurity with data set #373";d:0.038;s:43:"RunAllTest::testSecurity with data set #374";d:0.036;s:43:"RunAllTest::testSecurity with data set #375";d:0.041;s:43:"RunAllTest::testSecurity with data set #376";d:0.054;s:43:"RunAllTest::testSecurity with data set #377";d:0.04;s:43:"RunAllTest::testSecurity with data set #378";d:0.041;s:43:"RunAllTest::testSecurity with data set #379";d:0.032;s:43:"RunAllTest::testSecurity with data set #380";d:0.03;s:43:"RunAllTest::testSecurity with data set #381";d:0.031;s:43:"RunAllTest::testSecurity with data set #382";d:0.028;s:43:"RunAllTest::testSecurity with data set #383";d:0.052;s:43:"RunAllTest::testSecurity with data set #384";d:0.032;s:43:"RunAllTest::testSecurity with data set #385";d:0.037;s:43:"RunAllTest::testSecurity with data set #386";d:0.042;s:43:"RunAllTest::testSecurity with data set #387";d:0.034;s:43:"RunAllTest::testSecurity with data set #388";d:0.033;s:43:"RunAllTest::testSecurity with data set #389";d:0.042;s:43:"RunAllTest::testSecurity with data set #390";d:0.034;s:43:"RunAllTest::testSecurity with data set #391";d:0.036;s:43:"RunAllTest::testSecurity with data set #392";d:0.059;s:43:"RunAllTest::testSecurity with data set #393";d:0.043;s:43:"RunAllTest::testSecurity with data set #394";d:0.045;s:43:"RunAllTest::testSecurity with data set #395";d:0.043;s:43:"RunAllTest::testSecurity with data set #396";d:0.041;s:43:"RunAllTest::testSecurity with data set #397";d:0.032;s:43:"RunAllTest::testSecurity with data set #398";d:0.032;s:43:"RunAllTest::testSecurity with data set #399";d:0.035;s:43:"RunAllTest::testSecurity with data set #400";d:0.055;s:43:"RunAllTest::testSecurity with data set #401";d:0.033;s:43:"RunAllTest::testSecurity with data set #402";d:0.034;s:43:"RunAllTest::testSecurity with data set #403";d:0.036;s:43:"RunAllTest::testSecurity with data set #404";d:0.033;s:43:"RunAllTest::testSecurity with data set #405";d:0.028;s:43:"RunAllTest::testSecurity with data set #406";d:0.028;s:43:"RunAllTest::testSecurity with data set #407";d:0.038;s:43:"RunAllTest::testSecurity with data set #408";d:0.033;s:43:"RunAllTest::testSecurity with data set #409";d:0.033;s:43:"RunAllTest::testSecurity with data set #410";d:0.057;s:43:"RunAllTest::testSecurity with data set #411";d:0.038;s:43:"RunAllTest::testSecurity with data set #412";d:0.044;s:43:"RunAllTest::testSecurity with data set #413";d:0.036;s:43:"RunAllTest::testSecurity with data set #414";d:0.049;s:43:"RunAllTest::testSecurity with data set #415";d:0.032;s:43:"RunAllTest::testSecurity with data set #416";d:0.029;s:43:"RunAllTest::testSecurity with data set #417";d:0.03;s:43:"RunAllTest::testSecurity with data set #418";d:0.031;s:43:"RunAllTest::testSecurity with data set #419";d:0.061;s:43:"RunAllTest::testSecurity with data set #420";d:0.033;s:43:"RunAllTest::testSecurity with data set #421";d:0.033;s:43:"RunAllTest::testSecurity with data set #422";d:0.038;s:43:"RunAllTest::testSecurity with data set #423";d:0.037;s:43:"RunAllTest::testSecurity with data set #424";d:0.037;s:43:"RunAllTest::testSecurity with data set #425";d:0.041;s:43:"RunAllTest::testSecurity with data set #426";d:0.045;s:43:"RunAllTest::testSecurity with data set #427";d:0.04;s:43:"RunAllTest::testSecurity with data set #428";d:0.062;s:43:"RunAllTest::testSecurity with data set #429";d:0.054;s:43:"RunAllTest::testSecurity with data set #430";d:0.042;s:43:"RunAllTest::testSecurity with data set #431";d:0.042;s:43:"RunAllTest::testSecurity with data set #432";d:0.043;s:43:"RunAllTest::testSecurity with data set #433";d:0.03;s:43:"RunAllTest::testSecurity with data set #434";d:0.028;s:43:"RunAllTest::testSecurity with data set #435";d:0.049;s:43:"RunAllTest::testSecurity with data set #436";d:0.035;s:43:"RunAllTest::testSecurity with data set #437";d:0.046;s:43:"RunAllTest::testSecurity with data set #438";d:0.049;s:43:"RunAllTest::testSecurity with data set #439";d:0.042;s:43:"RunAllTest::testSecurity with data set #440";d:0.033;s:43:"RunAllTest::testSecurity with data set #441";d:0.031;s:43:"RunAllTest::testSecurity with data set #442";d:0.031;s:43:"RunAllTest::testSecurity with data set #443";d:0.039;s:43:"RunAllTest::testSecurity with data set #444";d:0.054;s:43:"RunAllTest::testSecurity with data set #445";d:0.035;s:43:"RunAllTest::testSecurity with data set #446";d:0.038;s:43:"RunAllTest::testSecurity with data set #447";d:0.038;s:43:"RunAllTest::testSecurity with data set #448";d:0.041;s:43:"RunAllTest::testSecurity with data set #449";d:0.038;s:43:"RunAllTest::testSecurity with data set #450";d:0.048;s:43:"RunAllTest::testSecurity with data set #451";d:0.056;s:43:"RunAllTest::testSecurity with data set #452";d:0.121;s:43:"RunAllTest::testSecurity with data set #453";d:0.06;s:43:"RunAllTest::testSecurity with data set #454";d:0.055;s:43:"RunAllTest::testSecurity with data set #455";d:0.053;s:43:"RunAllTest::testSecurity with data set #456";d:0.045;s:43:"RunAllTest::testSecurity with data set #457";d:0.059;s:43:"RunAllTest::testSecurity with data set #458";d:0.053;s:43:"RunAllTest::testSecurity with data set #459";d:0.052;s:43:"RunAllTest::testSecurity with data set #460";d:0.045;s:43:"RunAllTest::testSecurity with data set #461";d:0.052;s:43:"RunAllTest::testSecurity with data set #462";d:0.066;s:43:"RunAllTest::testSecurity with data set #463";d:0.053;s:43:"RunAllTest::testSecurity with data set #464";d:0.049;s:43:"RunAllTest::testSecurity with data set #465";d:0.057;s:43:"RunAllTest::testSecurity with data set #466";d:0.053;s:43:"RunAllTest::testSecurity with data set #467";d:0.073;s:43:"RunAllTest::testSecurity with data set #468";d:0.057;s:43:"RunAllTest::testSecurity with data set #469";d:0.043;s:43:"RunAllTest::testSecurity with data set #470";d:0.045;s:43:"RunAllTest::testSecurity with data set #471";d:0.042;s:43:"RunAllTest::testSecurity with data set #472";d:0.039;s:43:"RunAllTest::testSecurity with data set #473";d:0.064;s:43:"RunAllTest::testSecurity with data set #474";d:0.046;s:43:"RunAllTest::testSecurity with data set #475";d:0.048;s:43:"RunAllTest::testSecurity with data set #476";d:0.055;s:43:"RunAllTest::testSecurity with data set #477";d:0.03;s:43:"RunAllTest::testSecurity with data set #478";d:0.03;s:43:"RunAllTest::testSecurity with data set #479";d:0.042;s:43:"RunAllTest::testSecurity with data set #480";d:0.032;s:43:"RunAllTest::testSecurity with data set #481";d:0.037;s:43:"RunAllTest::testSecurity with data set #482";d:0.055;s:43:"RunAllTest::testSecurity with data set #483";d:0.043;s:43:"RunAllTest::testSecurity with data set #484";d:0.041;s:43:"RunAllTest::testSecurity with data set #485";d:0.039;s:43:"RunAllTest::testSecurity with data set #486";d:0.053;s:43:"RunAllTest::testSecurity with data set #487";d:0.031;s:43:"RunAllTest::testSecurity with data set #488";d:0.032;s:43:"RunAllTest::testSecurity with data set #489";d:0.029;s:43:"RunAllTest::testSecurity with data set #490";d:0.031;s:43:"RunAllTest::testSecurity with data set #491";d:0.032;s:43:"RunAllTest::testSecurity with data set #492";d:0.03;s:43:"RunAllTest::testSecurity with data set #493";d:0.056;s:43:"RunAllTest::testSecurity with data set #494";d:0.038;s:43:"RunAllTest::testSecurity with data set #495";d:0.034;s:43:"RunAllTest::testSecurity with data set #496";d:0.035;s:43:"RunAllTest::testSecurity with data set #497";d:0.041;s:43:"RunAllTest::testSecurity with data set #498";d:0.035;s:43:"RunAllTest::testSecurity with data set #499";d:0.039;s:43:"RunAllTest::testSecurity with data set #500";d:0.038;s:43:"RunAllTest::testSecurity with data set #501";d:0.044;s:43:"RunAllTest::testSecurity with data set #502";d:0.044;s:43:"RunAllTest::testSecurity with data set #503";d:0.039;s:43:"RunAllTest::testSecurity with data set #504";d:0.064;s:43:"RunAllTest::testSecurity with data set #505";d:0.03;s:43:"RunAllTest::testSecurity with data set #506";d:0.035;s:43:"RunAllTest::testSecurity with data set #507";d:0.033;s:43:"RunAllTest::testSecurity with data set #508";d:0.032;s:43:"RunAllTest::testSecurity with data set #509";d:0.038;s:43:"RunAllTest::testSecurity with data set #510";d:0.035;s:43:"RunAllTest::testSecurity with data set #511";d:0.033;s:43:"RunAllTest::testSecurity with data set #512";d:0.033;s:43:"RunAllTest::testSecurity with data set #513";d:0.038;s:43:"RunAllTest::testSecurity with data set #514";d:0.033;s:43:"RunAllTest::testSecurity with data set #515";d:0.056;s:43:"RunAllTest::testSecurity with data set #516";d:0.04;s:43:"RunAllTest::testSecurity with data set #517";d:0.046;s:43:"RunAllTest::testSecurity with data set #518";d:0.041;s:43:"RunAllTest::testSecurity with data set #519";d:0.04;s:43:"RunAllTest::testSecurity with data set #520";d:0.04;s:43:"RunAllTest::testSecurity with data set #521";d:0.031;s:43:"RunAllTest::testSecurity with data set #522";d:0.029;s:43:"RunAllTest::testSecurity with data set #523";d:0.033;s:43:"RunAllTest::testSecurity with data set #524";d:0.029;s:43:"RunAllTest::testSecurity with data set #525";d:0.062;s:43:"RunAllTest::testSecurity with data set #526";d:0.034;s:43:"RunAllTest::testSecurity with data set #527";d:0.039;s:43:"RunAllTest::testSecurity with data set #528";d:0.043;s:43:"RunAllTest::testSecurity with data set #529";d:0.031;s:43:"RunAllTest::testSecurity with data set #530";d:0.039;s:43:"RunAllTest::testSecurity with data set #531";d:0.035;s:43:"RunAllTest::testSecurity with data set #532";d:0.036;s:43:"RunAllTest::testSecurity with data set #533";d:0.029;s:43:"RunAllTest::testSecurity with data set #534";d:0.026;s:43:"RunAllTest::testSecurity with data set #535";d:0.028;s:43:"RunAllTest::testSecurity with data set #536";d:0.03;s:43:"RunAllTest::testSecurity with data set #537";d:0.026;s:43:"RunAllTest::testSecurity with data set #538";d:0.027;s:43:"RunAllTest::testSecurity with data set #539";d:0.029;s:43:"RunAllTest::testSecurity with data set #540";d:0.031;s:43:"RunAllTest::testSecurity with data set #541";d:0.055;s:43:"RunAllTest::testSecurity with data set #542";d:0.038;s:43:"RunAllTest::testSecurity with data set #543";d:0.034;s:43:"RunAllTest::testSecurity with data set #544";d:0.039;s:43:"RunAllTest::testSecurity with data set #545";d:0.034;s:43:"RunAllTest::testSecurity with data set #546";d:0.034;s:43:"RunAllTest::testSecurity with data set #547";d:0.031;s:43:"RunAllTest::testSecurity with data set #548";d:0.033;s:43:"RunAllTest::testSecurity with data set #549";d:0.035;s:43:"RunAllTest::testSecurity with data set #550";d:0.034;s:43:"RunAllTest::testSecurity with data set #551";d:0.031;s:43:"RunAllTest::testSecurity with data set #552";d:0.032;s:43:"RunAllTest::testSecurity with data set #553";d:0.065;s:43:"RunAllTest::testSecurity with data set #554";d:0.041;s:43:"RunAllTest::testSecurity with data set #555";d:0.029;s:43:"RunAllTest::testSecurity with data set #556";d:0.04;s:43:"RunAllTest::testSecurity with data set #557";d:0.036;s:43:"RunAllTest::testSecurity with data set #558";d:0.029;s:43:"RunAllTest::testSecurity with data set #559";d:0.031;s:43:"RunAllTest::testSecurity with data set #560";d:0.03;s:43:"RunAllTest::testSecurity with data set #561";d:0.029;s:43:"RunAllTest::testSecurity with data set #562";d:0.026;s:43:"RunAllTest::testSecurity with data set #563";d:0.028;s:43:"RunAllTest::testSecurity with data set #564";d:0.031;s:43:"RunAllTest::testSecurity with data set #565";d:0.03;s:43:"RunAllTest::testSecurity with data set #566";d:0.026;s:43:"RunAllTest::testSecurity with data set #567";d:0.025;s:43:"RunAllTest::testSecurity with data set #568";d:0.058;s:43:"RunAllTest::testSecurity with data set #569";d:0.034;s:43:"RunAllTest::testSecurity with data set #570";d:0.034;s:43:"RunAllTest::testSecurity with data set #571";d:0.031;s:43:"RunAllTest::testSecurity with data set #572";d:0.029;s:43:"RunAllTest::testSecurity with data set #573";d:0.033;s:43:"RunAllTest::testSecurity with data set #574";d:0.032;s:43:"RunAllTest::testSecurity with data set #575";d:0.037;s:43:"RunAllTest::testSecurity with data set #576";d:0.039;s:43:"RunAllTest::testSecurity with data set #577";d:0.04;s:43:"RunAllTest::testSecurity with data set #578";d:0.034;s:43:"RunAllTest::testSecurity with data set #579";d:0.036;s:43:"RunAllTest::testSecurity with data set #580";d:0.031;s:43:"RunAllTest::testSecurity with data set #581";d:0.026;s:43:"RunAllTest::testSecurity with data set #582";d:0.056;s:43:"RunAllTest::testSecurity with data set #583";d:0.03;s:43:"RunAllTest::testSecurity with data set #584";d:0.035;s:43:"RunAllTest::testSecurity with data set #585";d:0.041;s:43:"RunAllTest::testSecurity with data set #586";d:0.034;s:43:"RunAllTest::testSecurity with data set #587";d:0.035;s:43:"RunAllTest::testSecurity with data set #588";d:0.034;s:43:"RunAllTest::testSecurity with data set #589";d:0.034;s:43:"RunAllTest::testSecurity with data set #590";d:0.031;s:43:"RunAllTest::testSecurity with data set #591";d:0.037;s:43:"RunAllTest::testSecurity with data set #592";d:0.043;s:43:"RunAllTest::testSecurity with data set #593";d:0.033;s:43:"RunAllTest::testSecurity with data set #594";d:0.062;s:43:"RunAllTest::testSecurity with data set #595";d:0.037;s:43:"RunAllTest::testSecurity with data set #596";d:0.037;s:43:"RunAllTest::testSecurity with data set #597";d:0.035;s:43:"RunAllTest::testSecurity with data set #598";d:0.033;s:43:"RunAllTest::testSecurity with data set #599";d:0.043;s:43:"RunAllTest::testSecurity with data set #600";d:0.039;s:43:"RunAllTest::testSecurity with data set #601";d:0.03;s:43:"RunAllTest::testSecurity with data set #602";d:0.032;s:43:"RunAllTest::testSecurity with data set #603";d:0.052;s:43:"RunAllTest::testSecurity with data set #604";d:0.04;s:43:"RunAllTest::testSecurity with data set #605";d:0.037;s:43:"RunAllTest::testSecurity with data set #606";d:0.033;s:43:"RunAllTest::testSecurity with data set #607";d:0.034;s:43:"RunAllTest::testSecurity with data set #608";d:0.032;s:43:"RunAllTest::testSecurity with data set #609";d:0.037;s:43:"RunAllTest::testSecurity with data set #610";d:0.036;s:43:"RunAllTest::testSecurity with data set #611";d:0.033;s:43:"RunAllTest::testSecurity with data set #612";d:0.042;s:43:"RunAllTest::testSecurity with data set #613";d:0.035;s:43:"RunAllTest::testSecurity with data set #614";d:0.062;s:43:"RunAllTest::testSecurity with data set #615";d:0.039;s:43:"RunAllTest::testSecurity with data set #616";d:0.034;s:43:"RunAllTest::testSecurity with data set #617";d:0.037;s:43:"RunAllTest::testSecurity with data set #618";d:0.034;s:43:"RunAllTest::testSecurity with data set #619";d:0.041;s:43:"RunAllTest::testSecurity with data set #620";d:0.035;s:43:"RunAllTest::testSecurity with data set #621";d:0.041;s:43:"RunAllTest::testSecurity with data set #622";d:0.039;s:43:"RunAllTest::testSecurity with data set #623";d:0.056;s:43:"RunAllTest::testSecurity with data set #624";d:0.046;s:43:"RunAllTest::testSecurity with data set #625";d:0.051;s:43:"RunAllTest::testSecurity with data set #626";d:0.028;s:43:"RunAllTest::testSecurity with data set #627";d:0.029;s:43:"RunAllTest::testSecurity with data set #628";d:0.03;s:43:"RunAllTest::testSecurity with data set #629";d:0.037;s:43:"RunAllTest::testSecurity with data set #630";d:0.03;s:43:"RunAllTest::testSecurity with data set #631";d:0.028;s:43:"RunAllTest::testSecurity with data set #632";d:0.029;s:43:"RunAllTest::testSecurity with data set #633";d:0.026;s:43:"RunAllTest::testSecurity with data set #634";d:0.03;s:43:"RunAllTest::testSecurity with data set #635";d:0.061;s:43:"RunAllTest::testSecurity with data set #636";d:0.031;s:43:"RunAllTest::testSecurity with data set #637";d:0.033;s:43:"RunAllTest::testSecurity with data set #638";d:0.038;s:43:"RunAllTest::testSecurity with data set #639";d:0.041;s:43:"RunAllTest::testSecurity with data set #640";d:0.035;s:43:"RunAllTest::testSecurity with data set #641";d:0.028;s:43:"RunAllTest::testSecurity with data set #642";d:0.027;s:43:"RunAllTest::testSecurity with data set #643";d:0.021;s:43:"RunAllTest::testSecurity with data set #644";d:0.027;s:43:"RunAllTest::testSecurity with data set #645";d:0.022;s:43:"RunAllTest::testSecurity with data set #646";d:0.025;s:43:"RunAllTest::testSecurity with data set #647";d:0.019;s:43:"RunAllTest::testSecurity with data set #648";d:0.022;s:43:"RunAllTest::testSecurity with data set #649";d:0.024;s:43:"RunAllTest::testSecurity with data set #650";d:0.024;s:43:"RunAllTest::testSecurity with data set #651";d:0.026;s:43:"RunAllTest::testSecurity with data set #652";d:0.022;s:43:"RunAllTest::testSecurity with data set #653";d:0.022;s:43:"RunAllTest::testSecurity with data set #654";d:0.057;s:43:"RunAllTest::testSecurity with data set #655";d:0.023;s:43:"RunAllTest::testSecurity with data set #656";d:0.029;s:43:"RunAllTest::testSecurity with data set #657";d:0.029;s:43:"RunAllTest::testSecurity with data set #658";d:0.031;s:43:"RunAllTest::testSecurity with data set #659";d:0.028;s:43:"RunAllTest::testSecurity with data set #660";d:0.027;s:43:"RunAllTest::testSecurity with data set #661";d:0.027;s:43:"RunAllTest::testSecurity with data set #662";d:0.024;s:43:"RunAllTest::testSecurity with data set #663";d:0.028;s:43:"RunAllTest::testSecurity with data set #664";d:0.028;s:43:"RunAllTest::testSecurity with data set #665";d:0.026;s:43:"RunAllTest::testSecurity with data set #666";d:0.021;s:43:"RunAllTest::testSecurity with data set #667";d:0.024;s:43:"RunAllTest::testSecurity with data set #668";d:0.022;s:43:"RunAllTest::testSecurity with data set #669";d:0.025;s:43:"RunAllTest::testSecurity with data set #670";d:0.023;s:43:"RunAllTest::testSecurity with data set #671";d:0.031;s:43:"RunAllTest::testSecurity with data set #672";d:0.023;s:43:"RunAllTest::testSecurity with data set #673";d:0.023;s:43:"RunAllTest::testSecurity with data set #674";d:0.022;s:43:"RunAllTest::testSecurity with data set #675";d:0.031;s:43:"RunAllTest::testSecurity with data set #676";d:0.022;s:43:"RunAllTest::testSecurity with data set #677";d:0.06;s:43:"RunAllTest::testSecurity with data set #678";d:0.03;s:43:"RunAllTest::testSecurity with data set #679";d:0.03;s:43:"RunAllTest::testSecurity with data set #680";d:0.029;s:43:"RunAllTest::testSecurity with data set #681";d:0.028;s:43:"RunAllTest::testSecurity with data set #682";d:0.028;s:43:"RunAllTest::testSecurity with data set #683";d:0.03;s:43:"RunAllTest::testSecurity with data set #684";d:0.029;s:43:"RunAllTest::testSecurity with data set #685";d:0.028;s:43:"RunAllTest::testSecurity with data set #686";d:0.026;s:43:"RunAllTest::testSecurity with data set #687";d:0.025;s:43:"RunAllTest::testSecurity with data set #688";d:0.029;s:43:"RunAllTest::testSecurity with data set #689";d:0.023;s:43:"RunAllTest::testSecurity with data set #690";d:0.028;s:43:"RunAllTest::testSecurity with data set #691";d:0.027;s:43:"RunAllTest::testSecurity with data set #692";d:0.024;s:43:"RunAllTest::testSecurity with data set #693";d:0.025;s:43:"RunAllTest::testSecurity with data set #694";d:0.023;s:43:"RunAllTest::testSecurity with data set #695";d:0.028;s:43:"RunAllTest::testSecurity with data set #696";d:0.028;s:43:"RunAllTest::testSecurity with data set #697";d:0.025;s:43:"RunAllTest::testSecurity with data set #698";d:0.066;s:43:"RunAllTest::testSecurity with data set #699";d:0.029;s:43:"RunAllTest::testSecurity with data set #700";d:0.032;s:43:"RunAllTest::testSecurity with data set #701";d:0.035;s:43:"RunAllTest::testSecurity with data set #702";d:0.036;s:43:"RunAllTest::testSecurity with data set #703";d:0.038;s:43:"RunAllTest::testSecurity with data set #704";d:0.033;s:43:"RunAllTest::testSecurity with data set #705";d:0.033;s:43:"RunAllTest::testSecurity with data set #706";d:0.037;s:43:"RunAllTest::testSecurity with data set #707";d:0.041;s:43:"RunAllTest::testSecurity with data set #708";d:0.034;s:43:"RunAllTest::testSecurity with data set #709";d:0.037;s:43:"RunAllTest::testSecurity with data set #710";d:0.039;s:43:"RunAllTest::testSecurity with data set #711";d:0.031;s:43:"RunAllTest::testSecurity with data set #712";d:0.031;s:43:"RunAllTest::testSecurity with data set #713";d:0.068;s:43:"RunAllTest::testSecurity with data set #714";d:0.035;s:43:"RunAllTest::testSecurity with data set #715";d:0.038;s:43:"RunAllTest::testSecurity with data set #716";d:0.035;s:43:"RunAllTest::testSecurity with data set #717";d:0.035;s:43:"RunAllTest::testSecurity with data set #718";d:0.04;s:43:"RunAllTest::testSecurity with data set #719";d:0.037;s:43:"RunAllTest::testSecurity with data set #720";d:0.037;s:43:"RunAllTest::testSecurity with data set #721";d:0.034;s:43:"RunAllTest::testSecurity with data set #722";d:0.033;s:43:"RunAllTest::testSecurity with data set #723";d:0.032;s:43:"RunAllTest::testSecurity with data set #724";d:0.064;s:43:"RunAllTest::testSecurity with data set #725";d:0.042;s:43:"RunAllTest::testSecurity with data set #726";d:0.037;s:43:"RunAllTest::testSecurity with data set #727";d:0.035;s:43:"RunAllTest::testSecurity with data set #728";d:0.036;s:43:"RunAllTest::testSecurity with data set #729";d:0.033;s:43:"RunAllTest::testSecurity with data set #730";d:0.05;s:43:"RunAllTest::testSecurity with data set #731";d:0.049;s:43:"RunAllTest::testSecurity with data set #732";d:0.039;s:43:"RunAllTest::testSecurity with data set #733";d:0.032;s:43:"RunAllTest::testSecurity with data set #734";d:0.031;s:43:"RunAllTest::testSecurity with data set #735";d:0.031;s:43:"RunAllTest::testSecurity with data set #736";d:0.029;s:43:"RunAllTest::testSecurity with data set #737";d:0.094;s:43:"RunAllTest::testSecurity with data set #738";d:0.034;s:43:"RunAllTest::testSecurity with data set #739";d:0.04;s:43:"RunAllTest::testSecurity with data set #740";d:0.033;s:43:"RunAllTest::testSecurity with data set #741";d:0.034;s:43:"RunAllTest::testSecurity with data set #742";d:0.033;s:43:"RunAllTest::testSecurity with data set #743";d:0.03;s:43:"RunAllTest::testSecurity with data set #744";d:0.03;s:43:"RunAllTest::testSecurity with data set #745";d:0.031;s:43:"RunAllTest::testSecurity with data set #746";d:0.028;s:43:"RunAllTest::testSecurity with data set #747";d:0.029;s:43:"RunAllTest::testSecurity with data set #748";d:0.026;s:43:"RunAllTest::testSecurity with data set #749";d:0.027;s:43:"RunAllTest::testSecurity with data set #750";d:0.029;s:43:"RunAllTest::testSecurity with data set #751";d:0.027;s:43:"RunAllTest::testSecurity with data set #752";d:0.027;s:43:"RunAllTest::testSecurity with data set #753";d:0.057;s:43:"RunAllTest::testSecurity with data set #754";d:0.031;s:43:"RunAllTest::testSecurity with data set #755";d:0.034;s:43:"RunAllTest::testSecurity with data set #756";d:0.035;s:43:"RunAllTest::testSecurity with data set #757";d:0.053;s:43:"RunAllTest::testSecurity with data set #758";d:0.031;s:43:"RunAllTest::testSecurity with data set #759";d:0.032;s:43:"RunAllTest::testSecurity with data set #760";d:0.036;s:43:"RunAllTest::testSecurity with data set #761";d:0.032;s:43:"RunAllTest::testSecurity with data set #762";d:0.032;s:43:"RunAllTest::testSecurity with data set #763";d:0.031;s:43:"RunAllTest::testSecurity with data set #764";d:0.031;s:43:"RunAllTest::testSecurity with data set #765";d:0.054;s:43:"RunAllTest::testSecurity with data set #766";d:0.032;s:43:"RunAllTest::testSecurity with data set #767";d:0.032;s:43:"RunAllTest::testSecurity with data set #768";d:0.035;s:43:"RunAllTest::testSecurity with data set #769";d:0.035;s:43:"RunAllTest::testSecurity with data set #770";d:0.033;s:43:"RunAllTest::testSecurity with data set #771";d:0.028;s:43:"RunAllTest::testSecurity with data set #772";d:0.024;s:43:"RunAllTest::testSecurity with data set #773";d:0.028;s:43:"RunAllTest::testSecurity with data set #774";d:0.031;s:43:"RunAllTest::testSecurity with data set #775";d:0.032;s:43:"RunAllTest::testSecurity with data set #776";d:0.026;s:43:"RunAllTest::testSecurity with data set #777";d:0.026;s:43:"RunAllTest::testSecurity with data set #778";d:0.025;s:43:"RunAllTest::testSecurity with data set #779";d:0.025;s:43:"RunAllTest::testSecurity with data set #780";d:0.055;s:43:"RunAllTest::testSecurity with data set #781";d:0.032;s:43:"RunAllTest::testSecurity with data set #782";d:0.033;s:43:"RunAllTest::testSecurity with data set #783";d:0.032;s:43:"RunAllTest::testSecurity with data set #784";d:0.034;s:43:"RunAllTest::testSecurity with data set #785";d:0.038;s:43:"RunAllTest::testSecurity with data set #786";d:0.044;s:43:"RunAllTest::testSecurity with data set #787";d:0.045;s:43:"RunAllTest::testSecurity with data set #788";d:0.038;s:43:"RunAllTest::testSecurity with data set #789";d:0.04;s:43:"RunAllTest::testSecurity with data set #790";d:0.042;s:43:"RunAllTest::testSecurity with data set #791";d:0.06;s:43:"RunAllTest::testSecurity with data set #792";d:0.038;s:43:"RunAllTest::testSecurity with data set #793";d:0.062;s:43:"RunAllTest::testSecurity with data set #794";d:0.036;s:43:"RunAllTest::testSecurity with data set #795";d:0.039;s:43:"RunAllTest::testSecurity with data set #796";d:0.043;s:43:"RunAllTest::testSecurity with data set #797";d:0.02;s:43:"RunAllTest::testSecurity with data set #798";d:0.038;s:43:"RunAllTest::testSecurity with data set #799";d:0.026;s:43:"RunAllTest::testSecurity with data set #800";d:0.024;s:43:"RunAllTest::testSecurity with data set #801";d:0.027;s:43:"RunAllTest::testSecurity with data set #802";d:0.022;s:43:"RunAllTest::testSecurity with data set #803";d:0.025;s:43:"RunAllTest::testSecurity with data set #804";d:0.02;s:43:"RunAllTest::testSecurity with data set #805";d:0.063;s:43:"RunAllTest::testSecurity with data set #806";d:0.027;s:43:"RunAllTest::testSecurity with data set #807";d:0.027;s:43:"RunAllTest::testSecurity with data set #808";d:0.03;s:43:"RunAllTest::testSecurity with data set #809";d:0.031;s:43:"RunAllTest::testSecurity with data set #810";d:0.03;s:43:"RunAllTest::testSecurity with data set #811";d:0.027;s:43:"RunAllTest::testSecurity with data set #812";d:0.027;s:43:"RunAllTest::testSecurity with data set #813";d:0.028;s:43:"RunAllTest::testSecurity with data set #814";d:0.026;s:43:"RunAllTest::testSecurity with data set #815";d:0.024;s:43:"RunAllTest::testSecurity with data set #816";d:0.028;s:43:"RunAllTest::testSecurity with data set #817";d:0.031;s:43:"RunAllTest::testSecurity with data set #818";d:0.025;s:43:"RunAllTest::testSecurity with data set #819";d:0.025;s:43:"RunAllTest::testSecurity with data set #820";d:0.036;s:43:"RunAllTest::testSecurity with data set #821";d:0.025;s:43:"RunAllTest::testSecurity with data set #822";d:0.022;s:43:"RunAllTest::testSecurity with data set #823";d:0.024;s:43:"RunAllTest::testSecurity with data set #824";d:0.028;s:43:"RunAllTest::testSecurity with data set #825";d:0.022;s:43:"RunAllTest::testSecurity with data set #826";d:0.065;s:43:"RunAllTest::testSecurity with data set #827";d:0.039;s:43:"RunAllTest::testSecurity with data set #828";d:0.027;s:43:"RunAllTest::testSecurity with data set #829";d:0.032;s:43:"RunAllTest::testSecurity with data set #830";d:0.034;s:43:"RunAllTest::testSecurity with data set #831";d:0.029;s:43:"RunAllTest::testSecurity with data set #832";d:0.034;s:43:"RunAllTest::testSecurity with data set #833";d:0.038;s:43:"RunAllTest::testSecurity with data set #834";d:0.038;s:43:"RunAllTest::testSecurity with data set #835";d:0.043;s:43:"RunAllTest::testSecurity with data set #836";d:0.037;s:43:"RunAllTest::testSecurity with data set #837";d:0.046;s:43:"RunAllTest::testSecurity with data set #838";d:0.037;s:43:"RunAllTest::testSecurity with data set #839";d:0.03;s:43:"RunAllTest::testSecurity with data set #840";d:0.033;s:43:"RunAllTest::testSecurity with data set #841";d:0.063;s:43:"RunAllTest::testSecurity with data set #842";d:0.031;s:43:"RunAllTest::testSecurity with data set #843";d:0.033;s:43:"RunAllTest::testSecurity with data set #844";d:0.031;s:43:"RunAllTest::testSecurity with data set #845";d:0.033;s:43:"RunAllTest::testSecurity with data set #846";d:0.032;s:43:"RunAllTest::testSecurity with data set #847";d:0.04;s:43:"RunAllTest::testSecurity with data set #848";d:0.052;s:43:"RunAllTest::testSecurity with data set #849";d:0.034;s:43:"RunAllTest::testSecurity with data set #850";d:0.041;s:43:"RunAllTest::testSecurity with data set #851";d:0.034;s:43:"RunAllTest::testSecurity with data set #852";d:0.054;s:43:"RunAllTest::testSecurity with data set #853";d:0.033;s:43:"RunAllTest::testSecurity with data set #854";d:0.035;s:43:"RunAllTest::testSecurity with data set #855";d:0.032;s:43:"RunAllTest::testSecurity with data set #856";d:0.031;s:43:"RunAllTest::testSecurity with data set #857";d:0.029;s:43:"RunAllTest::testSecurity with data set #858";d:0.025;s:43:"RunAllTest::testSecurity with data set #859";d:0.025;s:43:"RunAllTest::testSecurity with data set #860";d:0.025;s:43:"RunAllTest::testSecurity with data set #861";d:0.036;s:43:"RunAllTest::testSecurity with data set #862";d:0.032;s:43:"RunAllTest::testSecurity with data set #863";d:0.038;s:43:"RunAllTest::testSecurity with data set #864";d:0.031;s:43:"RunAllTest::testSecurity with data set #865";d:0.035;s:43:"RunAllTest::testSecurity with data set #866";d:0.032;s:43:"RunAllTest::testSecurity with data set #867";d:0.06;s:43:"RunAllTest::testSecurity with data set #868";d:0.038;s:43:"RunAllTest::testSecurity with data set #869";d:0.035;s:43:"RunAllTest::testSecurity with data set #870";d:0.036;s:43:"RunAllTest::testSecurity with data set #871";d:0.035;s:43:"RunAllTest::testSecurity with data set #872";d:0.034;s:43:"RunAllTest::testSecurity with data set #873";d:0.033;s:43:"RunAllTest::testSecurity with data set #874";d:0.035;s:43:"RunAllTest::testSecurity with data set #875";d:0.033;s:43:"RunAllTest::testSecurity with data set #876";d:0.043;s:43:"RunAllTest::testSecurity with data set #877";d:0.037;s:43:"RunAllTest::testSecurity with data set #878";d:0.036;s:43:"RunAllTest::testSecurity with data set #879";d:0.062;s:43:"RunAllTest::testSecurity with data set #880";d:0.034;s:43:"RunAllTest::testSecurity with data set #881";d:0.041;s:43:"RunAllTest::testSecurity with data set #882";d:0.036;s:43:"RunAllTest::testSecurity with data set #883";d:0.034;s:43:"RunAllTest::testSecurity with data set #884";d:0.035;s:43:"RunAllTest::testSecurity with data set #885";d:0.038;s:43:"RunAllTest::testSecurity with data set #886";d:0.042;s:43:"RunAllTest::testSecurity with data set #887";d:0.036;s:43:"RunAllTest::testSecurity with data set #888";d:0.036;s:43:"RunAllTest::testSecurity with data set #889";d:0.035;s:43:"RunAllTest::testSecurity with data set #890";d:0.056;s:43:"RunAllTest::testSecurity with data set #891";d:0.036;s:43:"RunAllTest::testSecurity with data set #892";d:0.037;s:43:"RunAllTest::testSecurity with data set #893";d:0.042;s:43:"RunAllTest::testSecurity with data set #894";d:0.035;s:43:"RunAllTest::testSecurity with data set #895";d:0.035;s:43:"RunAllTest::testSecurity with data set #896";d:0.035;s:43:"RunAllTest::testSecurity with data set #897";d:0.034;s:43:"RunAllTest::testSecurity with data set #898";d:0.035;s:43:"RunAllTest::testSecurity with data set #899";d:0.04;s:43:"RunAllTest::testSecurity with data set #900";d:0.035;s:43:"RunAllTest::testSecurity with data set #901";d:0.069;s:43:"RunAllTest::testSecurity with data set #902";d:0.039;s:43:"RunAllTest::testSecurity with data set #903";d:0.042;s:43:"RunAllTest::testSecurity with data set #904";d:0.037;s:43:"RunAllTest::testSecurity with data set #905";d:0.035;s:43:"RunAllTest::testSecurity with data set #906";d:0.042;s:43:"RunAllTest::testSecurity with data set #907";d:0.036;s:43:"RunAllTest::testSecurity with data set #908";d:0.036;s:43:"RunAllTest::testSecurity with data set #909";d:0.065;s:43:"RunAllTest::testSecurity with data set #910";d:0.034;s:43:"RunAllTest::testSecurity with data set #911";d:0.034;s:43:"RunAllTest::testSecurity with data set #912";d:0.061;s:43:"RunAllTest::testSecurity with data set #913";d:0.041;s:43:"RunAllTest::testSecurity with data set #914";d:0.042;s:43:"RunAllTest::testSecurity with data set #915";d:0.043;s:43:"RunAllTest::testSecurity with data set #916";d:0.045;s:43:"RunAllTest::testSecurity with data set #917";d:0.035;s:43:"RunAllTest::testSecurity with data set #918";d:0.036;s:43:"RunAllTest::testSecurity with data set #919";d:0.036;s:43:"RunAllTest::testSecurity with data set #920";d:0.038;s:43:"RunAllTest::testSecurity with data set #921";d:0.036;s:43:"RunAllTest::testSecurity with data set #922";d:0.063;s:43:"RunAllTest::testSecurity with data set #923";d:0.041;s:43:"RunAllTest::testSecurity with data set #924";d:0.047;s:43:"RunAllTest::testSecurity with data set #925";d:0.049;s:43:"RunAllTest::testSecurity with data set #926";d:0.041;s:43:"RunAllTest::testSecurity with data set #927";d:0.039;s:43:"RunAllTest::testSecurity with data set #928";d:0.046;s:43:"RunAllTest::testSecurity with data set #929";d:0.035;s:43:"RunAllTest::testSecurity with data set #930";d:0.059;s:43:"RunAllTest::testSecurity with data set #931";d:0.042;s:43:"RunAllTest::testSecurity with data set #932";d:0.039;s:43:"RunAllTest::testSecurity with data set #933";d:0.047;s:43:"RunAllTest::testSecurity with data set #934";d:0.04;s:43:"RunAllTest::testSecurity with data set #935";d:0.046;s:43:"RunAllTest::testSecurity with data set #936";d:0.048;s:43:"RunAllTest::testSecurity with data set #937";d:0.056;s:43:"RunAllTest::testSecurity with data set #938";d:0.039;s:43:"RunAllTest::testSecurity with data set #939";d:0.045;s:43:"RunAllTest::testSecurity with data set #940";d:0.039;s:43:"RunAllTest::testSecurity with data set #941";d:0.042;s:43:"RunAllTest::testSecurity with data set #942";d:0.037;s:43:"RunAllTest::testSecurity with data set #943";d:0.037;s:43:"RunAllTest::testSecurity with data set #944";d:0.041;s:43:"RunAllTest::testSecurity with data set #945";d:0.061;s:43:"RunAllTest::testSecurity with data set #946";d:0.044;s:43:"RunAllTest::testSecurity with data set #947";d:0.061;s:43:"RunAllTest::testSecurity with data set #948";d:0.042;s:43:"RunAllTest::testSecurity with data set #949";d:0.044;s:43:"RunAllTest::testSecurity with data set #950";d:0.04;s:43:"RunAllTest::testSecurity with data set #951";d:0.041;s:43:"RunAllTest::testSecurity with data set #952";d:0.038;s:43:"RunAllTest::testSecurity with data set #953";d:0.059;s:43:"RunAllTest::testSecurity with data set #954";d:0.043;s:43:"RunAllTest::testSecurity with data set #955";d:0.042;s:43:"RunAllTest::testSecurity with data set #956";d:0.037;s:43:"RunAllTest::testSecurity with data set #957";d:0.036;s:43:"RunAllTest::testSecurity with data set #958";d:0.04;s:43:"RunAllTest::testSecurity with data set #959";d:0.035;s:43:"RunAllTest::testSecurity with data set #960";d:0.035;s:43:"RunAllTest::testSecurity with data set #961";d:0.042;s:43:"RunAllTest::testSecurity with data set #962";d:0.059;s:43:"RunAllTest::testSecurity with data set #963";d:0.038;s:43:"RunAllTest::testSecurity with data set #964";d:0.042;s:43:"RunAllTest::testSecurity with data set #965";d:0.041;s:43:"RunAllTest::testSecurity with data set #966";d:0.045;s:43:"RunAllTest::testSecurity with data set #967";d:0.042;s:43:"RunAllTest::testSecurity with data set #968";d:0.046;s:43:"RunAllTest::testSecurity with data set #969";d:0.043;s:43:"RunAllTest::testSecurity with data set #970";d:0.05;s:43:"RunAllTest::testSecurity with data set #971";d:0.07;s:43:"RunAllTest::testSecurity with data set #972";d:0.052;s:43:"RunAllTest::testSecurity with data set #973";d:0.041;s:43:"RunAllTest::testSecurity with data set #974";d:0.039;s:43:"RunAllTest::testSecurity with data set #975";d:0.043;s:43:"RunAllTest::testSecurity with data set #976";d:0.043;s:43:"RunAllTest::testSecurity with data set #977";d:0.04;s:43:"RunAllTest::testSecurity with data set #978";d:0.035;s:43:"RunAllTest::testSecurity with data set #979";d:0.035;s:43:"RunAllTest::testSecurity with data set #980";d:0.06;s:43:"RunAllTest::testSecurity with data set #981";d:0.037;s:43:"RunAllTest::testSecurity with data set #982";d:0.05;s:43:"RunAllTest::testSecurity with data set #983";d:0.049;s:43:"RunAllTest::testSecurity with data set #984";d:0.048;s:43:"RunAllTest::testSecurity with data set #985";d:0.047;s:43:"RunAllTest::testSecurity with data set #986";d:0.045;s:43:"RunAllTest::testSecurity with data set #987";d:0.068;s:43:"RunAllTest::testSecurity with data set #988";d:0.05;s:43:"RunAllTest::testSecurity with data set #989";d:0.051;s:43:"RunAllTest::testSecurity with data set #990";d:0.049;s:43:"RunAllTest::testSecurity with data set #991";d:0.065;s:43:"RunAllTest::testSecurity with data set #992";d:0.053;s:43:"RunAllTest::testSecurity with data set #993";d:0.048;s:43:"RunAllTest::testSecurity with data set #994";d:0.037;s:43:"RunAllTest::testSecurity with data set #995";d:0.037;s:43:"RunAllTest::testSecurity with data set #996";d:0.04;s:43:"RunAllTest::testSecurity with data set #997";d:0.044;s:43:"RunAllTest::testSecurity with data set #998";d:0.034;s:43:"RunAllTest::testSecurity with data set #999";d:0.042;s:44:"RunAllTest::testSecurity with data set #1000";d:0.034;s:44:"RunAllTest::testSecurity with data set #1001";d:0.034;s:44:"RunAllTest::testSecurity with data set #1002";d:0.036;s:44:"RunAllTest::testSecurity with data set #1003";d:0.032;s:44:"RunAllTest::testSecurity with data set #1004";d:0.059;s:44:"RunAllTest::testSecurity with data set #1005";d:0.042;s:44:"RunAllTest::testSecurity with data set #1006";d:0.037;s:44:"RunAllTest::testSecurity with data set #1007";d:0.042;s:44:"RunAllTest::testSecurity with data set #1008";d:0.038;s:44:"RunAllTest::testSecurity with data set #1009";d:0.036;s:44:"RunAllTest::testSecurity with data set #1010";d:0.037;s:44:"RunAllTest::testSecurity with data set #1011";d:0.035;s:44:"RunAllTest::testSecurity with data set #1012";d:0.037;s:44:"RunAllTest::testSecurity with data set #1013";d:0.037;s:44:"RunAllTest::testSecurity with data set #1014";d:0.03;s:44:"RunAllTest::testSecurity with data set #1015";d:0.063;s:44:"RunAllTest::testSecurity with data set #1016";d:0.036;s:44:"RunAllTest::testSecurity with data set #1017";d:0.038;s:44:"RunAllTest::testSecurity with data set #1018";d:0.042;s:44:"RunAllTest::testSecurity with data set #1019";d:0.037;s:44:"RunAllTest::testSecurity with data set #1020";d:0.039;s:44:"RunAllTest::testSecurity with data set #1021";d:0.038;s:44:"RunAllTest::testSecurity with data set #1022";d:0.035;s:44:"RunAllTest::testSecurity with data set #1023";d:0.034;s:44:"RunAllTest::testSecurity with data set #1024";d:0.033;s:44:"RunAllTest::testSecurity with data set #1025";d:0.034;s:44:"RunAllTest::testSecurity with data set #1026";d:0.058;s:44:"RunAllTest::testSecurity with data set #1027";d:0.036;s:44:"RunAllTest::testSecurity with data set #1028";d:0.041;s:44:"RunAllTest::testSecurity with data set #1029";d:0.034;s:44:"RunAllTest::testSecurity with data set #1030";d:0.034;s:44:"RunAllTest::testSecurity with data set #1031";d:0.043;s:44:"RunAllTest::testSecurity with data set #1032";d:0.038;s:44:"RunAllTest::testSecurity with data set #1033";d:0.035;s:44:"RunAllTest::testSecurity with data set #1034";d:0.034;s:44:"RunAllTest::testSecurity with data set #1035";d:0.041;s:44:"RunAllTest::testSecurity with data set #1036";d:0.055;s:44:"RunAllTest::testSecurity with data set #1037";d:0.049;s:44:"RunAllTest::testSecurity with data set #1038";d:0.044;s:44:"RunAllTest::testSecurity with data set #1039";d:0.043;s:44:"RunAllTest::testSecurity with data set #1040";d:0.054;s:44:"RunAllTest::testSecurity with data set #1041";d:0.038;s:44:"RunAllTest::testSecurity with data set #1042";d:0.033;s:44:"RunAllTest::testSecurity with data set #1043";d:0.036;s:44:"RunAllTest::testSecurity with data set #1044";d:0.042;s:44:"RunAllTest::testSecurity with data set #1045";d:0.057;s:44:"RunAllTest::testSecurity with data set #1046";d:0.034;s:44:"RunAllTest::testSecurity with data set #1047";d:0.042;s:44:"RunAllTest::testSecurity with data set #1048";d:0.037;s:44:"RunAllTest::testSecurity with data set #1049";d:0.035;s:44:"RunAllTest::testSecurity with data set #1050";d:0.036;s:44:"RunAllTest::testSecurity with data set #1051";d:0.03;s:44:"RunAllTest::testSecurity with data set #1052";d:0.029;s:44:"RunAllTest::testSecurity with data set #1053";d:0.028;s:44:"RunAllTest::testSecurity with data set #1054";d:0.03;s:44:"RunAllTest::testSecurity with data set #1055";d:0.03;s:44:"RunAllTest::testSecurity with data set #1056";d:0.029;s:44:"RunAllTest::testSecurity with data set #1057";d:0.029;s:44:"RunAllTest::testSecurity with data set #1058";d:0.027;s:44:"RunAllTest::testSecurity with data set #1059";d:0.055;s:44:"RunAllTest::testSecurity with data set #1060";d:0.032;s:44:"RunAllTest::testSecurity with data set #1061";d:0.035;s:44:"RunAllTest::testSecurity with data set #1062";d:0.034;s:44:"RunAllTest::testSecurity with data set #1063";d:0.033;s:44:"RunAllTest::testSecurity with data set #1064";d:0.031;s:44:"RunAllTest::testSecurity with data set #1065";d:0.03;s:44:"RunAllTest::testSecurity with data set #1066";d:0.03;s:44:"RunAllTest::testSecurity with data set #1067";d:0.029;s:44:"RunAllTest::testSecurity with data set #1068";d:0.029;s:44:"RunAllTest::testSecurity with data set #1069";d:0.037;s:44:"RunAllTest::testSecurity with data set #1070";d:0.029;s:44:"RunAllTest::testSecurity with data set #1071";d:0.028;s:44:"RunAllTest::testSecurity with data set #1072";d:0.029;s:44:"RunAllTest::testSecurity with data set #1073";d:0.026;s:44:"RunAllTest::testSecurity with data set #1074";d:0.026;s:44:"RunAllTest::testSecurity with data set #1075";d:0.06;s:44:"RunAllTest::testSecurity with data set #1076";d:0.03;s:44:"RunAllTest::testSecurity with data set #1077";d:0.031;s:44:"RunAllTest::testSecurity with data set #1078";d:0.034;s:44:"RunAllTest::testSecurity with data set #1079";d:0.032;s:44:"RunAllTest::testSecurity with data set #1080";d:0.032;s:44:"RunAllTest::testSecurity with data set #1081";d:0.038;s:44:"RunAllTest::testSecurity with data set #1082";d:0.029;s:44:"RunAllTest::testSecurity with data set #1083";d:0.032;s:44:"RunAllTest::testSecurity with data set #1084";d:0.032;s:44:"RunAllTest::testSecurity with data set #1085";d:0.028;s:44:"RunAllTest::testSecurity with data set #1086";d:0.03;s:44:"RunAllTest::testSecurity with data set #1087";d:0.03;s:44:"RunAllTest::testSecurity with data set #1088";d:0.03;s:44:"RunAllTest::testSecurity with data set #1089";d:0.028;s:44:"RunAllTest::testSecurity with data set #1090";d:0.029;s:44:"RunAllTest::testSecurity with data set #1091";d:0.061;s:44:"RunAllTest::testSecurity with data set #1092";d:0.031;s:44:"RunAllTest::testSecurity with data set #1093";d:0.032;s:44:"RunAllTest::testSecurity with data set #1094";d:0.032;s:44:"RunAllTest::testSecurity with data set #1095";d:0.03;s:44:"RunAllTest::testSecurity with data set #1096";d:0.031;s:44:"RunAllTest::testSecurity with data set #1097";d:0.034;s:44:"RunAllTest::testSecurity with data set #1098";d:0.032;s:44:"RunAllTest::testSecurity with data set #1099";d:0.037;s:44:"RunAllTest::testSecurity with data set #1100";d:0.03;s:44:"RunAllTest::testSecurity with data set #1101";d:0.027;s:44:"RunAllTest::testSecurity with data set #1102";d:0.03;s:44:"RunAllTest::testSecurity with data set #1103";d:0.032;s:44:"RunAllTest::testSecurity with data set #1104";d:0.029;s:44:"RunAllTest::testSecurity with data set #1105";d:0.028;s:44:"RunAllTest::testSecurity with data set #1106";d:0.061;s:44:"RunAllTest::testSecurity with data set #1107";d:0.032;s:44:"RunAllTest::testSecurity with data set #1108";d:0.034;s:44:"RunAllTest::testSecurity with data set #1109";d:0.034;s:44:"RunAllTest::testSecurity with data set #1110";d:0.034;s:44:"RunAllTest::testSecurity with data set #1111";d:0.034;s:44:"RunAllTest::testSecurity with data set #1112";d:0.037;s:44:"RunAllTest::testSecurity with data set #1113";d:0.034;s:44:"RunAllTest::testSecurity with data set #1114";d:0.03;s:44:"RunAllTest::testSecurity with data set #1115";d:0.032;s:44:"RunAllTest::testSecurity with data set #1116";d:0.034;s:44:"RunAllTest::testSecurity with data set #1117";d:0.033;s:44:"RunAllTest::testSecurity with data set #1118";d:0.032;s:44:"RunAllTest::testSecurity with data set #1119";d:0.065;s:44:"RunAllTest::testSecurity with data set #1120";d:0.036;s:44:"RunAllTest::testSecurity with data set #1121";d:0.041;s:44:"RunAllTest::testSecurity with data set #1122";d:0.036;s:44:"RunAllTest::testSecurity with data set #1123";d:0.036;s:44:"RunAllTest::testSecurity with data set #1124";d:0.037;s:44:"RunAllTest::testSecurity with data set #1125";d:0.035;s:44:"RunAllTest::testSecurity with data set #1126";d:0.033;s:44:"RunAllTest::testSecurity with data set #1127";d:0.042;s:44:"RunAllTest::testSecurity with data set #1128";d:0.036;s:44:"RunAllTest::testSecurity with data set #1129";d:0.062;s:44:"RunAllTest::testSecurity with data set #1130";d:0.037;s:44:"RunAllTest::testSecurity with data set #1131";d:0.038;s:44:"RunAllTest::testSecurity with data set #1132";d:0.038;s:44:"RunAllTest::testSecurity with data set #1133";d:0.04;s:44:"RunAllTest::testSecurity with data set #1134";d:0.031;s:44:"RunAllTest::testSecurity with data set #1135";d:0.032;s:44:"RunAllTest::testSecurity with data set #1136";d:0.035;s:44:"RunAllTest::testSecurity with data set #1137";d:0.036;s:44:"RunAllTest::testSecurity with data set #1138";d:0.031;s:44:"RunAllTest::testSecurity with data set #1139";d:0.053;s:44:"RunAllTest::testSecurity with data set #1140";d:0.033;s:44:"RunAllTest::testSecurity with data set #1141";d:0.04;s:44:"RunAllTest::testSecurity with data set #1142";d:0.04;s:44:"RunAllTest::testSecurity with data set #1143";d:0.035;s:44:"RunAllTest::testSecurity with data set #1144";d:0.033;s:44:"RunAllTest::testSecurity with data set #1145";d:0.032;s:44:"RunAllTest::testSecurity with data set #1146";d:0.031;s:44:"RunAllTest::testSecurity with data set #1147";d:0.029;s:44:"RunAllTest::testSecurity with data set #1148";d:0.032;s:44:"RunAllTest::testSecurity with data set #1149";d:0.03;s:44:"RunAllTest::testSecurity with data set #1150";d:0.029;s:44:"RunAllTest::testSecurity with data set #1151";d:0.057;s:44:"RunAllTest::testSecurity with data set #1152";d:0.034;s:44:"RunAllTest::testSecurity with data set #1153";d:0.042;s:44:"RunAllTest::testSecurity with data set #1154";d:0.046;s:44:"RunAllTest::testSecurity with data set #1155";d:0.033;s:44:"RunAllTest::testSecurity with data set #1156";d:0.035;s:44:"RunAllTest::testSecurity with data set #1157";d:0.037;s:44:"RunAllTest::testSecurity with data set #1158";d:0.036;s:44:"RunAllTest::testSecurity with data set #1159";d:0.034;s:44:"RunAllTest::testSecurity with data set #1160";d:0.032;s:44:"RunAllTest::testSecurity with data set #1161";d:0.033;s:44:"RunAllTest::testSecurity with data set #1162";d:0.057;s:44:"RunAllTest::testSecurity with data set #1163";d:0.035;s:44:"RunAllTest::testSecurity with data set #1164";d:0.038;s:44:"RunAllTest::testSecurity with data set #1165";d:0.037;s:44:"RunAllTest::testSecurity with data set #1166";d:0.035;s:44:"RunAllTest::testSecurity with data set #1167";d:0.036;s:44:"RunAllTest::testSecurity with data set #1168";d:0.038;s:44:"RunAllTest::testSecurity with data set #1169";d:0.043;s:44:"RunAllTest::testSecurity with data set #1170";d:0.042;s:44:"RunAllTest::testSecurity with data set #1171";d:0.044;s:44:"RunAllTest::testSecurity with data set #1172";d:0.061;s:44:"RunAllTest::testSecurity with data set #1173";d:0.041;s:44:"RunAllTest::testSecurity with data set #1174";d:0.047;s:44:"RunAllTest::testSecurity with data set #1175";d:0.043;s:44:"RunAllTest::testSecurity with data set #1176";d:0.041;s:44:"RunAllTest::testSecurity with data set #1177";d:0.043;s:44:"RunAllTest::testSecurity with data set #1178";d:0.036;s:44:"RunAllTest::testSecurity with data set #1179";d:0.037;s:44:"RunAllTest::testSecurity with data set #1180";d:0.031;s:44:"RunAllTest::testSecurity with data set #1181";d:0.03;s:44:"RunAllTest::testSecurity with data set #1182";d:0.057;s:44:"RunAllTest::testSecurity with data set #1183";d:0.032;s:44:"RunAllTest::testSecurity with data set #1184";d:0.038;s:44:"RunAllTest::testSecurity with data set #1185";d:0.037;s:44:"RunAllTest::testSecurity with data set #1186";d:0.033;s:44:"RunAllTest::testSecurity with data set #1187";d:0.037;s:44:"RunAllTest::testSecurity with data set #1188";d:0.03;s:44:"RunAllTest::testSecurity with data set #1189";d:0.029;s:44:"RunAllTest::testSecurity with data set #1190";d:0.032;s:44:"RunAllTest::testSecurity with data set #1191";d:0.034;s:44:"RunAllTest::testSecurity with data set #1192";d:0.029;s:44:"RunAllTest::testSecurity with data set #1193";d:0.029;s:44:"RunAllTest::testSecurity with data set #1194";d:0.029;s:44:"RunAllTest::testSecurity with data set #1195";d:0.027;s:44:"RunAllTest::testSecurity with data set #1196";d:0.046;s:44:"RunAllTest::testSecurity with data set #1197";d:0.035;s:44:"RunAllTest::testSecurity with data set #1198";d:0.063;s:44:"RunAllTest::testSecurity with data set #1199";d:0.03;s:44:"RunAllTest::testSecurity with data set #1200";d:0.032;s:44:"RunAllTest::testSecurity with data set #1201";d:0.033;s:44:"RunAllTest::testSecurity with data set #1202";d:0.033;s:44:"RunAllTest::testSecurity with data set #1203";d:0.033;s:44:"RunAllTest::testSecurity with data set #1204";d:0.029;s:44:"RunAllTest::testSecurity with data set #1205";d:0.03;s:44:"RunAllTest::testSecurity with data set #1206";d:0.031;s:44:"RunAllTest::testSecurity with data set #1207";d:0.037;s:44:"RunAllTest::testSecurity with data set #1208";d:0.03;s:44:"RunAllTest::testSecurity with data set #1209";d:0.03;s:44:"RunAllTest::testSecurity with data set #1210";d:0.031;s:44:"RunAllTest::testSecurity with data set #1211";d:0.029;s:44:"RunAllTest::testSecurity with data set #1212";d:0.025;s:44:"RunAllTest::testSecurity with data set #1213";d:0.026;s:44:"RunAllTest::testSecurity with data set #1214";d:0.028;s:44:"RunAllTest::testSecurity with data set #1215";d:0.059;s:44:"RunAllTest::testSecurity with data set #1216";d:0.03;s:44:"RunAllTest::testSecurity with data set #1217";d:0.031;s:44:"RunAllTest::testSecurity with data set #1218";d:0.03;s:44:"RunAllTest::testSecurity with data set #1219";d:0.03;s:44:"RunAllTest::testSecurity with data set #1220";d:0.039;s:44:"RunAllTest::testSecurity with data set #1221";d:0.036;s:44:"RunAllTest::testSecurity with data set #1222";d:0.035;s:44:"RunAllTest::testSecurity with data set #1223";d:0.038;s:44:"RunAllTest::testSecurity with data set #1224";d:0.038;s:44:"RunAllTest::testSecurity with data set #1225";d:0.033;s:44:"RunAllTest::testSecurity with data set #1226";d:0.034;s:44:"RunAllTest::testSecurity with data set #1227";d:0.033;s:44:"RunAllTest::testSecurity with data set #1228";d:0.062;s:44:"RunAllTest::testSecurity with data set #1229";d:0.04;s:44:"RunAllTest::testSecurity with data set #1230";d:0.037;s:44:"RunAllTest::testSecurity with data set #1231";d:0.038;s:44:"RunAllTest::testSecurity with data set #1232";d:0.036;s:44:"RunAllTest::testSecurity with data set #1233";d:0.036;s:44:"RunAllTest::testSecurity with data set #1234";d:0.035;s:44:"RunAllTest::testSecurity with data set #1235";d:0.04;s:44:"RunAllTest::testSecurity with data set #1236";d:0.032;s:44:"RunAllTest::testSecurity with data set #1237";d:0.034;s:44:"RunAllTest::testSecurity with data set #1238";d:0.063;s:44:"RunAllTest::testSecurity with data set #1239";d:0.035;s:44:"RunAllTest::testSecurity with data set #1240";d:0.038;s:44:"RunAllTest::testSecurity with data set #1241";d:0.034;s:44:"RunAllTest::testSecurity with data set #1242";d:0.033;s:44:"RunAllTest::testSecurity with data set #1243";d:0.032;s:44:"RunAllTest::testSecurity with data set #1244";d:0.033;s:44:"RunAllTest::testSecurity with data set #1245";d:0.033;s:44:"RunAllTest::testSecurity with data set #1246";d:0.041;s:44:"RunAllTest::testSecurity with data set #1247";d:0.032;s:44:"RunAllTest::testSecurity with data set #1248";d:0.032;s:44:"RunAllTest::testSecurity with data set #1249";d:0.029;s:44:"RunAllTest::testSecurity with data set #1250";d:0.054;s:44:"RunAllTest::testSecurity with data set #1251";d:0.038;s:44:"RunAllTest::testSecurity with data set #1252";d:0.045;s:44:"RunAllTest::testSecurity with data set #1253";d:0.037;s:44:"RunAllTest::testSecurity with data set #1254";d:0.043;s:44:"RunAllTest::testSecurity with data set #1255";d:0.034;s:44:"RunAllTest::testSecurity with data set #1256";d:0.036;s:44:"RunAllTest::testSecurity with data set #1257";d:0.037;s:44:"RunAllTest::testSecurity with data set #1258";d:0.037;s:44:"RunAllTest::testSecurity with data set #1259";d:0.033;s:44:"RunAllTest::testSecurity with data set #1260";d:0.031;s:44:"RunAllTest::testSecurity with data set #1261";d:0.031;s:44:"RunAllTest::testSecurity with data set #1262";d:0.066;s:44:"RunAllTest::testSecurity with data set #1263";d:0.037;s:44:"RunAllTest::testSecurity with data set #1264";d:0.035;s:44:"RunAllTest::testSecurity with data set #1265";d:0.032;s:44:"RunAllTest::testSecurity with data set #1266";d:0.033;s:44:"RunAllTest::testSecurity with data set #1267";d:0.045;s:44:"RunAllTest::testSecurity with data set #1268";d:0.043;s:44:"RunAllTest::testSecurity with data set #1269";d:0.041;s:44:"RunAllTest::testSecurity with data set #1270";d:0.041;s:44:"RunAllTest::testSecurity with data set #1271";d:0.041;s:44:"RunAllTest::testSecurity with data set #1272";d:0.061;s:44:"RunAllTest::testSecurity with data set #1273";d:0.04;s:44:"RunAllTest::testSecurity with data set #1274";d:0.032;s:44:"RunAllTest::testSecurity with data set #1275";d:0.033;s:44:"RunAllTest::testSecurity with data set #1276";d:0.032;s:44:"RunAllTest::testSecurity with data set #1277";d:0.03;s:44:"RunAllTest::testSecurity with data set #1278";d:0.033;s:44:"RunAllTest::testSecurity with data set #1279";d:0.028;s:44:"RunAllTest::testSecurity with data set #1280";d:0.031;s:44:"RunAllTest::testSecurity with data set #1281";d:0.033;s:44:"RunAllTest::testSecurity with data set #1282";d:0.029;s:44:"RunAllTest::testSecurity with data set #1283";d:0.029;s:44:"RunAllTest::testSecurity with data set #1284";d:0.028;s:44:"RunAllTest::testSecurity with data set #1285";d:0.027;s:44:"RunAllTest::testSecurity with data set #1286";d:0.03;s:44:"RunAllTest::testSecurity with data set #1287";d:0.06;s:44:"RunAllTest::testSecurity with data set #1288";d:0.041;s:44:"RunAllTest::testSecurity with data set #1289";d:0.036;s:44:"RunAllTest::testSecurity with data set #1290";d:0.033;s:44:"RunAllTest::testSecurity with data set #1291";d:0.035;s:44:"RunAllTest::testSecurity with data set #1292";d:0.032;s:44:"RunAllTest::testSecurity with data set #1293";d:0.034;s:44:"RunAllTest::testSecurity with data set #1294";d:0.032;s:44:"RunAllTest::testSecurity with data set #1295";d:0.03;s:44:"RunAllTest::testSecurity with data set #1296";d:0.034;s:44:"RunAllTest::testSecurity with data set #1297";d:0.029;s:44:"RunAllTest::testSecurity with data set #1298";d:0.026;s:44:"RunAllTest::testSecurity with data set #1299";d:0.028;s:44:"RunAllTest::testSecurity with data set #1300";d:0.032;s:44:"RunAllTest::testSecurity with data set #1301";d:0.029;s:44:"RunAllTest::testSecurity with data set #1302";d:0.026;s:44:"RunAllTest::testSecurity with data set #1303";d:0.06;s:44:"RunAllTest::testSecurity with data set #1304";d:0.031;s:44:"RunAllTest::testSecurity with data set #1305";d:0.032;s:44:"RunAllTest::testSecurity with data set #1306";d:0.034;s:44:"RunAllTest::testSecurity with data set #1307";d:0.032;s:44:"RunAllTest::testSecurity with data set #1308";d:0.037;s:44:"RunAllTest::testSecurity with data set #1309";d:0.03;s:44:"RunAllTest::testSecurity with data set #1310";d:0.03;s:44:"RunAllTest::testSecurity with data set #1311";d:0.03;s:44:"RunAllTest::testSecurity with data set #1312";d:0.031;s:44:"RunAllTest::testSecurity with data set #1313";d:0.029;s:44:"RunAllTest::testSecurity with data set #1314";d:0.029;s:44:"RunAllTest::testSecurity with data set #1315";d:0.026;s:44:"RunAllTest::testSecurity with data set #1316";d:0.027;s:44:"RunAllTest::testSecurity with data set #1317";d:0.029;s:44:"RunAllTest::testSecurity with data set #1318";d:0.03;s:44:"RunAllTest::testSecurity with data set #1319";d:0.069;s:44:"RunAllTest::testSecurity with data set #1320";d:0.03;s:44:"RunAllTest::testSecurity with data set #1321";d:0.034;s:44:"RunAllTest::testSecurity with data set #1322";d:0.034;s:44:"RunAllTest::testSecurity with data set #1323";d:0.035;s:44:"RunAllTest::testSecurity with data set #1324";d:0.034;s:44:"RunAllTest::testSecurity with data set #1325";d:0.031;s:44:"RunAllTest::testSecurity with data set #1326";d:0.036;s:44:"RunAllTest::testSecurity with data set #1327";d:0.035;s:44:"RunAllTest::testSecurity with data set #1328";d:0.032;s:44:"RunAllTest::testSecurity with data set #1329";d:0.032;s:44:"RunAllTest::testSecurity with data set #1330";d:0.029;s:44:"RunAllTest::testSecurity with data set #1331";d:0.027;s:44:"RunAllTest::testSecurity with data set #1332";d:0.03;s:44:"RunAllTest::testSecurity with data set #1333";d:0.064;s:44:"RunAllTest::testSecurity with data set #1334";d:0.033;s:44:"RunAllTest::testSecurity with data set #1335";d:0.033;s:44:"RunAllTest::testSecurity with data set #1336";d:0.032;s:44:"RunAllTest::testSecurity with data set #1337";d:0.035;s:44:"RunAllTest::testSecurity with data set #1338";d:0.035;s:44:"RunAllTest::testSecurity with data set #1339";d:0.036;s:44:"RunAllTest::testSecurity with data set #1340";d:0.035;s:44:"RunAllTest::testSecurity with data set #1341";d:0.041;s:44:"RunAllTest::testSecurity with data set #1342";d:0.034;s:44:"RunAllTest::testSecurity with data set #1343";d:0.033;s:44:"RunAllTest::testSecurity with data set #1344";d:0.034;s:44:"RunAllTest::testSecurity with data set #1345";d:0.061;s:44:"RunAllTest::testSecurity with data set #1346";d:0.049;s:44:"RunAllTest::testSecurity with data set #1347";d:0.036;s:44:"RunAllTest::testSecurity with data set #1348";d:0.037;s:44:"RunAllTest::testSecurity with data set #1349";d:0.034;s:44:"RunAllTest::testSecurity with data set #1350";d:0.035;s:44:"RunAllTest::testSecurity with data set #1351";d:0.04;s:44:"RunAllTest::testSecurity with data set #1352";d:0.037;s:44:"RunAllTest::testSecurity with data set #1353";d:0.033;s:44:"RunAllTest::testSecurity with data set #1354";d:0.029;s:44:"RunAllTest::testSecurity with data set #1355";d:0.057;s:44:"RunAllTest::testSecurity with data set #1356";d:0.035;s:44:"RunAllTest::testSecurity with data set #1357";d:0.031;s:44:"RunAllTest::testSecurity with data set #1358";d:0.038;s:44:"RunAllTest::testSecurity with data set #1359";d:0.035;s:44:"RunAllTest::testSecurity with data set #1360";d:0.037;s:44:"RunAllTest::testSecurity with data set #1361";d:0.04;s:44:"RunAllTest::testSecurity with data set #1362";d:0.035;s:44:"RunAllTest::testSecurity with data set #1363";d:0.034;s:44:"RunAllTest::testSecurity with data set #1364";d:0.039;s:44:"RunAllTest::testSecurity with data set #1365";d:0.058;s:44:"RunAllTest::testSecurity with data set #1366";d:0.038;s:44:"RunAllTest::testSecurity with data set #1367";d:0.039;s:44:"RunAllTest::testSecurity with data set #1368";d:0.036;s:44:"RunAllTest::testSecurity with data set #1369";d:0.036;s:44:"RunAllTest::testSecurity with data set #1370";d:0.041;s:44:"RunAllTest::testSecurity with data set #1371";d:0.044;s:44:"RunAllTest::testSecurity with data set #1372";d:0.052;s:44:"RunAllTest::testSecurity with data set #1373";d:0.04;s:44:"RunAllTest::testSecurity with data set #1374";d:0.073;s:44:"RunAllTest::testSecurity with data set #1375";d:0.049;s:44:"RunAllTest::testSecurity with data set #1376";d:0.034;s:44:"RunAllTest::testSecurity with data set #1377";d:0.034;s:44:"RunAllTest::testSecurity with data set #1378";d:0.032;s:44:"RunAllTest::testSecurity with data set #1379";d:0.03;s:44:"RunAllTest::testSecurity with data set #1380";d:0.034;s:44:"RunAllTest::testSecurity with data set #1381";d:0.035;s:44:"RunAllTest::testSecurity with data set #1382";d:0.032;s:44:"RunAllTest::testSecurity with data set #1383";d:0.031;s:44:"RunAllTest::testSecurity with data set #1384";d:0.033;s:44:"RunAllTest::testSecurity with data set #1385";d:0.03;s:44:"RunAllTest::testSecurity with data set #1386";d:0.029;s:44:"RunAllTest::testSecurity with data set #1387";d:0.06;s:44:"RunAllTest::testSecurity with data set #1388";d:0.025;s:44:"RunAllTest::testSecurity with data set #1389";d:0.038;s:44:"RunAllTest::testSecurity with data set #1390";d:0.07;s:44:"RunAllTest::testSecurity with data set #1391";d:0.026;s:44:"RunAllTest::testSecurity with data set #1392";d:0.024;s:44:"RunAllTest::testSecurity with data set #1393";d:0.023;s:44:"RunAllTest::testSecurity with data set #1394";d:0.027;s:44:"RunAllTest::testSecurity with data set #1395";d:0.023;s:44:"RunAllTest::testSecurity with data set #1396";d:0.042;s:44:"RunAllTest::testSecurity with data set #1397";d:0.026;s:44:"RunAllTest::testSecurity with data set #1398";d:0.018;s:44:"RunAllTest::testSecurity with data set #1399";d:0.027;s:44:"RunAllTest::testSecurity with data set #1400";d:0.031;s:44:"RunAllTest::testSecurity with data set #1401";d:0.023;s:44:"RunAllTest::testSecurity with data set #1402";d:0.026;s:44:"RunAllTest::testSecurity with data set #1403";d:0.027;s:44:"RunAllTest::testSecurity with data set #1404";d:0.06;s:48:"RunAllFoldersTest::testSecurity with data set #0";d:0.036;s:48:"RunAllFoldersTest::testSecurity with data set #1";d:0.038;s:43:"RunFlowsTest::testSecurity with data set #0";d:0.024;s:43:"RunFlowsTest::testSecurity with data set #1";d:0.026;s:43:"RunFlowsTest::testSecurity with data set #2";d:0.02;s:50:"RunExcludeFilesTest::testSecurity with data set #0";d:0.04;s:50:"RunExcludeFilesTest::testSecurity with data set #1";d:0.022;}}} \ No newline at end of file diff --git a/projects/tests/composer.json b/projects/tests/composer.json index bdc2c2e5..20431f67 100644 --- a/projects/tests/composer.json +++ b/projects/tests/composer.json @@ -15,11 +15,11 @@ } ], "require": { - "php": "^7.0", + "php": ">=7.4", "progpilot/package": "@dev", - "ircmaxell/php-cfg": "1.0.x-dev" + "ircmaxell/php-cfg": "^0.6.0" }, "require-dev": { - "phpunit/phpunit": "^6.0" + "phpunit/phpunit": "^8.0 || ^9.0" } } diff --git a/projects/tests/conditionstest.php b/projects/tests/conditionstest.php index bc6e88c0..2b60617a 100644 --- a/projects/tests/conditionstest.php +++ b/projects/tests/conditionstest.php @@ -20,11 +20,13 @@ ], [ "./tests/conditions/condition7.php", - [["\$tainted", "3", "xss"]] - ], + [["\$notsafe", "7", "xss"], + ["\$tainted", "3", "xss"]] + ]/*, [ "./tests/conditions/condition8.php", - [["\$tainted", "47", "xss"], + [["\$tainted", "40", "xss"], + ["\$tainted", "47", "xss"], ["\$tainted", "53", "xss"]] - ] + ]*/ ]; diff --git a/projects/tests/config1.yml b/projects/tests/config1.yml new file mode 100644 index 00000000..7d0db1bf --- /dev/null +++ b/projects/tests/config1.yml @@ -0,0 +1,41 @@ +inputs: + dev_mode: false + languages: + - php + sources: + keep_defaults: true + config_files: ~ + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folderconfig1/" + exclusions: + - "vendor" + - "node_modules" + - "onefileexcludedtestconfig.php" + - "onefolderexcludedtestconfig" + - "./tests/folders/folderconfig1/sub_folder2" + resolved_includes_file: ~ + false_positives: ~ + +outputs: + tainted_flow: true + include_failures_file: ~ + +options: + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/tests/config2.yml b/projects/tests/config2.yml new file mode 100644 index 00000000..19b29a3c --- /dev/null +++ b/projects/tests/config2.yml @@ -0,0 +1,37 @@ +inputs: + dev_mode: false + languages: + - php + sources: + keep_defaults: true + config_files: ~ + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folderconfig2/fileonly.php" + exclusions: + - "vendor" + resolved_includes_file: ~ + false_positives: ~ + +outputs: + tainted_flow: true + include_failures_file: ~ + +options: + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/tests/config3.yml b/projects/tests/config3.yml new file mode 100644 index 00000000..7d9f06f5 --- /dev/null +++ b/projects/tests/config3.yml @@ -0,0 +1,39 @@ +inputs: + dev_mode: false + languages: + - php + sources: + keep_defaults: true + config_files: + - "./tests/folders/folderconfig3/sources.json" + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folderconfig3/" + exclusions: + - "vendor" + - "node_modules" + resolved_includes_file: ~ + false_positives: ~ + +outputs: + tainted_flow: true + include_failures_file: ~ + +options: + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/tests/config4.yml b/projects/tests/config4.yml new file mode 100644 index 00000000..9f8b07a2 --- /dev/null +++ b/projects/tests/config4.yml @@ -0,0 +1,39 @@ +inputs: + dev_mode: false + languages: + - php + sources: + keep_defaults: false + config_files: + - "./tests/folders/folderconfig4/sources.json" + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folderconfig4/" + exclusions: + - "vendor" + - "node_modules" + resolved_includes_file: ~ + false_positives: ~ + +outputs: + tainted_flow: true + include_failures_file: ~ + +options: + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/tests/config5.yml b/projects/tests/config5.yml new file mode 100644 index 00000000..4a276205 --- /dev/null +++ b/projects/tests/config5.yml @@ -0,0 +1,38 @@ +inputs: + dev_mode: false + languages: + - php + sources: + keep_defaults: true + config_files: ~ + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folderconfig5/" + exclusions: + - "vendor" + - "node_modules" + resolved_includes_file: ~ + false_positives: "./tests/folders/folderconfig5/false_positives.json" + +outputs: + tainted_flow: true + include_failures_file: ~ + +options: + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/tests/config6.yml b/projects/tests/config6.yml new file mode 100644 index 00000000..5a29d22f --- /dev/null +++ b/projects/tests/config6.yml @@ -0,0 +1,39 @@ +inputs: + dev_mode: false + languages: + - php + sources: + keep_defaults: true + config_files: ~ + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folderconfig6/" + - "./tests/folders/folderconfig6/vendor/" + exclusions: + - "vendor" + - "node_modules" + resolved_includes_file: ~ + false_positives: ~ + +outputs: + tainted_flow: true + include_failures_file: ~ + +options: + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/tests/config7.yml b/projects/tests/config7.yml new file mode 100644 index 00000000..8e79c711 --- /dev/null +++ b/projects/tests/config7.yml @@ -0,0 +1,36 @@ +inputs: + dev_mode: false + languages: + - php + sources: + keep_defaults: true + config_files: ~ + sinks: + keep_defaults: true + config_files: ~ + validators: + keep_defaults: true + config_files: ~ + sanitizers: + keep_defaults: true + config_files: ~ + customrules: + keep_defaults: true + config_files: ~ + inclusions: + - "./tests/folders/folderconfig7/mix3.php" + exclusions: ~ + resolved_includes_file: "./tests/folders/folderconfig7/resolved_includes.json" + false_positives: ~ + +outputs: + tainted_flow: true + include_failures_file: "./tests/folders/folderconfig7/include_failures.json" + +options: + analyze_includes: true + debug_mode: false + pretty_print: true + max_file_analysis_duration: 30 + max_definitions: 500 + max_file_size: 1000000 diff --git a/projects/tests/configtest.php b/projects/tests/configtest.php new file mode 100644 index 00000000..c947679c --- /dev/null +++ b/projects/tests/configtest.php @@ -0,0 +1,68 @@ +member1", "9", "xss"]] + [["\$inst->member1", "10", "xss"]] ], [ "./tests/data/source12.php", @@ -111,8 +107,8 @@ ], [ "./tests/data/source15.php", - [["\$inst->object->member1", "18", "xss"], - ["\$inst1->object->member1", "44", "xss"]] + [["\$member1", "10", "xss"], + ["\$member1", "36", "xss"]] ], [ "./tests/data/source16.php", @@ -133,7 +129,11 @@ ], [ "./tests/data/source20.php", - [["\$comment->ddd", "23", "xss"]] + [["\$b", "4", "xss"]] + ], + [ + "./tests/data/source21.php", + [["\$title", "0", "xss"]] ], [ "./tests/data/sanitizer1.php", @@ -172,5 +172,62 @@ [ "./tests/data/sanitizer9.php", [["\$tainted3", "19", "xss"]] + ], + [ + "./tests/data/sanitizer10.php", + [] + ], + [ + "./tests/data/sanitizer11.php", + [] + ] + /*, + // need support of proper validator propagation + [ + "./tests/data/validator1.php", + [["\$tainted3", "19", "xss"]] + ] + */, + [ + "./tests/data/validator2.php", + [["\$tainted", "3", "xss"], + ["\$tainted", "3", "xss"]] + ], + [ + "./tests/data/validator3.php", + [["\$tainted", "3", "xss"], + ["\$tainted", "3", "xss"]] + ], + [ + "./tests/data/validator4.php", + [["\$tainted", "3", "xss"]] + ], + [ + "./tests/data/validator5.php", + [["\$tainted", "3", "xss"]] + ], + [ + "./tests/data/validator6.php", + [["\$show_updated", "3", "xss"]] + ], + [ + "./tests/data/customvalidator1.php", + [["\$a", "25", "file_inclusion"]] + ], + [ + "./tests/data/customvalidator2.php", + [["\$a", "14", "file_inclusion"]] + ], + [ + "./tests/data/customvalidator3.php", + [["\$a", "16", "file_inclusion"]] + ], + [ + "./tests/data/customvalidator4.php", + [["\$a", "16", "file_inclusion"]] + ], + [ + "./tests/data/customvalidator5.php", + [["\$a", "16", "file_inclusion"]] ] ]; diff --git a/projects/tests/flowstest.php b/projects/tests/flowstest.php index 633e185d..3605ab4b 100644 --- a/projects/tests/flowstest.php +++ b/projects/tests/flowstest.php @@ -11,7 +11,7 @@ ], [ "./tests/flows/flow2.php", - [["\$this->object1", "6"], + [["\$this->object1", "15"], ["\$val", "8"], ["\$_GET[\"p\"]", "21"]] ], @@ -19,5 +19,20 @@ "./tests/flows/flow3.php", [["\$var1", "3"], ["\$_GET[\"p1\"]", "3"]] + ], + [ + "./tests/flows/flow4.php", + [["\$plugin_basename_return", "6"], + ["\$file", "5"], + ["\$preg_replace_return", "5"], + ["\$preg_replace_param2_line5_column101_progpilot", "5"], + ["\$file", "4"], + ["\$preg_replace_return", "4"], + ["\$preg_replace_param2_line4_column51_progpilot", "4"], + ["\$file", "3"], + ["\$plugin_page", "20"], + ["\$stripslashes_return", "20"], + ["\$stripslashes_param0_line20_column417_progpilot", "20"], + ["\$_GET[\"page\"]", "20"]] ] ]; diff --git a/projects/tests/folderexcludedtest.php b/projects/tests/folderexcludedtest.php index 61752b82..2ffc5786 100644 --- a/projects/tests/folderexcludedtest.php +++ b/projects/tests/folderexcludedtest.php @@ -3,12 +3,23 @@ return [ [ "./tests/folders/folder1", - [["\$var1[0]", '4', "xss"], + [["\$var1[0]", '9', "xss"], ["\$var1", '6', "xss"], ["\$var2", '9', "xss"]] ], [ "./tests/folders/folder2", - [["\$var1[0]", "4", "xss"]] + [["\$var1[0]", "9", "xss"]] + ], + [ + "./tests/folders/folder3", + [["\$var2", "9", "xss"], + ["\$var1[0]", "9", "xss"], + ["\$var1", "6", "xss"]] + ], + [ + "./tests/folders/folder4", + [["\$_GET[\"p2\"]", "3", "xss"], + ["\$_GET[\"p4\"]", "3", "xss"]] ] ]; diff --git a/projects/tests/foldertest.php b/projects/tests/foldertest.php index 9ad22dfc..41df381d 100644 --- a/projects/tests/foldertest.php +++ b/projects/tests/foldertest.php @@ -1,16 +1,46 @@ last_query", "102", "xss"]] + ]/*, + [ + "./tests/real/wordpress3", + [["\$query", "131", "sql_injection"], + ["\$this->last_query", "101", "xss"], + ["\$query", "131", "sql_injection"], + ["\$this->last_query", "101", "xss"]] + ]*/, + [ + "./tests/real/namespaces1", + [["\$tainted", "7", "xss"], + ["\$tainted", "7", "xss"]] + ], + [ + "./tests/real/incallstack/", + [["\$rtrim_return", "77", "xss"]] ] ]; diff --git a/projects/tests/frameworkstest.php b/projects/tests/frameworkstest.php index fe883bc6..a38b1ba1 100644 --- a/projects/tests/frameworkstest.php +++ b/projects/tests/frameworkstest.php @@ -3,7 +3,7 @@ return [ [ "./tests/frameworks/codeigniter1.php", - [["\$_GET[\"p\"]", '6', "xss"]] + [["\$data[\"title\"]", '8', "xss"]] ], [ "./tests/frameworks/codeigniter2.php", @@ -15,7 +15,7 @@ ], [ "./tests/frameworks/symfony1.php", - [["5", "67", "security misconfiguration"], + [["5", "79", "security misconfiguration"], ["11", "183", "security misconfiguration"]] ] ]; diff --git a/projects/tests/generictest.php b/projects/tests/generictest.php index 5304bc15..50a52b93 100644 --- a/projects/tests/generictest.php +++ b/projects/tests/generictest.php @@ -19,11 +19,11 @@ ], [ "./tests/generic/alias5.php", - [["\$var5[\"t\"]", "3", "xss"]] + [["\$var5[\"t\"]", "6", "xss"]] ], [ "./tests/generic/mix1.php", - [["\$var1[0]", "4", "xss"]] + [["\$var1[0]", "9", "xss"]] ], [ "./tests/generic/mix2.php", @@ -51,7 +51,7 @@ ], [ "./tests/generic/simple5.php", - [["\$myvar1[11]", "3", "xss"]] + [["\$myvar1[11]", "5", "xss"]] ], [ "./tests/generic/simple6.php", @@ -63,8 +63,8 @@ ], [ "./tests/generic/simple8.php", - [["\$var_gauche[0]", "3", "xss"], - ["\$ret[0]", "8", "xss"]] + [["\$var_gauche[0]", "5", "xss"], + ["\$ret[0]", "10", "xss"]] ], [ "./tests/generic/simple9.php", @@ -94,24 +94,24 @@ [ "./tests/generic/arrays1.php", - [["\$newmyarr[11][9865]", "4", "xss"]] + [["\$newmyarr[11][9865]", "7", "xss"]] ], [ "./tests/generic/arrays2.php", - [["\$newmyarr[11][9865]", "3", "xss"]] + [["\$newmyarr[11][9865]", "4", "xss"]] ], [ "./tests/generic/arrays3.php", - [["\$newmyarr[11]", "4", "xss"]] + [["\$newmyarr[11]", "7", "xss"]] ], [ "./tests/generic/arrays4.php", - [["\$newmyarr[11]", "4", "xss"]] + [["\$copy[11]", "8", "xss"]] ], [ "./tests/generic/arrays5.php", - [["\$var1", "3", "xss"]] + [["\$newmyarr[11][9865]", "6", "xss"]] ], [ "./tests/generic/arrays6.php", @@ -119,27 +119,27 @@ ], [ "./tests/generic/arrays7.php", - [["\$onearr[9865]", "3", "xss"]] + [["\$testarr[11][9865]", "7", "xss"]] ], [ "./tests/generic/arrays8.php", - [["\$arr[1113]", "6", "xss"]] + [["\$arr[1113]", "9", "xss"]] ], [ "./tests/generic/arrays9.php", - [["\$onearr[11][6661]", "4", "xss"]] + [["\$testarr[6661]", "8", "xss"]] ], [ "./tests/generic/arrays10.php", - [["\$arr[0]", "3", "xss"]] + [["\$arr[0]", "5", "xss"]] ], [ "./tests/generic/arrays11.php", - [["\$arr[0][2]", "3", "xss"]] + [["\$arr[0][1]", "7", "xss"]] ], [ "./tests/generic/arrays12.php", - [["\$var1[1]", "6", "xss"]] + [["\$var2[1]", "13", "xss"]] ], [ "./tests/generic/arrays13.php", @@ -147,35 +147,52 @@ ], [ "./tests/generic/arrays14.php", - [["\$var1[11][1]", "6", "xss"]] + [["\$var2[11][1]", "12", "xss"]] ], [ "./tests/generic/arrays15.php", - [["\$var", "8", "xss"]] + [["\$var1", "8", "xss"]] ], [ "./tests/generic/arrays16.php", - [["\$var[\"t\"]", "3", "xss"]] + [["\$var[\"t\"]", "5", "xss"]] + ], + [ + "./tests/generic/arrays17.php", + [["\$values[0]", "7", "xss"]] + ], + [ + "./tests/generic/arrays18.php", + [["\$var5[\"t\"]", "5", "xss"]] + ], + [ + "./tests/generic/arrays19.php", + [["\$var5[\"t\"]", "6", "xss"], + ["\$var5[\"t\"]", "14", "xss"]] + ], + [ + "./tests/generic/arrays20.php", + [["\$a", "3", "xss"]] ], [ "./tests/generic/arraysrec1.php", - [["\$var1[1]", "8", "xss"]] + [["\$var1[1]", "10", "xss"]] ], [ "./tests/generic/arraysexpr1.php", - [["\$newmyarr[2]", "3", "xss"]] + [["\$newmyarr[2]", "7", "xss"]] ], [ "./tests/generic/arraysexpr2.php", - [["\$newmyarr[2][1][2]", "3", "xss"]] + [["\$newmyarr[2][1][2]", "6", "xss"]] ], [ "./tests/generic/arraysexpr3.php", - [["\$onearr[11][6661]", "3", "xss"]] + [["\$newmyarr[2][2][11][6661]", "10", "xss"]] ], [ "./tests/generic/arraysexpr4.php", - [["\$var_main[\"TEST1\"]", "4", "xss"]] + [["\$var_main[\"TEST1\"]", "10", "xss"]] ], [ "./tests/generic/functions1.php", @@ -183,11 +200,11 @@ ], [ "./tests/generic/functions2.php", - [["\$safea", "8", "xss"]] + [["\$safea[0]", "10", "xss"]] ], [ "./tests/generic/functions3.php", - [["\$testf_return[0]", "7", "xss"]] + [["\$arraysafe[0]", "14", "xss"]] ], [ "./tests/generic/functions4.php", @@ -199,7 +216,9 @@ ], [ "./tests/generic/functions6.php", - [["\$_GET[\"p\"]", "15", "xss"]] + [["\$_GET[\"p\"]", "5", "xss"], + ["\$_GET[\"p\"]", "10", "xss"], + ["\$_GET[\"p\"]", "15", "xss"]] ], [ "./tests/generic/functions7.php", @@ -211,23 +230,23 @@ ], [ "./tests/generic/functions9.php", - [["\$arr[8989][1]", "5", "xss"]] + [["\$ret[0][8989][1]", "12", "xss"]] ], [ "./tests/generic/functions10.php", - [["\$arr[8989][1][989]", "5", "xss"]] + [["\$ret[0][1][989]", "12", "xss"]] ], [ "./tests/generic/functions11.php", - [["\$ret[0]", "8", "xss"]] + [["\$ret[0]", "10", "xss"]] ], [ "./tests/generic/functions12.php", - [["\$arr[8989][1][989]", "5", "xss"]] + [["\$ret_gauche[0][8989][1][989]", "12", "xss"]] ], [ "./tests/generic/functions13.php", - [["\$testf1_return[0]", "5", "xss"]] + [["\$ret[0][0]", "10", "xss"]] ], [ "./tests/generic/functions14.php", @@ -235,11 +254,11 @@ ], [ "./tests/generic/functions15.php", - [["\$var[1][12]", "9", "xss"]] + [["\$var_param[1][12]", "5", "xss"]] ], [ "./tests/generic/functions16.php", - [["\$var[1][12]", "9", "xss"]] + [["\$var_param[1][12]", "5", "xss"]] ], [ "./tests/generic/functions17.php", @@ -251,11 +270,28 @@ ], [ "./tests/generic/functions19.php", - [["\$testf1_param0_line10_column65_progpilot[\"test\"]", "10", "xss"]] + [["\$param2[\"test\"]", "6", "xss"]] + ], + [ + "./tests/generic/functions20.php", + [["\$thisfunctionexistandreturn_return", "23", "xss"]] + ], + [ + "./tests/generic/functions21.php", + [["\$ret", "16", "xss"]] + ], + [ + "./tests/generic/functions22.php", + [["\$ret1", "10", "xss"], + ["\$ret2", "14", "xss"]] + ], + [ + "./tests/generic/functions23.php", + [["\$b", "19", "xss"]] ], [ "./tests/generic/functionsrec1.php", - [["\$var", "10", "xss"]] + [["\$var", "15", "xss"]] ], [ "./tests/generic/strings1.php", @@ -265,8 +301,6 @@ ["\$id1", "19", "sql_injection"], ["\$id2", "22", "sql_injection"], ["\$id3", "25", "sql_injection"], - ["\$id5", "31", "sql_injection"], - ["\$id7", "35", "sql_injection"], ["\$tainted1", "48", "xss"], ["\$tainted2", "51", "xss"], ["\$tainted3", "54", "xss"], @@ -276,7 +310,7 @@ ], [ "./tests/generic/foreach1.php", - [["\$array_value", "6", "xss"]] + [["\$array_value", "12", "xss"]] ], [ @@ -288,12 +322,12 @@ "./tests/generic/global2.php", [["\$myvar1", "4", "xss"]] ], - +/* // removed because optimizations [ "./tests/generic/global3.php", [["\$myvar1", "3", "xss"]] ], - +*/ [ "./tests/generic/namespace1.php", [["\$_GET[\"p\"]", "9", "xss"]] @@ -317,6 +351,22 @@ [ "./tests/generic/calluserfunc4.php", [["\$a", "3", "xss"]] + ], + [ + "./tests/generic/loop1.php", + [["\$file", "10", "xss"]] + ], + [ + "./tests/generic/loop2.php", + [["\$row->title", "7", "xss"]] + ], + [ + "./tests/generic/loop3.php", + [["\$row->title", "5", "xss"]] + ], + [ + "./tests/generic/loop4.php", + [["\$this->last_query", "6", "xss"]] ] ]; diff --git a/projects/tests/graphtest.dot b/projects/tests/graphtest.dot deleted file mode 100644 index 204c97d7..00000000 --- a/projects/tests/graphtest.dot +++ /dev/null @@ -1,1184 +0,0 @@ -[0] enter_func -name = {main} -[1] enter_block -id = a121a959c6502ed2e0c52e64059062307b865f5392902193636f1bc8c6884637 -[2] enter_block -id = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 -[3] start_assign -[4] start_expression -[5] start_assign -[6] start_expression -[7] temporarySimple -def id 11 :: \ - name = result :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -[8] end_expression -expression et tainted = -[9] end_assign -[10] definition -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -[11] funccall -name = mysql_fetch_object -[12] end_expression -expression et tainted = -[13] end_assign -[14] definition -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -[15] condition_start_if -[16] enter_block -id = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 -[17] start_expression -[18] start_assign -[19] start_expression -[20] temporarySimple -def id 22 :: \ - name = row :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = 1 :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -property : ->fullname -class_name : -visibility : public -__________________________________________ - - -[21] end_expression -expression et tainted = -[22] end_assign -[23] definition -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -[24] funccall -name = echo -[25] end_expression -expression et tainted = -[26] enter_block -id = 367fcacffd5660455af447204e8fce47cec8756a520978f357f3f78c58fd5aa1 -[27] leave_block -id = 367fcacffd5660455af447204e8fce47cec8756a520978f357f3f78c58fd5aa1 -[28] leave_block -id = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 -[29] enter_block -id = 62588e91d0de623b8152991465814a808eba78be1f76dfb9723d0ad08d6706c6 -[30] enter_block -id = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 -[31] start_assign -[32] start_expression -[33] temporarySimple -def id 28 :: \ - name = 1 :: \ - line = 3 :: column = 7 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 :: \ - cast = cast_string -last_known_value : -array(1) { - [0]=> - string(1) "1" -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -[34] end_expression -expression et tainted = -[35] end_assign -[36] definition -def id 30 :: \ - name = {main}_return :: \ - line = 3 :: column = 7 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -[37] return -[38] leave_block -id = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 -[39] leave_block -id = 62588e91d0de623b8152991465814a808eba78be1f76dfb9723d0ad08d6706c6 -[40] leave_block -id = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 -[41] leave_block -id = a121a959c6502ed2e0c52e64059062307b865f5392902193636f1bc8c6884637 -[42] leave_func -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -Opcodes::TEMPORARY -def id 22 :: \ - name = row :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = 1 :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -property : ->fullname -class_name : -visibility : public -__________________________________________ - - -Opcodes::TEMPORARY FOREACH -def id 22 :: \ - name = row :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = 1 :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -property : ->fullname -class_name : -visibility : public -__________________________________________ - - -Opcodes::FUNC_CALL ------------------------------> echo -funccallSource func_name = echo -array(0) { -} -source name = 'mysql_fetch_object' -funccallSource func_name = echo -array(0) { -} -source name = 'mysql_fetch_object' -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - bool(false) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - bool(false) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -Opcodes::TEMPORARY -def id 11 :: \ - name = result :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -Opcodes::TEMPORARY FOREACH -def id 11 :: \ - name = result :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -Opcodes::FUNC_CALL ------------------------------> mysql_fetch_object -funccallSource func_name = mysql_fetch_object -array(0) { -} -source name = 'mysql_fetch_object' -SOURCE IS OBJECT -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = 1 :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -property value : PROGPILOT_ALL_PROPERTIES_TAINTED -instance : -object id : -1 -__________________________________________ - - -funccallSource func_name = mysql_fetch_object -array(0) { -} -source name = 'mysql_fetch_object' -SOURCE IS OBJECT -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = 1 :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -property value : PROGPILOT_ALL_PROPERTIES_TAINTED -instance : -object id : -1 -__________________________________________ - - -selectDefinitions FOREACH -def id 30 :: \ - name = {main}_return :: \ - line = 3 :: column = 7 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - bool(false) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = 1 :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -property value : PROGPILOT_ALL_PROPERTIES_TAINTED -instance : -object id : -1 -__________________________________________ - - -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - bool(false) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 30 :: \ - name = {main}_return :: \ - line = 3 :: column = 7 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 8 :: \ - name = mysql_fetch_object_param0_line3_column13_progpilot :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - bool(false) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -selectDefinitions FOREACH -def id 13 :: \ - name = row :: \ - line = 3 :: column = 13 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = 1 :: \ - is_const = :: \ - blockid = 8c82cdbbcb5bb74ab4bd7baf99d9b56cd9c513bdfbfa6c1c7d32794c19fd9ac3 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(0) { -} -type_sanitized : -array(0) { -} -property value : PROGPILOT_ALL_PROPERTIES_TAINTED -instance : -object id : -1 -__________________________________________ - - -selectDefinitions FOREACH -def id 19 :: \ - name = echo_param0_line4_column60_progpilot :: \ - line = 4 :: column = 60 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = fefa1e5e8c09917c7bd52cd210658eba264c6f35f873d5e341060c59664ed779 :: \ - cast = cast_string -last_known_value : -array(0) { -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - bool(false) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -Opcodes::TEMPORARY -def id 28 :: \ - name = 1 :: \ - line = 3 :: column = 7 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 :: \ - cast = cast_string -last_known_value : -array(1) { - [0]=> - string(1) "1" -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -__________________________________________ - - -Opcodes::TEMPORARY FOREACH -def id 28 :: \ - name = 1 :: \ - line = 3 :: column = 7 :: \ - tainted = :: \ - ref = :: \ - is_property = :: \ - is_static_property = :: \ - isInstance = :: \ - is_const = :: \ - blockid = af852e868eafafbf5043fb141dca2cdf4af66e80680500d70067f933a871d889 :: \ - cast = cast_string -last_known_value : -array(1) { - [0]=> - string(1) "1" -} -is_embeddedbychar : -array(3) { - ["'"]=> - bool(false) - ["<"]=> - bool(false) - [">"]=> - int(0) -} -type_sanitized : -array(0) { -} -__________________________________________ - - - -digraph callgraph { -ordering=out; -node_092c79e8f80e559e404bcf660c48f3522b67aba9ff1484b0367e1a4ddef7431d [label="echo"]; -node_7288ab64f943bc31319375cc1ce4f0142ef44369c7d13f367eba7669867ab3d7 [label="mysql_fetch_object"]; - - -} - diff --git a/projects/tests/graphtest.php b/projects/tests/graphtest.php index 44c8f3ef..53ffe954 100644 --- a/projects/tests/graphtest.php +++ b/projects/tests/graphtest.php @@ -2,41 +2,36 @@ require_once './vendor/autoload.php'; -try { - //$file = "./tests/graphs/functionsgraph1.php"; - $file = "./tests/data/source16.php"; - $context = new \progpilot\Context; - $analyzer = new \progpilot\Analyzer; +$file = "./tests/data/source16.php"; +$context = new \progpilot\Context; +$analyzer = new \progpilot\Analyzer; - $context->inputs->setFile($file); - $analyzer->run($context); +$context->inputs->setFile($file); +$analyzer->run($context); - $graphCfgJson = $context->outputs->getCfg(); - $graphCallgraphJson = $context->outputs->getCallGraph(); +$graphCfgJson = $context->outputs->getCfg(); +$graphCallgraphJson = $context->outputs->getCallGraph(); - echo "\ndigraph callgraph {\nordering=out;\n"; - foreach ($graphCallgraphJson["nodes"] as $node) { - echo "node_".$node["id"]." [label=\"".$node["name"]."\"];\n"; - } +echo "\ndigraph callgraph {\nordering=out;\n"; +foreach ($graphCallgraphJson["nodes"] as $node) { + echo "node_".$node["id"]." [label=\"".$node["name"]."\"];\n"; +} - foreach ($graphCallgraphJson["links"] as $link) { - echo "node_".$link["source"]."->"."node_".$link["target"]."\n"; - } +foreach ($graphCallgraphJson["links"] as $link) { + echo "node_".$link["source"]."->"."node_".$link["target"]."\n"; +} - echo "\n\n}\n\n"; +echo "\n\n}\n\n"; - /* - echo "\ndigraph cfg {\nordering=out;\n"; - foreach ($graphCfgJson["nodes"] as $node) { - echo "node_".$node["id"]." [label=\"".$node["name"]."\"];\n"; - } +/* +echo "\ndigraph cfg {\nordering=out;\n"; +foreach ($graphCfgJson["nodes"] as $node) { + echo "node_".$node["id"]." [label=\"".$node["name"]."\"];\n"; +} - foreach ($graphCfgJson["links"] as $link) { - echo "node_".$link["source"]."->"."node_".$link["target"]."\n"; - } - - echo "\n\n}\n\n"; - */ -} catch (\RuntimeException $e) { - echo 'Exception : ', $e->getMessage(), "\n"; +foreach ($graphCfgJson["links"] as $link) { + echo "node_".$link["source"]."->"."node_".$link["target"]."\n"; } + +echo "\n\n}\n\n"; +*/ diff --git a/projects/tests/graphtest2.dot b/projects/tests/graphtest2.dot deleted file mode 100644 index 79262739..00000000 --- a/projects/tests/graphtest2.dot +++ /dev/null @@ -1,61 +0,0 @@ - -digraph cfg { -ordering=out; -node_a88f2bd4d86f39934d32cf755720f84631895fc614329120f7c1e4c8898d33ce [label="enter_func {main} -enter_block -start_expression -funccall secret -end_expression -start_expression -temporary_simple -end_expression -definition -leave_block -leave_func -"]; -node_26eed28db0595c1b5b0a27f15f60d6591bde2830db88ebe7155439b6430704e6 [label="enter_func secret -enter_block -start_expression -funccall dev_iam_authenticated -end_expression -start_expression -funccall rand -end_expression -leave_block -leave_block -leave_block -leave_func -"]; -node_bc2cc2132405f78753093324c428e6bd8573cda0032e1f49b487b0377998796b [label="enter_block -start_expression -funccall dev_iam_rights -end_expression -leave_block -"]; -node_235e69617f2088e0c1bcb7cad4d44e583956f5f444e377667bfdd424e063ceab [label="enter_block -start_expression -start_expression -funccall dev_retrieve_secret -end_expression -definition -funccall var_dump -end_expression -start_expression -temporary_simple -end_expression -definition -"]; -node_0224bc2af5eaa39cde0e1a84d2c5619d1aad9d775f17ccc155c598a41cd1c4dd [label="enter_block -start_expression -funccall nada -end_expression -"]; -node_26eed28db0595c1b5b0a27f15f60d6591bde2830db88ebe7155439b6430704e6->node_bc2cc2132405f78753093324c428e6bd8573cda0032e1f49b487b0377998796b -node_bc2cc2132405f78753093324c428e6bd8573cda0032e1f49b487b0377998796b->node_235e69617f2088e0c1bcb7cad4d44e583956f5f444e377667bfdd424e063ceab -node_0224bc2af5eaa39cde0e1a84d2c5619d1aad9d775f17ccc155c598a41cd1c4dd->node_235e69617f2088e0c1bcb7cad4d44e583956f5f444e377667bfdd424e063ceab -node_26eed28db0595c1b5b0a27f15f60d6591bde2830db88ebe7155439b6430704e6->node_0224bc2af5eaa39cde0e1a84d2c5619d1aad9d775f17ccc155c598a41cd1c4dd -node_a88f2bd4d86f39934d32cf755720f84631895fc614329120f7c1e4c8898d33ce->node_26eed28db0595c1b5b0a27f15f60d6591bde2830db88ebe7155439b6430704e6 - - -} - diff --git a/projects/tests/graphtest2.png b/projects/tests/graphtest2.png deleted file mode 100644 index f233473d..00000000 Binary files a/projects/tests/graphtest2.png and /dev/null differ diff --git a/projects/tests/include_files.json b/projects/tests/include_files.json index baba29f4..a33552f0 100644 --- a/projects/tests/include_files.json +++ b/projects/tests/include_files.json @@ -1,10 +1,6 @@ { - "include_files": + "inclusions": [ - "./tests/folders/folder2/mix3.php" - ], - "include_folders": - [ - "./tests/folders/folder2/sub_folder1/sub_folder2" + "../../../../vulnapps/laravel8/" ] } diff --git a/projects/tests/includetest.php b/projects/tests/includetest.php index b7d20aea..48229fe5 100644 --- a/projects/tests/includetest.php +++ b/projects/tests/includetest.php @@ -40,13 +40,13 @@ "./tests/includes/simple8.php", [["\$_GET[\"p\"]", "3", "xss"]] ], - [ "./tests/includes/simple9.php", - [["158", "3442", "security misconfiguration"], - ["159", "3521", "security misconfiguration"], - ["\$pLocation", "505", "header_injection"], - ["\$page[\"body\"]", "51", "xss"]] + [["158", "3599", "security misconfiguration"], + ["158", "3599", "security misconfiguration"], + ["159", "3679", "security misconfiguration"], + ["159", "3679", "security misconfiguration"], + ["\$pPage[\"body\"]", "329", "xss"]] ], [ "./tests/includes/simple10.php", @@ -60,7 +60,7 @@ [ "./tests/includes/simple12.php", - [["\${main}_return[\"cb36d7468e442c354c5037bbb4d59b1c\"]", "7", "xss"]] + [["\$ret[\"cb36d7468e442c354c5037bbb4d59b1c\"]", "5", "xss"]] ], [ @@ -86,5 +86,20 @@ [ "./tests/includes/simple17.php", [["\$var", "3", "xss"]] + ], + + [ + "./tests/includes/simple18.php", + [["\$_GET[\"p\"]", "3", "xss"]] + ], + + [ + "./tests/includes/simple19.php", + [["\$_GET[\"p\"]", "3", "xss"]] + ], + + [ + "./tests/includes/simple20.php", + [["\$comments->post", "6", "xss"]] ] ]; diff --git a/projects/tests/negativetest.php b/projects/tests/negativetest.php index ec955ccf..10edf678 100644 --- a/projects/tests/negativetest.php +++ b/projects/tests/negativetest.php @@ -2,6 +2,27 @@ return [ [ - "./tests/negative/parser_error1..php", [] - ] + "./tests/negative/parser_error1.php", [] + ], + [ + "./tests/negative/parser_error2.php", [] + ], + [ + "./tests/negative/parser_error3.php", [] + ], + [ + "./tests/negative/issue_13.php", [] + ], + [ + "./tests/negative/missing_argument_func.php", [] + ], + [ + "./tests/negative/undefined_class.php", [] + ], + [ + "./tests/negative/undefined_func.php", [] + ], + [ + "./tests/negative/undefined_method.php", [] + ], ]; diff --git a/projects/tests/ooptest.php b/projects/tests/ooptest.php index 8b1eea03..4f5a368e 100644 --- a/projects/tests/ooptest.php +++ b/projects/tests/ooptest.php @@ -3,11 +3,11 @@ return [ [ "./tests/oop/simple1.php", - [["\$instance1->boum2", "12", "xss"]] + [["\$instance1->boum2", "15", "xss"]] ], [ "./tests/oop/simple2.php", - [["\$instance1->boum2", "11", "xss"]] + [["\$instance1->boum2", "13", "xss"]] ], [ "./tests/oop/simple3.php", @@ -19,19 +19,19 @@ ], [ "./tests/oop/simple5.php", - [["\$instance->boum1", "25", "xss"]] + [["\$get_boum1_return", "14", "xss"]] ], [ "./tests/oop/simple6.php", - [["\$instance->boum1", "24", "xss"]] + [["\$instance->boum1", "26", "xss"]] ], [ "./tests/oop/simple7.php", - [["\$instance->boum1", "15", "xss"]] + [["\$instance->boum1", "17", "xss"]] ], [ "./tests/oop/simple8.php", - [["\$instance1->boum2", "16", "xss"]] + [["\$instance1->boum2", "18", "xss"]] ], [ "./tests/oop/simple9.php", @@ -39,49 +39,52 @@ ], [ "./tests/oop/simple10.php", - [["\$instance1->boum1", "5", "xss"]] + [["\$instance1->boum1", "15", "xss"]] ], [ "./tests/oop/simple11.php", - [["\$instance1->boum1", "18", "xss"]] + [["\$instance1->boum1", "20", "xss"]] ], [ "./tests/oop/simple12.php", - [["\$instance1->boum1", "9", "xss"]] + [["\$instance1->boum1", "15", "xss"]] ], [ "./tests/oop/simple13.php", - [["\$instance1->boum1", "14", "xss"]] + [["\$instance1->boum1", "32", "xss"], + ["\$instance1->boum1", "36", "xss"]] ], [ "./tests/oop/simple14.php", - [["\$instance1->boum2", "11", "xss"]] + [["\$instance1->boum2", "16", "xss"]] ], [ "./tests/oop/simple15.php", - [["\$this->boum1", "5", "xss"]] + [["\$this->boum1", "10", "xss"]] ], [ "./tests/oop/simple16.php", - [["\$instance1->boum1", "5", "xss"]] + [["\$instance1->boum1", "31", "xss"]] ], [ "./tests/oop/simple17.php", - [["\$this->boum1[0]", "9", "xss"]] + [["\$copy[0]", "33", "xss"]] ], [ "./tests/oop/simple18.php", - [["\$instance1->boum1", "15", "xss"]] + [["\$instance1->boum1", "34", "xss"]] ], [ "./tests/oop/simple19.php", - [["\$testc1->object1->object2", "29", "xss"], - ["\$newtestc1->object1", "37", "xss"], - ["\$newsettestc1->object1", "7", "xss"]] + [["\$toto", "30", "xss"], + ["\$newtestc1->object1", "40", "xss"], + ["\$newsettestc1->object1", "47", "xss"], + ["\$testc1_encore2->member1", "67", "xss"], + ["\$val", "65", "xss"]] ], [ "./tests/oop/simple20.php", - [["\$this->member2", "7", "xss"]] + [["\$this->member2", "12", "xss"]] ], [ "./tests/oop/simple21.php", @@ -90,9 +93,8 @@ ], [ "./tests/oop/simple22.php", - [["\$a->data", "11", "xss"], - ["\$this->data", "6", "xss"], - ["\$a->data", "11", "xss"]] + [["\$a->data", "15", "xss"], + ["\$b->data", "18", "xss"]] ], [ "./tests/oop/simple23/a.php", @@ -100,17 +102,17 @@ ], [ "./tests/oop/simple24.php", - [["\$a->data", "6", "xss"], - ["\$b->data", "6", "xss"]] + [["\$a->data", "22", "xss"], + ["\$b->data", "31", "xss"]] ], [ "./tests/oop/simple25.php", - [["\$query", "6", "sql_injection"]] + [["\$query", "7", "sql_injection"]] ], [ "./tests/oop/simple26.php", - [["\$_GET[\"t\"]", "17", "xss"], - ["\$_GET[\"p\"]", "8", "xss"]] + [["\$taint2", "15", "xss"], + ["\$taint1", "6", "xss"]] ], [ "./tests/oop/simple27.php", @@ -127,15 +129,40 @@ ], [ "./tests/oop/simple30.php", - [["testa::\$stavar", "12", "xss"]] + [["testa::\$stavar", "16", "xss"]] ], [ "./tests/oop/simple31.php", - [["\$a->stavar", "11", "xss"]] + [["\$a->stavar", "14", "xss"]] ], [ "./tests/oop/simple32.php", - [["testa::\$stavar", "9", "xss"]] + [["testa::\$stavar", "11", "xss"]] + ], + [ + "./tests/oop/simple33.php", + [["\$tainted", "35", "xss"]] + ], + [ + "./tests/oop/simple34.php", + [["\$tainted", "16", "xss"]] + ], + [ + "./tests/oop/simple35.php", + [["\$get_boum1_return", "14", "xss"], + ["\$get_boum1_return", "14", "xss"]] + ], + [ + "./tests/oop/simple36.php", + [["\$aaas->title", "33", "xss"]] + ], + [ + "./tests/oop/chained1.php", + [["\$tainted", "33", "xss"]] + ], + [ + "./tests/oop/chained2.php", + [["\$aaa->title", "22", "xss"]] ] ]; diff --git a/projects/tests/optimizationstest.php b/projects/tests/optimizationstest.php new file mode 100644 index 00000000..363a29dd --- /dev/null +++ b/projects/tests/optimizationstest.php @@ -0,0 +1,7 @@ +_message", "11", "xss"]] + ] + ]; diff --git a/projects/tests/phpunit.xml.dist b/projects/tests/phpunit.xml.dist index ebc1d110..05f117af 100644 --- a/projects/tests/phpunit.xml.dist +++ b/projects/tests/phpunit.xml.dist @@ -15,6 +15,7 @@ run_all_folders.php run_flows.php run_exclude_files.php + run_all_configurations.php diff --git a/projects/tests/phpwandertest.php b/projects/tests/phpwandertest.php index ebdc68ef..6c192e30 100644 --- a/projects/tests/phpwandertest.php +++ b/projects/tests/phpwandertest.php @@ -3,9 +3,9 @@ return [ [ "./tests/phpwander/test0.php", - [["\$id_return", "4", "file_disclosure"], - ["\$return_basename", "9", "file_disclosure"], - ["\$_GET[\"f\"]", "11", "file_disclosure"], + [["\$id_return", "4", "path_traversal"], + ["\$basename_return", "9", "path_traversal"], + ["\$_GET[\"f\"]", "11", "path_traversal"], ["\$b", "9", "xss"], ["\$c", "11", "xss"]] ], @@ -27,8 +27,8 @@ [ "./tests/phpwander/test4.php", [[["\$_GET[\"user\"]", "\$_GET[\"password\"]"], ["4", "4"], "sql_injection"], - ["\$_GET[\"file\"]", "8", "file_inclusion"], - ["\$user[\"email\"]", "12", "xss"]] + ["\$user[\"email\"]", "5", "xss"], + ["\$_GET[\"file\"]", "11", "file_inclusion"]] ], [ "./tests/phpwander/test5.php", @@ -41,7 +41,7 @@ ], [ "./tests/phpwander/test7.php", - [["\$param", "9", "xss"]] + [["\$param", "10", "xss"]] ],// handle with report confidence to check [ "./tests/phpwander/test8.php", @@ -63,7 +63,7 @@ ],*/ [ "./tests/phpwander/test12.php", - [["D::\$vars", "8", "xss"], + [["D::\$vars", "10", "xss"], ["\$danger_return", "12", "xss"]] ],// a corriger [ diff --git a/projects/tests/realtest.php b/projects/tests/realtest.php index d8c709c6..efcffc41 100644 --- a/projects/tests/realtest.php +++ b/projects/tests/realtest.php @@ -18,8 +18,11 @@ ], [ "./tests/real/wordpress1/wp-commentsrss2.php", - [["\$title", "38", "xss"], - [array("\$apply_filters_return", "\$apply_filters_return"), array("65", "88"), "xss"]] + [["\$title", "40", "xss"]] + ], + [ + "./tests/real/wordpress2/wp-commentsrss2.php", + [["\$query", "132", "sql_injection"], + ["\$this->last_query", "102", "xss"]] ] - ]; diff --git a/projects/tests/resolve_includes.json b/projects/tests/resolve_includes.json index 7744f98b..33da466a 100644 --- a/projects/tests/resolve_includes.json +++ b/projects/tests/resolve_includes.json @@ -1 +1 @@ -{"includes_not_resolved":""} \ No newline at end of file +{"include_failures":""} \ No newline at end of file diff --git a/projects/tests/run_all.php b/projects/tests/run_all.php index 691a2236..b1112c10 100644 --- a/projects/tests/run_all.php +++ b/projects/tests/run_all.php @@ -13,20 +13,17 @@ public function testSecurity($file, $expectedVulns) $context = new \progpilot\Context; $analyzer = new \progpilot\Analyzer; - $context->setAnalyzeHardrules(true); - $context->setAnalyzeFunctions(false); $context->outputs->taintedFlow(true); $nbVulns = 0; $context->inputs->setDev(true); $context->inputs->setLanguages(["php", "js"]); $context->inputs->setFile($file); + // enable "vendor" folder analysis + // test case: tests/real/composer/ + $context->inputs->setExclusions([]); - try { - $analyzer->run($context); - } catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; - } + $analyzer->run($context); $results = $context->outputs->getResults(); @@ -44,17 +41,17 @@ public function testSecurity($file, $expectedVulns) } foreach ($expectedSourceLine as $oneSourceLine) { - $this->assertContains($oneSourceLine, $vuln["source_line"]); + $this->assertContains((int)$oneSourceLine, $vuln["source_line"]); } } else { $this->assertContains($expectedSourceName, $vuln["source_name"]); - $this->assertContains($expectedSourceLine, $vuln["source_line"]); + $this->assertContains((int)$expectedSourceLine, $vuln["source_line"]); $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); } } // custom else { - $this->assertEquals($expectedSourceName, $vuln["vuln_line"]); - $this->assertEquals($expectedSourceLine, $vuln["vuln_column"]); + $this->assertEquals((int)$expectedSourceName, $vuln["vuln_line"]); + $this->assertEquals((int)$expectedSourceLine, $vuln["vuln_column"]); $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); } } else { @@ -69,6 +66,7 @@ public function testSecurity($file, $expectedVulns) public function dataProvider() { + $tabopti = include("optimizationstest.php"); $taboop = include("ooptest.php"); $tabreal = include("realtest.php"); $tabgen = include("generictest.php"); @@ -91,6 +89,7 @@ public function dataProvider() } $tab = array_merge( + $tabopti, $taboop, $tabreal, $tabgen, diff --git a/projects/tests/run_all_configurations.php b/projects/tests/run_all_configurations.php new file mode 100644 index 00000000..5436df0f --- /dev/null +++ b/projects/tests/run_all_configurations.php @@ -0,0 +1,66 @@ +setConfiguration($configfile); + + $analyzer->run($context); + + $results = $context->outputs->getResults(); + + foreach ($results as $vuln) { + if (isset($expectedVulns[$nbVulns])) { + $expectedSourceName = $expectedVulns[$nbVulns][0]; + $expectedSourceLine = $expectedVulns[$nbVulns][1]; + $expectedVulnName = $expectedVulns[$nbVulns][2]; + + // taint-style + if (isset($vuln['source_name']) && isset($vuln['source_line'])) { + if (is_array($expectedSourceName) && is_array($expectedSourceLine)) { + foreach ($expectedSourceName as $oneSourceName) { + $this->assertContains($oneSourceName, $vuln["source_name"]); + } + + foreach ($expectedSourceLine as $oneSourceLine) { + $this->assertContains((int)$oneSourceLine, $vuln["source_line"]); + } + } else { + $this->assertContains($expectedSourceName, $vuln["source_name"]); + $this->assertContains((int)$expectedSourceLine, $vuln["source_line"]); + $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); + } + } // custom + else { + $this->assertEquals((int)$expectedSourceName, $vuln["vuln_line"]); + $this->assertEquals((int)$expectedSourceLine, $vuln["vuln_column"]); + $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); + } + } else { + $this->assertTrue(false); + } + + $nbVulns ++; + } + + $this->assertCount(count($expectedVulns), $results); + } + + public function dataProvider() + { + $tabconfig = include("configtest.php"); + + return array_merge($tabconfig); + } +} diff --git a/projects/tests/run_all_folders.php b/projects/tests/run_all_folders.php index 3e4719f5..c927b861 100644 --- a/projects/tests/run_all_folders.php +++ b/projects/tests/run_all_folders.php @@ -13,19 +13,13 @@ public function testSecurity($folder, $expectedVulns) $context = new \progpilot\Context; $analyzer = new \progpilot\Analyzer; - $context->setAnalyzeHardrules(true); - $context->setAnalyzeFunctions(false); $context->outputs->taintedFlow(true); $nbVulns = 0; $context->inputs->setDev(true); $context->inputs->setFolder($folder); - try { - $analyzer->run($context); - } catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; - } + $analyzer->run($context); $results = $context->outputs->getResults(); @@ -43,17 +37,17 @@ public function testSecurity($folder, $expectedVulns) } foreach ($expectedSourceLine as $oneSourceLine) { - $this->assertContains($oneSourceLine, $vuln["source_line"]); + $this->assertContains((int)$oneSourceLine, $vuln["source_line"]); } } else { $this->assertContains($expectedSourceName, $vuln["source_name"]); - $this->assertContains($expectedSourceLine, $vuln["source_line"]); + $this->assertContains((int)$expectedSourceLine, $vuln["source_line"]); $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); } } // custom else { - $this->assertEquals($expectedSourceName, $vuln["vuln_line"]); - $this->assertEquals($expectedSourceLine, $vuln["vuln_column"]); + $this->assertEquals((int)$expectedSourceName, $vuln["vuln_line"]); + $this->assertEquals((int)$expectedSourceLine, $vuln["vuln_column"]); $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); } } else { diff --git a/projects/tests/run_config.php b/projects/tests/run_config.php new file mode 100644 index 00000000..8dcc3015 --- /dev/null +++ b/projects/tests/run_config.php @@ -0,0 +1,14 @@ + 1) { + $context = new \progpilot\Context; + $analyzer = new \progpilot\Analyzer; + + $context->setConfiguration($argv[1]); + + $analyzer->run($context); + + var_dump($context->outputs->getResults()); +} diff --git a/projects/tests/run_exclude_files.php b/projects/tests/run_exclude_files.php index cf2977ce..5edeadf2 100644 --- a/projects/tests/run_exclude_files.php +++ b/projects/tests/run_exclude_files.php @@ -13,25 +13,20 @@ public function testSecurity($folder, $expectedVulns) $context = new \progpilot\Context; $analyzer = new \progpilot\Analyzer; - $context->setAnalyzeHardrules(true); - $context->setAnalyzeFunctions(false); $context->outputs->taintedFlow(true); - $arr = array( - "exclude_files" => ["./tests/folders/folder2/mix3.php"], - "exclude_folders" => ["./tests/folders/folder2/sub_folder1/sub_folder2"] - ); + $exclusions = [ + "./tests/folders/folder2/mix3.php", + "./tests/folders/folder2/sub_folder1/sub_folder2", + "onefolderexcludedtest", + "onefileexcludedtest.php" + ]; $nbVulns = 0; $context->inputs->setDev(true); - $context->inputs->setExcludes($arr); + $context->inputs->setExclusions($exclusions); $context->inputs->setFolder($folder); - - try { - $analyzer->run($context); - } catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; - } + $analyzer->run($context); $results = $context->outputs->getResults(); @@ -49,17 +44,17 @@ public function testSecurity($folder, $expectedVulns) } foreach ($expectedSourceLine as $oneSourceLine) { - $this->assertContains($oneSourceLine, $vuln["source_line"]); + $this->assertContains((int)$oneSourceLine, $vuln["source_line"]); } } else { $this->assertContains($expectedSourceName, $vuln["source_name"]); - $this->assertContains($expectedSourceLine, $vuln["source_line"]); + $this->assertContains((int)$expectedSourceLine, $vuln["source_line"]); $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); } } // custom else { - $this->assertEquals($expectedSourceName, $vuln["vuln_line"]); - $this->assertEquals($expectedSourceLine, $vuln["vuln_column"]); + $this->assertEquals((int)$expectedSourceName, $vuln["vuln_line"]); + $this->assertEquals((int)$expectedSourceLine, $vuln["vuln_column"]); $this->assertEquals($expectedVulnName, $vuln["vuln_name"]); } } else { diff --git a/projects/tests/run_file.php b/projects/tests/run_file.php index 12ba3a22..e21714ef 100644 --- a/projects/tests/run_file.php +++ b/projects/tests/run_file.php @@ -2,27 +2,20 @@ require_once './vendor/autoload.php'; -try { - if ($argc > 1) { - $context = new \progpilot\Context; - $analyzer = new \progpilot\Analyzer; - $context->inputs->setFile($argv[1]); - $context->inputs->setDev(true); - $context->inputs->setLanguages(["php", "js"]); - $context->inputs->setFrameworks(["suitecrm", "codeigniter", "wordpress", "prestashop", "symfony"]); +if ($argc > 1) { + $context = new \progpilot\Context; + $analyzer = new \progpilot\Analyzer; + $context->inputs->setFile($argv[1]); + $context->inputs->setDev(true); + $context->inputs->setLanguages(["php", "js"]); - $context->setAnalyzeHardrules(true); - $context->setAnalyzeFunctions(false); - $context->outputs->taintedFlow(true); + $context->setDebugMode(true); + $context->outputs->taintedFlow(true); + /* + $context->outputs->resolveIncludes(true); + $context->outputs->resolveIncludesFile("tmpresolvedincludes.json"); + */ + $analyzer->run($context); - try { - $analyzer->run($context); - } catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; - } - - var_dump($context->outputs->getResults()); - } -} catch (\RuntimeException $e) { - $result = $e->getMessage(); + var_dump($context->outputs->getResults()); } diff --git a/projects/tests/run_flows.php b/projects/tests/run_flows.php index b3e5afe6..62a4f312 100644 --- a/projects/tests/run_flows.php +++ b/projects/tests/run_flows.php @@ -13,19 +13,12 @@ public function testSecurity($file, $expectedVulns) $context = new \progpilot\Context; $analyzer = new \progpilot\Analyzer; - $context->setAnalyzeHardrules(true); - $context->setAnalyzeFunctions(false); $context->outputs->taintedFlow(true); $nbVulns = 0; $context->inputs->setDev(true); $context->inputs->setFile($file); - - try { - $analyzer->run($context); - } catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; - } + $analyzer->run($context); $results = $context->outputs->getResults(); diff --git a/projects/tests/run_folder.php b/projects/tests/run_folder.php new file mode 100644 index 00000000..20c64f2b --- /dev/null +++ b/projects/tests/run_folder.php @@ -0,0 +1,23 @@ + 1) { + $context = new \progpilot\Context; + $analyzer = new \progpilot\Analyzer; + $context->inputs->setFolder($argv[1]); + $context->inputs->setDev(true); + $context->inputs->setLanguages(["php", "js"]); + $context->setDebugMode(true); + $context->outputs->taintedFlow(true); + + $exclusions = [ + "./tests/folders/folder2/mix3.php", + "./tests/folders/folder2/sub_folder1/sub_folder2" + ]; + + $context->inputs->addExclusions($exclusions); + $analyzer->run($context); + + var_dump($context->outputs->getResults()); +} diff --git a/projects/tests/run_include_files.php b/projects/tests/run_include_files.php index 83d80011..969b02d9 100644 --- a/projects/tests/run_include_files.php +++ b/projects/tests/run_include_files.php @@ -2,24 +2,23 @@ require_once './vendor/autoload.php'; -try { - $context = new \progpilot\Context; - $analyzer = new \progpilot\Analyzer; +$context = new \progpilot\Context; +$analyzer = new \progpilot\Analyzer; - $context->inputs->setDev(true); - $context->inputs->setIncludeFiles("include_files.json"); +$context->inputs->setDev(true); +$context->outputs->taintedFlow(true); +$context->setDebugMode(true); +$context->inputs->setInclusions("include_files.json"); - try { - $analyzer->run($context); - } catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; - } +$var = function ($result) { + echo "a result:\n"; + var_dump($result); +}; +$context->outputs->setOnAddResult($var); +$analyzer->run($context); - $results = $context->outputs->getResults(); - $outputjson = array('results' => $results); - $parsed_json = $outputjson["results"]; +$results = $context->outputs->getResults(); +$outputjson = array('results' => $results); +$parsed_json = $outputjson["results"]; - var_dump($parsed_json); -} catch (\RuntimeException $e) { - $result = $e->getMessage(); -} +var_dump($parsed_json); diff --git a/projects/tests/storedtest.php b/projects/tests/storedtest.php new file mode 100644 index 00000000..2070bb9b --- /dev/null +++ b/projects/tests/storedtest.php @@ -0,0 +1,16 @@ +setAnalyzeJs(false); $context->setAnalyzeIncludes(false); $context->inputs->setCode($code); -try { - $analyzer->run($context); -} catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; -} +$analyzer->run($context); $results = $context->outputs->getAst(); diff --git a/projects/tests/testcode.php b/projects/tests/testcode.php index a43fba47..8e123883 100644 --- a/projects/tests/testcode.php +++ b/projects/tests/testcode.php @@ -16,12 +16,7 @@ $analyzer = new \progpilot\Analyzer; $context->inputs->setCode($code); - -try { - $analyzer->run($context); -} catch (Exception $e) { - echo 'Exception : ', $e->getMessage(), "\n"; -} +$analyzer->run($context); $results = $context->outputs->getResults(); $outputjson = array('results' => $results); diff --git a/projects/tests/testfalsepositives.php b/projects/tests/testfalsepositives.php index 23c20038..617aff98 100644 --- a/projects/tests/testfalsepositives.php +++ b/projects/tests/testfalsepositives.php @@ -17,7 +17,7 @@ $arr = [ array("vuln_id" => "a6c0d68d130972bd4e23ae6c68d548d5cf8d40a78e73136a1b89dab8f3b441c1") ]; -$context->setAnalyzeHardrules(true); + $context->inputs->setDev(true); $context->inputs->setFile("./tests/custom/custom1.php"); $context->inputs->setFalsePositives($arr); diff --git a/projects/tests/tests/conditions/condition5.php b/projects/tests/tests/conditions/condition5.php index e2866547..b949521e 100644 --- a/projects/tests/tests/conditions/condition5.php +++ b/projects/tests/tests/conditions/condition5.php @@ -3,7 +3,7 @@ $tainted = $_GET["p"]; $legal_table = array("safe1", "safe2"); -if (!in_array($tainted, $legal_table, true) && in_array($tainted, $legal_table, true)) { +if (!in_array($tainted, $legal_table, true)) { $tainted = $tainted; } else { $tainted = $legal_table[0]; diff --git a/projects/tests/tests/conditions/condition7.php b/projects/tests/tests/conditions/condition7.php index bd080efa..629e6ee9 100644 --- a/projects/tests/tests/conditions/condition7.php +++ b/projects/tests/tests/conditions/condition7.php @@ -4,6 +4,10 @@ $safe = (int) $tainted; +$notsafe = (string) $tainted; + +echo $notsafe; + echo $safe; echo $tainted; diff --git a/projects/tests/tests/conditions/condition8.php b/projects/tests/tests/conditions/condition8.php index ea092238..fca1b524 100644 --- a/projects/tests/tests/conditions/condition8.php +++ b/projects/tests/tests/conditions/condition8.php @@ -4,54 +4,55 @@ $tainted = $_GET["p"]; if (filter_var($tainted, FILTER_VALIDATE_BOOLEAN)) { - echo $tainted; + echo $tainted; // OK } $tainted = $_GET["p"]; if (filter_var($tainted, FILTER_VALIDATE_FLOAT)) { - echo $tainted; + echo $tainted; // OK } $tainted = $_GET["p"]; if (filter_var($tainted, FILTER_VALIDATE_INT)) { - echo $tainted; + echo $tainted; // OK } $tainted = $_GET["p"]; if (filter_var($tainted, FILTER_VALIDATE_IP)) { - echo $tainted; + echo $tainted; // OK } $tainted = $_GET["p"]; if (filter_var($tainted, FILTER_VALIDATE_MAC)) { - echo $tainted; + echo $tainted; // OK } $tainted = $_GET["p"]; if (filter_var($tainted, FILTER_VALIDATE_URL)) { - echo $tainted; + echo $tainted; // OK } -/* + $tainted = $_GET["p"]; if(filter_var($tainted, FILTER_VALIDATE_EMAIL)) { - echo $tainted; + echo $tainted;// KO } -*/ + $tainted = $_GET["p"]; if (filter_var($tainted, FILTER_VALIDATE_REGEXP)) { - echo $tainted; + echo $tainted; // KO } $tainted = $_GET["p"]; -if (filter_var($tainted, nexiste_pas)) { - echo $tainted; +if (filter_var($tainted, $nexiste_pas)) { + echo $tainted; // KO } + diff --git a/projects/tests/tests/custom/conditionalvalues.php b/projects/tests/tests/custom/conditionalvalues.php new file mode 100644 index 00000000..86904855 --- /dev/null +++ b/projects/tests/tests/custom/conditionalvalues.php @@ -0,0 +1,12 @@ + + diff --git a/projects/tests/tests/custom/cookies1.php b/projects/tests/tests/custom/cookies1.php new file mode 100644 index 00000000..9313d326 --- /dev/null +++ b/projects/tests/tests/custom/cookies1.php @@ -0,0 +1,9 @@ + 'Lax' ); -setcookie("token", $token, $options); - +setcookie("token", "123", $options); diff --git a/projects/tests/tests/custom/unsetcookies.php b/projects/tests/tests/custom/unsetcookies.php new file mode 100644 index 00000000..f9c113c3 --- /dev/null +++ b/projects/tests/tests/custom/unsetcookies.php @@ -0,0 +1,17 @@ +middle_object"} $instance2 = new known_class1; -$instance2->middle_object->mysink($_GET["p"]); +$instance2->middle_object_unknown->mysink($_GET["p"]); // 1)c) : should be found by this sink declaration : {"name" : "mysink", "instanceof" : "known_class1->middle_object"} diff --git a/projects/tests/tests/data/source16.php b/projects/tests/tests/data/source16.php index 17b9aa23..7ce1c1fb 100644 --- a/projects/tests/tests/data/source16.php +++ b/projects/tests/tests/data/source16.php @@ -3,8 +3,26 @@ while($row = mysql_fetch_object($result)) { echo $row->fullname; } +/* +switch($baba) { + case '1': + $html = "case1"; + break; + case '2': + $html = "case1"; + break; +} +echo $html;*/ /* $row = mysql_fetch_object($result); echo $row->fullname; */ + + +if($vuln == "") { +$html = "toto"; +} +else { +$html = "ldld"; +} diff --git a/projects/tests/tests/data/source20.php b/projects/tests/tests/data/source20.php index d0ac4bf9..fc18ab01 100644 --- a/projects/tests/tests/data/source20.php +++ b/projects/tests/tests/data/source20.php @@ -1,25 +1,8 @@ get_results("SELECT comment_ID, comment_author, comment_author_email, -comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, -$wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments -LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE $wpdb->posts.post_status IN ('publish', 'static', 'object') -AND $wpdb->comments.comment_approved = '1' AND post_date_gmt < '" . gmdate("Y-m-d H:i:s") . "' -ORDER BY comment_date_gmt DESC LIMIT " . get_settings('posts_per_rss') ); - -// this line is WordPress' motor, do not delete it. -if ($comments) { - foreach ($comments as $comment) { - // Some plugins may need to know the metadata - // associated with this comment's post: - get_post_custom($comment->comment_post_ID); - - echo $comment->ddd; - } -} +echo $b; diff --git a/projects/tests/tests/data/source21.php b/projects/tests/tests/data/source21.php new file mode 100644 index 00000000..b610167a --- /dev/null +++ b/projects/tests/tests/data/source21.php @@ -0,0 +1,21 @@ +last_result = null; // block 9 + if(true) { + echo "baba"; // block 19 + } + else { + echo "titi"; // block 33 + + while ( $row = @mysql_fetch_object(true) ) { // block 41 et 48 (row) + $this->last_result = $row; // block 52 + } + + echo "bibi"; // block 62 + } + + echo $this->last_result->title; // block 26 + } +} diff --git a/projects/tests/tests/data/source3.php b/projects/tests/tests/data/source3.php index c8bedaa5..fbf4be04 100644 --- a/projects/tests/tests/data/source3.php +++ b/projects/tests/tests/data/source3.php @@ -1,5 +1,5 @@ comment_ID' class='$class'>"; - -?> diff --git a/projects/tests/tests/generic/functions19.php b/projects/tests/tests/generic/functions19.php index 3b34e102..4e1fb203 100644 --- a/projects/tests/tests/generic/functions19.php +++ b/projects/tests/tests/generic/functions19.php @@ -6,5 +6,6 @@ function testf1($param2) echo $param2["test"]; } +$toto = array("test" => $_GET["p"]); testf1(array("test" => $_GET["p"])); diff --git a/projects/tests/tests/generic/functions2.php b/projects/tests/tests/generic/functions2.php index 92064c09..2f6923f0 100644 --- a/projects/tests/tests/generic/functions2.php +++ b/projects/tests/tests/generic/functions2.php @@ -2,9 +2,9 @@ function testf($parama, $boum, $tchka) { - return $parama; + return [$parama, ""]; } $safea = testf($_GET["p"], "argument2", "argument3"); -echo $safea; +echo $safea[0]; diff --git a/projects/tests/tests/generic/functions20.php b/projects/tests/tests/generic/functions20.php new file mode 100644 index 00000000..8c5c6835 --- /dev/null +++ b/projects/tests/tests/generic/functions20.php @@ -0,0 +1,26 @@ + $file) { // block 10 + echo $file; + } diff --git a/projects/tests/tests/generic/loop2.php b/projects/tests/tests/generic/loop2.php new file mode 100644 index 00000000..2f548228 --- /dev/null +++ b/projects/tests/tests/generic/loop2.php @@ -0,0 +1,10 @@ +result) ) { // block 29 (row) & 22 (fetch) + echo $row->title; // block 33 + } + } + echo "salut"; // block 13 diff --git a/projects/tests/tests/generic/loop3.php b/projects/tests/tests/generic/loop3.php new file mode 100644 index 00000000..b036b5d2 --- /dev/null +++ b/projects/tests/tests/generic/loop3.php @@ -0,0 +1,7 @@ +title; +} + diff --git a/projects/tests/tests/generic/loop4.php b/projects/tests/tests/generic/loop4.php new file mode 100644 index 00000000..0d75799f --- /dev/null +++ b/projects/tests/tests/generic/loop4.php @@ -0,0 +1,20 @@ +last_query; + } + + function query($query) { + $this->last_query = $query; + if ( true ) { + $this->print_error(); + } + } +} + + +$wpdb = new wpdb; +$wpdb->query($_GET["p"]); + diff --git a/projects/tests/tests/generic/simple1.php b/projects/tests/tests/generic/simple1.php index 464293c1..87a30f30 100644 --- a/projects/tests/tests/generic/simple1.php +++ b/projects/tests/tests/generic/simple1.php @@ -1,6 +1,6 @@ 'Damn Vulnerable Web Application (DVWA) v' . dvwaVersionGet() . '', - 'title_separator' => ' :: ', - 'bodyff' => '', - 'page_id' => '', - 'help_button' => '', - 'source_button'=>'qsd' - ); - - return $returnArray; -} - -function dvwaSecurityLevelGet() -{ - return isset($_COOKIE[ 'security' ]) ? $_COOKIE[ 'security' ] : 'impossible'; -} - - -function dvwaSecurityLevelSet($pSecurityLevel) -{ - if ($pSecurityLevel == 'impossible') { - $httponly = true; - } else { - $httponly = false; - } - setcookie(session_name(), session_id(), null, '/', null, null, $httponly); - setcookie('security', $pSecurityLevel, null, null, null, null, $httponly); -} - - -// Start message functions -- - -function dvwaMessagePush($pMessage) -{ - $dvwaSession =& dvwaSessionGrab(); - if (!isset($dvwaSession[ 'messages' ])) { - $dvwaSession[ 'messages' ] = array(); - } - $dvwaSession[ 'messages' ][] = $pMessage; -} - - -function dvwaMessagePop() -{ - $dvwaSession =& dvwaSessionGrab(); - if (!isset($dvwaSession[ 'messages' ]) || count($dvwaSession[ 'messages' ]) == 0) { - return false; - } - return array_shift($dvwaSession[ 'messages' ]); -} - - -function messagesPopAllToHtml() -{ - $messagesHtml = ''; - while ($message = dvwaMessagePop()) { // TODO- sharpen! - $messagesHtml .= "
{$message}
"; - } - - return $messagesHtml; -} -// --END (message functions) - -function dvwaHtmlEcho($pPage) -{ - $menuBlocks = array(); - - $menuBlocks[ 'home' ] = array(); - if (dvwaIsLoggedIn()) { - $menuBlocks[ 'home' ][] = array( 'id' => 'home', 'name' => 'Home', 'url' => '.' ); - $menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' ); - $menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup / Reset DB', 'url' => 'setup.php' ); - } else { - $menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup DVWA', 'url' => 'setup.php' ); - $menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' ); - } - - if (dvwaIsLoggedIn()) { - $menuBlocks[ 'vulnerabilities' ] = array(); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'brute', 'name' => 'Brute Force', 'url' => 'vulnerabilities/brute/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'exec', 'name' => 'Command Injection', 'url' => 'vulnerabilities/exec/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'csrf', 'name' => 'CSRF', 'url' => 'vulnerabilities/csrf/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'fi', 'name' => 'File Inclusion', 'url' => 'vulnerabilities/fi/.?page=include.php' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'upload', 'name' => 'File Upload', 'url' => 'vulnerabilities/upload/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'captcha', 'name' => 'Insecure CAPTCHA', 'url' => 'vulnerabilities/captcha/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli', 'name' => 'SQL Injection', 'url' => 'vulnerabilities/sqli/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli_blind', 'name' => 'SQL Injection (Blind)', 'url' => 'vulnerabilities/sqli_blind/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'weak_id', 'name' => 'Weak Session IDs', 'url' => 'vulnerabilities/weak_id/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_d', 'name' => 'XSS (DOM)', 'url' => 'vulnerabilities/xss_d/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_r', 'name' => 'XSS (Reflected)', 'url' => 'vulnerabilities/xss_r/' ); - $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_s', 'name' => 'XSS (Stored)', 'url' => 'vulnerabilities/xss_s/' ); - } - - $menuBlocks[ 'meta' ] = array(); - if (dvwaIsLoggedIn()) { - $menuBlocks[ 'meta' ][] = array( 'id' => 'security', 'name' => 'DVWA Security', 'url' => 'security.php' ); - $menuBlocks[ 'meta' ][] = array( 'id' => 'phpinfo', 'name' => 'PHP Info', 'url' => 'phpinfo.php' ); - } - $menuBlocks[ 'meta' ][] = array( 'id' => 'about', 'name' => 'About', 'url' => 'about.php' ); - - if (dvwaIsLoggedIn()) { - $menuBlocks[ 'logout' ] = array(); - $menuBlocks[ 'logout' ][] = array( 'id' => 'logout', 'name' => 'Logout', 'url' => 'logout.php' ); - } - - $menuHtml = ''; - - foreach ($menuBlocks as $menuBlock) { - $menuBlockHtml = ''; - foreach ($menuBlock as $menuItem) { - $selectedClass = ($menuItem[ 'id' ] == $pPage[ 'page_id' ]) ? 'selected' : ''; - $fixedUrl = DVWA_WEB_PAGE_TO_ROOT.$menuItem[ 'url' ]; - $menuBlockHtml .= "
  • {$menuItem[ 'name' ]}
  • \n"; - } - $menuHtml .= ""; - } - - // Get security cookie -- - $securityLevelHtml = ''; - switch (dvwaSecurityLevelGet()) { - case 'low': - $securityLevelHtml = 'low'; - break; - case 'medium': - $securityLevelHtml = 'medium'; - break; - case 'high': - $securityLevelHtml = 'high'; - break; - default: - $securityLevelHtml = 'impossible'; - break; - } - // -- END (security cookie) - - $phpIdsHtml = 'PHPIDS: ' . (dvwaPhpIdsIsEnabled() ? 'enabled' : 'disabled'); - $userInfoHtml = 'Username: ' . (dvwaCurrentUser()); - - $messagesHtml = messagesPopAllToHtml(); - if ($messagesHtml) { - $messagesHtml = "
    {$messagesHtml}
    "; - } - - $systemInfoHtml = ""; - if (dvwaIsLoggedIn()) { - $systemInfoHtml = "
    {$userInfoHtml}
    Security Level: {$securityLevelHtml}
    {$phpIdsHtml}
    "; - } - if ($pPage[ 'source_button' ]) { - $systemInfoHtml = dvwaButtonSourceHtmlGet($pPage[ 'source_button' ]) . " $systemInfoHtml"; - } - if ($pPage[ 'help_button' ]) { - $systemInfoHtml = dvwaButtonHelpHtmlGet($pPage[ 'help_button' ]) . " $systemInfoHtml"; - } - - // Send Headers + main HTML code - Header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 - Header('Content-Type: text/html;charset=utf-8'); // TODO- proper XHTML headers... - Header('Expires: Tue, 23 Jun 2009 12:00:00 GMT'); // Date in the past - + 'Damn Vulnerable Web Application (DVWA) v' . dvwaVersionGet() . '', + 'title_separator' => ' :: ', + 'bodyff' => '', + 'page_id' => '', + 'help_button' => '', + 'source_button'=>'qsd' + ); + + return $returnArray; +} + +function dvwaSecurityLevelGet() +{ + return isset($_COOKIE[ 'security' ]) ? $_COOKIE[ 'security' ] : 'impossible'; +} + + +function dvwaSecurityLevelSet($pSecurityLevel) +{ + if ($pSecurityLevel == 'impossible') { + $httponly = true; + } else { + $httponly = false; + } + setcookie(session_name(), session_id(), null, '/', null, null, $httponly); + setcookie('security', $pSecurityLevel, null, null, null, null, $httponly); +} + + +// Start message functions -- + +function dvwaMessagePush($pMessage) +{ + $dvwaSession =& dvwaSessionGrab(); + if (!isset($dvwaSession[ 'messages' ])) { + $dvwaSession[ 'messages' ] = array(); + } + $dvwaSession[ 'messages' ][] = $pMessage; +} + + +function dvwaMessagePop() +{ + $dvwaSession =& dvwaSessionGrab(); + if (!isset($dvwaSession[ 'messages' ]) || count($dvwaSession[ 'messages' ]) == 0) { + return false; + } + return array_shift($dvwaSession[ 'messages' ]); +} + + +function messagesPopAllToHtml() +{ + $messagesHtml = ''; + while ($message = dvwaMessagePop()) { // TODO- sharpen! + $messagesHtml .= "
    {$message}
    "; + } + + return $messagesHtml; +} +// --END (message functions) + +function dvwaHtmlEcho($pPage) +{ + $menuBlocks = array(); + + $menuBlocks[ 'home' ] = array(); + if (dvwaIsLoggedIn()) { + $menuBlocks[ 'home' ][] = array( 'id' => 'home', 'name' => 'Home', 'url' => '.' ); + $menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' ); + $menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup / Reset DB', 'url' => 'setup.php' ); + } else { + $menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup DVWA', 'url' => 'setup.php' ); + $menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' ); + } + + if (dvwaIsLoggedIn()) { + $menuBlocks[ 'vulnerabilities' ] = array(); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'brute', 'name' => 'Brute Force', 'url' => 'vulnerabilities/brute/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'exec', 'name' => 'Command Injection', 'url' => 'vulnerabilities/exec/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'csrf', 'name' => 'CSRF', 'url' => 'vulnerabilities/csrf/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'fi', 'name' => 'File Inclusion', 'url' => 'vulnerabilities/fi/.?page=include.php' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'upload', 'name' => 'File Upload', 'url' => 'vulnerabilities/upload/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'captcha', 'name' => 'Insecure CAPTCHA', 'url' => 'vulnerabilities/captcha/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli', 'name' => 'SQL Injection', 'url' => 'vulnerabilities/sqli/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli_blind', 'name' => 'SQL Injection (Blind)', 'url' => 'vulnerabilities/sqli_blind/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'weak_id', 'name' => 'Weak Session IDs', 'url' => 'vulnerabilities/weak_id/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_d', 'name' => 'XSS (DOM)', 'url' => 'vulnerabilities/xss_d/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_r', 'name' => 'XSS (Reflected)', 'url' => 'vulnerabilities/xss_r/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_s', 'name' => 'XSS (Stored)', 'url' => 'vulnerabilities/xss_s/' ); + } + + $menuBlocks[ 'meta' ] = array(); + if (dvwaIsLoggedIn()) { + $menuBlocks[ 'meta' ][] = array( 'id' => 'security', 'name' => 'DVWA Security', 'url' => 'security.php' ); + $menuBlocks[ 'meta' ][] = array( 'id' => 'phpinfo', 'name' => 'PHP Info', 'url' => 'phpinfo.php' ); + } + $menuBlocks[ 'meta' ][] = array( 'id' => 'about', 'name' => 'About', 'url' => 'about.php' ); + + if (dvwaIsLoggedIn()) { + $menuBlocks[ 'logout' ] = array(); + $menuBlocks[ 'logout' ][] = array( 'id' => 'logout', 'name' => 'Logout', 'url' => 'logout.php' ); + } + + $menuHtml = ''; + + foreach ($menuBlocks as $menuBlock) { + $menuBlockHtml = ''; + foreach ($menuBlock as $menuItem) { + $selectedClass = ($menuItem[ 'id' ] == $pPage[ 'page_id' ]) ? 'selected' : ''; + $fixedUrl = DVWA_WEB_PAGE_TO_ROOT.$menuItem[ 'url' ]; + $menuBlockHtml .= "
  • {$menuItem[ 'name' ]}
  • \n"; + } + $menuHtml .= ""; + } + + // Get security cookie -- + $securityLevelHtml = ''; + switch (dvwaSecurityLevelGet()) { + case 'low': + $securityLevelHtml = 'low'; + break; + case 'medium': + $securityLevelHtml = 'medium'; + break; + case 'high': + $securityLevelHtml = 'high'; + break; + default: + $securityLevelHtml = 'impossible'; + break; + } + // -- END (security cookie) + + $phpIdsHtml = 'PHPIDS: ' . (dvwaPhpIdsIsEnabled() ? 'enabled' : 'disabled'); + $userInfoHtml = 'Username: ' . (dvwaCurrentUser()); + + $messagesHtml = messagesPopAllToHtml(); + if ($messagesHtml) { + $messagesHtml = "
    {$messagesHtml}
    "; + } + + $systemInfoHtml = ""; + if (dvwaIsLoggedIn()) { + $systemInfoHtml = "
    {$userInfoHtml}
    Security Level: {$securityLevelHtml}
    {$phpIdsHtml}
    "; + } + if ($pPage[ 'source_button' ]) { + $systemInfoHtml = dvwaButtonSourceHtmlGet($pPage[ 'source_button' ]) . " $systemInfoHtml"; + } + if ($pPage[ 'help_button' ]) { + $systemInfoHtml = dvwaButtonHelpHtmlGet($pPage[ 'help_button' ]) . " $systemInfoHtml"; + } + + // Send Headers + main HTML code + Header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 + Header('Content-Type: text/html;charset=utf-8'); // TODO- proper XHTML headers... + Header('Expires: Tue, 23 Jun 2009 12:00:00 GMT'); // Date in the past + echo " @@ -349,16 +349,16 @@ function dvwaHtmlEcho($pPage) -"; -} - -function dvwaHelpHtmlEcho($pPage) -{ - // Send Headers - Header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 - Header('Content-Type: text/html;charset=utf-8'); // TODO- proper XHTML headers... - Header('Expires: Tue, 23 Jun 2009 12:00:00 GMT'); // Date in the past - +"; +} + +function dvwaHelpHtmlEcho($pPage) +{ + // Send Headers + Header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 + Header('Content-Type: text/html;charset=utf-8'); // TODO- proper XHTML headers... + Header('Expires: Tue, 23 Jun 2009 12:00:00 GMT'); // Date in the past + echo " @@ -386,17 +386,17 @@ function dvwaHelpHtmlEcho($pPage) -"; -} - - -function dvwaSourceHtmlEcho($pPage) -{ - // Send Headers - Header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 - Header('Content-Type: text/html;charset=utf-8'); // TODO- proper XHTML headers... - Header('Expires: Tue, 23 Jun 2009 12:00:00 GMT'); // Date in the past - +"; +} + + +function dvwaSourceHtmlEcho($pPage) +{ + // Send Headers + Header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 + Header('Content-Type: text/html;charset=utf-8'); // TODO- proper XHTML headers... + Header('Expires: Tue, 23 Jun 2009 12:00:00 GMT'); // Date in the past + echo " @@ -424,171 +424,172 @@ function dvwaSourceHtmlEcho($pPage) -"; -} - -// To be used on all external links -- -function dvwaExternalLinkUrlGet($pLink, $text = null) -{ - if (is_null($text)) { - return '' . $pLink . ''; - } else { - return '' . $text . ''; - } -} -// -- END ( external links) - -function dvwaButtonHelpHtmlGet($pId) -{ - $security = dvwaSecurityLevelGet(); - return ""; -} - - -function dvwaButtonSourceHtmlGet($pId) -{ - $security = dvwaSecurityLevelGet(); - return ""; -} - - -// Database Management -- - -if ($DBMS == 'MySQL') { - $DBMS = htmlspecialchars(strip_tags($DBMS)); - $DBMS_errorFunc = 'mysqli_error()'; -} elseif ($DBMS == 'PGSQL') { - $DBMS = htmlspecialchars(strip_tags($DBMS)); - $DBMS_errorFunc = 'pg_last_error()'; -} else { - $DBMS = "No DBMS selected."; - $DBMS_errorFunc = ''; -} - -//$DBMS_connError = ' -//
    -// -//
    Unable to connect to the database.
    ' . $DBMS_errorFunc . '

    -// Click here to setup the database. -//
    '; - -function dvwaDatabaseConnect() -{ - global $_DVWA; - global $DBMS; - //global $DBMS_connError; - global $db; - - if ($DBMS == 'MySQL') { - if (!@($GLOBALS["___mysqli_ston"] = mysqli_connect($_DVWA[ 'db_server' ], $_DVWA[ 'db_user' ], $_DVWA[ 'db_password' ])) - || !@((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . $_DVWA[ 'db_database' ]))) { - //die( $DBMS_connError ); - dvwaLogout(); - dvwaMessagePush('Unable to connect to the database.
    ' . $DBMS_errorFunc); - dvwaRedirect(DVWA_WEB_PAGE_TO_ROOT . 'setup.php'); - } - // MySQL PDO Prepared Statements (for impossible levels) - $db = new PDO('mysql:host=' . $_DVWA[ 'db_server' ].';dbname=' . $_DVWA[ 'db_database' ].';charset=utf8', $_DVWA[ 'db_user' ], $_DVWA[ 'db_password' ]); - $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); - } elseif ($DBMS == 'PGSQL') { - //$dbconn = pg_connect("host={$_DVWA[ 'db_server' ]} dbname={$_DVWA[ 'db_database' ]} user={$_DVWA[ 'db_user' ]} password={$_DVWA[ 'db_password' ])}" - //or die( $DBMS_connError ); - dvwaMessagePush('PostgreSQL is not yet fully supported.'); - dvwaPageReload(); - } else { - die("Unknown {$DBMS} selected."); - } -} - - -function dvwaRedirect($pLocation) -{ - session_commit(); - header("Location: {$pLocation}"); - exit; -} - -// XSS Stored guestbook function -- -function dvwaGuestbook() -{ - $query = "SELECT name, comment FROM guestbook"; - $result = mysqli_query($GLOBALS["___mysqli_ston"], $query); - - $guestbook = ''; - - while ($row = mysqli_fetch_row($result)) { - if (dvwaSecurityLevelGet() == 'impossible') { - $name = htmlspecialchars($row[0]); - $comment = htmlspecialchars($row[1]); - } else { - $name = $row[0]; - $comment = $row[1]; - } - - $guestbook .= "
    Name: {$name}
    " . "Message: {$comment}
    \n"; - } - return $guestbook; -} -// -- END (XSS Stored guestbook) - - -// Token functions -- -function checkToken($user_token, $session_token, $returnURL) -{ - # Validate the given (CSRF) token - if ($user_token !== $session_token || !isset($session_token)) { - dvwaMessagePush('CSRF token is incorrect'); - dvwaRedirect($returnURL); - } -} - -function generateSessionToken() -{ - # Generate a brand new (CSRF) token - if (isset($_SESSION[ 'session_token' ])) { - destroySessionToken(); - } - $_SESSION[ 'session_token' ] = md5(uniqid()); -} - -function destroySessionToken() -{ - # Destroy any session with the name 'session_token' - unset($_SESSION[ 'session_token' ]); -} - -function tokenField() -{ - # Return a field for the (CSRF) token - return ""; -} -// -- END (Token functions) - - -// Setup Functions -- -$PHPUploadPath = realpath(getcwd() . DIRECTORY_SEPARATOR . DVWA_WEB_PAGE_TO_ROOT . "hackable" . DIRECTORY_SEPARATOR . "uploads") . DIRECTORY_SEPARATOR; -$PHPIDSPath = realpath(getcwd() . DIRECTORY_SEPARATOR . DVWA_WEB_PAGE_TO_ROOT . "external" . DIRECTORY_SEPARATOR . "phpids" . DIRECTORY_SEPARATOR . dvwaPhpIdsVersionGet() . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . "IDS" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . "phpids_log.txt"); - -$phpDisplayErrors = 'PHP function display_errors: ' . (ini_get('display_errors') ? 'Enabled (Easy Mode!)' : 'Disabled'); // Verbose error messages (e.g. full path disclosure) -$phpSafeMode = 'PHP function safe_mode: Enabled' : 'success">Disabled') . ''; // DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0 -$phpMagicQuotes = 'PHP function magic_quotes_gpc: Enabled' : 'success">Disabled') . ''; // DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0 -$phpURLInclude = 'PHP function allow_url_include: Enabled' : 'failure">Disabled') . ''; // RFI -$phpURLFopen = 'PHP function allow_url_fopen: Enabled' : 'failure">Disabled') . ''; // RFI -$phpGD = 'PHP module gd: Installed' : 'failure">Missing') . ''; // File Upload -$phpMySQL = 'PHP module mysql: Installed' : 'failure">Missing') . ''; // Core DVWA -$phpPDO = 'PHP module pdo_mysql: Installed' : 'failure">Missing') . ''; // SQLi - -$DVWARecaptcha = 'reCAPTCHA key: ' . $_DVWA[ 'recaptcha_public_key' ] : 'failure">Missing') . ''; - -$DVWAUploadsWrite = '[User: ' . get_current_user() . '] Writable folder ' . $PHPUploadPath . ': Yes' : 'failure">No') . ''; // File Upload -$DVWAPHPWrite = '[User: ' . get_current_user() . '] Writable file ' . $PHPIDSPath . ': Yes' : 'failure">No') . ''; // PHPIDS - -$DVWAOS = 'Operating system: ' . (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'Windows' : '*nix') . ''; -$SERVER_NAME = 'Web Server SERVER_NAME: ' . $_SERVER[ 'SERVER_NAME' ] . ''; // CSRF - -$MYSQL_USER = 'MySQL username: ' . $_DVWA[ 'db_user' ] . ''; -$MYSQL_PASS = 'MySQL password: ' . (($_DVWA[ 'db_password' ] != "") ? '******' : '*blank*') . ''; -$MYSQL_DB = 'MySQL database: ' . $_DVWA[ 'db_database' ] . ''; -$MYSQL_SERVER = 'MySQL host: ' . $_DVWA[ 'db_server' ] . ''; -// -- END (Setup Functions) +"; +} + +// To be used on all external links -- +function dvwaExternalLinkUrlGet($pLink, $text = null) +{ + if (is_null($text)) { + return '' . $pLink . ''; + } else { + return '' . $text . ''; + } +} +// -- END ( external links) + +function dvwaButtonHelpHtmlGet($pId) +{ + $security = dvwaSecurityLevelGet(); + return ""; +} + + +function dvwaButtonSourceHtmlGet($pId) +{ + $security = dvwaSecurityLevelGet(); + return ""; +} + + +// Database Management -- + +if ($DBMS == 'MySQL') { + $DBMS = htmlspecialchars(strip_tags($DBMS)); + $DBMS_errorFunc = 'mysqli_error()'; +} elseif ($DBMS == 'PGSQL') { + $DBMS = htmlspecialchars(strip_tags($DBMS)); + $DBMS_errorFunc = 'pg_last_error()'; +} else { + $DBMS = "No DBMS selected."; + $DBMS_errorFunc = ''; +} + +//$DBMS_connError = ' +//
    +// +//
    Unable to connect to the database.
    ' . $DBMS_errorFunc . '

    +// Click here to setup the database. +//
    '; + +function dvwaDatabaseConnect() +{ + global $_DVWA; + global $DBMS; + //global $DBMS_connError; + global $db; + + if ($DBMS == 'MySQL') { + if (!@($GLOBALS["___mysqli_ston"] = mysqli_connect($_DVWA[ 'db_server' ], $_DVWA[ 'db_user' ], $_DVWA[ 'db_password' ])) + || !@((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . $_DVWA[ 'db_database' ]))) { + //die( $DBMS_connError ); + dvwaLogout(); + dvwaMessagePush('Unable to connect to the database.
    ' . $DBMS_errorFunc); + dvwaRedirect(DVWA_WEB_PAGE_TO_ROOT . 'setup.php'); + } + // MySQL PDO Prepared Statements (for impossible levels) + $db = new PDO('mysql:host=' . $_DVWA[ 'db_server' ].';dbname=' . $_DVWA[ 'db_database' ].';charset=utf8', $_DVWA[ 'db_user' ], $_DVWA[ 'db_password' ]); + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + } elseif ($DBMS == 'PGSQL') { + //$dbconn = pg_connect("host={$_DVWA[ 'db_server' ]} dbname={$_DVWA[ 'db_database' ]} user={$_DVWA[ 'db_user' ]} password={$_DVWA[ 'db_password' ])}" + //or die( $DBMS_connError ); + dvwaMessagePush('PostgreSQL is not yet fully supported.'); + dvwaPageReload(); + } else { + die("Unknown {$DBMS} selected."); + } +} + + +function dvwaRedirect($pLocation) +{ + session_commit(); + header("Location: {$pLocation}"); + exit; +} + +// XSS Stored guestbook function -- +function dvwaGuestbook() +{ + $query = "SELECT name, comment FROM guestbook"; + $result = mysqli_query($GLOBALS["___mysqli_ston"], $query); + + $guestbook = ''; + + while ($row = mysqli_fetch_row($result)) { + if (dvwaSecurityLevelGet() == 'impossible') { + $name = htmlspecialchars($row[0]); + $comment = htmlspecialchars($row[1]); + } else { + $name = $row[0]; + $comment = $row[1]; + } + + $guestbook .= "
    Name: {$name}
    " . "Message: {$comment}
    \n"; + } + return $guestbook; +} +// -- END (XSS Stored guestbook) + + +// Token functions -- +function checkToken($user_token, $session_token, $returnURL) +{ + # Validate the given (CSRF) token + if ($user_token !== $session_token || !isset($session_token)) { + dvwaMessagePush('CSRF token is incorrect'); + dvwaRedirect($returnURL); + } +} + +function generateSessionToken() +{ + # Generate a brand new (CSRF) token + if (isset($_SESSION[ 'session_token' ])) { + destroySessionToken(); + } + $_SESSION[ 'session_token' ] = md5(uniqid()); +} + +function destroySessionToken() +{ + # Destroy any session with the name 'session_token' + unset($_SESSION[ 'session_token' ]); +} + +function tokenField() +{ + # Return a field for the (CSRF) token + return ""; +} +// -- END (Token functions) + + +// Setup Functions -- +$PHPUploadPath = realpath(getcwd() . DIRECTORY_SEPARATOR . DVWA_WEB_PAGE_TO_ROOT . "hackable" . DIRECTORY_SEPARATOR . "uploads") . DIRECTORY_SEPARATOR; +$PHPIDSPath = realpath(getcwd() . DIRECTORY_SEPARATOR . DVWA_WEB_PAGE_TO_ROOT . "external" . DIRECTORY_SEPARATOR . "phpids" . DIRECTORY_SEPARATOR . dvwaPhpIdsVersionGet() . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . "IDS" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . "phpids_log.txt"); + +$phpDisplayErrors = 'PHP function display_errors: ' . (ini_get('display_errors') ? 'Enabled (Easy Mode!)' : 'Disabled'); // Verbose error messages (e.g. full path disclosure) +$phpSafeMode = 'PHP function safe_mode: Enabled' : 'success">Disabled') . ''; // DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0 +$phpMagicQuotes = 'PHP function magic_quotes_gpc: Enabled' : 'success">Disabled') . ''; // DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0 +$phpURLInclude = 'PHP function allow_url_include: Enabled' : 'failure">Disabled') . ''; // RFI +$phpURLFopen = 'PHP function allow_url_fopen: Enabled' : 'failure">Disabled') . ''; // RFI +$phpGD = 'PHP module gd: Installed' : 'failure">Missing') . ''; // File Upload +$phpMySQL = 'PHP module mysql: Installed' : 'failure">Missing') . ''; // Core DVWA +$phpPDO = 'PHP module pdo_mysql: Installed' : 'failure">Missing') . ''; // SQLi + +$DVWARecaptcha = 'reCAPTCHA key: ' . $_DVWA[ 'recaptcha_public_key' ] : 'failure">Missing') . ''; + +$DVWAUploadsWrite = '[User: ' . get_current_user() . '] Writable folder ' . $PHPUploadPath . ': Yes' : 'failure">No') . ''; // File Upload +$DVWAPHPWrite = '[User: ' . get_current_user() . '] Writable file ' . $PHPIDSPath . ': Yes' : 'failure">No') . ''; // PHPIDS + +$DVWAOS = 'Operating system: ' . (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'Windows' : '*nix') . ''; +$SERVER_NAME = 'Web Server SERVER_NAME: ' . $_SERVER[ 'SERVER_NAME' ] . ''; // CSRF + +$MYSQL_USER = 'MySQL username: ' . $_DVWA[ 'db_user' ] . ''; +$MYSQL_PASS = 'MySQL password: ' . (($_DVWA[ 'db_password' ] != "") ? '******' : '*blank*') . ''; +$MYSQL_DB = 'MySQL database: ' . $_DVWA[ 'db_database' ] . ''; +$MYSQL_SERVER = 'MySQL host: ' . $_DVWA[ 'db_server' ] . ''; +// -- END (Setup Functions) + diff --git a/projects/tests/tests/includes/dvwa/low.php b/projects/tests/tests/includes/dvwa/low.php index d318f906..ada7e104 100644 --- a/projects/tests/tests/includes/dvwa/low.php +++ b/projects/tests/tests/includes/dvwa/low.php @@ -1,9 +1,10 @@ Hello ' . $_GET[ 'a' ] . ''; + */ + $html .= '
    Hello ' . $_GET[ 'a' ] . '
    ';/* } if (true) { @@ -11,3 +12,4 @@ } else { $html = "eee"; } +*/ diff --git a/projects/tests/tests/includes/includes_simple5.txt b/projects/tests/tests/includes/includes_simple5.txt deleted file mode 100644 index 302f356b..00000000 --- a/projects/tests/tests/includes/includes_simple5.txt +++ /dev/null @@ -1 +0,0 @@ -{"includes_not_resolved":[["/home/eric/git/designsecurity/progpilot/projects/tests/tests/includes/simple5.php",11,51]]} diff --git a/projects/tests/tests/includes/simple18.php b/projects/tests/tests/includes/simple18.php new file mode 100644 index 00000000..402513e2 --- /dev/null +++ b/projects/tests/tests/includes/simple18.php @@ -0,0 +1,9 @@ +get_results("SE"); +echo $comments->post; diff --git a/projects/tests/tests/includes/simple20_include.php b/projects/tests/tests/includes/simple20_include.php new file mode 100644 index 00000000..ce81d1bf --- /dev/null +++ b/projects/tests/tests/includes/simple20_include.php @@ -0,0 +1,4 @@ +test2 = new test2(); + //$this->test2property = new test2(); + } + + public function getTest2() { + // blockid 37 + return $this->test2; + } +}; + +class test2 +{ + //private $test3; + public $test3property; + + public function __construct() { + // blockid 43 + $this->test3property = new test3(); + } +}; + +class test3 +{ + public function insecure($tainted) { + // blockid 51 + echo $tainted; + } +}; + +// blockid3 +$t = new test1(); +$a = $t->getTest2()->test3property->insecure($_GET["p"]); diff --git a/projects/tests/tests/oop/chained2.php b/projects/tests/tests/oop/chained2.php new file mode 100644 index 00000000..680a1a16 --- /dev/null +++ b/projects/tests/tests/oop/chained2.php @@ -0,0 +1,23 @@ +baba(); // remove and it works again + return $this->last_result; + } + + function baba() { + $num_rows = 0; + while ( $row = @mysql_fetch_object(true) ) { + $this->last_result[$num_rows] = $row; + $num_rows++; + } + } +} + +$inst = new toto; +$aaas = $inst->getresults(); + +foreach($aaas as $aaa) { + echo $aaa->title; +} diff --git a/projects/tests/tests/oop/simple12.php b/projects/tests/tests/oop/simple12.php index 70862125..2917ec4c 100644 --- a/projects/tests/tests/oop/simple12.php +++ b/projects/tests/tests/oop/simple12.php @@ -6,11 +6,11 @@ class testc1 }; $instance1 = new testc1; -$instance1->boum1 = $_GET["p"]; +$instance1->boum1 = $_GET["p"]; // block 3 if (rand() % 2) { - $instance1->boum1 = "eee"; + $instance1->boum1 = "eee"; // block 16 echo $instance1->boum1; } else { - echo $instance1->boum1; + echo $instance1->boum1; // block 18 } diff --git a/projects/tests/tests/oop/simple13.php b/projects/tests/tests/oop/simple13.php index 7c5adaa0..4a1d8f1c 100644 --- a/projects/tests/tests/oop/simple13.php +++ b/projects/tests/tests/oop/simple13.php @@ -4,18 +4,33 @@ class testc1 { public $boum1; + public function dosomething() + { + // block 52 + } + public function sanitize() { + // block 56 $this->boum1 = htmlentities($this->boum1); } + }; +// block 3 $instance1 = new testc1; $instance1->boum1 = $_GET["p"]; -if (rand() % 2) { +if (rand() % 2) { // block 7 + // block 20 $instance1->sanitize(); echo $instance1->boum1; + $instance1->dosomething(); + echo $instance1->boum1; } else { + // block 42 echo $instance1->boum1; } + +// block 35 +echo $instance1->boum1; diff --git a/projects/tests/tests/oop/simple15.php b/projects/tests/tests/oop/simple15.php index 8517596a..34e15f1c 100644 --- a/projects/tests/tests/oop/simple15.php +++ b/projects/tests/tests/oop/simple15.php @@ -6,16 +6,21 @@ class testc1 public function echo_boum1() { + // block 34 echo $this->boum1; } }; +// block 7 $instance1 = new testc1; if (rand() % 2) { + // block 13 $instance1->boum1 = $_GET["p"]; } else { + // block 23 $instance1->boum1 = "eee"; } +// block 21 $instance1->echo_boum1(); diff --git a/projects/tests/tests/oop/simple17.php b/projects/tests/tests/oop/simple17.php index 17733a87..30245a12 100644 --- a/projects/tests/tests/oop/simple17.php +++ b/projects/tests/tests/oop/simple17.php @@ -5,7 +5,7 @@ class testc1 public $boum1; public function set_boum1($boum1) - { + { // block 24 $this->boum1[0] = $boum1; } }; @@ -27,7 +27,7 @@ public function set_boum1($boum1) } $instance1->set_boum1($_GET["p"]); - +// block 3 $copy = $instance1->boum1; echo $copy[0]; diff --git a/projects/tests/tests/oop/simple19.php b/projects/tests/tests/oop/simple19.php index 99c84993..b4c58ebf 100644 --- a/projects/tests/tests/oop/simple19.php +++ b/projects/tests/tests/oop/simple19.php @@ -2,8 +2,6 @@ class testc1 { - private $member1; - public $object1; public function set_object1($val) @@ -22,13 +20,16 @@ public function set_object2($val) } }; + $testc1 = new testc1; $testc1->object1 = new testc2; $testc1->object1->object2 = $_GET["p"]; -echo $testc1->object1->object2; +$toto = $testc1->object1->object2; + +echo $toto; // NOT OK @@ -36,17 +37,14 @@ public function set_object2($val) $newtestc1->object1 = $_GET["p"]; -echo $newtestc1->object1; +echo $newtestc1->object1; // NOT OK $newsettestc1 = new testc1; $newsettestc1->set_object1($_GET["p"]); -echo $newsettestc1->object1; - - - +echo $newsettestc1->object1; // NOT OK @@ -56,7 +54,7 @@ public function set_object2($val) $testc1_encore->object1 = new testc2; -echo $testc1_encore->object1->object2; +echo $testc1_encore->object1->object2; // OK @@ -66,6 +64,11 @@ public function set_object2($val) $val = $testc1_encore2->member1; -echo $testc1_encore2->member1; +echo $testc1_encore2->member1; // NOT OK + +echo $val; // NOT OK + + + + -echo $val; diff --git a/projects/tests/tests/oop/simple20.php b/projects/tests/tests/oop/simple20.php index 947cc099..511ef03b 100644 --- a/projects/tests/tests/oop/simple20.php +++ b/projects/tests/tests/oop/simple20.php @@ -1,6 +1,6 @@ member1 = $_GET["p"]; diff --git a/projects/tests/tests/oop/simple22.php b/projects/tests/tests/oop/simple22.php index e625e534..77dc6a81 100644 --- a/projects/tests/tests/oop/simple22.php +++ b/projects/tests/tests/oop/simple22.php @@ -12,13 +12,13 @@ class Testc1 $b = $a; -echo $a->data; +echo $a->data; // KO -echo $b->data; +echo $b->data; // KO $b->data = "eee"; -echo $a->data; +echo $a->data; // OK -echo $b->data; +echo $b->data; // OK diff --git a/projects/tests/tests/oop/simple24.php b/projects/tests/tests/oop/simple24.php index 8e3da5e0..41292aed 100644 --- a/projects/tests/tests/oop/simple24.php +++ b/projects/tests/tests/oop/simple24.php @@ -6,7 +6,7 @@ class Testc1 public $data; public function change_data($data) - { + { // block 48 $this->data = $data; } } @@ -19,13 +19,13 @@ public function change_data($data) $a->change_data("eee"); } -echo $a->data; +echo $a->data; // id 62 $b = new Testc1; if (rand() === 1) { $b->change_data("eee"); -} else { +} else { // block 35 $b->change_data($_GET["p"]); } - -echo $b->data; +// block 30 +echo $b->data; // id 76 diff --git a/projects/tests/tests/oop/simple25.php b/projects/tests/tests/oop/simple25.php index eaca6aea..53dcc4bb 100644 --- a/projects/tests/tests/oop/simple25.php +++ b/projects/tests/tests/oop/simple25.php @@ -1,4 +1,5 @@ babar(); -$a->bobo1(); +$a->babar($_GET["p"]); +$a->bobo1($_GET["t"]); diff --git a/projects/tests/tests/oop/simple33.php b/projects/tests/tests/oop/simple33.php new file mode 100644 index 00000000..9d656611 --- /dev/null +++ b/projects/tests/tests/oop/simple33.php @@ -0,0 +1,45 @@ +test2 = new test2(); + } + + public function getTest2() { + // block 33 (parents = 3) + return $this->test2; + } +}; + +class test2 +{ + private $test3; + + public function __construct() { + // block 39 (parents = 26) + $this->test3 = new test3(); + } + + public function getTest3() { + // block 46 (parents = 3) + return $this->test3; + } +}; + +class test3 +{ + public function insecure($tainted) { + // block 53 + echo $tainted; + } +}; + + +// block 3 +$t = new test1(); +$t->getTest2()->getTest3()->insecure($_GET["p"]); + diff --git a/projects/tests/tests/oop/simple34.php b/projects/tests/tests/oop/simple34.php new file mode 100644 index 00000000..e742627a --- /dev/null +++ b/projects/tests/tests/oop/simple34.php @@ -0,0 +1,19 @@ +input = $_GET['UserData']; + + return $this->input; + } +} +$temp = new Input(); + +$tainted = $temp->getInput(); + +echo $tainted; + diff --git a/projects/tests/tests/oop/simple35.php b/projects/tests/tests/oop/simple35.php new file mode 100644 index 00000000..448aaf8f --- /dev/null +++ b/projects/tests/tests/oop/simple35.php @@ -0,0 +1,32 @@ +boum1 = $boum1; // block 40 (tainted state = 26) + } + + public function get_boum1() + { + return $this->boum1; // block 47 + } +}; + +// block 3 +$instance = new testc1; + +if (true) { + // block 10 + echo "toto\n"; + echo $instance->get_boum1(); +} else { + // block 26 + $instance->set_boum1($_GET["p"]); + echo $instance->get_boum1(); +} + +// block 20 +echo $instance->get_boum1(); diff --git a/projects/tests/tests/oop/simple36.php b/projects/tests/tests/oop/simple36.php new file mode 100644 index 00000000..ad2cd635 --- /dev/null +++ b/projects/tests/tests/oop/simple36.php @@ -0,0 +1,33 @@ +last_result = null; + + if ( true ) { + } else { + $num_rows = 0; + while ( $row = @mysql_fetch_object(true) ) { + $this->last_result = $row; + $num_rows++; + } + } + } + + function get_results() { + $this->query($query); + return $this->last_result; + } +} + +$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); + + +if (true) { + $aaas = $wpdb->get_results(); +} +else { + $aaas = $wpdb->get_results(); +} + +echo $aaas->title; diff --git a/projects/tests/tests/oop/simple5.php b/projects/tests/tests/oop/simple5.php index ba505618..45e38b01 100644 --- a/projects/tests/tests/oop/simple5.php +++ b/projects/tests/tests/oop/simple5.php @@ -2,11 +2,16 @@ class testc1 { - private $boum1; + private $boum1; public function set_boum1($boum1) { - $this->boum1 = $boum1; + $this->boum1 = $boum1; // block 23 + } + + public function get_boum1() + { + return $this->boum1; // block 30 } }; @@ -16,16 +21,15 @@ class testc2 }; if (true) { - $instance = new testc1; + $instance = new testc1; // block 3 } else { $instance = new testc2; } if (true) { $instance->boum1 = $_GET["p"]; -//$instance->boum1 = "eee"; } else { - $instance->set_boum1($_GET["p"]); + $instance->set_boum1($_GET["p"]); // block 3 } -echo $instance->boum1; +echo $instance->get_boum1(); // block 3 diff --git a/projects/tests/tests/optimizations/cleanopinfoconcatleft.php b/projects/tests/tests/optimizations/cleanopinfoconcatleft.php new file mode 100644 index 00000000..6482ba6e --- /dev/null +++ b/projects/tests/tests/optimizations/cleanopinfoconcatleft.php @@ -0,0 +1,13 @@ +_message.=$tmp; + + echo $this->_message; + } +} diff --git a/projects/tests/tests/performance/ID3/getid3.lib.php b/projects/tests/tests/performance/ID3/getid3.lib.php new file mode 100644 index 00000000..3a5983fc --- /dev/null +++ b/projects/tests/tests/performance/ID3/getid3.lib.php @@ -0,0 +1,1807 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// // +// getid3.lib.php - part of getID3() // +// see readme.txt for more details // +// /// +///////////////////////////////////////////////////////////////// + + +class getid3_lib +{ + /** + * @param string $string + * @param bool $hex + * @param bool $spaces + * @param string|bool $htmlencoding + * + * @return string + */ + public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') { + $returnstring = ''; + for ($i = 0; $i < strlen($string); $i++) { + if ($hex) { + $returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT); + } else { + $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ? $string[$i] : '¤'); + } + if ($spaces) { + $returnstring .= ' '; + } + } + if (!empty($htmlencoding)) { + if ($htmlencoding === true) { + $htmlencoding = 'UTF-8'; // prior to getID3 v1.9.0 the function's 4th parameter was boolean + } + $returnstring = htmlentities($returnstring, ENT_QUOTES, $htmlencoding); + } + return $returnstring; + } + + /** + * Truncates a floating-point number at the decimal point. + * + * @param float $floatnumber + * + * @return float|int returns int (if possible, otherwise float) + */ + public static function trunc($floatnumber) { + if ($floatnumber >= 1) { + $truncatednumber = floor($floatnumber); + } elseif ($floatnumber <= -1) { + $truncatednumber = ceil($floatnumber); + } else { + $truncatednumber = 0; + } + if (self::intValueSupported($truncatednumber)) { + $truncatednumber = (int) $truncatednumber; + } + return $truncatednumber; + } + + /** + * @param int|null $variable + * @param int $increment + * + * @return bool + */ + public static function safe_inc(&$variable, $increment=1) { + if (isset($variable)) { + $variable += $increment; + } else { + $variable = $increment; + } + return true; + } + + /** + * @param int|float $floatnum + * + * @return int|float + */ + public static function CastAsInt($floatnum) { + // convert to float if not already + $floatnum = (float) $floatnum; + + // convert a float to type int, only if possible + if (self::trunc($floatnum) == $floatnum) { + // it's not floating point + if (self::intValueSupported($floatnum)) { + // it's within int range + $floatnum = (int) $floatnum; + } + } + return $floatnum; + } + + /** + * @param int $num + * + * @return bool + */ + public static function intValueSupported($num) { + // check if integers are 64-bit + static $hasINT64 = null; + if ($hasINT64 === null) { // 10x faster than is_null() + $hasINT64 = is_int(pow(2, 31)); // 32-bit int are limited to (2^31)-1 + if (!$hasINT64 && !defined('PHP_INT_MIN')) { + define('PHP_INT_MIN', ~PHP_INT_MAX); + } + } + // if integers are 64-bit - no other check required + if ($hasINT64 || (($num <= PHP_INT_MAX) && ($num >= PHP_INT_MIN))) { // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound + return true; + } + return false; + } + + /** + * @param string $fraction + * + * @return float + */ + public static function DecimalizeFraction($fraction) { + list($numerator, $denominator) = explode('/', $fraction); + return $numerator / ($denominator ? $denominator : 1); + } + + /** + * @param string $binarynumerator + * + * @return float + */ + public static function DecimalBinary2Float($binarynumerator) { + $numerator = self::Bin2Dec($binarynumerator); + $denominator = self::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator))); + return ($numerator / $denominator); + } + + /** + * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html + * + * @param string $binarypointnumber + * @param int $maxbits + * + * @return array + */ + public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) { + if (strpos($binarypointnumber, '.') === false) { + $binarypointnumber = '0.'.$binarypointnumber; + } elseif ($binarypointnumber[0] == '.') { + $binarypointnumber = '0'.$binarypointnumber; + } + $exponent = 0; + while (($binarypointnumber[0] != '1') || (substr($binarypointnumber, 1, 1) != '.')) { + if (substr($binarypointnumber, 1, 1) == '.') { + $exponent--; + $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3); + } else { + $pointpos = strpos($binarypointnumber, '.'); + $exponent += ($pointpos - 1); + $binarypointnumber = str_replace('.', '', $binarypointnumber); + $binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1); + } + } + $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT); + return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent); + } + + /** + * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html + * + * @param float $floatvalue + * + * @return string + */ + public static function Float2BinaryDecimal($floatvalue) { + $maxbits = 128; // to how many bits of precision should the calculations be taken? + $intpart = self::trunc($floatvalue); + $floatpart = abs($floatvalue - $intpart); + $pointbitstring = ''; + while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) { + $floatpart *= 2; + $pointbitstring .= (string) self::trunc($floatpart); + $floatpart -= self::trunc($floatpart); + } + $binarypointnumber = decbin($intpart).'.'.$pointbitstring; + return $binarypointnumber; + } + + /** + * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html + * + * @param float $floatvalue + * @param int $bits + * + * @return string|false + */ + public static function Float2String($floatvalue, $bits) { + $exponentbits = 0; + $fractionbits = 0; + switch ($bits) { + case 32: + $exponentbits = 8; + $fractionbits = 23; + break; + + case 64: + $exponentbits = 11; + $fractionbits = 52; + break; + + default: + return false; + } + if ($floatvalue >= 0) { + $signbit = '0'; + } else { + $signbit = '1'; + } + $normalizedbinary = self::NormalizeBinaryPoint(self::Float2BinaryDecimal($floatvalue), $fractionbits); + $biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent + $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT); + $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT); + + return self::BigEndian2String(self::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false); + } + + /** + * @param string $byteword + * + * @return float|false + */ + public static function LittleEndian2Float($byteword) { + return self::BigEndian2Float(strrev($byteword)); + } + + /** + * ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic + * + * @link http://www.psc.edu/general/software/packages/ieee/ieee.html + * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html + * + * @param string $byteword + * + * @return float|false + */ + public static function BigEndian2Float($byteword) { + $bitword = self::BigEndian2Bin($byteword); + if (!$bitword) { + return 0; + } + $signbit = $bitword[0]; + $floatvalue = 0; + $exponentbits = 0; + $fractionbits = 0; + + switch (strlen($byteword) * 8) { + case 32: + $exponentbits = 8; + $fractionbits = 23; + break; + + case 64: + $exponentbits = 11; + $fractionbits = 52; + break; + + case 80: + // 80-bit Apple SANE format + // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/ + $exponentstring = substr($bitword, 1, 15); + $isnormalized = intval($bitword[16]); + $fractionstring = substr($bitword, 17, 63); + $exponent = pow(2, self::Bin2Dec($exponentstring) - 16383); + $fraction = $isnormalized + self::DecimalBinary2Float($fractionstring); + $floatvalue = $exponent * $fraction; + if ($signbit == '1') { + $floatvalue *= -1; + } + return $floatvalue; + + default: + return false; + } + $exponentstring = substr($bitword, 1, $exponentbits); + $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits); + $exponent = self::Bin2Dec($exponentstring); + $fraction = self::Bin2Dec($fractionstring); + + if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) { + // Not a Number + $floatvalue = false; + } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) { + if ($signbit == '1') { + $floatvalue = '-infinity'; + } else { + $floatvalue = '+infinity'; + } + } elseif (($exponent == 0) && ($fraction == 0)) { + if ($signbit == '1') { + $floatvalue = -0; + } else { + $floatvalue = 0; + } + $floatvalue = ($signbit ? 0 : -0); + } elseif (($exponent == 0) && ($fraction != 0)) { + // These are 'unnormalized' values + $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * self::DecimalBinary2Float($fractionstring); + if ($signbit == '1') { + $floatvalue *= -1; + } + } elseif ($exponent != 0) { + $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + self::DecimalBinary2Float($fractionstring)); + if ($signbit == '1') { + $floatvalue *= -1; + } + } + return (float) $floatvalue; + } + + /** + * @param string $byteword + * @param bool $synchsafe + * @param bool $signed + * + * @return int|float|false + * @throws Exception + */ + public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) { + $intvalue = 0; + $bytewordlen = strlen($byteword); + if ($bytewordlen == 0) { + return false; + } + for ($i = 0; $i < $bytewordlen; $i++) { + if ($synchsafe) { // disregard MSB, effectively 7-bit bytes + //$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems + $intvalue += (ord($byteword[$i]) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7); + } else { + $intvalue += ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i)); + } + } + if ($signed && !$synchsafe) { + // synchsafe ints are not allowed to be signed + if ($bytewordlen <= PHP_INT_SIZE) { + $signMaskBit = 0x80 << (8 * ($bytewordlen - 1)); + if ($intvalue & $signMaskBit) { + $intvalue = 0 - ($intvalue & ($signMaskBit - 1)); + } + } else { + throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in self::BigEndian2Int()'); + } + } + return self::CastAsInt($intvalue); + } + + /** + * @param string $byteword + * @param bool $signed + * + * @return int|float|false + */ + public static function LittleEndian2Int($byteword, $signed=false) { + return self::BigEndian2Int(strrev($byteword), false, $signed); + } + + /** + * @param string $byteword + * + * @return string + */ + public static function LittleEndian2Bin($byteword) { + return self::BigEndian2Bin(strrev($byteword)); + } + + /** + * @param string $byteword + * + * @return string + */ + public static function BigEndian2Bin($byteword) { + $binvalue = ''; + $bytewordlen = strlen($byteword); + for ($i = 0; $i < $bytewordlen; $i++) { + $binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT); + } + return $binvalue; + } + + /** + * @param int $number + * @param int $minbytes + * @param bool $synchsafe + * @param bool $signed + * + * @return string + * @throws Exception + */ + public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) { + if ($number < 0) { + throw new Exception('ERROR: self::BigEndian2String() does not support negative numbers'); + } + $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF); + $intstring = ''; + if ($signed) { + if ($minbytes > PHP_INT_SIZE) { + throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits in self::BigEndian2String()'); + } + $number = $number & (0x80 << (8 * ($minbytes - 1))); + } + while ($number != 0) { + $quotient = ($number / ($maskbyte + 1)); + $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring; + $number = floor($quotient); + } + return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT); + } + + /** + * @param int $number + * + * @return string + */ + public static function Dec2Bin($number) { + while ($number >= 256) { + $bytes[] = (($number / 256) - (floor($number / 256))) * 256; + $number = floor($number / 256); + } + $bytes[] = $number; + $binstring = ''; + for ($i = 0; $i < count($bytes); $i++) { + $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring; + } + return $binstring; + } + + /** + * @param string $binstring + * @param bool $signed + * + * @return int|float + */ + public static function Bin2Dec($binstring, $signed=false) { + $signmult = 1; + if ($signed) { + if ($binstring[0] == '1') { + $signmult = -1; + } + $binstring = substr($binstring, 1); + } + $decvalue = 0; + for ($i = 0; $i < strlen($binstring); $i++) { + $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i); + } + return self::CastAsInt($decvalue * $signmult); + } + + /** + * @param string $binstring + * + * @return string + */ + public static function Bin2String($binstring) { + // return 'hi' for input of '0110100001101001' + $string = ''; + $binstringreversed = strrev($binstring); + for ($i = 0; $i < strlen($binstringreversed); $i += 8) { + $string = chr(self::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string; + } + return $string; + } + + /** + * @param int $number + * @param int $minbytes + * @param bool $synchsafe + * + * @return string + */ + public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) { + $intstring = ''; + while ($number > 0) { + if ($synchsafe) { + $intstring = $intstring.chr($number & 127); + $number >>= 7; + } else { + $intstring = $intstring.chr($number & 255); + $number >>= 8; + } + } + return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT); + } + + /** + * @param mixed $array1 + * @param mixed $array2 + * + * @return array|false + */ + public static function array_merge_clobber($array1, $array2) { + // written by kcØhireability*com + // taken from http://www.php.net/manual/en/function.array-merge-recursive.php + if (!is_array($array1) || !is_array($array2)) { + return false; + } + $newarray = $array1; + foreach ($array2 as $key => $val) { + if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) { + $newarray[$key] = self::array_merge_clobber($newarray[$key], $val); + } else { + $newarray[$key] = $val; + } + } + return $newarray; + } + + /** + * @param mixed $array1 + * @param mixed $array2 + * + * @return array|false + */ + public static function array_merge_noclobber($array1, $array2) { + if (!is_array($array1) || !is_array($array2)) { + return false; + } + $newarray = $array1; + foreach ($array2 as $key => $val) { + if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) { + $newarray[$key] = self::array_merge_noclobber($newarray[$key], $val); + } elseif (!isset($newarray[$key])) { + $newarray[$key] = $val; + } + } + return $newarray; + } + + /** + * @param mixed $array1 + * @param mixed $array2 + * + * @return array|false|null + */ + public static function flipped_array_merge_noclobber($array1, $array2) { + if (!is_array($array1) || !is_array($array2)) { + return false; + } + # naturally, this only works non-recursively + $newarray = array_flip($array1); + foreach (array_flip($array2) as $key => $val) { + if (!isset($newarray[$key])) { + $newarray[$key] = count($newarray); + } + } + return array_flip($newarray); + } + + /** + * @param array $theArray + * + * @return bool + */ + public static function ksort_recursive(&$theArray) { + ksort($theArray); + foreach ($theArray as $key => $value) { + if (is_array($value)) { + self::ksort_recursive($theArray[$key]); + } + } + return true; + } + + /** + * @param string $filename + * @param int $numextensions + * + * @return string + */ + public static function fileextension($filename, $numextensions=1) { + if (strstr($filename, '.')) { + $reversedfilename = strrev($filename); + $offset = 0; + for ($i = 0; $i < $numextensions; $i++) { + $offset = strpos($reversedfilename, '.', $offset + 1); + if ($offset === false) { + return ''; + } + } + return strrev(substr($reversedfilename, 0, $offset)); + } + return ''; + } + + /** + * @param int $seconds + * + * @return string + */ + public static function PlaytimeString($seconds) { + $sign = (($seconds < 0) ? '-' : ''); + $seconds = round(abs($seconds)); + $H = (int) floor( $seconds / 3600); + $M = (int) floor(($seconds - (3600 * $H) ) / 60); + $S = (int) round( $seconds - (3600 * $H) - (60 * $M) ); + return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT); + } + + /** + * @param int $macdate + * + * @return int|float + */ + public static function DateMac2Unix($macdate) { + // Macintosh timestamp: seconds since 00:00h January 1, 1904 + // UNIX timestamp: seconds since 00:00h January 1, 1970 + return self::CastAsInt($macdate - 2082844800); + } + + /** + * @param string $rawdata + * + * @return float + */ + public static function FixedPoint8_8($rawdata) { + return self::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (self::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8)); + } + + /** + * @param string $rawdata + * + * @return float + */ + public static function FixedPoint16_16($rawdata) { + return self::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (self::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16)); + } + + /** + * @param string $rawdata + * + * @return float + */ + public static function FixedPoint2_30($rawdata) { + $binarystring = self::BigEndian2Bin($rawdata); + return self::Bin2Dec(substr($binarystring, 0, 2)) + (float) (self::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30)); + } + + + /** + * @param string $ArrayPath + * @param string $Separator + * @param mixed $Value + * + * @return array + */ + public static function CreateDeepArray($ArrayPath, $Separator, $Value) { + // assigns $Value to a nested array path: + // $foo = self::CreateDeepArray('/path/to/my', '/', 'file.txt') + // is the same as: + // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt')))); + // or + // $foo['path']['to']['my'] = 'file.txt'; + $ArrayPath = ltrim($ArrayPath, $Separator); + if (($pos = strpos($ArrayPath, $Separator)) !== false) { + $ReturnedArray[substr($ArrayPath, 0, $pos)] = self::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value); + } else { + $ReturnedArray[$ArrayPath] = $Value; + } + return $ReturnedArray; + } + + /** + * @param array $arraydata + * @param bool $returnkey + * + * @return int|false + */ + public static function array_max($arraydata, $returnkey=false) { + $maxvalue = false; + $maxkey = false; + foreach ($arraydata as $key => $value) { + if (!is_array($value)) { + if (($maxvalue === false) || ($value > $maxvalue)) { + $maxvalue = $value; + $maxkey = $key; + } + } + } + return ($returnkey ? $maxkey : $maxvalue); + } + + /** + * @param array $arraydata + * @param bool $returnkey + * + * @return int|false + */ + public static function array_min($arraydata, $returnkey=false) { + $minvalue = false; + $minkey = false; + foreach ($arraydata as $key => $value) { + if (!is_array($value)) { + if (($minvalue === false) || ($value < $minvalue)) { + $minvalue = $value; + $minkey = $key; + } + } + } + return ($returnkey ? $minkey : $minvalue); + } + + /** + * @param string $XMLstring + * + * @return array|false + */ + public static function XML2array($XMLstring) { + if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) { + if (PHP_VERSION_ID < 80000) { + // http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html + // https://core.trac.wordpress.org/changeset/29378 + // This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading is + // disabled by default, so this function is no longer needed to protect against XXE attacks. + $loader = libxml_disable_entity_loader(true); + } + $XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT); + $return = self::SimpleXMLelement2array($XMLobject); + if (PHP_VERSION_ID < 80000 && isset($loader)) { + libxml_disable_entity_loader($loader); + } + return $return; + } + return false; + } + + /** + * @param SimpleXMLElement|array|mixed $XMLobject + * + * @return mixed + */ + public static function SimpleXMLelement2array($XMLobject) { + if (!is_object($XMLobject) && !is_array($XMLobject)) { + return $XMLobject; + } + $XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject; + foreach ($XMLarray as $key => $value) { + $XMLarray[$key] = self::SimpleXMLelement2array($value); + } + return $XMLarray; + } + + /** + * Returns checksum for a file from starting position to absolute end position. + * + * @param string $file + * @param int $offset + * @param int $end + * @param string $algorithm + * + * @return string|false + * @throws getid3_exception + */ + public static function hash_data($file, $offset, $end, $algorithm) { + if (!self::intValueSupported($end)) { + return false; + } + if (!in_array($algorithm, array('md5', 'sha1'))) { + throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()'); + } + + $size = $end - $offset; + + $fp = fopen($file, 'rb'); + fseek($fp, $offset); + $ctx = hash_init($algorithm); + while ($size > 0) { + $buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE)); + hash_update($ctx, $buffer); + $size -= getID3::FREAD_BUFFER_SIZE; + } + $hash = hash_final($ctx); + fclose($fp); + + return $hash; + } + + /** + * @param string $filename_source + * @param string $filename_dest + * @param int $offset + * @param int $length + * + * @return bool + * @throws Exception + * + * @deprecated Unused, may be removed in future versions of getID3 + */ + public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) { + if (!self::intValueSupported($offset + $length)) { + throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit'); + } + if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) { + if (($fp_dest = fopen($filename_dest, 'wb'))) { + if (fseek($fp_src, $offset) == 0) { + $byteslefttowrite = $length; + while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) { + $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite); + $byteslefttowrite -= $byteswritten; + } + fclose($fp_dest); + return true; + } else { + fclose($fp_src); + throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source); + } + } else { + throw new Exception('failed to create file for writing '.$filename_dest); + } + } else { + throw new Exception('failed to open file for reading '.$filename_source); + } + } + + /** + * @param int $charval + * + * @return string + */ + public static function iconv_fallback_int_utf8($charval) { + if ($charval < 128) { + // 0bbbbbbb + $newcharstring = chr($charval); + } elseif ($charval < 2048) { + // 110bbbbb 10bbbbbb + $newcharstring = chr(($charval >> 6) | 0xC0); + $newcharstring .= chr(($charval & 0x3F) | 0x80); + } elseif ($charval < 65536) { + // 1110bbbb 10bbbbbb 10bbbbbb + $newcharstring = chr(($charval >> 12) | 0xE0); + $newcharstring .= chr(($charval >> 6) | 0xC0); + $newcharstring .= chr(($charval & 0x3F) | 0x80); + } else { + // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb + $newcharstring = chr(($charval >> 18) | 0xF0); + $newcharstring .= chr(($charval >> 12) | 0xC0); + $newcharstring .= chr(($charval >> 6) | 0xC0); + $newcharstring .= chr(($charval & 0x3F) | 0x80); + } + return $newcharstring; + } + + /** + * ISO-8859-1 => UTF-8 + * + * @param string $string + * @param bool $bom + * + * @return string + */ + public static function iconv_fallback_iso88591_utf8($string, $bom=false) { + if (function_exists('utf8_encode')) { + return utf8_encode($string); + } + // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) + $newcharstring = ''; + if ($bom) { + $newcharstring .= "\xEF\xBB\xBF"; + } + for ($i = 0; $i < strlen($string); $i++) { + $charval = ord($string[$i]); + $newcharstring .= self::iconv_fallback_int_utf8($charval); + } + return $newcharstring; + } + + /** + * ISO-8859-1 => UTF-16BE + * + * @param string $string + * @param bool $bom + * + * @return string + */ + public static function iconv_fallback_iso88591_utf16be($string, $bom=false) { + $newcharstring = ''; + if ($bom) { + $newcharstring .= "\xFE\xFF"; + } + for ($i = 0; $i < strlen($string); $i++) { + $newcharstring .= "\x00".$string[$i]; + } + return $newcharstring; + } + + /** + * ISO-8859-1 => UTF-16LE + * + * @param string $string + * @param bool $bom + * + * @return string + */ + public static function iconv_fallback_iso88591_utf16le($string, $bom=false) { + $newcharstring = ''; + if ($bom) { + $newcharstring .= "\xFF\xFE"; + } + for ($i = 0; $i < strlen($string); $i++) { + $newcharstring .= $string[$i]."\x00"; + } + return $newcharstring; + } + + /** + * ISO-8859-1 => UTF-16LE (BOM) + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_iso88591_utf16($string) { + return self::iconv_fallback_iso88591_utf16le($string, true); + } + + /** + * UTF-8 => ISO-8859-1 + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf8_iso88591($string) { + if (function_exists('utf8_decode')) { + return utf8_decode($string); + } + // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) + $newcharstring = ''; + $offset = 0; + $stringlength = strlen($string); + while ($offset < $stringlength) { + if ((ord($string[$offset]) | 0x07) == 0xF7) { + // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & + ((ord($string[($offset + 1)]) & 0x3F) << 12) & + ((ord($string[($offset + 2)]) & 0x3F) << 6) & + (ord($string[($offset + 3)]) & 0x3F); + $offset += 4; + } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { + // 1110bbbb 10bbbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & + ((ord($string[($offset + 1)]) & 0x3F) << 6) & + (ord($string[($offset + 2)]) & 0x3F); + $offset += 3; + } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { + // 110bbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & + (ord($string[($offset + 1)]) & 0x3F); + $offset += 2; + } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { + // 0bbbbbbb + $charval = ord($string[$offset]); + $offset += 1; + } else { + // error? throw some kind of warning here? + $charval = false; + $offset += 1; + } + if ($charval !== false) { + $newcharstring .= (($charval < 256) ? chr($charval) : '?'); + } + } + return $newcharstring; + } + + /** + * UTF-8 => UTF-16BE + * + * @param string $string + * @param bool $bom + * + * @return string + */ + public static function iconv_fallback_utf8_utf16be($string, $bom=false) { + $newcharstring = ''; + if ($bom) { + $newcharstring .= "\xFE\xFF"; + } + $offset = 0; + $stringlength = strlen($string); + while ($offset < $stringlength) { + if ((ord($string[$offset]) | 0x07) == 0xF7) { + // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & + ((ord($string[($offset + 1)]) & 0x3F) << 12) & + ((ord($string[($offset + 2)]) & 0x3F) << 6) & + (ord($string[($offset + 3)]) & 0x3F); + $offset += 4; + } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { + // 1110bbbb 10bbbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & + ((ord($string[($offset + 1)]) & 0x3F) << 6) & + (ord($string[($offset + 2)]) & 0x3F); + $offset += 3; + } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { + // 110bbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & + (ord($string[($offset + 1)]) & 0x3F); + $offset += 2; + } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { + // 0bbbbbbb + $charval = ord($string[$offset]); + $offset += 1; + } else { + // error? throw some kind of warning here? + $charval = false; + $offset += 1; + } + if ($charval !== false) { + $newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?'); + } + } + return $newcharstring; + } + + /** + * UTF-8 => UTF-16LE + * + * @param string $string + * @param bool $bom + * + * @return string + */ + public static function iconv_fallback_utf8_utf16le($string, $bom=false) { + $newcharstring = ''; + if ($bom) { + $newcharstring .= "\xFF\xFE"; + } + $offset = 0; + $stringlength = strlen($string); + while ($offset < $stringlength) { + if ((ord($string[$offset]) | 0x07) == 0xF7) { + // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & + ((ord($string[($offset + 1)]) & 0x3F) << 12) & + ((ord($string[($offset + 2)]) & 0x3F) << 6) & + (ord($string[($offset + 3)]) & 0x3F); + $offset += 4; + } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { + // 1110bbbb 10bbbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & + ((ord($string[($offset + 1)]) & 0x3F) << 6) & + (ord($string[($offset + 2)]) & 0x3F); + $offset += 3; + } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { + // 110bbbbb 10bbbbbb + $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & + (ord($string[($offset + 1)]) & 0x3F); + $offset += 2; + } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { + // 0bbbbbbb + $charval = ord($string[$offset]); + $offset += 1; + } else { + // error? maybe throw some warning here? + $charval = false; + $offset += 1; + } + if ($charval !== false) { + $newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00"); + } + } + return $newcharstring; + } + + /** + * UTF-8 => UTF-16LE (BOM) + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf8_utf16($string) { + return self::iconv_fallback_utf8_utf16le($string, true); + } + + /** + * UTF-16BE => UTF-8 + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf16be_utf8($string) { + if (substr($string, 0, 2) == "\xFE\xFF") { + // strip BOM + $string = substr($string, 2); + } + $newcharstring = ''; + for ($i = 0; $i < strlen($string); $i += 2) { + $charval = self::BigEndian2Int(substr($string, $i, 2)); + $newcharstring .= self::iconv_fallback_int_utf8($charval); + } + return $newcharstring; + } + + /** + * UTF-16LE => UTF-8 + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf16le_utf8($string) { + if (substr($string, 0, 2) == "\xFF\xFE") { + // strip BOM + $string = substr($string, 2); + } + $newcharstring = ''; + for ($i = 0; $i < strlen($string); $i += 2) { + $charval = self::LittleEndian2Int(substr($string, $i, 2)); + $newcharstring .= self::iconv_fallback_int_utf8($charval); + } + return $newcharstring; + } + + /** + * UTF-16BE => ISO-8859-1 + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf16be_iso88591($string) { + if (substr($string, 0, 2) == "\xFE\xFF") { + // strip BOM + $string = substr($string, 2); + } + $newcharstring = ''; + for ($i = 0; $i < strlen($string); $i += 2) { + $charval = self::BigEndian2Int(substr($string, $i, 2)); + $newcharstring .= (($charval < 256) ? chr($charval) : '?'); + } + return $newcharstring; + } + + /** + * UTF-16LE => ISO-8859-1 + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf16le_iso88591($string) { + if (substr($string, 0, 2) == "\xFF\xFE") { + // strip BOM + $string = substr($string, 2); + } + $newcharstring = ''; + for ($i = 0; $i < strlen($string); $i += 2) { + $charval = self::LittleEndian2Int(substr($string, $i, 2)); + $newcharstring .= (($charval < 256) ? chr($charval) : '?'); + } + return $newcharstring; + } + + /** + * UTF-16 (BOM) => ISO-8859-1 + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf16_iso88591($string) { + $bom = substr($string, 0, 2); + if ($bom == "\xFE\xFF") { + return self::iconv_fallback_utf16be_iso88591(substr($string, 2)); + } elseif ($bom == "\xFF\xFE") { + return self::iconv_fallback_utf16le_iso88591(substr($string, 2)); + } + return $string; + } + + /** + * UTF-16 (BOM) => UTF-8 + * + * @param string $string + * + * @return string + */ + public static function iconv_fallback_utf16_utf8($string) { + $bom = substr($string, 0, 2); + if ($bom == "\xFE\xFF") { + return self::iconv_fallback_utf16be_utf8(substr($string, 2)); + } elseif ($bom == "\xFF\xFE") { + return self::iconv_fallback_utf16le_utf8(substr($string, 2)); + } + return $string; + } + + /** + * @param string $in_charset + * @param string $out_charset + * @param string $string + * + * @return string + * @throws Exception + */ + public static function iconv_fallback($in_charset, $out_charset, $string) { + + if ($in_charset == $out_charset) { + return $string; + } + + // mb_convert_encoding() available + if (function_exists('mb_convert_encoding')) { + if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) { + // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM + $string = "\xFF\xFE".$string; + } + if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) { + if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) { + // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified + return ''; + } + } + if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) { + switch ($out_charset) { + case 'ISO-8859-1': + $converted_string = rtrim($converted_string, "\x00"); + break; + } + return $converted_string; + } + return $string; + + // iconv() available + } elseif (function_exists('iconv')) { + if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) { + switch ($out_charset) { + case 'ISO-8859-1': + $converted_string = rtrim($converted_string, "\x00"); + break; + } + return $converted_string; + } + + // iconv() may sometimes fail with "illegal character in input string" error message + // and return an empty string, but returning the unconverted string is more useful + return $string; + } + + + // neither mb_convert_encoding or iconv() is available + static $ConversionFunctionList = array(); + if (empty($ConversionFunctionList)) { + $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8'; + $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16'; + $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be'; + $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le'; + $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591'; + $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16'; + $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be'; + $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le'; + $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591'; + $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8'; + $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591'; + $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8'; + $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591'; + $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8'; + } + if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) { + $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)]; + return self::$ConversionFunction($string); + } + throw new Exception('PHP does not has mb_convert_encoding() or iconv() support - cannot convert from '.$in_charset.' to '.$out_charset); + } + + /** + * @param mixed $data + * @param string $charset + * + * @return mixed + */ + public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') { + if (is_string($data)) { + return self::MultiByteCharString2HTML($data, $charset); + } elseif (is_array($data)) { + $return_data = array(); + foreach ($data as $key => $value) { + $return_data[$key] = self::recursiveMultiByteCharString2HTML($value, $charset); + } + return $return_data; + } + // integer, float, objects, resources, etc + return $data; + } + + /** + * @param string|int|float $string + * @param string $charset + * + * @return string + */ + public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') { + $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string + $HTMLstring = ''; + + switch (strtolower($charset)) { + case '1251': + case '1252': + case '866': + case '932': + case '936': + case '950': + case 'big5': + case 'big5-hkscs': + case 'cp1251': + case 'cp1252': + case 'cp866': + case 'euc-jp': + case 'eucjp': + case 'gb2312': + case 'ibm866': + case 'iso-8859-1': + case 'iso-8859-15': + case 'iso8859-1': + case 'iso8859-15': + case 'koi8-r': + case 'koi8-ru': + case 'koi8r': + case 'shift_jis': + case 'sjis': + case 'win-1251': + case 'windows-1251': + case 'windows-1252': + $HTMLstring = htmlentities($string, ENT_COMPAT, $charset); + break; + + case 'utf-8': + $strlen = strlen($string); + for ($i = 0; $i < $strlen; $i++) { + $char_ord_val = ord($string[$i]); + $charval = 0; + if ($char_ord_val < 0x80) { + $charval = $char_ord_val; + } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) { + $charval = (($char_ord_val & 0x07) << 18); + $charval += ((ord($string[++$i]) & 0x3F) << 12); + $charval += ((ord($string[++$i]) & 0x3F) << 6); + $charval += (ord($string[++$i]) & 0x3F); + } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) { + $charval = (($char_ord_val & 0x0F) << 12); + $charval += ((ord($string[++$i]) & 0x3F) << 6); + $charval += (ord($string[++$i]) & 0x3F); + } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) { + $charval = (($char_ord_val & 0x1F) << 6); + $charval += (ord($string[++$i]) & 0x3F); + } + if (($charval >= 32) && ($charval <= 127)) { + $HTMLstring .= htmlentities(chr($charval)); + } else { + $HTMLstring .= '&#'.$charval.';'; + } + } + break; + + case 'utf-16le': + for ($i = 0; $i < strlen($string); $i += 2) { + $charval = self::LittleEndian2Int(substr($string, $i, 2)); + if (($charval >= 32) && ($charval <= 127)) { + $HTMLstring .= chr($charval); + } else { + $HTMLstring .= '&#'.$charval.';'; + } + } + break; + + case 'utf-16be': + for ($i = 0; $i < strlen($string); $i += 2) { + $charval = self::BigEndian2Int(substr($string, $i, 2)); + if (($charval >= 32) && ($charval <= 127)) { + $HTMLstring .= chr($charval); + } else { + $HTMLstring .= '&#'.$charval.';'; + } + } + break; + + default: + $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()'; + break; + } + return $HTMLstring; + } + + /** + * @param int $namecode + * + * @return string + */ + public static function RGADnameLookup($namecode) { + static $RGADname = array(); + if (empty($RGADname)) { + $RGADname[0] = 'not set'; + $RGADname[1] = 'Track Gain Adjustment'; + $RGADname[2] = 'Album Gain Adjustment'; + } + + return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : ''); + } + + /** + * @param int $originatorcode + * + * @return string + */ + public static function RGADoriginatorLookup($originatorcode) { + static $RGADoriginator = array(); + if (empty($RGADoriginator)) { + $RGADoriginator[0] = 'unspecified'; + $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer'; + $RGADoriginator[2] = 'set by user'; + $RGADoriginator[3] = 'determined automatically'; + } + + return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : ''); + } + + /** + * @param int $rawadjustment + * @param int $signbit + * + * @return float + */ + public static function RGADadjustmentLookup($rawadjustment, $signbit) { + $adjustment = (float) $rawadjustment / 10; + if ($signbit == 1) { + $adjustment *= -1; + } + return $adjustment; + } + + /** + * @param int $namecode + * @param int $originatorcode + * @param int $replaygain + * + * @return string + */ + public static function RGADgainString($namecode, $originatorcode, $replaygain) { + if ($replaygain < 0) { + $signbit = '1'; + } else { + $signbit = '0'; + } + $storedreplaygain = intval(round($replaygain * 10)); + $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT); + $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT); + $gainstring .= $signbit; + $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT); + + return $gainstring; + } + + /** + * @param float $amplitude + * + * @return float + */ + public static function RGADamplitude2dB($amplitude) { + return 20 * log10($amplitude); + } + + /** + * @param string $imgData + * @param array $imageinfo + * + * @return array|false + */ + public static function GetDataImageSize($imgData, &$imageinfo=array()) { + if (PHP_VERSION_ID >= 50400) { + $GetDataImageSize = @getimagesizefromstring($imgData, $imageinfo); + if ($GetDataImageSize === false || !isset($GetDataImageSize[0], $GetDataImageSize[1])) { + return false; + } + $GetDataImageSize['height'] = $GetDataImageSize[0]; + $GetDataImageSize['width'] = $GetDataImageSize[1]; + return $GetDataImageSize; + } + static $tempdir = ''; + if (empty($tempdir)) { + if (function_exists('sys_get_temp_dir')) { + $tempdir = sys_get_temp_dir(); // https://github.com/JamesHeinrich/getID3/issues/52 + } + + // yes this is ugly, feel free to suggest a better way + if (include_once(dirname(__FILE__).'/getid3.php')) { + $getid3_temp = new getID3(); + if ($getid3_temp_tempdir = $getid3_temp->tempdir) { + $tempdir = $getid3_temp_tempdir; + } + unset($getid3_temp, $getid3_temp_tempdir); + } + } + $GetDataImageSize = false; + if ($tempfilename = tempnam($tempdir, 'gI3')) { + if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) { + fwrite($tmp, $imgData); + fclose($tmp); + $GetDataImageSize = @getimagesize($tempfilename, $imageinfo); + if (($GetDataImageSize === false) || !isset($GetDataImageSize[0]) || !isset($GetDataImageSize[1])) { + return false; + } + $GetDataImageSize['height'] = $GetDataImageSize[0]; + $GetDataImageSize['width'] = $GetDataImageSize[1]; + } + unlink($tempfilename); + } + return $GetDataImageSize; + } + + /** + * @param string $mime_type + * + * @return string + */ + public static function ImageExtFromMime($mime_type) { + // temporary way, works OK for now, but should be reworked in the future + return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type); + } + + /** + * @param array $ThisFileInfo + * @param bool $option_tags_html default true (just as in the main getID3 class) + * + * @return bool + */ + public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true) { + // Copy all entries from ['tags'] into common ['comments'] + if (!empty($ThisFileInfo['tags'])) { + if (isset($ThisFileInfo['tags']['id3v1'])) { + // bubble ID3v1 to the end, if present to aid in detecting bad ID3v1 encodings + $ID3v1 = $ThisFileInfo['tags']['id3v1']; + unset($ThisFileInfo['tags']['id3v1']); + $ThisFileInfo['tags']['id3v1'] = $ID3v1; + unset($ID3v1); + } + foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) { + foreach ($tagarray as $tagname => $tagdata) { + foreach ($tagdata as $key => $value) { + if (!empty($value)) { + if (empty($ThisFileInfo['comments'][$tagname])) { + + // fall through and append value + + } elseif ($tagtype == 'id3v1') { + + $newvaluelength = strlen(trim($value)); + foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { + $oldvaluelength = strlen(trim($existingvalue)); + if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) { + // new value is identical but shorter-than (or equal-length to) one already in comments - skip + break 2; + } + } + if (function_exists('mb_convert_encoding')) { + if (trim($value) == trim(substr(mb_convert_encoding($existingvalue, $ThisFileInfo['id3v1']['encoding'], $ThisFileInfo['encoding']), 0, 30))) { + // value stored in ID3v1 appears to be probably the multibyte value transliterated (badly) into ISO-8859-1 in ID3v1. + // As an example, Foobar2000 will do this if you tag a file with Chinese or Arabic or Cyrillic or something that doesn't fit into ISO-8859-1 the ID3v1 will consist of mostly "?" characters, one per multibyte unrepresentable character + break 2; + } + } + + } elseif (!is_array($value)) { + + $newvaluelength = strlen(trim($value)); + foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { + $oldvaluelength = strlen(trim($existingvalue)); + if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) { + $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value); + break; + } + } + + } + if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) { + $value = (is_string($value) ? trim($value) : $value); + if (!is_int($key) && !ctype_digit($key)) { + $ThisFileInfo['comments'][$tagname][$key] = $value; + } else { + if (!isset($ThisFileInfo['comments'][$tagname])) { + $ThisFileInfo['comments'][$tagname] = array($value); + } else { + $ThisFileInfo['comments'][$tagname][] = $value; + } + } + } + } + } + } + } + + // attempt to standardize spelling of returned keys + $StandardizeFieldNames = array( + 'tracknumber' => 'track_number', + 'track' => 'track_number', + ); + foreach ($StandardizeFieldNames as $badkey => $goodkey) { + if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) { + $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey]; + unset($ThisFileInfo['comments'][$badkey]); + } + } + + if ($option_tags_html) { + // Copy ['comments'] to ['comments_html'] + if (!empty($ThisFileInfo['comments'])) { + foreach ($ThisFileInfo['comments'] as $field => $values) { + if ($field == 'picture') { + // pictures can take up a lot of space, and we don't need multiple copies of them + // let there be a single copy in [comments][picture], and not elsewhere + continue; + } + foreach ($values as $index => $value) { + if (is_array($value)) { + $ThisFileInfo['comments_html'][$field][$index] = $value; + } else { + $ThisFileInfo['comments_html'][$field][$index] = str_replace('�', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding'])); + } + } + } + } + } + + } + return true; + } + + /** + * @param string $key + * @param int $begin + * @param int $end + * @param string $file + * @param string $name + * + * @return string + */ + public static function EmbeddedLookup($key, $begin, $end, $file, $name) { + + // Cached + static $cache; + if (isset($cache[$file][$name])) { + return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); + } + + // Init + $keylength = strlen($key); + $line_count = $end - $begin - 7; + + // Open php file + $fp = fopen($file, 'r'); + + // Discard $begin lines + for ($i = 0; $i < ($begin + 3); $i++) { + fgets($fp, 1024); + } + + // Loop thru line + while (0 < $line_count--) { + + // Read line + $line = ltrim(fgets($fp, 1024), "\t "); + + // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key + //$keycheck = substr($line, 0, $keylength); + //if ($key == $keycheck) { + // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1); + // break; + //} + + // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key + //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1)); + $explodedLine = explode("\t", $line, 2); + $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : ''); + $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : ''); + $cache[$file][$name][$ThisKey] = trim($ThisValue); + } + + // Close and return + fclose($fp); + return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); + } + + /** + * @param string $filename + * @param string $sourcefile + * @param bool $DieOnFailure + * + * @return bool + * @throws Exception + */ + public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) { + global $GETID3_ERRORARRAY; + + if (file_exists($filename)) { + if (include_once($filename)) { + return true; + } else { + $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors'; + } + } else { + $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing'; + } + if ($DieOnFailure) { + throw new Exception($diemessage); + } else { + $GETID3_ERRORARRAY[] = $diemessage; + } + return false; + } + + /** + * @param string $string + * + * @return string + */ + public static function trimNullByte($string) { + return trim($string, "\x00"); + } + + /** + * @param string $path + * + * @return float|bool + */ + public static function getFileSizeSyscall($path) { + $filesize = false; + + if (GETID3_OS_ISWINDOWS) { + if (class_exists('COM')) { // From PHP 5.3.15 and 5.4.5, COM and DOTNET is no longer built into the php core.you have to add COM support in php.ini: + $filesystem = new COM('Scripting.FileSystemObject'); + $file = $filesystem->GetFile($path); + $filesize = $file->Size(); + unset($filesystem, $file); + } else { + $commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI'; + } + } else { + $commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\''; + } + if (isset($commandline)) { + $output = trim(`$commandline`); + if (ctype_digit($output)) { + $filesize = (float) $output; + } + } + return $filesize; + } + + /** + * @param string $filename + * + * @return string|false + */ + public static function truepath($filename) { + // 2017-11-08: this could use some improvement, patches welcome + if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) { + // PHP's built-in realpath function does not work on UNC Windows shares + $goodpath = array(); + foreach (explode('/', str_replace('\\', '/', $filename)) as $part) { + if ($part == '.') { + continue; + } + if ($part == '..') { + if (count($goodpath)) { + array_pop($goodpath); + } else { + // cannot step above this level, already at top level + return false; + } + } else { + $goodpath[] = $part; + } + } + return implode(DIRECTORY_SEPARATOR, $goodpath); + } + return realpath($filename); + } + + /** + * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268) + * + * @param string $path A path. + * @param string $suffix If the name component ends in suffix this will also be cut off. + * + * @return string + */ + public static function mb_basename($path, $suffix = null) { + $splited = preg_split('#/#', rtrim($path, '/ ')); + return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1); + } + +} diff --git a/projects/tests/tests/performance/ID3/getid3.php b/projects/tests/tests/performance/ID3/getid3.php new file mode 100644 index 00000000..5cb3c036 --- /dev/null +++ b/projects/tests/tests/performance/ID3/getid3.php @@ -0,0 +1,2303 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// // +// Please see readme.txt for more information // +// /// +///////////////////////////////////////////////////////////////// + +// define a constant rather than looking up every time it is needed +if (!defined('GETID3_OS_ISWINDOWS')) { + define('GETID3_OS_ISWINDOWS', (stripos(PHP_OS, 'WIN') === 0)); +} +// Get base path of getID3() - ONCE +if (!defined('GETID3_INCLUDEPATH')) { + define('GETID3_INCLUDEPATH', dirname(__FILE__).DIRECTORY_SEPARATOR); +} +// Workaround Bug #39923 (https://bugs.php.net/bug.php?id=39923) +if (!defined('IMG_JPG') && defined('IMAGETYPE_JPEG')) { + define('IMG_JPG', IMAGETYPE_JPEG); +} +if (!defined('ENT_SUBSTITUTE')) { // PHP5.3 adds ENT_IGNORE, PHP5.4 adds ENT_SUBSTITUTE + define('ENT_SUBSTITUTE', (defined('ENT_IGNORE') ? ENT_IGNORE : 8)); +} + +/* +https://www.getid3.org/phpBB3/viewtopic.php?t=2114 +If you are running into a the problem where filenames with special characters are being handled +incorrectly by external helper programs (e.g. metaflac), notably with the special characters removed, +and you are passing in the filename in UTF8 (typically via a HTML form), try uncommenting this line: +*/ +//setlocale(LC_CTYPE, 'en_US.UTF-8'); + +// attempt to define temp dir as something flexible but reliable +$temp_dir = ini_get('upload_tmp_dir'); +if ($temp_dir && (!is_dir($temp_dir) || !is_readable($temp_dir))) { + $temp_dir = ''; +} +if (!$temp_dir && function_exists('sys_get_temp_dir')) { // sys_get_temp_dir added in PHP v5.2.1 + // sys_get_temp_dir() may give inaccessible temp dir, e.g. with open_basedir on virtual hosts + $temp_dir = sys_get_temp_dir(); +} +$temp_dir = @realpath($temp_dir); // see https://github.com/JamesHeinrich/getID3/pull/10 +$open_basedir = ini_get('open_basedir'); +if ($open_basedir) { + // e.g. "/var/www/vhosts/getid3.org/httpdocs/:/tmp/" + $temp_dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $temp_dir); + $open_basedir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $open_basedir); + if (substr($temp_dir, -1, 1) != DIRECTORY_SEPARATOR) { + $temp_dir .= DIRECTORY_SEPARATOR; + } + $found_valid_tempdir = false; + $open_basedirs = explode(PATH_SEPARATOR, $open_basedir); + foreach ($open_basedirs as $basedir) { + if (substr($basedir, -1, 1) != DIRECTORY_SEPARATOR) { + $basedir .= DIRECTORY_SEPARATOR; + } + if (preg_match('#^'.preg_quote($basedir).'#', $temp_dir)) { + $found_valid_tempdir = true; + break; + } + } + if (!$found_valid_tempdir) { + $temp_dir = ''; + } + unset($open_basedirs, $found_valid_tempdir, $basedir); +} +if (!$temp_dir) { + $temp_dir = '*'; // invalid directory name should force tempnam() to use system default temp dir +} +// $temp_dir = '/something/else/'; // feel free to override temp dir here if it works better for your system +if (!defined('GETID3_TEMP_DIR')) { + define('GETID3_TEMP_DIR', $temp_dir); +} +unset($open_basedir, $temp_dir); + +// End: Defines + + +class getID3 +{ + /* + * Settings + */ + + /** + * CASE SENSITIVE! - i.e. (must be supported by iconv()). Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE + * + * @var string + */ + public $encoding = 'UTF-8'; + + /** + * Should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'EUC-CN' or 'CP1252' + * + * @var string + */ + public $encoding_id3v1 = 'ISO-8859-1'; + + /** + * ID3v1 should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'Windows-1251' or 'KOI8-R'. If true attempt to detect these encodings, but may return incorrect values for some tags actually in ISO-8859-1 encoding + * + * @var bool + */ + public $encoding_id3v1_autodetect = false; + + /* + * Optional tag checks - disable for speed. + */ + + /** + * Read and process ID3v1 tags + * + * @var bool + */ + public $option_tag_id3v1 = true; + + /** + * Read and process ID3v2 tags + * + * @var bool + */ + public $option_tag_id3v2 = true; + + /** + * Read and process Lyrics3 tags + * + * @var bool + */ + public $option_tag_lyrics3 = true; + + /** + * Read and process APE tags + * + * @var bool + */ + public $option_tag_apetag = true; + + /** + * Copy tags to root key 'tags' and encode to $this->encoding + * + * @var bool + */ + public $option_tags_process = true; + + /** + * Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities + * + * @var bool + */ + public $option_tags_html = true; + + /* + * Optional tag/comment calculations + */ + + /** + * Calculate additional info such as bitrate, channelmode etc + * + * @var bool + */ + public $option_extra_info = true; + + /* + * Optional handling of embedded attachments (e.g. images) + */ + + /** + * Defaults to true (ATTACHMENTS_INLINE) for backward compatibility + * + * @var bool|string + */ + public $option_save_attachments = true; + + /* + * Optional calculations + */ + + /** + * Get MD5 sum of data part - slow + * + * @var bool + */ + public $option_md5_data = false; + + /** + * Use MD5 of source file if availble - only FLAC and OptimFROG + * + * @var bool + */ + public $option_md5_data_source = false; + + /** + * Get SHA1 sum of data part - slow + * + * @var bool + */ + public $option_sha1_data = false; + + /** + * Check whether file is larger than 2GB and thus not supported by 32-bit PHP (null: auto-detect based on + * PHP_INT_MAX) + * + * @var bool|null + */ + public $option_max_2gb_check; + + /** + * Read buffer size in bytes + * + * @var int + */ + public $option_fread_buffer_size = 32768; + + // Public variables + + /** + * Filename of file being analysed. + * + * @var string + */ + public $filename; + + /** + * Filepointer to file being analysed. + * + * @var resource + */ + public $fp; + + /** + * Result array. + * + * @var array + */ + public $info; + + /** + * @var string + */ + public $tempdir = GETID3_TEMP_DIR; + + /** + * @var int + */ + public $memory_limit = 0; + + /** + * @var string + */ + protected $startup_error = ''; + + /** + * @var string + */ + protected $startup_warning = ''; + + const VERSION = '1.9.20-202006061653'; + const FREAD_BUFFER_SIZE = 32768; + + const ATTACHMENTS_NONE = false; + const ATTACHMENTS_INLINE = true; + + public function __construct() { + + // Check for PHP version + $required_php_version = '5.3.0'; + if (version_compare(PHP_VERSION, $required_php_version, '<')) { + $this->startup_error .= 'getID3() requires PHP v'.$required_php_version.' or higher - you are running v'.PHP_VERSION."\n"; + return; + } + + // Check memory + $memoryLimit = ini_get('memory_limit'); + if (preg_match('#([0-9]+) ?M#i', $memoryLimit, $matches)) { + // could be stored as "16M" rather than 16777216 for example + $memoryLimit = $matches[1] * 1048576; + } elseif (preg_match('#([0-9]+) ?G#i', $memoryLimit, $matches)) { // The 'G' modifier is available since PHP 5.1.0 + // could be stored as "2G" rather than 2147483648 for example + $memoryLimit = $matches[1] * 1073741824; + } + $this->memory_limit = $memoryLimit; + + if ($this->memory_limit <= 0) { + // memory limits probably disabled + } elseif ($this->memory_limit <= 4194304) { + $this->startup_error .= 'PHP has less than 4MB available memory and will very likely run out. Increase memory_limit in php.ini'."\n"; + } elseif ($this->memory_limit <= 12582912) { + $this->startup_warning .= 'PHP has less than 12MB available memory and might run out if all modules are loaded. Increase memory_limit in php.ini'."\n"; + } + + // Check safe_mode off + if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved + $this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.'); + } + + // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated + if (($mbstring_func_overload = (int) ini_get('mbstring.func_overload')) && ($mbstring_func_overload & 0x02)) { + // http://php.net/manual/en/mbstring.overload.php + // "mbstring.func_overload in php.ini is a positive value that represents a combination of bitmasks specifying the categories of functions to be overloaded. It should be set to 1 to overload the mail() function. 2 for string functions, 4 for regular expression functions" + // getID3 cannot run when string functions are overloaded. It doesn't matter if mail() or ereg* functions are overloaded since getID3 does not use those. + // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated + $this->startup_error .= 'WARNING: php.ini contains "mbstring.func_overload = '.ini_get('mbstring.func_overload').'", getID3 cannot run with this setting (bitmask 2 (string functions) cannot be set). Recommended to disable entirely.'."\n"; + } + + // check for magic quotes in PHP < 7.4.0 (when these functions became deprecated) + if (version_compare(PHP_VERSION, '7.4.0', '<')) { + // Check for magic_quotes_runtime + if (function_exists('get_magic_quotes_runtime')) { + // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.get_magic_quotes_runtimeDeprecated + if (get_magic_quotes_runtime()) { + $this->startup_error .= 'magic_quotes_runtime must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_runtime(0) and set_magic_quotes_runtime(1).'."\n"; + } + } + // Check for magic_quotes_gpc + if (function_exists('get_magic_quotes_gpc')) { + // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.get_magic_quotes_gpcDeprecated + if (get_magic_quotes_gpc()) { + $this->startup_error .= 'magic_quotes_gpc must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_gpc(0) and set_magic_quotes_gpc(1).'."\n"; + } + } + } + + // Load support library + if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.php')) { + $this->startup_error .= 'getid3.lib.php is missing or corrupt'."\n"; + } + + if ($this->option_max_2gb_check === null) { + $this->option_max_2gb_check = (PHP_INT_MAX <= 2147483647); + } + + + // Needed for Windows only: + // Define locations of helper applications for Shorten, VorbisComment, MetaFLAC + // as well as other helper functions such as head, etc + // This path cannot contain spaces, but the below code will attempt to get the + // 8.3-equivalent path automatically + // IMPORTANT: This path must include the trailing slash + if (GETID3_OS_ISWINDOWS && !defined('GETID3_HELPERAPPSDIR')) { + + $helperappsdir = GETID3_INCLUDEPATH.'..'.DIRECTORY_SEPARATOR.'helperapps'; // must not have any space in this path + + if (!is_dir($helperappsdir)) { + $this->startup_warning .= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist'."\n"; + } elseif (strpos(realpath($helperappsdir), ' ') !== false) { + $DirPieces = explode(DIRECTORY_SEPARATOR, realpath($helperappsdir)); + $path_so_far = array(); + foreach ($DirPieces as $key => $value) { + if (strpos($value, ' ') !== false) { + if (!empty($path_so_far)) { + $commandline = 'dir /x '.escapeshellarg(implode(DIRECTORY_SEPARATOR, $path_so_far)); + $dir_listing = `$commandline`; + $lines = explode("\n", $dir_listing); + foreach ($lines as $line) { + $line = trim($line); + if (preg_match('#^([0-9/]{10}) +([0-9:]{4,5}( [AP]M)?) +(|[0-9,]+) +([^ ]{0,11}) +(.+)$#', $line, $matches)) { + list($dummy, $date, $time, $ampm, $filesize, $shortname, $filename) = $matches; + if ((strtoupper($filesize) == '') && (strtolower($filename) == strtolower($value))) { + $value = $shortname; + } + } + } + } else { + $this->startup_warning .= 'GETID3_HELPERAPPSDIR must not have any spaces in it - use 8dot3 naming convention if neccesary. You can run "dir /x" from the commandline to see the correct 8.3-style names.'."\n"; + } + } + $path_so_far[] = $value; + } + $helperappsdir = implode(DIRECTORY_SEPARATOR, $path_so_far); + } + define('GETID3_HELPERAPPSDIR', $helperappsdir.DIRECTORY_SEPARATOR); + } + + if (!empty($this->startup_error)) { + echo $this->startup_error; + throw new getid3_exception($this->startup_error); + } + } + + /** + * @return string + */ + public function version() { + return self::VERSION; + } + + /** + * @return int + */ + public function fread_buffer_size() { + return $this->option_fread_buffer_size; + } + + /** + * @param array $optArray + * + * @return bool + */ + public function setOption($optArray) { + if (!is_array($optArray) || empty($optArray)) { + return false; + } + foreach ($optArray as $opt => $val) { + if (isset($this->$opt) === false) { + continue; + } + $this->$opt = $val; + } + return true; + } + + /** + * @param string $filename + * @param int $filesize + * @param resource $fp + * + * @return bool + * + * @throws getid3_exception + */ + public function openfile($filename, $filesize=null, $fp=null) { + try { + if (!empty($this->startup_error)) { + throw new getid3_exception($this->startup_error); + } + if (!empty($this->startup_warning)) { + foreach (explode("\n", $this->startup_warning) as $startup_warning) { + $this->warning($startup_warning); + } + } + + // init result array and set parameters + $this->filename = $filename; + $this->info = array(); + $this->info['GETID3_VERSION'] = $this->version(); + $this->info['php_memory_limit'] = (($this->memory_limit > 0) ? $this->memory_limit : false); + + // remote files not supported + if (preg_match('#^(ht|f)tp://#', $filename)) { + throw new getid3_exception('Remote files are not supported - please copy the file locally first'); + } + + $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename); + //$filename = preg_replace('#(?fp = fopen($filename, 'rb'))) { // see https://www.getid3.org/phpBB3/viewtopic.php?t=1720 + if (($fp != null) && ((get_resource_type($fp) == 'file') || (get_resource_type($fp) == 'stream'))) { + $this->fp = $fp; + } elseif ((is_readable($filename) || file_exists($filename)) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) { + // great + } else { + $errormessagelist = array(); + if (!is_readable($filename)) { + $errormessagelist[] = '!is_readable'; + } + if (!is_file($filename)) { + $errormessagelist[] = '!is_file'; + } + if (!file_exists($filename)) { + $errormessagelist[] = '!file_exists'; + } + if (empty($errormessagelist)) { + $errormessagelist[] = 'fopen failed'; + } + throw new getid3_exception('Could not open "'.$filename.'" ('.implode('; ', $errormessagelist).')'); + } + + $this->info['filesize'] = (!is_null($filesize) ? $filesize : filesize($filename)); + // set redundant parameters - might be needed in some include file + // filenames / filepaths in getID3 are always expressed with forward slashes (unix-style) for both Windows and other to try and minimize confusion + $filename = str_replace('\\', '/', $filename); + $this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename))); + $this->info['filename'] = getid3_lib::mb_basename($filename); + $this->info['filenamepath'] = $this->info['filepath'].'/'.$this->info['filename']; + + // set more parameters + $this->info['avdataoffset'] = 0; + $this->info['avdataend'] = $this->info['filesize']; + $this->info['fileformat'] = ''; // filled in later + $this->info['audio']['dataformat'] = ''; // filled in later, unset if not used + $this->info['video']['dataformat'] = ''; // filled in later, unset if not used + $this->info['tags'] = array(); // filled in later, unset if not used + $this->info['error'] = array(); // filled in later, unset if not used + $this->info['warning'] = array(); // filled in later, unset if not used + $this->info['comments'] = array(); // filled in later, unset if not used + $this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired + + // option_max_2gb_check + if ($this->option_max_2gb_check) { + // PHP (32-bit all, and 64-bit Windows) doesn't support integers larger than 2^31 (~2GB) + // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize + // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer + $fseek = fseek($this->fp, 0, SEEK_END); + if (($fseek < 0) || (($this->info['filesize'] != 0) && (ftell($this->fp) == 0)) || + ($this->info['filesize'] < 0) || + (ftell($this->fp) < 0)) { + $real_filesize = getid3_lib::getFileSizeSyscall($this->info['filenamepath']); + + if ($real_filesize === false) { + unset($this->info['filesize']); + fclose($this->fp); + throw new getid3_exception('Unable to determine actual filesize. File is most likely larger than '.round(PHP_INT_MAX / 1073741824).'GB and is not supported by PHP.'); + } elseif (getid3_lib::intValueSupported($real_filesize)) { + unset($this->info['filesize']); + fclose($this->fp); + throw new getid3_exception('PHP seems to think the file is larger than '.round(PHP_INT_MAX / 1073741824).'GB, but filesystem reports it as '.number_format($real_filesize / 1073741824, 3).'GB, please report to info@getid3.org'); + } + $this->info['filesize'] = $real_filesize; + $this->warning('File is larger than '.round(PHP_INT_MAX / 1073741824).'GB (filesystem reports it as '.number_format($real_filesize / 1073741824, 3).'GB) and is not properly supported by PHP.'); + } + } + + return true; + + } catch (Exception $e) { + $this->error($e->getMessage()); + } + return false; + } + + /** + * analyze file + * + * @param string $filename + * @param int $filesize + * @param string $original_filename + * @param resource $fp + * + * @return array + */ + public function analyze($filename, $filesize=null, $original_filename='', $fp=null) { + try { + if (!$this->openfile($filename, $filesize, $fp)) { + return $this->info; + } + + // Handle tags + foreach (array('id3v2'=>'id3v2', 'id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) { + $option_tag = 'option_tag_'.$tag_name; + if ($this->$option_tag) { + $this->include_module('tag.'.$tag_name); + try { + $tag_class = 'getid3_'.$tag_name; + $tag = new $tag_class($this); + $tag->Analyze(); + } + catch (getid3_exception $e) { + throw $e; + } + } + } + if (isset($this->info['id3v2']['tag_offset_start'])) { + $this->info['avdataoffset'] = max($this->info['avdataoffset'], $this->info['id3v2']['tag_offset_end']); + } + foreach (array('id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) { + if (isset($this->info[$tag_key]['tag_offset_start'])) { + $this->info['avdataend'] = min($this->info['avdataend'], $this->info[$tag_key]['tag_offset_start']); + } + } + + // ID3v2 detection (NOT parsing), even if ($this->option_tag_id3v2 == false) done to make fileformat easier + if (!$this->option_tag_id3v2) { + fseek($this->fp, 0); + $header = fread($this->fp, 10); + if ((substr($header, 0, 3) == 'ID3') && (strlen($header) == 10)) { + $this->info['id3v2']['header'] = true; + $this->info['id3v2']['majorversion'] = ord($header[3]); + $this->info['id3v2']['minorversion'] = ord($header[4]); + $this->info['avdataoffset'] += getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length + } + } + + // read 32 kb file data + fseek($this->fp, $this->info['avdataoffset']); + $formattest = fread($this->fp, 32774); + + // determine format + $determined_format = $this->GetFileFormat($formattest, ($original_filename ? $original_filename : $filename)); + + // unable to determine file format + if (!$determined_format) { + fclose($this->fp); + return $this->error('unable to determine file format'); + } + + // check for illegal ID3 tags + if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags']))) { + if ($determined_format['fail_id3'] === 'ERROR') { + fclose($this->fp); + return $this->error('ID3 tags not allowed on this file type.'); + } elseif ($determined_format['fail_id3'] === 'WARNING') { + $this->warning('ID3 tags not allowed on this file type.'); + } + } + + // check for illegal APE tags + if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags'])) { + if ($determined_format['fail_ape'] === 'ERROR') { + fclose($this->fp); + return $this->error('APE tags not allowed on this file type.'); + } elseif ($determined_format['fail_ape'] === 'WARNING') { + $this->warning('APE tags not allowed on this file type.'); + } + } + + // set mime type + $this->info['mime_type'] = $determined_format['mime_type']; + + // supported format signature pattern detected, but module deleted + if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) { + fclose($this->fp); + return $this->error('Format not supported, module "'.$determined_format['include'].'" was removed.'); + } + + // module requires mb_convert_encoding/iconv support + // Check encoding/iconv support + if (!empty($determined_format['iconv_req']) && !function_exists('mb_convert_encoding') && !function_exists('iconv') && !in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) { + $errormessage = 'mb_convert_encoding() or iconv() support is required for this module ('.$determined_format['include'].') for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. '; + if (GETID3_OS_ISWINDOWS) { + $errormessage .= 'PHP does not have mb_convert_encoding() or iconv() support. Please enable php_mbstring.dll / php_iconv.dll in php.ini, and copy php_mbstring.dll / iconv.dll from c:/php/dlls to c:/windows/system32'; + } else { + $errormessage .= 'PHP is not compiled with mb_convert_encoding() or iconv() support. Please recompile with the --enable-mbstring / --with-iconv switch'; + } + return $this->error($errormessage); + } + + // include module + include_once(GETID3_INCLUDEPATH.$determined_format['include']); + + // instantiate module class + $class_name = 'getid3_'.$determined_format['module']; + if (!class_exists($class_name)) { + return $this->error('Format not supported, module "'.$determined_format['include'].'" is corrupt.'); + } + $class = new $class_name($this); + $class->Analyze(); + unset($class); + + // close file + fclose($this->fp); + + // process all tags - copy to 'tags' and convert charsets + if ($this->option_tags_process) { + $this->HandleAllTags(); + } + + // perform more calculations + if ($this->option_extra_info) { + $this->ChannelsBitratePlaytimeCalculations(); + $this->CalculateCompressionRatioVideo(); + $this->CalculateCompressionRatioAudio(); + $this->CalculateReplayGain(); + $this->ProcessAudioStreams(); + } + + // get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags + if ($this->option_md5_data) { + // do not calc md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too + if (!$this->option_md5_data_source || empty($this->info['md5_data_source'])) { + $this->getHashdata('md5'); + } + } + + // get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags + if ($this->option_sha1_data) { + $this->getHashdata('sha1'); + } + + // remove undesired keys + $this->CleanUp(); + + } catch (Exception $e) { + $this->error('Caught exception: '.$e->getMessage()); + } + + // return info array + return $this->info; + } + + + /** + * Error handling. + * + * @param string $message + * + * @return array + */ + public function error($message) { + $this->CleanUp(); + if (!isset($this->info['error'])) { + $this->info['error'] = array(); + } + $this->info['error'][] = $message; + return $this->info; + } + + + /** + * Warning handling. + * + * @param string $message + * + * @return bool + */ + public function warning($message) { + $this->info['warning'][] = $message; + return true; + } + + + /** + * @return bool + */ + private function CleanUp() { + + // remove possible empty keys + $AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate'); + foreach ($AVpossibleEmptyKeys as $dummy => $key) { + if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key])) { + unset($this->info['audio'][$key]); + } + if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) { + unset($this->info['video'][$key]); + } + } + + // remove empty root keys + if (!empty($this->info)) { + foreach ($this->info as $key => $value) { + if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0')) { + unset($this->info[$key]); + } + } + } + + // remove meaningless entries from unknown-format files + if (empty($this->info['fileformat'])) { + if (isset($this->info['avdataoffset'])) { + unset($this->info['avdataoffset']); + } + if (isset($this->info['avdataend'])) { + unset($this->info['avdataend']); + } + } + + // remove possible duplicated identical entries + if (!empty($this->info['error'])) { + $this->info['error'] = array_values(array_unique($this->info['error'])); + } + if (!empty($this->info['warning'])) { + $this->info['warning'] = array_values(array_unique($this->info['warning'])); + } + + // remove "global variable" type keys + unset($this->info['php_memory_limit']); + + return true; + } + + /** + * Return array containing information about all supported formats. + * + * @return array + */ + public function GetFileFormatArray() { + static $format_info = array(); + if (empty($format_info)) { + $format_info = array( + + // Audio formats + + // AC-3 - audio - Dolby AC-3 / Dolby Digital + 'ac3' => array( + 'pattern' => '^\\x0B\\x77', + 'group' => 'audio', + 'module' => 'ac3', + 'mime_type' => 'audio/ac3', + ), + + // AAC - audio - Advanced Audio Coding (AAC) - ADIF format + 'adif' => array( + 'pattern' => '^ADIF', + 'group' => 'audio', + 'module' => 'aac', + 'mime_type' => 'audio/aac', + 'fail_ape' => 'WARNING', + ), + +/* + // AA - audio - Audible Audiobook + 'aa' => array( + 'pattern' => '^.{4}\\x57\\x90\\x75\\x36', + 'group' => 'audio', + 'module' => 'aa', + 'mime_type' => 'audio/audible', + ), +*/ + // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3) + 'adts' => array( + 'pattern' => '^\\xFF[\\xF0-\\xF1\\xF8-\\xF9]', + 'group' => 'audio', + 'module' => 'aac', + 'mime_type' => 'audio/aac', + 'fail_ape' => 'WARNING', + ), + + + // AU - audio - NeXT/Sun AUdio (AU) + 'au' => array( + 'pattern' => '^\\.snd', + 'group' => 'audio', + 'module' => 'au', + 'mime_type' => 'audio/basic', + ), + + // AMR - audio - Adaptive Multi Rate + 'amr' => array( + 'pattern' => '^\\x23\\x21AMR\\x0A', // #!AMR[0A] + 'group' => 'audio', + 'module' => 'amr', + 'mime_type' => 'audio/amr', + ), + + // AVR - audio - Audio Visual Research + 'avr' => array( + 'pattern' => '^2BIT', + 'group' => 'audio', + 'module' => 'avr', + 'mime_type' => 'application/octet-stream', + ), + + // BONK - audio - Bonk v0.9+ + 'bonk' => array( + 'pattern' => '^\\x00(BONK|INFO|META| ID3)', + 'group' => 'audio', + 'module' => 'bonk', + 'mime_type' => 'audio/xmms-bonk', + ), + + // DSF - audio - Direct Stream Digital (DSD) Storage Facility files (DSF) - https://en.wikipedia.org/wiki/Direct_Stream_Digital + 'dsf' => array( + 'pattern' => '^DSD ', // including trailing space: 44 53 44 20 + 'group' => 'audio', + 'module' => 'dsf', + 'mime_type' => 'audio/dsd', + ), + + // DSS - audio - Digital Speech Standard + 'dss' => array( + 'pattern' => '^[\\x02-\\x08]ds[s2]', + 'group' => 'audio', + 'module' => 'dss', + 'mime_type' => 'application/octet-stream', + ), + + // DSDIFF - audio - Direct Stream Digital Interchange File Format + 'dsdiff' => array( + 'pattern' => '^FRM8', + 'group' => 'audio', + 'module' => 'dsdiff', + 'mime_type' => 'audio/dsd', + ), + + // DTS - audio - Dolby Theatre System + 'dts' => array( + 'pattern' => '^\\x7F\\xFE\\x80\\x01', + 'group' => 'audio', + 'module' => 'dts', + 'mime_type' => 'audio/dts', + ), + + // FLAC - audio - Free Lossless Audio Codec + 'flac' => array( + 'pattern' => '^fLaC', + 'group' => 'audio', + 'module' => 'flac', + 'mime_type' => 'audio/flac', + ), + + // LA - audio - Lossless Audio (LA) + 'la' => array( + 'pattern' => '^LA0[2-4]', + 'group' => 'audio', + 'module' => 'la', + 'mime_type' => 'application/octet-stream', + ), + + // LPAC - audio - Lossless Predictive Audio Compression (LPAC) + 'lpac' => array( + 'pattern' => '^LPAC', + 'group' => 'audio', + 'module' => 'lpac', + 'mime_type' => 'application/octet-stream', + ), + + // MIDI - audio - MIDI (Musical Instrument Digital Interface) + 'midi' => array( + 'pattern' => '^MThd', + 'group' => 'audio', + 'module' => 'midi', + 'mime_type' => 'audio/midi', + ), + + // MAC - audio - Monkey's Audio Compressor + 'mac' => array( + 'pattern' => '^MAC ', + 'group' => 'audio', + 'module' => 'monkey', + 'mime_type' => 'audio/x-monkeys-audio', + ), + +// has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available +// // MOD - audio - MODule (assorted sub-formats) +// 'mod' => array( +// 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)', +// 'group' => 'audio', +// 'module' => 'mod', +// 'option' => 'mod', +// 'mime_type' => 'audio/mod', +// ), + + // MOD - audio - MODule (Impulse Tracker) + 'it' => array( + 'pattern' => '^IMPM', + 'group' => 'audio', + 'module' => 'mod', + //'option' => 'it', + 'mime_type' => 'audio/it', + ), + + // MOD - audio - MODule (eXtended Module, various sub-formats) + 'xm' => array( + 'pattern' => '^Extended Module', + 'group' => 'audio', + 'module' => 'mod', + //'option' => 'xm', + 'mime_type' => 'audio/xm', + ), + + // MOD - audio - MODule (ScreamTracker) + 's3m' => array( + 'pattern' => '^.{44}SCRM', + 'group' => 'audio', + 'module' => 'mod', + //'option' => 's3m', + 'mime_type' => 'audio/s3m', + ), + + // MPC - audio - Musepack / MPEGplus + 'mpc' => array( + 'pattern' => '^(MPCK|MP\\+|[\\x00\\x01\\x10\\x11\\x40\\x41\\x50\\x51\\x80\\x81\\x90\\x91\\xC0\\xC1\\xD0\\xD1][\\x20-\\x37][\\x00\\x20\\x40\\x60\\x80\\xA0\\xC0\\xE0])', + 'group' => 'audio', + 'module' => 'mpc', + 'mime_type' => 'audio/x-musepack', + ), + + // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS) + 'mp3' => array( + 'pattern' => '^\\xFF[\\xE2-\\xE7\\xF2-\\xF7\\xFA-\\xFF][\\x00-\\x0B\\x10-\\x1B\\x20-\\x2B\\x30-\\x3B\\x40-\\x4B\\x50-\\x5B\\x60-\\x6B\\x70-\\x7B\\x80-\\x8B\\x90-\\x9B\\xA0-\\xAB\\xB0-\\xBB\\xC0-\\xCB\\xD0-\\xDB\\xE0-\\xEB\\xF0-\\xFB]', + 'group' => 'audio', + 'module' => 'mp3', + 'mime_type' => 'audio/mpeg', + ), + + // OFR - audio - OptimFROG + 'ofr' => array( + 'pattern' => '^(\\*RIFF|OFR)', + 'group' => 'audio', + 'module' => 'optimfrog', + 'mime_type' => 'application/octet-stream', + ), + + // RKAU - audio - RKive AUdio compressor + 'rkau' => array( + 'pattern' => '^RKA', + 'group' => 'audio', + 'module' => 'rkau', + 'mime_type' => 'application/octet-stream', + ), + + // SHN - audio - Shorten + 'shn' => array( + 'pattern' => '^ajkg', + 'group' => 'audio', + 'module' => 'shorten', + 'mime_type' => 'audio/xmms-shn', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // TAK - audio - Tom's lossless Audio Kompressor + 'tak' => array( + 'pattern' => '^tBaK', + 'group' => 'audio', + 'module' => 'tak', + 'mime_type' => 'application/octet-stream', + ), + + // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org) + 'tta' => array( + 'pattern' => '^TTA', // could also be '^TTA(\\x01|\\x02|\\x03|2|1)' + 'group' => 'audio', + 'module' => 'tta', + 'mime_type' => 'application/octet-stream', + ), + + // VOC - audio - Creative Voice (VOC) + 'voc' => array( + 'pattern' => '^Creative Voice File', + 'group' => 'audio', + 'module' => 'voc', + 'mime_type' => 'audio/voc', + ), + + // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF) + 'vqf' => array( + 'pattern' => '^TWIN', + 'group' => 'audio', + 'module' => 'vqf', + 'mime_type' => 'application/octet-stream', + ), + + // WV - audio - WavPack (v4.0+) + 'wv' => array( + 'pattern' => '^wvpk', + 'group' => 'audio', + 'module' => 'wavpack', + 'mime_type' => 'application/octet-stream', + ), + + + // Audio-Video formats + + // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio + 'asf' => array( + 'pattern' => '^\\x30\\x26\\xB2\\x75\\x8E\\x66\\xCF\\x11\\xA6\\xD9\\x00\\xAA\\x00\\x62\\xCE\\x6C', + 'group' => 'audio-video', + 'module' => 'asf', + 'mime_type' => 'video/x-ms-asf', + 'iconv_req' => false, + ), + + // BINK - audio/video - Bink / Smacker + 'bink' => array( + 'pattern' => '^(BIK|SMK)', + 'group' => 'audio-video', + 'module' => 'bink', + 'mime_type' => 'application/octet-stream', + ), + + // FLV - audio/video - FLash Video + 'flv' => array( + 'pattern' => '^FLV[\\x01]', + 'group' => 'audio-video', + 'module' => 'flv', + 'mime_type' => 'video/x-flv', + ), + + // IVF - audio/video - IVF + 'ivf' => array( + 'pattern' => '^DKIF', + 'group' => 'audio-video', + 'module' => 'ivf', + 'mime_type' => 'video/x-ivf', + ), + + // MKAV - audio/video - Mastroka + 'matroska' => array( + 'pattern' => '^\\x1A\\x45\\xDF\\xA3', + 'group' => 'audio-video', + 'module' => 'matroska', + 'mime_type' => 'video/x-matroska', // may also be audio/x-matroska + ), + + // MPEG - audio/video - MPEG (Moving Pictures Experts Group) + 'mpeg' => array( + 'pattern' => '^\\x00\\x00\\x01[\\xB3\\xBA]', + 'group' => 'audio-video', + 'module' => 'mpeg', + 'mime_type' => 'video/mpeg', + ), + + // NSV - audio/video - Nullsoft Streaming Video (NSV) + 'nsv' => array( + 'pattern' => '^NSV[sf]', + 'group' => 'audio-video', + 'module' => 'nsv', + 'mime_type' => 'application/octet-stream', + ), + + // Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*)) + 'ogg' => array( + 'pattern' => '^OggS', + 'group' => 'audio', + 'module' => 'ogg', + 'mime_type' => 'application/ogg', + 'fail_id3' => 'WARNING', + 'fail_ape' => 'WARNING', + ), + + // QT - audio/video - Quicktime + 'quicktime' => array( + 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)', + 'group' => 'audio-video', + 'module' => 'quicktime', + 'mime_type' => 'video/quicktime', + ), + + // RIFF - audio/video - Resource Interchange File Format (RIFF) / WAV / AVI / CD-audio / SDSS = renamed variant used by SmartSound QuickTracks (www.smartsound.com) / FORM = Audio Interchange File Format (AIFF) + 'riff' => array( + 'pattern' => '^(RIFF|SDSS|FORM)', + 'group' => 'audio-video', + 'module' => 'riff', + 'mime_type' => 'audio/wav', + 'fail_ape' => 'WARNING', + ), + + // Real - audio/video - RealAudio, RealVideo + 'real' => array( + 'pattern' => '^\\.(RMF|ra)', + 'group' => 'audio-video', + 'module' => 'real', + 'mime_type' => 'audio/x-realaudio', + ), + + // SWF - audio/video - ShockWave Flash + 'swf' => array( + 'pattern' => '^(F|C)WS', + 'group' => 'audio-video', + 'module' => 'swf', + 'mime_type' => 'application/x-shockwave-flash', + ), + + // TS - audio/video - MPEG-2 Transport Stream + 'ts' => array( + 'pattern' => '^(\\x47.{187}){10,}', // packets are 188 bytes long and start with 0x47 "G". Check for at least 10 packets matching this pattern + 'group' => 'audio-video', + 'module' => 'ts', + 'mime_type' => 'video/MP2T', + ), + + // WTV - audio/video - Windows Recorded TV Show + 'wtv' => array( + 'pattern' => '^\\xB7\\xD8\\x00\\x20\\x37\\x49\\xDA\\x11\\xA6\\x4E\\x00\\x07\\xE9\\x5E\\xAD\\x8D', + 'group' => 'audio-video', + 'module' => 'wtv', + 'mime_type' => 'video/x-ms-wtv', + ), + + + // Still-Image formats + + // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4) + 'bmp' => array( + 'pattern' => '^BM', + 'group' => 'graphic', + 'module' => 'bmp', + 'mime_type' => 'image/bmp', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // GIF - still image - Graphics Interchange Format + 'gif' => array( + 'pattern' => '^GIF', + 'group' => 'graphic', + 'module' => 'gif', + 'mime_type' => 'image/gif', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // JPEG - still image - Joint Photographic Experts Group (JPEG) + 'jpg' => array( + 'pattern' => '^\\xFF\\xD8\\xFF', + 'group' => 'graphic', + 'module' => 'jpg', + 'mime_type' => 'image/jpeg', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // PCD - still image - Kodak Photo CD + 'pcd' => array( + 'pattern' => '^.{2048}PCD_IPI\\x00', + 'group' => 'graphic', + 'module' => 'pcd', + 'mime_type' => 'image/x-photo-cd', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + + // PNG - still image - Portable Network Graphics (PNG) + 'png' => array( + 'pattern' => '^\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A', + 'group' => 'graphic', + 'module' => 'png', + 'mime_type' => 'image/png', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + + // SVG - still image - Scalable Vector Graphics (SVG) + 'svg' => array( + 'pattern' => '( 'graphic', + 'module' => 'svg', + 'mime_type' => 'image/svg+xml', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + + // TIFF - still image - Tagged Information File Format (TIFF) + 'tiff' => array( + 'pattern' => '^(II\\x2A\\x00|MM\\x00\\x2A)', + 'group' => 'graphic', + 'module' => 'tiff', + 'mime_type' => 'image/tiff', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + + // EFAX - still image - eFax (TIFF derivative) + 'efax' => array( + 'pattern' => '^\\xDC\\xFE', + 'group' => 'graphic', + 'module' => 'efax', + 'mime_type' => 'image/efax', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + + // Data formats + + // ISO - data - International Standards Organization (ISO) CD-ROM Image + 'iso' => array( + 'pattern' => '^.{32769}CD001', + 'group' => 'misc', + 'module' => 'iso', + 'mime_type' => 'application/octet-stream', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + 'iconv_req' => false, + ), + + // HPK - data - HPK compressed data + 'hpk' => array( + 'pattern' => '^BPUL', + 'group' => 'archive', + 'module' => 'hpk', + 'mime_type' => 'application/octet-stream', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // RAR - data - RAR compressed data + 'rar' => array( + 'pattern' => '^Rar\\!', + 'group' => 'archive', + 'module' => 'rar', + 'mime_type' => 'application/vnd.rar', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // SZIP - audio/data - SZIP compressed data + 'szip' => array( + 'pattern' => '^SZ\\x0A\\x04', + 'group' => 'archive', + 'module' => 'szip', + 'mime_type' => 'application/octet-stream', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // TAR - data - TAR compressed data + 'tar' => array( + 'pattern' => '^.{100}[0-9\\x20]{7}\\x00[0-9\\x20]{7}\\x00[0-9\\x20]{7}\\x00[0-9\\x20\\x00]{12}[0-9\\x20\\x00]{12}', + 'group' => 'archive', + 'module' => 'tar', + 'mime_type' => 'application/x-tar', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // GZIP - data - GZIP compressed data + 'gz' => array( + 'pattern' => '^\\x1F\\x8B\\x08', + 'group' => 'archive', + 'module' => 'gzip', + 'mime_type' => 'application/gzip', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // ZIP - data - ZIP compressed data + 'zip' => array( + 'pattern' => '^PK\\x03\\x04', + 'group' => 'archive', + 'module' => 'zip', + 'mime_type' => 'application/zip', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // XZ - data - XZ compressed data + 'xz' => array( + 'pattern' => '^\\xFD7zXZ\\x00', + 'group' => 'archive', + 'module' => 'xz', + 'mime_type' => 'application/x-xz', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + + // Misc other formats + + // PAR2 - data - Parity Volume Set Specification 2.0 + 'par2' => array ( + 'pattern' => '^PAR2\\x00PKT', + 'group' => 'misc', + 'module' => 'par2', + 'mime_type' => 'application/octet-stream', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // PDF - data - Portable Document Format + 'pdf' => array( + 'pattern' => '^\\x25PDF', + 'group' => 'misc', + 'module' => 'pdf', + 'mime_type' => 'application/pdf', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // MSOFFICE - data - ZIP compressed data + 'msoffice' => array( + 'pattern' => '^\\xD0\\xCF\\x11\\xE0\\xA1\\xB1\\x1A\\xE1', // D0CF11E == DOCFILE == Microsoft Office Document + 'group' => 'misc', + 'module' => 'msoffice', + 'mime_type' => 'application/octet-stream', + 'fail_id3' => 'ERROR', + 'fail_ape' => 'ERROR', + ), + + // CUE - data - CUEsheet (index to single-file disc images) + 'cue' => array( + 'pattern' => '', // empty pattern means cannot be automatically detected, will fall through all other formats and match based on filename and very basic file contents + 'group' => 'misc', + 'module' => 'cue', + 'mime_type' => 'application/octet-stream', + ), + + ); + } + + return $format_info; + } + + /** + * @param string $filedata + * @param string $filename + * + * @return mixed|false + */ + public function GetFileFormat(&$filedata, $filename='') { + // this function will determine the format of a file based on usually + // the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG, + // and in the case of ISO CD image, 6 bytes offset 32kb from the start + // of the file). + + // Identify file format - loop through $format_info and detect with reg expr + foreach ($this->GetFileFormatArray() as $format_name => $info) { + // The /s switch on preg_match() forces preg_match() NOT to treat + // newline (0x0A) characters as special chars but do a binary match + if (!empty($info['pattern']) && preg_match('#'.$info['pattern'].'#s', $filedata)) { + $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php'; + return $info; + } + } + + + if (preg_match('#\\.mp[123a]$#i', $filename)) { + // Too many mp3 encoders on the market put garbage in front of mpeg files + // use assume format on these if format detection failed + $GetFileFormatArray = $this->GetFileFormatArray(); + $info = $GetFileFormatArray['mp3']; + $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php'; + return $info; + } elseif (preg_match('#\\.cue$#i', $filename) && preg_match('#FILE "[^"]+" (BINARY|MOTOROLA|AIFF|WAVE|MP3)#', $filedata)) { + // there's not really a useful consistent "magic" at the beginning of .cue files to identify them + // so until I think of something better, just go by filename if all other format checks fail + // and verify there's at least one instance of "TRACK xx AUDIO" in the file + $GetFileFormatArray = $this->GetFileFormatArray(); + $info = $GetFileFormatArray['cue']; + $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php'; + return $info; + } + + return false; + } + + /** + * Converts array to $encoding charset from $this->encoding. + * + * @param array $array + * @param string $encoding + */ + public function CharConvert(&$array, $encoding) { + + // identical encoding - end here + if ($encoding == $this->encoding) { + return; + } + + // loop thru array + foreach ($array as $key => $value) { + + // go recursive + if (is_array($value)) { + $this->CharConvert($array[$key], $encoding); + } + + // convert string + elseif (is_string($value)) { + $array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value)); + } + } + } + + /** + * @return bool + */ + public function HandleAllTags() { + + // key name => array (tag name, character encoding) + static $tags; + if (empty($tags)) { + $tags = array( + 'asf' => array('asf' , 'UTF-16LE'), + 'midi' => array('midi' , 'ISO-8859-1'), + 'nsv' => array('nsv' , 'ISO-8859-1'), + 'ogg' => array('vorbiscomment' , 'UTF-8'), + 'png' => array('png' , 'UTF-8'), + 'tiff' => array('tiff' , 'ISO-8859-1'), + 'quicktime' => array('quicktime' , 'UTF-8'), + 'real' => array('real' , 'ISO-8859-1'), + 'vqf' => array('vqf' , 'ISO-8859-1'), + 'zip' => array('zip' , 'ISO-8859-1'), + 'riff' => array('riff' , 'ISO-8859-1'), + 'lyrics3' => array('lyrics3' , 'ISO-8859-1'), + 'id3v1' => array('id3v1' , $this->encoding_id3v1), + 'id3v2' => array('id3v2' , 'UTF-8'), // not according to the specs (every frame can have a different encoding), but getID3() force-converts all encodings to UTF-8 + 'ape' => array('ape' , 'UTF-8'), + 'cue' => array('cue' , 'ISO-8859-1'), + 'matroska' => array('matroska' , 'UTF-8'), + 'flac' => array('vorbiscomment' , 'UTF-8'), + 'divxtag' => array('divx' , 'ISO-8859-1'), + 'iptc' => array('iptc' , 'ISO-8859-1'), + 'dsdiff' => array('dsdiff' , 'ISO-8859-1'), + ); + } + + // loop through comments array + foreach ($tags as $comment_name => $tagname_encoding_array) { + list($tag_name, $encoding) = $tagname_encoding_array; + + // fill in default encoding type if not already present + if (isset($this->info[$comment_name]) && !isset($this->info[$comment_name]['encoding'])) { + $this->info[$comment_name]['encoding'] = $encoding; + } + + // copy comments if key name set + if (!empty($this->info[$comment_name]['comments'])) { + foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray) { + foreach ($valuearray as $key => $value) { + if (is_string($value)) { + $value = trim($value, " \r\n\t"); // do not trim nulls from $value!! Unicode characters will get mangled if trailing nulls are removed! + } + if ($value) { + if (!is_numeric($key)) { + $this->info['tags'][trim($tag_name)][trim($tag_key)][$key] = $value; + } else { + $this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value; + } + } + } + if ($tag_key == 'picture') { + // pictures can take up a lot of space, and we don't need multiple copies of them; let there be a single copy in [comments][picture], and not elsewhere + unset($this->info[$comment_name]['comments'][$tag_key]); + } + } + + if (!isset($this->info['tags'][$tag_name])) { + // comments are set but contain nothing but empty strings, so skip + continue; + } + + $this->CharConvert($this->info['tags'][$tag_name], $this->info[$comment_name]['encoding']); // only copy gets converted! + + if ($this->option_tags_html) { + foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) { + if ($tag_key == 'picture') { + // Do not to try to convert binary picture data to HTML + // https://github.com/JamesHeinrich/getID3/issues/178 + continue; + } + $this->info['tags_html'][$tag_name][$tag_key] = getid3_lib::recursiveMultiByteCharString2HTML($valuearray, $this->info[$comment_name]['encoding']); + } + } + + } + + } + + // pictures can take up a lot of space, and we don't need multiple copies of them; let there be a single copy in [comments][picture], and not elsewhere + if (!empty($this->info['tags'])) { + $unset_keys = array('tags', 'tags_html'); + foreach ($this->info['tags'] as $tagtype => $tagarray) { + foreach ($tagarray as $tagname => $tagdata) { + if ($tagname == 'picture') { + foreach ($tagdata as $key => $tagarray) { + $this->info['comments']['picture'][] = $tagarray; + if (isset($tagarray['data']) && isset($tagarray['image_mime'])) { + if (isset($this->info['tags'][$tagtype][$tagname][$key])) { + unset($this->info['tags'][$tagtype][$tagname][$key]); + } + if (isset($this->info['tags_html'][$tagtype][$tagname][$key])) { + unset($this->info['tags_html'][$tagtype][$tagname][$key]); + } + } + } + } + } + foreach ($unset_keys as $unset_key) { + // remove possible empty keys from (e.g. [tags][id3v2][picture]) + if (empty($this->info[$unset_key][$tagtype]['picture'])) { + unset($this->info[$unset_key][$tagtype]['picture']); + } + if (empty($this->info[$unset_key][$tagtype])) { + unset($this->info[$unset_key][$tagtype]); + } + if (empty($this->info[$unset_key])) { + unset($this->info[$unset_key]); + } + } + // remove duplicate copy of picture data from (e.g. [id3v2][comments][picture]) + if (isset($this->info[$tagtype]['comments']['picture'])) { + unset($this->info[$tagtype]['comments']['picture']); + } + if (empty($this->info[$tagtype]['comments'])) { + unset($this->info[$tagtype]['comments']); + } + if (empty($this->info[$tagtype])) { + unset($this->info[$tagtype]); + } + } + } + return true; + } + + /** + * Calls getid3_lib::CopyTagsToComments() but passes in the option_tags_html setting from this instance of getID3 + * + * @param array $ThisFileInfo + * + * @return bool + */ + public function CopyTagsToComments(&$ThisFileInfo) { + return getid3_lib::CopyTagsToComments($ThisFileInfo, $this->option_tags_html); + } + + /** + * @param string $algorithm + * + * @return array|bool + */ + public function getHashdata($algorithm) { + switch ($algorithm) { + case 'md5': + case 'sha1': + break; + + default: + return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()'); + } + + if (!empty($this->info['fileformat']) && !empty($this->info['dataformat']) && ($this->info['fileformat'] == 'ogg') && ($this->info['audio']['dataformat'] == 'vorbis')) { + + // We cannot get an identical md5_data value for Ogg files where the comments + // span more than 1 Ogg page (compared to the same audio data with smaller + // comments) using the normal getID3() method of MD5'ing the data between the + // end of the comments and the end of the file (minus any trailing tags), + // because the page sequence numbers of the pages that the audio data is on + // do not match. Under normal circumstances, where comments are smaller than + // the nominal 4-8kB page size, then this is not a problem, but if there are + // very large comments, the only way around it is to strip off the comment + // tags with vorbiscomment and MD5 that file. + // This procedure must be applied to ALL Ogg files, not just the ones with + // comments larger than 1 page, because the below method simply MD5's the + // whole file with the comments stripped, not just the portion after the + // comments block (which is the standard getID3() method. + + // The above-mentioned problem of comments spanning multiple pages and changing + // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but + // currently vorbiscomment only works on OggVorbis files. + + // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved + if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) { + + $this->warning('Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)'); + $this->info[$algorithm.'_data'] = false; + + } else { + + // Prevent user from aborting script + $old_abort = ignore_user_abort(true); + + // Create empty file + $empty = tempnam(GETID3_TEMP_DIR, 'getID3'); + touch($empty); + + // Use vorbiscomment to make temp file without comments + $temp = tempnam(GETID3_TEMP_DIR, 'getID3'); + $file = $this->info['filenamepath']; + + if (GETID3_OS_ISWINDOWS) { + + if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) { + + $commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"'; + $VorbisCommentError = `$commandline`; + + } else { + + $VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR; + + } + + } else { + + $commandline = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg($file).' '.escapeshellarg($temp).' 2>&1'; + $VorbisCommentError = `$commandline`; + + } + + if (!empty($VorbisCommentError)) { + + $this->warning('Failed making system call to vorbiscomment(.exe) - '.$algorithm.'_data will be incorrect. If vorbiscomment is unavailable, please download from http://www.vorbis.com/download.psp and put in the getID3() directory. Error returned: '.$VorbisCommentError); + $this->info[$algorithm.'_data'] = false; + + } else { + + // Get hash of newly created file + switch ($algorithm) { + case 'md5': + $this->info[$algorithm.'_data'] = md5_file($temp); + break; + + case 'sha1': + $this->info[$algorithm.'_data'] = sha1_file($temp); + break; + } + } + + // Clean up + unlink($empty); + unlink($temp); + + // Reset abort setting + ignore_user_abort($old_abort); + + } + + } else { + + if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) { + + // get hash from part of file + $this->info[$algorithm.'_data'] = getid3_lib::hash_data($this->info['filenamepath'], $this->info['avdataoffset'], $this->info['avdataend'], $algorithm); + + } else { + + // get hash from whole file + switch ($algorithm) { + case 'md5': + $this->info[$algorithm.'_data'] = md5_file($this->info['filenamepath']); + break; + + case 'sha1': + $this->info[$algorithm.'_data'] = sha1_file($this->info['filenamepath']); + break; + } + } + + } + return true; + } + + public function ChannelsBitratePlaytimeCalculations() { + + // set channelmode on audio + if (!empty($this->info['audio']['channelmode']) || !isset($this->info['audio']['channels'])) { + // ignore + } elseif ($this->info['audio']['channels'] == 1) { + $this->info['audio']['channelmode'] = 'mono'; + } elseif ($this->info['audio']['channels'] == 2) { + $this->info['audio']['channelmode'] = 'stereo'; + } + + // Calculate combined bitrate - audio + video + $CombinedBitrate = 0; + $CombinedBitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0); + $CombinedBitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0); + if (($CombinedBitrate > 0) && empty($this->info['bitrate'])) { + $this->info['bitrate'] = $CombinedBitrate; + } + //if ((isset($this->info['video']) && !isset($this->info['video']['bitrate'])) || (isset($this->info['audio']) && !isset($this->info['audio']['bitrate']))) { + // // for example, VBR MPEG video files cannot determine video bitrate: + // // should not set overall bitrate and playtime from audio bitrate only + // unset($this->info['bitrate']); + //} + + // video bitrate undetermined, but calculable + if (isset($this->info['video']['dataformat']) && $this->info['video']['dataformat'] && (!isset($this->info['video']['bitrate']) || ($this->info['video']['bitrate'] == 0))) { + // if video bitrate not set + if (isset($this->info['audio']['bitrate']) && ($this->info['audio']['bitrate'] > 0) && ($this->info['audio']['bitrate'] == $this->info['bitrate'])) { + // AND if audio bitrate is set to same as overall bitrate + if (isset($this->info['playtime_seconds']) && ($this->info['playtime_seconds'] > 0)) { + // AND if playtime is set + if (isset($this->info['avdataend']) && isset($this->info['avdataoffset'])) { + // AND if AV data offset start/end is known + // THEN we can calculate the video bitrate + $this->info['bitrate'] = round((($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds']); + $this->info['video']['bitrate'] = $this->info['bitrate'] - $this->info['audio']['bitrate']; + } + } + } + } + + if ((!isset($this->info['playtime_seconds']) || ($this->info['playtime_seconds'] <= 0)) && !empty($this->info['bitrate'])) { + $this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate']; + } + + if (!isset($this->info['bitrate']) && !empty($this->info['playtime_seconds'])) { + $this->info['bitrate'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds']; + } + if (isset($this->info['bitrate']) && empty($this->info['audio']['bitrate']) && empty($this->info['video']['bitrate'])) { + if (isset($this->info['audio']['dataformat']) && empty($this->info['video']['resolution_x'])) { + // audio only + $this->info['audio']['bitrate'] = $this->info['bitrate']; + } elseif (isset($this->info['video']['resolution_x']) && empty($this->info['audio']['dataformat'])) { + // video only + $this->info['video']['bitrate'] = $this->info['bitrate']; + } + } + + // Set playtime string + if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) { + $this->info['playtime_string'] = getid3_lib::PlaytimeString($this->info['playtime_seconds']); + } + } + + /** + * @return bool + */ + public function CalculateCompressionRatioVideo() { + if (empty($this->info['video'])) { + return false; + } + if (empty($this->info['video']['resolution_x']) || empty($this->info['video']['resolution_y'])) { + return false; + } + if (empty($this->info['video']['bits_per_sample'])) { + return false; + } + + switch ($this->info['video']['dataformat']) { + case 'bmp': + case 'gif': + case 'jpeg': + case 'jpg': + case 'png': + case 'tiff': + $FrameRate = 1; + $PlaytimeSeconds = 1; + $BitrateCompressed = $this->info['filesize'] * 8; + break; + + default: + if (!empty($this->info['video']['frame_rate'])) { + $FrameRate = $this->info['video']['frame_rate']; + } else { + return false; + } + if (!empty($this->info['playtime_seconds'])) { + $PlaytimeSeconds = $this->info['playtime_seconds']; + } else { + return false; + } + if (!empty($this->info['video']['bitrate'])) { + $BitrateCompressed = $this->info['video']['bitrate']; + } else { + return false; + } + break; + } + $BitrateUncompressed = $this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $FrameRate; + + $this->info['video']['compression_ratio'] = $BitrateCompressed / $BitrateUncompressed; + return true; + } + + /** + * @return bool + */ + public function CalculateCompressionRatioAudio() { + if (empty($this->info['audio']['bitrate']) || empty($this->info['audio']['channels']) || empty($this->info['audio']['sample_rate']) || !is_numeric($this->info['audio']['sample_rate'])) { + return false; + } + $this->info['audio']['compression_ratio'] = $this->info['audio']['bitrate'] / ($this->info['audio']['channels'] * $this->info['audio']['sample_rate'] * (!empty($this->info['audio']['bits_per_sample']) ? $this->info['audio']['bits_per_sample'] : 16)); + + if (!empty($this->info['audio']['streams'])) { + foreach ($this->info['audio']['streams'] as $streamnumber => $streamdata) { + if (!empty($streamdata['bitrate']) && !empty($streamdata['channels']) && !empty($streamdata['sample_rate'])) { + $this->info['audio']['streams'][$streamnumber]['compression_ratio'] = $streamdata['bitrate'] / ($streamdata['channels'] * $streamdata['sample_rate'] * (!empty($streamdata['bits_per_sample']) ? $streamdata['bits_per_sample'] : 16)); + } + } + } + return true; + } + + /** + * @return bool + */ + public function CalculateReplayGain() { + if (isset($this->info['replay_gain'])) { + if (!isset($this->info['replay_gain']['reference_volume'])) { + $this->info['replay_gain']['reference_volume'] = 89.0; + } + if (isset($this->info['replay_gain']['track']['adjustment'])) { + $this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment']; + } + if (isset($this->info['replay_gain']['album']['adjustment'])) { + $this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment']; + } + + if (isset($this->info['replay_gain']['track']['peak'])) { + $this->info['replay_gain']['track']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['track']['peak']); + } + if (isset($this->info['replay_gain']['album']['peak'])) { + $this->info['replay_gain']['album']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['album']['peak']); + } + } + return true; + } + + /** + * @return bool + */ + public function ProcessAudioStreams() { + if (!empty($this->info['audio']['bitrate']) || !empty($this->info['audio']['channels']) || !empty($this->info['audio']['sample_rate'])) { + if (!isset($this->info['audio']['streams'])) { + foreach ($this->info['audio'] as $key => $value) { + if ($key != 'streams') { + $this->info['audio']['streams'][0][$key] = $value; + } + } + } + } + return true; + } + + /** + * @return string|bool + */ + public function getid3_tempnam() { + return tempnam($this->tempdir, 'gI3'); + } + + /** + * @param string $name + * + * @return bool + * + * @throws getid3_exception + */ + public function include_module($name) { + //if (!file_exists($this->include_path.'module.'.$name.'.php')) { + if (!file_exists(GETID3_INCLUDEPATH.'module.'.$name.'.php')) { + throw new getid3_exception('Required module.'.$name.'.php is missing.'); + } + include_once(GETID3_INCLUDEPATH.'module.'.$name.'.php'); + return true; + } + + /** + * @param string $filename + * + * @return bool + */ + public static function is_writable ($filename) { + $ret = is_writable($filename); + if (!$ret) { + $perms = fileperms($filename); + $ret = ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002); + } + return $ret; + } + +} + + +abstract class getid3_handler +{ + + /** + * @var getID3 + */ + protected $getid3; // pointer + + /** + * Analyzing filepointer or string. + * + * @var bool + */ + protected $data_string_flag = false; + + /** + * String to analyze. + * + * @var string + */ + protected $data_string = ''; + + /** + * Seek position in string. + * + * @var int + */ + protected $data_string_position = 0; + + /** + * String length. + * + * @var int + */ + protected $data_string_length = 0; + + /** + * @var string + */ + private $dependency_to; + + /** + * getid3_handler constructor. + * + * @param getID3 $getid3 + * @param string $call_module + */ + public function __construct(getID3 $getid3, $call_module=null) { + $this->getid3 = $getid3; + + if ($call_module) { + $this->dependency_to = str_replace('getid3_', '', $call_module); + } + } + + /** + * Analyze from file pointer. + * + * @return bool + */ + abstract public function Analyze(); + + /** + * Analyze from string instead. + * + * @param string $string + */ + public function AnalyzeString($string) { + // Enter string mode + $this->setStringMode($string); + + // Save info + $saved_avdataoffset = $this->getid3->info['avdataoffset']; + $saved_avdataend = $this->getid3->info['avdataend']; + $saved_filesize = (isset($this->getid3->info['filesize']) ? $this->getid3->info['filesize'] : null); // may be not set if called as dependency without openfile() call + + // Reset some info + $this->getid3->info['avdataoffset'] = 0; + $this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = $this->data_string_length; + + // Analyze + $this->Analyze(); + + // Restore some info + $this->getid3->info['avdataoffset'] = $saved_avdataoffset; + $this->getid3->info['avdataend'] = $saved_avdataend; + $this->getid3->info['filesize'] = $saved_filesize; + + // Exit string mode + $this->data_string_flag = false; + } + + /** + * @param string $string + */ + public function setStringMode($string) { + $this->data_string_flag = true; + $this->data_string = $string; + $this->data_string_length = strlen($string); + } + + /** + * @return int|bool + */ + protected function ftell() { + if ($this->data_string_flag) { + return $this->data_string_position; + } + return ftell($this->getid3->fp); + } + + /** + * @param int $bytes + * + * @return string|false + * + * @throws getid3_exception + */ + protected function fread($bytes) { + if ($this->data_string_flag) { + $this->data_string_position += $bytes; + return substr($this->data_string, $this->data_string_position - $bytes, $bytes); + } + $pos = $this->ftell() + $bytes; + if (!getid3_lib::intValueSupported($pos)) { + throw new getid3_exception('cannot fread('.$bytes.' from '.$this->ftell().') because beyond PHP filesystem limit', 10); + } + + //return fread($this->getid3->fp, $bytes); + /* + * https://www.getid3.org/phpBB3/viewtopic.php?t=1930 + * "I found out that the root cause for the problem was how getID3 uses the PHP system function fread(). + * It seems to assume that fread() would always return as many bytes as were requested. + * However, according the PHP manual (http://php.net/manual/en/function.fread.php), this is the case only with regular local files, but not e.g. with Linux pipes. + * The call may return only part of the requested data and a new call is needed to get more." + */ + $contents = ''; + do { + //if (($this->getid3->memory_limit > 0) && ($bytes > $this->getid3->memory_limit)) { + if (($this->getid3->memory_limit > 0) && (($bytes / $this->getid3->memory_limit) > 0.99)) { // enable a more-fuzzy match to prevent close misses generating errors like "PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 33554464 bytes)" + throw new getid3_exception('cannot fread('.$bytes.' from '.$this->ftell().') that is more than available PHP memory ('.$this->getid3->memory_limit.')', 10); + } + $part = fread($this->getid3->fp, $bytes); + $partLength = strlen($part); + $bytes -= $partLength; + $contents .= $part; + } while (($bytes > 0) && ($partLength > 0)); + return $contents; + } + + /** + * @param int $bytes + * @param int $whence + * + * @return int + * + * @throws getid3_exception + */ + protected function fseek($bytes, $whence=SEEK_SET) { + if ($this->data_string_flag) { + switch ($whence) { + case SEEK_SET: + $this->data_string_position = $bytes; + break; + + case SEEK_CUR: + $this->data_string_position += $bytes; + break; + + case SEEK_END: + $this->data_string_position = $this->data_string_length + $bytes; + break; + } + return 0; + } else { + $pos = $bytes; + if ($whence == SEEK_CUR) { + $pos = $this->ftell() + $bytes; + } elseif ($whence == SEEK_END) { + $pos = $this->getid3->info['filesize'] + $bytes; + } + if (!getid3_lib::intValueSupported($pos)) { + throw new getid3_exception('cannot fseek('.$pos.') because beyond PHP filesystem limit', 10); + } + } + return fseek($this->getid3->fp, $bytes, $whence); + } + + /** + * @return string|false + * + * @throws getid3_exception + */ + protected function fgets() { + // must be able to handle CR/LF/CRLF but not read more than one lineend + $buffer = ''; // final string we will return + $prevchar = ''; // save previously-read character for end-of-line checking + if ($this->data_string_flag) { + while (true) { + $thischar = substr($this->data_string, $this->data_string_position++, 1); + if (($prevchar == "\r") && ($thischar != "\n")) { + // read one byte too many, back up + $this->data_string_position--; + break; + } + $buffer .= $thischar; + if ($thischar == "\n") { + break; + } + if ($this->data_string_position >= $this->data_string_length) { + // EOF + break; + } + $prevchar = $thischar; + } + + } else { + + // Ideally we would just use PHP's fgets() function, however... + // it does not behave consistently with regards to mixed line endings, may be system-dependent + // and breaks entirely when given a file with mixed \r vs \n vs \r\n line endings (e.g. some PDFs) + //return fgets($this->getid3->fp); + while (true) { + $thischar = fgetc($this->getid3->fp); + if (($prevchar == "\r") && ($thischar != "\n")) { + // read one byte too many, back up + fseek($this->getid3->fp, -1, SEEK_CUR); + break; + } + $buffer .= $thischar; + if ($thischar == "\n") { + break; + } + if (feof($this->getid3->fp)) { + break; + } + $prevchar = $thischar; + } + + } + return $buffer; + } + + /** + * @return bool + */ + protected function feof() { + if ($this->data_string_flag) { + return $this->data_string_position >= $this->data_string_length; + } + return feof($this->getid3->fp); + } + + /** + * @param string $module + * + * @return bool + */ + final protected function isDependencyFor($module) { + return $this->dependency_to == $module; + } + + /** + * @param string $text + * + * @return bool + */ + protected function error($text) { + $this->getid3->info['error'][] = $text; + + return false; + } + + /** + * @param string $text + * + * @return bool + */ + protected function warning($text) { + return $this->getid3->warning($text); + } + + /** + * @param string $text + */ + protected function notice($text) { + // does nothing for now + } + + /** + * @param string $name + * @param int $offset + * @param int $length + * @param string $image_mime + * + * @return string|null + * + * @throws Exception + * @throws getid3_exception + */ + public function saveAttachment($name, $offset, $length, $image_mime=null) { + try { + + // do not extract at all + if ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_NONE) { + + $attachment = null; // do not set any + + // extract to return array + } elseif ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_INLINE) { + + $this->fseek($offset); + $attachment = $this->fread($length); // get whole data in one pass, till it is anyway stored in memory + if ($attachment === false || strlen($attachment) != $length) { + throw new Exception('failed to read attachment data'); + } + + // assume directory path is given + } else { + + // set up destination path + $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->getid3->option_save_attachments), DIRECTORY_SEPARATOR); + if (!is_dir($dir) || !getID3::is_writable($dir)) { // check supplied directory + throw new Exception('supplied path ('.$dir.') does not exist, or is not writable'); + } + $dest = $dir.DIRECTORY_SEPARATOR.$name.($image_mime ? '.'.getid3_lib::ImageExtFromMime($image_mime) : ''); + + // create dest file + if (($fp_dest = fopen($dest, 'wb')) == false) { + throw new Exception('failed to create file '.$dest); + } + + // copy data + $this->fseek($offset); + $buffersize = ($this->data_string_flag ? $length : $this->getid3->fread_buffer_size()); + $bytesleft = $length; + while ($bytesleft > 0) { + if (($buffer = $this->fread(min($buffersize, $bytesleft))) === false || ($byteswritten = fwrite($fp_dest, $buffer)) === false || ($byteswritten === 0)) { + throw new Exception($buffer === false ? 'not enough data to read' : 'failed to write to destination file, may be not enough disk space'); + } + $bytesleft -= $byteswritten; + } + + fclose($fp_dest); + $attachment = $dest; + + } + + } catch (Exception $e) { + + // close and remove dest file if created + if (isset($fp_dest) && is_resource($fp_dest)) { + fclose($fp_dest); + } + + if (isset($dest) && file_exists($dest)) { + unlink($dest); + } + + // do not set any is case of error + $attachment = null; + $this->warning('Failed to extract attachment '.$name.': '.$e->getMessage()); + + } + + // seek to the end of attachment + $this->fseek($offset + $length); + + return $attachment; + } + +} + + +class getid3_exception extends Exception +{ + public $message; +} diff --git a/projects/tests/tests/performance/ID3/license.commercial.txt b/projects/tests/tests/performance/ID3/license.commercial.txt new file mode 100644 index 00000000..416e5a14 --- /dev/null +++ b/projects/tests/tests/performance/ID3/license.commercial.txt @@ -0,0 +1,27 @@ + getID3() Commercial License + =========================== + +getID3() is licensed under the "GNU Public License" (GPL) and/or the +"getID3() Commercial License" (gCL). This document describes the gCL. + +--------------------------------------------------------------------- + +The license is non-exclusively granted to a single person or company, +per payment of the license fee, for the lifetime of that person or +company. The license is non-transferrable. + +The gCL grants the licensee the right to use getID3() in commercial +closed-source projects. Modifications may be made to getID3() with no +obligation to release the modified source code. getID3() (or pieces +thereof) may be included in any number of projects authored (in whole +or in part) by the licensee. + +The licensee may use any version of getID3(), past, present or future, +as is most convenient. This license does not entitle the licensee to +receive any technical support, updates or bugfixes, except as such are +made publicly available to all getID3() users. + +The licensee may not sub-license getID3() itself, meaning that any +commercially released product containing all or parts of getID3() must +have added functionality beyond what is available in getID3(); +getID3() itself may not be re-licensed by the licensee. diff --git a/projects/tests/tests/performance/ID3/license.txt b/projects/tests/tests/performance/ID3/license.txt new file mode 100644 index 00000000..c67873ae --- /dev/null +++ b/projects/tests/tests/performance/ID3/license.txt @@ -0,0 +1,29 @@ +///////////////////////////////////////////////////////////////// +/// getID3() by James Heinrich // +// available at http://getid3.sourceforge.net // +// or https://www.getid3.org // +// also https://github.com/JamesHeinrich/getID3 // +///////////////////////////////////////////////////////////////// + +***************************************************************** +***************************************************************** + + getID3() is released under multiple licenses. You may choose + from the following licenses, and use getID3 according to the + terms of the license most suitable to your project. + +GNU GPL: https://gnu.org/licenses/gpl.html (v3) + https://gnu.org/licenses/old-licenses/gpl-2.0.html (v2) + https://gnu.org/licenses/old-licenses/gpl-1.0.html (v1) + +GNU LGPL: https://gnu.org/licenses/lgpl.html (v3) + +Mozilla MPL: https://www.mozilla.org/MPL/2.0/ (v2) + +getID3 Commercial License: https://www.getid3.org/#gCL (payment required) + +***************************************************************** +***************************************************************** + +Copies of each of the above licenses are included in the 'licenses' +directory of the getID3 distribution. diff --git a/projects/tests/tests/performance/ID3/module.audio-video.asf.php b/projects/tests/tests/performance/ID3/module.audio-video.asf.php new file mode 100644 index 00000000..fce923c0 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio-video.asf.php @@ -0,0 +1,2087 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio-video.asf.php // +// module for analyzing ASF, WMA and WMV files // +// dependencies: module.audio-video.riff.php // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true); + +class getid3_asf extends getid3_handler +{ + /** + * @param getID3 $getid3 + */ + public function __construct(getID3 $getid3) { + parent::__construct($getid3); // extends getid3_handler::__construct() + + // initialize all GUID constants + $GUIDarray = $this->KnownGUIDs(); + foreach ($GUIDarray as $GUIDname => $hexstringvalue) { + if (!defined($GUIDname)) { + define($GUIDname, $this->GUIDtoBytestring($hexstringvalue)); + } + } + } + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + // Shortcuts + $thisfile_audio = &$info['audio']; + $thisfile_video = &$info['video']; + $info['asf'] = array(); + $thisfile_asf = &$info['asf']; + $thisfile_asf['comments'] = array(); + $thisfile_asf_comments = &$thisfile_asf['comments']; + $thisfile_asf['header_object'] = array(); + $thisfile_asf_headerobject = &$thisfile_asf['header_object']; + + + // ASF structure: + // * Header Object [required] + // * File Properties Object [required] (global file attributes) + // * Stream Properties Object [required] (defines media stream & characteristics) + // * Header Extension Object [required] (additional functionality) + // * Content Description Object (bibliographic information) + // * Script Command Object (commands for during playback) + // * Marker Object (named jumped points within the file) + // * Data Object [required] + // * Data Packets + // * Index Object + + // Header Object: (mandatory, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for header object - GETID3_ASF_Header_Object + // Object Size QWORD 64 // size of header object, including 30 bytes of Header Object header + // Number of Header Objects DWORD 32 // number of objects in header object + // Reserved1 BYTE 8 // hardcoded: 0x01 + // Reserved2 BYTE 8 // hardcoded: 0x02 + + $info['fileformat'] = 'asf'; + + $this->fseek($info['avdataoffset']); + $HeaderObjectData = $this->fread(30); + + $thisfile_asf_headerobject['objectid'] = substr($HeaderObjectData, 0, 16); + $thisfile_asf_headerobject['objectid_guid'] = $this->BytestringToGUID($thisfile_asf_headerobject['objectid']); + if ($thisfile_asf_headerobject['objectid'] != GETID3_ASF_Header_Object) { + unset($info['fileformat'], $info['asf']); + return $this->error('ASF header GUID {'.$this->BytestringToGUID($thisfile_asf_headerobject['objectid']).'} does not match expected "GETID3_ASF_Header_Object" GUID {'.$this->BytestringToGUID(GETID3_ASF_Header_Object).'}'); + } + $thisfile_asf_headerobject['objectsize'] = getid3_lib::LittleEndian2Int(substr($HeaderObjectData, 16, 8)); + $thisfile_asf_headerobject['headerobjects'] = getid3_lib::LittleEndian2Int(substr($HeaderObjectData, 24, 4)); + $thisfile_asf_headerobject['reserved1'] = getid3_lib::LittleEndian2Int(substr($HeaderObjectData, 28, 1)); + $thisfile_asf_headerobject['reserved2'] = getid3_lib::LittleEndian2Int(substr($HeaderObjectData, 29, 1)); + + $NextObjectOffset = $this->ftell(); + $ASFHeaderData = $this->fread($thisfile_asf_headerobject['objectsize'] - 30); + $offset = 0; + $thisfile_asf_streambitratepropertiesobject = array(); + $thisfile_asf_codeclistobject = array(); + + for ($HeaderObjectsCounter = 0; $HeaderObjectsCounter < $thisfile_asf_headerobject['headerobjects']; $HeaderObjectsCounter++) { + $NextObjectGUID = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $NextObjectGUIDtext = $this->BytestringToGUID($NextObjectGUID); + $NextObjectSize = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + switch ($NextObjectGUID) { + + case GETID3_ASF_File_Properties_Object: + // File Properties Object: (mandatory, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for file properties object - GETID3_ASF_File_Properties_Object + // Object Size QWORD 64 // size of file properties object, including 104 bytes of File Properties Object header + // File ID GUID 128 // unique ID - identical to File ID in Data Object + // File Size QWORD 64 // entire file in bytes. Invalid if Broadcast Flag == 1 + // Creation Date QWORD 64 // date & time of file creation. Maybe invalid if Broadcast Flag == 1 + // Data Packets Count QWORD 64 // number of data packets in Data Object. Invalid if Broadcast Flag == 1 + // Play Duration QWORD 64 // playtime, in 100-nanosecond units. Invalid if Broadcast Flag == 1 + // Send Duration QWORD 64 // time needed to send file, in 100-nanosecond units. Players can ignore this value. Invalid if Broadcast Flag == 1 + // Preroll QWORD 64 // time to buffer data before starting to play file, in 1-millisecond units. If <> 0, PlayDuration and PresentationTime have been offset by this amount + // Flags DWORD 32 // + // * Broadcast Flag bits 1 (0x01) // file is currently being written, some header values are invalid + // * Seekable Flag bits 1 (0x02) // is file seekable + // * Reserved bits 30 (0xFFFFFFFC) // reserved - set to zero + // Minimum Data Packet Size DWORD 32 // in bytes. should be same as Maximum Data Packet Size. Invalid if Broadcast Flag == 1 + // Maximum Data Packet Size DWORD 32 // in bytes. should be same as Minimum Data Packet Size. Invalid if Broadcast Flag == 1 + // Maximum Bitrate DWORD 32 // maximum instantaneous bitrate in bits per second for entire file, including all data streams and ASF overhead + + // shortcut + $thisfile_asf['file_properties_object'] = array(); + $thisfile_asf_filepropertiesobject = &$thisfile_asf['file_properties_object']; + + $thisfile_asf_filepropertiesobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_filepropertiesobject['objectid'] = $NextObjectGUID; + $thisfile_asf_filepropertiesobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_filepropertiesobject['objectsize'] = $NextObjectSize; + $thisfile_asf_filepropertiesobject['fileid'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $thisfile_asf_filepropertiesobject['fileid_guid'] = $this->BytestringToGUID($thisfile_asf_filepropertiesobject['fileid']); + $thisfile_asf_filepropertiesobject['filesize'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $thisfile_asf_filepropertiesobject['creation_date'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $thisfile_asf_filepropertiesobject['creation_date_unix'] = $this->FILETIMEtoUNIXtime($thisfile_asf_filepropertiesobject['creation_date']); + $offset += 8; + $thisfile_asf_filepropertiesobject['data_packets'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $thisfile_asf_filepropertiesobject['play_duration'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $thisfile_asf_filepropertiesobject['send_duration'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $thisfile_asf_filepropertiesobject['preroll'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $thisfile_asf_filepropertiesobject['flags_raw'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_filepropertiesobject['flags']['broadcast'] = (bool) ($thisfile_asf_filepropertiesobject['flags_raw'] & 0x0001); + $thisfile_asf_filepropertiesobject['flags']['seekable'] = (bool) ($thisfile_asf_filepropertiesobject['flags_raw'] & 0x0002); + + $thisfile_asf_filepropertiesobject['min_packet_size'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_filepropertiesobject['max_packet_size'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_filepropertiesobject['max_bitrate'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + + if ($thisfile_asf_filepropertiesobject['flags']['broadcast']) { + + // broadcast flag is set, some values invalid + unset($thisfile_asf_filepropertiesobject['filesize']); + unset($thisfile_asf_filepropertiesobject['data_packets']); + unset($thisfile_asf_filepropertiesobject['play_duration']); + unset($thisfile_asf_filepropertiesobject['send_duration']); + unset($thisfile_asf_filepropertiesobject['min_packet_size']); + unset($thisfile_asf_filepropertiesobject['max_packet_size']); + + } else { + + // broadcast flag NOT set, perform calculations + $info['playtime_seconds'] = ($thisfile_asf_filepropertiesobject['play_duration'] / 10000000) - ($thisfile_asf_filepropertiesobject['preroll'] / 1000); + + //$info['bitrate'] = $thisfile_asf_filepropertiesobject['max_bitrate']; + $info['bitrate'] = ((isset($thisfile_asf_filepropertiesobject['filesize']) ? $thisfile_asf_filepropertiesobject['filesize'] : $info['filesize']) * 8) / $info['playtime_seconds']; + } + break; + + case GETID3_ASF_Stream_Properties_Object: + // Stream Properties Object: (mandatory, one per media stream) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for stream properties object - GETID3_ASF_Stream_Properties_Object + // Object Size QWORD 64 // size of stream properties object, including 78 bytes of Stream Properties Object header + // Stream Type GUID 128 // GETID3_ASF_Audio_Media, GETID3_ASF_Video_Media or GETID3_ASF_Command_Media + // Error Correction Type GUID 128 // GETID3_ASF_Audio_Spread for audio-only streams, GETID3_ASF_No_Error_Correction for other stream types + // Time Offset QWORD 64 // 100-nanosecond units. typically zero. added to all timestamps of samples in the stream + // Type-Specific Data Length DWORD 32 // number of bytes for Type-Specific Data field + // Error Correction Data Length DWORD 32 // number of bytes for Error Correction Data field + // Flags WORD 16 // + // * Stream Number bits 7 (0x007F) // number of this stream. 1 <= valid <= 127 + // * Reserved bits 8 (0x7F80) // reserved - set to zero + // * Encrypted Content Flag bits 1 (0x8000) // stream contents encrypted if set + // Reserved DWORD 32 // reserved - set to zero + // Type-Specific Data BYTESTREAM variable // type-specific format data, depending on value of Stream Type + // Error Correction Data BYTESTREAM variable // error-correction-specific format data, depending on value of Error Correct Type + + // There is one GETID3_ASF_Stream_Properties_Object for each stream (audio, video) but the + // stream number isn't known until halfway through decoding the structure, hence it + // it is decoded to a temporary variable and then stuck in the appropriate index later + + $StreamPropertiesObjectData['offset'] = $NextObjectOffset + $offset; + $StreamPropertiesObjectData['objectid'] = $NextObjectGUID; + $StreamPropertiesObjectData['objectid_guid'] = $NextObjectGUIDtext; + $StreamPropertiesObjectData['objectsize'] = $NextObjectSize; + $StreamPropertiesObjectData['stream_type'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $StreamPropertiesObjectData['stream_type_guid'] = $this->BytestringToGUID($StreamPropertiesObjectData['stream_type']); + $StreamPropertiesObjectData['error_correct_type'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $StreamPropertiesObjectData['error_correct_guid'] = $this->BytestringToGUID($StreamPropertiesObjectData['error_correct_type']); + $StreamPropertiesObjectData['time_offset'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $StreamPropertiesObjectData['type_data_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $StreamPropertiesObjectData['error_data_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $StreamPropertiesObjectData['flags_raw'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $StreamPropertiesObjectStreamNumber = $StreamPropertiesObjectData['flags_raw'] & 0x007F; + $StreamPropertiesObjectData['flags']['encrypted'] = (bool) ($StreamPropertiesObjectData['flags_raw'] & 0x8000); + + $offset += 4; // reserved - DWORD + $StreamPropertiesObjectData['type_specific_data'] = substr($ASFHeaderData, $offset, $StreamPropertiesObjectData['type_data_length']); + $offset += $StreamPropertiesObjectData['type_data_length']; + $StreamPropertiesObjectData['error_correct_data'] = substr($ASFHeaderData, $offset, $StreamPropertiesObjectData['error_data_length']); + $offset += $StreamPropertiesObjectData['error_data_length']; + + switch ($StreamPropertiesObjectData['stream_type']) { + + case GETID3_ASF_Audio_Media: + $thisfile_audio['dataformat'] = (!empty($thisfile_audio['dataformat']) ? $thisfile_audio['dataformat'] : 'asf'); + $thisfile_audio['bitrate_mode'] = (!empty($thisfile_audio['bitrate_mode']) ? $thisfile_audio['bitrate_mode'] : 'cbr'); + + $audiodata = getid3_riff::parseWAVEFORMATex(substr($StreamPropertiesObjectData['type_specific_data'], 0, 16)); + unset($audiodata['raw']); + $thisfile_audio = getid3_lib::array_merge_noclobber($audiodata, $thisfile_audio); + break; + + case GETID3_ASF_Video_Media: + $thisfile_video['dataformat'] = (!empty($thisfile_video['dataformat']) ? $thisfile_video['dataformat'] : 'asf'); + $thisfile_video['bitrate_mode'] = (!empty($thisfile_video['bitrate_mode']) ? $thisfile_video['bitrate_mode'] : 'cbr'); + break; + + case GETID3_ASF_Command_Media: + default: + // do nothing + break; + + } + + $thisfile_asf['stream_properties_object'][$StreamPropertiesObjectStreamNumber] = $StreamPropertiesObjectData; + unset($StreamPropertiesObjectData); // clear for next stream, if any + break; + + case GETID3_ASF_Header_Extension_Object: + // Header Extension Object: (mandatory, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Header Extension object - GETID3_ASF_Header_Extension_Object + // Object Size QWORD 64 // size of Header Extension object, including 46 bytes of Header Extension Object header + // Reserved Field 1 GUID 128 // hardcoded: GETID3_ASF_Reserved_1 + // Reserved Field 2 WORD 16 // hardcoded: 0x00000006 + // Header Extension Data Size DWORD 32 // in bytes. valid: 0, or > 24. equals object size minus 46 + // Header Extension Data BYTESTREAM variable // array of zero or more extended header objects + + // shortcut + $thisfile_asf['header_extension_object'] = array(); + $thisfile_asf_headerextensionobject = &$thisfile_asf['header_extension_object']; + + $thisfile_asf_headerextensionobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_headerextensionobject['objectid'] = $NextObjectGUID; + $thisfile_asf_headerextensionobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_headerextensionobject['objectsize'] = $NextObjectSize; + $thisfile_asf_headerextensionobject['reserved_1'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $thisfile_asf_headerextensionobject['reserved_1_guid'] = $this->BytestringToGUID($thisfile_asf_headerextensionobject['reserved_1']); + if ($thisfile_asf_headerextensionobject['reserved_1'] != GETID3_ASF_Reserved_1) { + $this->warning('header_extension_object.reserved_1 GUID ('.$this->BytestringToGUID($thisfile_asf_headerextensionobject['reserved_1']).') does not match expected "GETID3_ASF_Reserved_1" GUID ('.$this->BytestringToGUID(GETID3_ASF_Reserved_1).')'); + //return false; + break; + } + $thisfile_asf_headerextensionobject['reserved_2'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + if ($thisfile_asf_headerextensionobject['reserved_2'] != 6) { + $this->warning('header_extension_object.reserved_2 ('.getid3_lib::PrintHexBytes($thisfile_asf_headerextensionobject['reserved_2']).') does not match expected value of "6"'); + //return false; + break; + } + $thisfile_asf_headerextensionobject['extension_data_size'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_headerextensionobject['extension_data'] = substr($ASFHeaderData, $offset, $thisfile_asf_headerextensionobject['extension_data_size']); + $unhandled_sections = 0; + $thisfile_asf_headerextensionobject['extension_data_parsed'] = $this->HeaderExtensionObjectDataParse($thisfile_asf_headerextensionobject['extension_data'], $unhandled_sections); + if ($unhandled_sections === 0) { + unset($thisfile_asf_headerextensionobject['extension_data']); + } + $offset += $thisfile_asf_headerextensionobject['extension_data_size']; + break; + + case GETID3_ASF_Codec_List_Object: + // Codec List Object: (optional, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Codec List object - GETID3_ASF_Codec_List_Object + // Object Size QWORD 64 // size of Codec List object, including 44 bytes of Codec List Object header + // Reserved GUID 128 // hardcoded: 86D15241-311D-11D0-A3A4-00A0C90348F6 + // Codec Entries Count DWORD 32 // number of entries in Codec Entries array + // Codec Entries array of: variable // + // * Type WORD 16 // 0x0001 = Video Codec, 0x0002 = Audio Codec, 0xFFFF = Unknown Codec + // * Codec Name Length WORD 16 // number of Unicode characters stored in the Codec Name field + // * Codec Name WCHAR variable // array of Unicode characters - name of codec used to create the content + // * Codec Description Length WORD 16 // number of Unicode characters stored in the Codec Description field + // * Codec Description WCHAR variable // array of Unicode characters - description of format used to create the content + // * Codec Information Length WORD 16 // number of Unicode characters stored in the Codec Information field + // * Codec Information BYTESTREAM variable // opaque array of information bytes about the codec used to create the content + + // shortcut + $thisfile_asf['codec_list_object'] = array(); + $thisfile_asf_codeclistobject = &$thisfile_asf['codec_list_object']; + + $thisfile_asf_codeclistobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_codeclistobject['objectid'] = $NextObjectGUID; + $thisfile_asf_codeclistobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_codeclistobject['objectsize'] = $NextObjectSize; + $thisfile_asf_codeclistobject['reserved'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $thisfile_asf_codeclistobject['reserved_guid'] = $this->BytestringToGUID($thisfile_asf_codeclistobject['reserved']); + if ($thisfile_asf_codeclistobject['reserved'] != $this->GUIDtoBytestring('86D15241-311D-11D0-A3A4-00A0C90348F6')) { + $this->warning('codec_list_object.reserved GUID {'.$this->BytestringToGUID($thisfile_asf_codeclistobject['reserved']).'} does not match expected "GETID3_ASF_Reserved_1" GUID {86D15241-311D-11D0-A3A4-00A0C90348F6}'); + //return false; + break; + } + $thisfile_asf_codeclistobject['codec_entries_count'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + for ($CodecEntryCounter = 0; $CodecEntryCounter < $thisfile_asf_codeclistobject['codec_entries_count']; $CodecEntryCounter++) { + // shortcut + $thisfile_asf_codeclistobject['codec_entries'][$CodecEntryCounter] = array(); + $thisfile_asf_codeclistobject_codecentries_current = &$thisfile_asf_codeclistobject['codec_entries'][$CodecEntryCounter]; + + $thisfile_asf_codeclistobject_codecentries_current['type_raw'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_codeclistobject_codecentries_current['type'] = self::codecListObjectTypeLookup($thisfile_asf_codeclistobject_codecentries_current['type_raw']); + + $CodecNameLength = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)) * 2; // 2 bytes per character + $offset += 2; + $thisfile_asf_codeclistobject_codecentries_current['name'] = substr($ASFHeaderData, $offset, $CodecNameLength); + $offset += $CodecNameLength; + + $CodecDescriptionLength = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)) * 2; // 2 bytes per character + $offset += 2; + $thisfile_asf_codeclistobject_codecentries_current['description'] = substr($ASFHeaderData, $offset, $CodecDescriptionLength); + $offset += $CodecDescriptionLength; + + $CodecInformationLength = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_codeclistobject_codecentries_current['information'] = substr($ASFHeaderData, $offset, $CodecInformationLength); + $offset += $CodecInformationLength; + + if ($thisfile_asf_codeclistobject_codecentries_current['type_raw'] == 2) { // audio codec + + if (strpos($thisfile_asf_codeclistobject_codecentries_current['description'], ',') === false) { + $this->warning('[asf][codec_list_object][codec_entries]['.$CodecEntryCounter.'][description] expected to contain comma-separated list of parameters: "'.$thisfile_asf_codeclistobject_codecentries_current['description'].'"'); + } else { + + list($AudioCodecBitrate, $AudioCodecFrequency, $AudioCodecChannels) = explode(',', $this->TrimConvert($thisfile_asf_codeclistobject_codecentries_current['description'])); + $thisfile_audio['codec'] = $this->TrimConvert($thisfile_asf_codeclistobject_codecentries_current['name']); + + if (!isset($thisfile_audio['bitrate']) && strstr($AudioCodecBitrate, 'kbps')) { + $thisfile_audio['bitrate'] = (int) trim(str_replace('kbps', '', $AudioCodecBitrate)) * 1000; + } + //if (!isset($thisfile_video['bitrate']) && isset($thisfile_audio['bitrate']) && isset($thisfile_asf['file_properties_object']['max_bitrate']) && ($thisfile_asf_codeclistobject['codec_entries_count'] > 1)) { + if (empty($thisfile_video['bitrate']) && !empty($thisfile_audio['bitrate']) && !empty($info['bitrate'])) { + //$thisfile_video['bitrate'] = $thisfile_asf['file_properties_object']['max_bitrate'] - $thisfile_audio['bitrate']; + $thisfile_video['bitrate'] = $info['bitrate'] - $thisfile_audio['bitrate']; + } + + $AudioCodecFrequency = (int) trim(str_replace('kHz', '', $AudioCodecFrequency)); + switch ($AudioCodecFrequency) { + case 8: + case 8000: + $thisfile_audio['sample_rate'] = 8000; + break; + + case 11: + case 11025: + $thisfile_audio['sample_rate'] = 11025; + break; + + case 12: + case 12000: + $thisfile_audio['sample_rate'] = 12000; + break; + + case 16: + case 16000: + $thisfile_audio['sample_rate'] = 16000; + break; + + case 22: + case 22050: + $thisfile_audio['sample_rate'] = 22050; + break; + + case 24: + case 24000: + $thisfile_audio['sample_rate'] = 24000; + break; + + case 32: + case 32000: + $thisfile_audio['sample_rate'] = 32000; + break; + + case 44: + case 441000: + $thisfile_audio['sample_rate'] = 44100; + break; + + case 48: + case 48000: + $thisfile_audio['sample_rate'] = 48000; + break; + + default: + $this->warning('unknown frequency: "'.$AudioCodecFrequency.'" ('.$this->TrimConvert($thisfile_asf_codeclistobject_codecentries_current['description']).')'); + break; + } + + if (!isset($thisfile_audio['channels'])) { + if (strstr($AudioCodecChannels, 'stereo')) { + $thisfile_audio['channels'] = 2; + } elseif (strstr($AudioCodecChannels, 'mono')) { + $thisfile_audio['channels'] = 1; + } + } + + } + } + } + break; + + case GETID3_ASF_Script_Command_Object: + // Script Command Object: (optional, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Script Command object - GETID3_ASF_Script_Command_Object + // Object Size QWORD 64 // size of Script Command object, including 44 bytes of Script Command Object header + // Reserved GUID 128 // hardcoded: 4B1ACBE3-100B-11D0-A39B-00A0C90348F6 + // Commands Count WORD 16 // number of Commands structures in the Script Commands Objects + // Command Types Count WORD 16 // number of Command Types structures in the Script Commands Objects + // Command Types array of: variable // + // * Command Type Name Length WORD 16 // number of Unicode characters for Command Type Name + // * Command Type Name WCHAR variable // array of Unicode characters - name of a type of command + // Commands array of: variable // + // * Presentation Time DWORD 32 // presentation time of that command, in milliseconds + // * Type Index WORD 16 // type of this command, as a zero-based index into the array of Command Types of this object + // * Command Name Length WORD 16 // number of Unicode characters for Command Name + // * Command Name WCHAR variable // array of Unicode characters - name of this command + + // shortcut + $thisfile_asf['script_command_object'] = array(); + $thisfile_asf_scriptcommandobject = &$thisfile_asf['script_command_object']; + + $thisfile_asf_scriptcommandobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_scriptcommandobject['objectid'] = $NextObjectGUID; + $thisfile_asf_scriptcommandobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_scriptcommandobject['objectsize'] = $NextObjectSize; + $thisfile_asf_scriptcommandobject['reserved'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $thisfile_asf_scriptcommandobject['reserved_guid'] = $this->BytestringToGUID($thisfile_asf_scriptcommandobject['reserved']); + if ($thisfile_asf_scriptcommandobject['reserved'] != $this->GUIDtoBytestring('4B1ACBE3-100B-11D0-A39B-00A0C90348F6')) { + $this->warning('script_command_object.reserved GUID {'.$this->BytestringToGUID($thisfile_asf_scriptcommandobject['reserved']).'} does not match expected "GETID3_ASF_Reserved_1" GUID {4B1ACBE3-100B-11D0-A39B-00A0C90348F6}'); + //return false; + break; + } + $thisfile_asf_scriptcommandobject['commands_count'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_scriptcommandobject['command_types_count'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + for ($CommandTypesCounter = 0; $CommandTypesCounter < $thisfile_asf_scriptcommandobject['command_types_count']; $CommandTypesCounter++) { + $CommandTypeNameLength = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)) * 2; // 2 bytes per character + $offset += 2; + $thisfile_asf_scriptcommandobject['command_types'][$CommandTypesCounter]['name'] = substr($ASFHeaderData, $offset, $CommandTypeNameLength); + $offset += $CommandTypeNameLength; + } + for ($CommandsCounter = 0; $CommandsCounter < $thisfile_asf_scriptcommandobject['commands_count']; $CommandsCounter++) { + $thisfile_asf_scriptcommandobject['commands'][$CommandsCounter]['presentation_time'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_scriptcommandobject['commands'][$CommandsCounter]['type_index'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + + $CommandTypeNameLength = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)) * 2; // 2 bytes per character + $offset += 2; + $thisfile_asf_scriptcommandobject['commands'][$CommandsCounter]['name'] = substr($ASFHeaderData, $offset, $CommandTypeNameLength); + $offset += $CommandTypeNameLength; + } + break; + + case GETID3_ASF_Marker_Object: + // Marker Object: (optional, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Marker object - GETID3_ASF_Marker_Object + // Object Size QWORD 64 // size of Marker object, including 48 bytes of Marker Object header + // Reserved GUID 128 // hardcoded: 4CFEDB20-75F6-11CF-9C0F-00A0C90349CB + // Markers Count DWORD 32 // number of Marker structures in Marker Object + // Reserved WORD 16 // hardcoded: 0x0000 + // Name Length WORD 16 // number of bytes in the Name field + // Name WCHAR variable // name of the Marker Object + // Markers array of: variable // + // * Offset QWORD 64 // byte offset into Data Object + // * Presentation Time QWORD 64 // in 100-nanosecond units + // * Entry Length WORD 16 // length in bytes of (Send Time + Flags + Marker Description Length + Marker Description + Padding) + // * Send Time DWORD 32 // in milliseconds + // * Flags DWORD 32 // hardcoded: 0x00000000 + // * Marker Description Length DWORD 32 // number of bytes in Marker Description field + // * Marker Description WCHAR variable // array of Unicode characters - description of marker entry + // * Padding BYTESTREAM variable // optional padding bytes + + // shortcut + $thisfile_asf['marker_object'] = array(); + $thisfile_asf_markerobject = &$thisfile_asf['marker_object']; + + $thisfile_asf_markerobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_markerobject['objectid'] = $NextObjectGUID; + $thisfile_asf_markerobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_markerobject['objectsize'] = $NextObjectSize; + $thisfile_asf_markerobject['reserved'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $thisfile_asf_markerobject['reserved_guid'] = $this->BytestringToGUID($thisfile_asf_markerobject['reserved']); + if ($thisfile_asf_markerobject['reserved'] != $this->GUIDtoBytestring('4CFEDB20-75F6-11CF-9C0F-00A0C90349CB')) { + $this->warning('marker_object.reserved GUID {'.$this->BytestringToGUID($thisfile_asf_markerobject['reserved_1']).'} does not match expected "GETID3_ASF_Reserved_1" GUID {4CFEDB20-75F6-11CF-9C0F-00A0C90349CB}'); + break; + } + $thisfile_asf_markerobject['markers_count'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_markerobject['reserved_2'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + if ($thisfile_asf_markerobject['reserved_2'] != 0) { + $this->warning('marker_object.reserved_2 ('.getid3_lib::PrintHexBytes($thisfile_asf_markerobject['reserved_2']).') does not match expected value of "0"'); + break; + } + $thisfile_asf_markerobject['name_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_markerobject['name'] = substr($ASFHeaderData, $offset, $thisfile_asf_markerobject['name_length']); + $offset += $thisfile_asf_markerobject['name_length']; + for ($MarkersCounter = 0; $MarkersCounter < $thisfile_asf_markerobject['markers_count']; $MarkersCounter++) { + $thisfile_asf_markerobject['markers'][$MarkersCounter]['offset'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $thisfile_asf_markerobject['markers'][$MarkersCounter]['presentation_time'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 8)); + $offset += 8; + $thisfile_asf_markerobject['markers'][$MarkersCounter]['entry_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_markerobject['markers'][$MarkersCounter]['send_time'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_markerobject['markers'][$MarkersCounter]['flags'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_markerobject['markers'][$MarkersCounter]['marker_description_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + $thisfile_asf_markerobject['markers'][$MarkersCounter]['marker_description'] = substr($ASFHeaderData, $offset, $thisfile_asf_markerobject['markers'][$MarkersCounter]['marker_description_length']); + $offset += $thisfile_asf_markerobject['markers'][$MarkersCounter]['marker_description_length']; + $PaddingLength = $thisfile_asf_markerobject['markers'][$MarkersCounter]['entry_length'] - 4 - 4 - 4 - $thisfile_asf_markerobject['markers'][$MarkersCounter]['marker_description_length']; + if ($PaddingLength > 0) { + $thisfile_asf_markerobject['markers'][$MarkersCounter]['padding'] = substr($ASFHeaderData, $offset, $PaddingLength); + $offset += $PaddingLength; + } + } + break; + + case GETID3_ASF_Bitrate_Mutual_Exclusion_Object: + // Bitrate Mutual Exclusion Object: (optional) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Bitrate Mutual Exclusion object - GETID3_ASF_Bitrate_Mutual_Exclusion_Object + // Object Size QWORD 64 // size of Bitrate Mutual Exclusion object, including 42 bytes of Bitrate Mutual Exclusion Object header + // Exlusion Type GUID 128 // nature of mutual exclusion relationship. one of: (GETID3_ASF_Mutex_Bitrate, GETID3_ASF_Mutex_Unknown) + // Stream Numbers Count WORD 16 // number of video streams + // Stream Numbers WORD variable // array of mutually exclusive video stream numbers. 1 <= valid <= 127 + + // shortcut + $thisfile_asf['bitrate_mutual_exclusion_object'] = array(); + $thisfile_asf_bitratemutualexclusionobject = &$thisfile_asf['bitrate_mutual_exclusion_object']; + + $thisfile_asf_bitratemutualexclusionobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_bitratemutualexclusionobject['objectid'] = $NextObjectGUID; + $thisfile_asf_bitratemutualexclusionobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_bitratemutualexclusionobject['objectsize'] = $NextObjectSize; + $thisfile_asf_bitratemutualexclusionobject['reserved'] = substr($ASFHeaderData, $offset, 16); + $thisfile_asf_bitratemutualexclusionobject['reserved_guid'] = $this->BytestringToGUID($thisfile_asf_bitratemutualexclusionobject['reserved']); + $offset += 16; + if (($thisfile_asf_bitratemutualexclusionobject['reserved'] != GETID3_ASF_Mutex_Bitrate) && ($thisfile_asf_bitratemutualexclusionobject['reserved'] != GETID3_ASF_Mutex_Unknown)) { + $this->warning('bitrate_mutual_exclusion_object.reserved GUID {'.$this->BytestringToGUID($thisfile_asf_bitratemutualexclusionobject['reserved']).'} does not match expected "GETID3_ASF_Mutex_Bitrate" GUID {'.$this->BytestringToGUID(GETID3_ASF_Mutex_Bitrate).'} or "GETID3_ASF_Mutex_Unknown" GUID {'.$this->BytestringToGUID(GETID3_ASF_Mutex_Unknown).'}'); + //return false; + break; + } + $thisfile_asf_bitratemutualexclusionobject['stream_numbers_count'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + for ($StreamNumberCounter = 0; $StreamNumberCounter < $thisfile_asf_bitratemutualexclusionobject['stream_numbers_count']; $StreamNumberCounter++) { + $thisfile_asf_bitratemutualexclusionobject['stream_numbers'][$StreamNumberCounter] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + } + break; + + case GETID3_ASF_Error_Correction_Object: + // Error Correction Object: (optional, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Error Correction object - GETID3_ASF_Error_Correction_Object + // Object Size QWORD 64 // size of Error Correction object, including 44 bytes of Error Correction Object header + // Error Correction Type GUID 128 // type of error correction. one of: (GETID3_ASF_No_Error_Correction, GETID3_ASF_Audio_Spread) + // Error Correction Data Length DWORD 32 // number of bytes in Error Correction Data field + // Error Correction Data BYTESTREAM variable // structure depends on value of Error Correction Type field + + // shortcut + $thisfile_asf['error_correction_object'] = array(); + $thisfile_asf_errorcorrectionobject = &$thisfile_asf['error_correction_object']; + + $thisfile_asf_errorcorrectionobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_errorcorrectionobject['objectid'] = $NextObjectGUID; + $thisfile_asf_errorcorrectionobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_errorcorrectionobject['objectsize'] = $NextObjectSize; + $thisfile_asf_errorcorrectionobject['error_correction_type'] = substr($ASFHeaderData, $offset, 16); + $offset += 16; + $thisfile_asf_errorcorrectionobject['error_correction_guid'] = $this->BytestringToGUID($thisfile_asf_errorcorrectionobject['error_correction_type']); + $thisfile_asf_errorcorrectionobject['error_correction_data_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + switch ($thisfile_asf_errorcorrectionobject['error_correction_type']) { + case GETID3_ASF_No_Error_Correction: + // should be no data, but just in case there is, skip to the end of the field + $offset += $thisfile_asf_errorcorrectionobject['error_correction_data_length']; + break; + + case GETID3_ASF_Audio_Spread: + // Field Name Field Type Size (bits) + // Span BYTE 8 // number of packets over which audio will be spread. + // Virtual Packet Length WORD 16 // size of largest audio payload found in audio stream + // Virtual Chunk Length WORD 16 // size of largest audio payload found in audio stream + // Silence Data Length WORD 16 // number of bytes in Silence Data field + // Silence Data BYTESTREAM variable // hardcoded: 0x00 * (Silence Data Length) bytes + + $thisfile_asf_errorcorrectionobject['span'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 1)); + $offset += 1; + $thisfile_asf_errorcorrectionobject['virtual_packet_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_errorcorrectionobject['virtual_chunk_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_errorcorrectionobject['silence_data_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_errorcorrectionobject['silence_data'] = substr($ASFHeaderData, $offset, $thisfile_asf_errorcorrectionobject['silence_data_length']); + $offset += $thisfile_asf_errorcorrectionobject['silence_data_length']; + break; + + default: + $this->warning('error_correction_object.error_correction_type GUID {'.$this->BytestringToGUID($thisfile_asf_errorcorrectionobject['reserved']).'} does not match expected "GETID3_ASF_No_Error_Correction" GUID {'.$this->BytestringToGUID(GETID3_ASF_No_Error_Correction).'} or "GETID3_ASF_Audio_Spread" GUID {'.$this->BytestringToGUID(GETID3_ASF_Audio_Spread).'}'); + //return false; + break; + } + + break; + + case GETID3_ASF_Content_Description_Object: + // Content Description Object: (optional, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Content Description object - GETID3_ASF_Content_Description_Object + // Object Size QWORD 64 // size of Content Description object, including 34 bytes of Content Description Object header + // Title Length WORD 16 // number of bytes in Title field + // Author Length WORD 16 // number of bytes in Author field + // Copyright Length WORD 16 // number of bytes in Copyright field + // Description Length WORD 16 // number of bytes in Description field + // Rating Length WORD 16 // number of bytes in Rating field + // Title WCHAR 16 // array of Unicode characters - Title + // Author WCHAR 16 // array of Unicode characters - Author + // Copyright WCHAR 16 // array of Unicode characters - Copyright + // Description WCHAR 16 // array of Unicode characters - Description + // Rating WCHAR 16 // array of Unicode characters - Rating + + // shortcut + $thisfile_asf['content_description_object'] = array(); + $thisfile_asf_contentdescriptionobject = &$thisfile_asf['content_description_object']; + + $thisfile_asf_contentdescriptionobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_contentdescriptionobject['objectid'] = $NextObjectGUID; + $thisfile_asf_contentdescriptionobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_contentdescriptionobject['objectsize'] = $NextObjectSize; + $thisfile_asf_contentdescriptionobject['title_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_contentdescriptionobject['author_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_contentdescriptionobject['copyright_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_contentdescriptionobject['description_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_contentdescriptionobject['rating_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_contentdescriptionobject['title'] = substr($ASFHeaderData, $offset, $thisfile_asf_contentdescriptionobject['title_length']); + $offset += $thisfile_asf_contentdescriptionobject['title_length']; + $thisfile_asf_contentdescriptionobject['author'] = substr($ASFHeaderData, $offset, $thisfile_asf_contentdescriptionobject['author_length']); + $offset += $thisfile_asf_contentdescriptionobject['author_length']; + $thisfile_asf_contentdescriptionobject['copyright'] = substr($ASFHeaderData, $offset, $thisfile_asf_contentdescriptionobject['copyright_length']); + $offset += $thisfile_asf_contentdescriptionobject['copyright_length']; + $thisfile_asf_contentdescriptionobject['description'] = substr($ASFHeaderData, $offset, $thisfile_asf_contentdescriptionobject['description_length']); + $offset += $thisfile_asf_contentdescriptionobject['description_length']; + $thisfile_asf_contentdescriptionobject['rating'] = substr($ASFHeaderData, $offset, $thisfile_asf_contentdescriptionobject['rating_length']); + $offset += $thisfile_asf_contentdescriptionobject['rating_length']; + + $ASFcommentKeysToCopy = array('title'=>'title', 'author'=>'artist', 'copyright'=>'copyright', 'description'=>'comment', 'rating'=>'rating'); + foreach ($ASFcommentKeysToCopy as $keytocopyfrom => $keytocopyto) { + if (!empty($thisfile_asf_contentdescriptionobject[$keytocopyfrom])) { + $thisfile_asf_comments[$keytocopyto][] = $this->TrimTerm($thisfile_asf_contentdescriptionobject[$keytocopyfrom]); + } + } + break; + + case GETID3_ASF_Extended_Content_Description_Object: + // Extended Content Description Object: (optional, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Extended Content Description object - GETID3_ASF_Extended_Content_Description_Object + // Object Size QWORD 64 // size of ExtendedContent Description object, including 26 bytes of Extended Content Description Object header + // Content Descriptors Count WORD 16 // number of entries in Content Descriptors list + // Content Descriptors array of: variable // + // * Descriptor Name Length WORD 16 // size in bytes of Descriptor Name field + // * Descriptor Name WCHAR variable // array of Unicode characters - Descriptor Name + // * Descriptor Value Data Type WORD 16 // Lookup array: + // 0x0000 = Unicode String (variable length) + // 0x0001 = BYTE array (variable length) + // 0x0002 = BOOL (DWORD, 32 bits) + // 0x0003 = DWORD (DWORD, 32 bits) + // 0x0004 = QWORD (QWORD, 64 bits) + // 0x0005 = WORD (WORD, 16 bits) + // * Descriptor Value Length WORD 16 // number of bytes stored in Descriptor Value field + // * Descriptor Value variable variable // value for Content Descriptor + + // shortcut + $thisfile_asf['extended_content_description_object'] = array(); + $thisfile_asf_extendedcontentdescriptionobject = &$thisfile_asf['extended_content_description_object']; + + $thisfile_asf_extendedcontentdescriptionobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_extendedcontentdescriptionobject['objectid'] = $NextObjectGUID; + $thisfile_asf_extendedcontentdescriptionobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_extendedcontentdescriptionobject['objectsize'] = $NextObjectSize; + $thisfile_asf_extendedcontentdescriptionobject['content_descriptors_count'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + for ($ExtendedContentDescriptorsCounter = 0; $ExtendedContentDescriptorsCounter < $thisfile_asf_extendedcontentdescriptionobject['content_descriptors_count']; $ExtendedContentDescriptorsCounter++) { + // shortcut + $thisfile_asf_extendedcontentdescriptionobject['content_descriptors'][$ExtendedContentDescriptorsCounter] = array(); + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current = &$thisfile_asf_extendedcontentdescriptionobject['content_descriptors'][$ExtendedContentDescriptorsCounter]; + + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['base_offset'] = $offset + 30; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['name_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['name'] = substr($ASFHeaderData, $offset, $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['name_length']); + $offset += $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['name_length']; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_type'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_length'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'] = substr($ASFHeaderData, $offset, $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_length']); + $offset += $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_length']; + switch ($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_type']) { + case 0x0000: // Unicode string + break; + + case 0x0001: // BYTE array + // do nothing + break; + + case 0x0002: // BOOL + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'] = (bool) getid3_lib::LittleEndian2Int($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value']); + break; + + case 0x0003: // DWORD + case 0x0004: // QWORD + case 0x0005: // WORD + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'] = getid3_lib::LittleEndian2Int($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value']); + break; + + default: + $this->warning('extended_content_description.content_descriptors.'.$ExtendedContentDescriptorsCounter.'.value_type is invalid ('.$thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_type'].')'); + //return false; + break; + } + switch ($this->TrimConvert(strtolower($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['name']))) { + + case 'wm/albumartist': + case 'artist': + // Note: not 'artist', that comes from 'author' tag + $thisfile_asf_comments['albumartist'] = array($this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + break; + + case 'wm/albumtitle': + case 'album': + $thisfile_asf_comments['album'] = array($this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + break; + + case 'wm/genre': + case 'genre': + $thisfile_asf_comments['genre'] = array($this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + break; + + case 'wm/partofset': + $thisfile_asf_comments['partofset'] = array($this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + break; + + case 'wm/tracknumber': + case 'tracknumber': + // be careful casting to int: casting unicode strings to int gives unexpected results (stops parsing at first non-numeric character) + $thisfile_asf_comments['track_number'] = array($this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + foreach ($thisfile_asf_comments['track_number'] as $key => $value) { + if (preg_match('/^[0-9\x00]+$/', $value)) { + $thisfile_asf_comments['track_number'][$key] = intval(str_replace("\x00", '', $value)); + } + } + break; + + case 'wm/track': + if (empty($thisfile_asf_comments['track_number'])) { + $thisfile_asf_comments['track_number'] = array(1 + (int) $this->TrimConvert($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + } + break; + + case 'wm/year': + case 'year': + case 'date': + $thisfile_asf_comments['year'] = array( $this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + break; + + case 'wm/lyrics': + case 'lyrics': + $thisfile_asf_comments['lyrics'] = array($this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + break; + + case 'isvbr': + if ($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value']) { + $thisfile_audio['bitrate_mode'] = 'vbr'; + $thisfile_video['bitrate_mode'] = 'vbr'; + } + break; + + case 'id3': + $this->getid3->include_module('tag.id3v2'); + + $getid3_id3v2 = new getid3_id3v2($this->getid3); + $getid3_id3v2->AnalyzeString($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value']); + unset($getid3_id3v2); + + if ($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_length'] > 1024) { + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'] = ''; + } + break; + + case 'wm/encodingtime': + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['encoding_time_unix'] = $this->FILETIMEtoUNIXtime($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value']); + $thisfile_asf_comments['encoding_time_unix'] = array($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['encoding_time_unix']); + break; + + case 'wm/picture': + $WMpicture = $this->ASF_WMpicture($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value']); + foreach ($WMpicture as $key => $value) { + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current[$key] = $value; + } + unset($WMpicture); +/* + $wm_picture_offset = 0; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_type_id'] = getid3_lib::LittleEndian2Int(substr($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'], $wm_picture_offset, 1)); + $wm_picture_offset += 1; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_type'] = self::WMpictureTypeLookup($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_type_id']); + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_size'] = getid3_lib::LittleEndian2Int(substr($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'], $wm_picture_offset, 4)); + $wm_picture_offset += 4; + + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_mime'] = ''; + do { + $next_byte_pair = substr($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'], $wm_picture_offset, 2); + $wm_picture_offset += 2; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_mime'] .= $next_byte_pair; + } while ($next_byte_pair !== "\x00\x00"); + + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_description'] = ''; + do { + $next_byte_pair = substr($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'], $wm_picture_offset, 2); + $wm_picture_offset += 2; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_description'] .= $next_byte_pair; + } while ($next_byte_pair !== "\x00\x00"); + + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['dataoffset'] = $wm_picture_offset; + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['data'] = substr($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'], $wm_picture_offset); + unset($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value']); + + $imageinfo = array(); + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_mime'] = ''; + $imagechunkcheck = getid3_lib::GetDataImageSize($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['data'], $imageinfo); + unset($imageinfo); + if (!empty($imagechunkcheck)) { + $thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_mime'] = image_type_to_mime_type($imagechunkcheck[2]); + } + if (!isset($thisfile_asf_comments['picture'])) { + $thisfile_asf_comments['picture'] = array(); + } + $thisfile_asf_comments['picture'][] = array('data'=>$thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['data'], 'image_mime'=>$thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['image_mime']); +*/ + break; + + default: + switch ($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value_type']) { + case 0: // Unicode string + if (substr($this->TrimConvert($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['name']), 0, 3) == 'WM/') { + $thisfile_asf_comments[str_replace('wm/', '', strtolower($this->TrimConvert($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['name'])))] = array($this->TrimTerm($thisfile_asf_extendedcontentdescriptionobject_contentdescriptor_current['value'])); + } + break; + + case 1: + break; + } + break; + } + + } + break; + + case GETID3_ASF_Stream_Bitrate_Properties_Object: + // Stream Bitrate Properties Object: (optional, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Stream Bitrate Properties object - GETID3_ASF_Stream_Bitrate_Properties_Object + // Object Size QWORD 64 // size of Extended Content Description object, including 26 bytes of Stream Bitrate Properties Object header + // Bitrate Records Count WORD 16 // number of records in Bitrate Records + // Bitrate Records array of: variable // + // * Flags WORD 16 // + // * * Stream Number bits 7 (0x007F) // number of this stream + // * * Reserved bits 9 (0xFF80) // hardcoded: 0 + // * Average Bitrate DWORD 32 // in bits per second + + // shortcut + $thisfile_asf['stream_bitrate_properties_object'] = array(); + $thisfile_asf_streambitratepropertiesobject = &$thisfile_asf['stream_bitrate_properties_object']; + + $thisfile_asf_streambitratepropertiesobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_streambitratepropertiesobject['objectid'] = $NextObjectGUID; + $thisfile_asf_streambitratepropertiesobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_streambitratepropertiesobject['objectsize'] = $NextObjectSize; + $thisfile_asf_streambitratepropertiesobject['bitrate_records_count'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + for ($BitrateRecordsCounter = 0; $BitrateRecordsCounter < $thisfile_asf_streambitratepropertiesobject['bitrate_records_count']; $BitrateRecordsCounter++) { + $thisfile_asf_streambitratepropertiesobject['bitrate_records'][$BitrateRecordsCounter]['flags_raw'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 2)); + $offset += 2; + $thisfile_asf_streambitratepropertiesobject['bitrate_records'][$BitrateRecordsCounter]['flags']['stream_number'] = $thisfile_asf_streambitratepropertiesobject['bitrate_records'][$BitrateRecordsCounter]['flags_raw'] & 0x007F; + $thisfile_asf_streambitratepropertiesobject['bitrate_records'][$BitrateRecordsCounter]['bitrate'] = getid3_lib::LittleEndian2Int(substr($ASFHeaderData, $offset, 4)); + $offset += 4; + } + break; + + case GETID3_ASF_Padding_Object: + // Padding Object: (optional) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Padding object - GETID3_ASF_Padding_Object + // Object Size QWORD 64 // size of Padding object, including 24 bytes of ASF Padding Object header + // Padding Data BYTESTREAM variable // ignore + + // shortcut + $thisfile_asf['padding_object'] = array(); + $thisfile_asf_paddingobject = &$thisfile_asf['padding_object']; + + $thisfile_asf_paddingobject['offset'] = $NextObjectOffset + $offset; + $thisfile_asf_paddingobject['objectid'] = $NextObjectGUID; + $thisfile_asf_paddingobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_paddingobject['objectsize'] = $NextObjectSize; + $thisfile_asf_paddingobject['padding_length'] = $thisfile_asf_paddingobject['objectsize'] - 16 - 8; + $thisfile_asf_paddingobject['padding'] = substr($ASFHeaderData, $offset, $thisfile_asf_paddingobject['padding_length']); + $offset += ($NextObjectSize - 16 - 8); + break; + + case GETID3_ASF_Extended_Content_Encryption_Object: + case GETID3_ASF_Content_Encryption_Object: + // WMA DRM - just ignore + $offset += ($NextObjectSize - 16 - 8); + break; + + default: + // Implementations shall ignore any standard or non-standard object that they do not know how to handle. + if ($this->GUIDname($NextObjectGUIDtext)) { + $this->warning('unhandled GUID "'.$this->GUIDname($NextObjectGUIDtext).'" {'.$NextObjectGUIDtext.'} in ASF header at offset '.($offset - 16 - 8)); + } else { + $this->warning('unknown GUID {'.$NextObjectGUIDtext.'} in ASF header at offset '.($offset - 16 - 8)); + } + $offset += ($NextObjectSize - 16 - 8); + break; + } + } + if (isset($thisfile_asf_streambitratepropertiesobject['bitrate_records_count'])) { + $ASFbitrateAudio = 0; + $ASFbitrateVideo = 0; + for ($BitrateRecordsCounter = 0; $BitrateRecordsCounter < $thisfile_asf_streambitratepropertiesobject['bitrate_records_count']; $BitrateRecordsCounter++) { + if (isset($thisfile_asf_codeclistobject['codec_entries'][$BitrateRecordsCounter])) { + switch ($thisfile_asf_codeclistobject['codec_entries'][$BitrateRecordsCounter]['type_raw']) { + case 1: + $ASFbitrateVideo += $thisfile_asf_streambitratepropertiesobject['bitrate_records'][$BitrateRecordsCounter]['bitrate']; + break; + + case 2: + $ASFbitrateAudio += $thisfile_asf_streambitratepropertiesobject['bitrate_records'][$BitrateRecordsCounter]['bitrate']; + break; + + default: + // do nothing + break; + } + } + } + if ($ASFbitrateAudio > 0) { + $thisfile_audio['bitrate'] = $ASFbitrateAudio; + } + if ($ASFbitrateVideo > 0) { + $thisfile_video['bitrate'] = $ASFbitrateVideo; + } + } + if (isset($thisfile_asf['stream_properties_object']) && is_array($thisfile_asf['stream_properties_object'])) { + + $thisfile_audio['bitrate'] = 0; + $thisfile_video['bitrate'] = 0; + + foreach ($thisfile_asf['stream_properties_object'] as $streamnumber => $streamdata) { + + switch ($streamdata['stream_type']) { + case GETID3_ASF_Audio_Media: + // Field Name Field Type Size (bits) + // Codec ID / Format Tag WORD 16 // unique ID of audio codec - defined as wFormatTag field of WAVEFORMATEX structure + // Number of Channels WORD 16 // number of channels of audio - defined as nChannels field of WAVEFORMATEX structure + // Samples Per Second DWORD 32 // in Hertz - defined as nSamplesPerSec field of WAVEFORMATEX structure + // Average number of Bytes/sec DWORD 32 // bytes/sec of audio stream - defined as nAvgBytesPerSec field of WAVEFORMATEX structure + // Block Alignment WORD 16 // block size in bytes of audio codec - defined as nBlockAlign field of WAVEFORMATEX structure + // Bits per sample WORD 16 // bits per sample of mono data. set to zero for variable bitrate codecs. defined as wBitsPerSample field of WAVEFORMATEX structure + // Codec Specific Data Size WORD 16 // size in bytes of Codec Specific Data buffer - defined as cbSize field of WAVEFORMATEX structure + // Codec Specific Data BYTESTREAM variable // array of codec-specific data bytes + + // shortcut + $thisfile_asf['audio_media'][$streamnumber] = array(); + $thisfile_asf_audiomedia_currentstream = &$thisfile_asf['audio_media'][$streamnumber]; + + $audiomediaoffset = 0; + + $thisfile_asf_audiomedia_currentstream = getid3_riff::parseWAVEFORMATex(substr($streamdata['type_specific_data'], $audiomediaoffset, 16)); + $audiomediaoffset += 16; + + $thisfile_audio['lossless'] = false; + switch ($thisfile_asf_audiomedia_currentstream['raw']['wFormatTag']) { + case 0x0001: // PCM + case 0x0163: // WMA9 Lossless + $thisfile_audio['lossless'] = true; + break; + } + + if (!empty($thisfile_asf['stream_bitrate_properties_object']['bitrate_records'])) { + foreach ($thisfile_asf['stream_bitrate_properties_object']['bitrate_records'] as $dummy => $dataarray) { + if (isset($dataarray['flags']['stream_number']) && ($dataarray['flags']['stream_number'] == $streamnumber)) { + $thisfile_asf_audiomedia_currentstream['bitrate'] = $dataarray['bitrate']; + $thisfile_audio['bitrate'] += $dataarray['bitrate']; + break; + } + } + } else { + if (!empty($thisfile_asf_audiomedia_currentstream['bytes_sec'])) { + $thisfile_audio['bitrate'] += $thisfile_asf_audiomedia_currentstream['bytes_sec'] * 8; + } elseif (!empty($thisfile_asf_audiomedia_currentstream['bitrate'])) { + $thisfile_audio['bitrate'] += $thisfile_asf_audiomedia_currentstream['bitrate']; + } + } + $thisfile_audio['streams'][$streamnumber] = $thisfile_asf_audiomedia_currentstream; + $thisfile_audio['streams'][$streamnumber]['wformattag'] = $thisfile_asf_audiomedia_currentstream['raw']['wFormatTag']; + $thisfile_audio['streams'][$streamnumber]['lossless'] = $thisfile_audio['lossless']; + $thisfile_audio['streams'][$streamnumber]['bitrate'] = $thisfile_audio['bitrate']; + $thisfile_audio['streams'][$streamnumber]['dataformat'] = 'wma'; + unset($thisfile_audio['streams'][$streamnumber]['raw']); + + $thisfile_asf_audiomedia_currentstream['codec_data_size'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $audiomediaoffset, 2)); + $audiomediaoffset += 2; + $thisfile_asf_audiomedia_currentstream['codec_data'] = substr($streamdata['type_specific_data'], $audiomediaoffset, $thisfile_asf_audiomedia_currentstream['codec_data_size']); + $audiomediaoffset += $thisfile_asf_audiomedia_currentstream['codec_data_size']; + + break; + + case GETID3_ASF_Video_Media: + // Field Name Field Type Size (bits) + // Encoded Image Width DWORD 32 // width of image in pixels + // Encoded Image Height DWORD 32 // height of image in pixels + // Reserved Flags BYTE 8 // hardcoded: 0x02 + // Format Data Size WORD 16 // size of Format Data field in bytes + // Format Data array of: variable // + // * Format Data Size DWORD 32 // number of bytes in Format Data field, in bytes - defined as biSize field of BITMAPINFOHEADER structure + // * Image Width LONG 32 // width of encoded image in pixels - defined as biWidth field of BITMAPINFOHEADER structure + // * Image Height LONG 32 // height of encoded image in pixels - defined as biHeight field of BITMAPINFOHEADER structure + // * Reserved WORD 16 // hardcoded: 0x0001 - defined as biPlanes field of BITMAPINFOHEADER structure + // * Bits Per Pixel Count WORD 16 // bits per pixel - defined as biBitCount field of BITMAPINFOHEADER structure + // * Compression ID FOURCC 32 // fourcc of video codec - defined as biCompression field of BITMAPINFOHEADER structure + // * Image Size DWORD 32 // image size in bytes - defined as biSizeImage field of BITMAPINFOHEADER structure + // * Horizontal Pixels / Meter DWORD 32 // horizontal resolution of target device in pixels per meter - defined as biXPelsPerMeter field of BITMAPINFOHEADER structure + // * Vertical Pixels / Meter DWORD 32 // vertical resolution of target device in pixels per meter - defined as biYPelsPerMeter field of BITMAPINFOHEADER structure + // * Colors Used Count DWORD 32 // number of color indexes in the color table that are actually used - defined as biClrUsed field of BITMAPINFOHEADER structure + // * Important Colors Count DWORD 32 // number of color index required for displaying bitmap. if zero, all colors are required. defined as biClrImportant field of BITMAPINFOHEADER structure + // * Codec Specific Data BYTESTREAM variable // array of codec-specific data bytes + + // shortcut + $thisfile_asf['video_media'][$streamnumber] = array(); + $thisfile_asf_videomedia_currentstream = &$thisfile_asf['video_media'][$streamnumber]; + + $videomediaoffset = 0; + $thisfile_asf_videomedia_currentstream['image_width'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['image_height'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['flags'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 1)); + $videomediaoffset += 1; + $thisfile_asf_videomedia_currentstream['format_data_size'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 2)); + $videomediaoffset += 2; + $thisfile_asf_videomedia_currentstream['format_data']['format_data_size'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['image_width'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['image_height'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['reserved'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 2)); + $videomediaoffset += 2; + $thisfile_asf_videomedia_currentstream['format_data']['bits_per_pixel'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 2)); + $videomediaoffset += 2; + $thisfile_asf_videomedia_currentstream['format_data']['codec_fourcc'] = substr($streamdata['type_specific_data'], $videomediaoffset, 4); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['image_size'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['horizontal_pels'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['vertical_pels'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['colors_used'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['colors_important'] = getid3_lib::LittleEndian2Int(substr($streamdata['type_specific_data'], $videomediaoffset, 4)); + $videomediaoffset += 4; + $thisfile_asf_videomedia_currentstream['format_data']['codec_data'] = substr($streamdata['type_specific_data'], $videomediaoffset); + + if (!empty($thisfile_asf['stream_bitrate_properties_object']['bitrate_records'])) { + foreach ($thisfile_asf['stream_bitrate_properties_object']['bitrate_records'] as $dummy => $dataarray) { + if (isset($dataarray['flags']['stream_number']) && ($dataarray['flags']['stream_number'] == $streamnumber)) { + $thisfile_asf_videomedia_currentstream['bitrate'] = $dataarray['bitrate']; + $thisfile_video['streams'][$streamnumber]['bitrate'] = $dataarray['bitrate']; + $thisfile_video['bitrate'] += $dataarray['bitrate']; + break; + } + } + } + + $thisfile_asf_videomedia_currentstream['format_data']['codec'] = getid3_riff::fourccLookup($thisfile_asf_videomedia_currentstream['format_data']['codec_fourcc']); + + $thisfile_video['streams'][$streamnumber]['fourcc'] = $thisfile_asf_videomedia_currentstream['format_data']['codec_fourcc']; + $thisfile_video['streams'][$streamnumber]['codec'] = $thisfile_asf_videomedia_currentstream['format_data']['codec']; + $thisfile_video['streams'][$streamnumber]['resolution_x'] = $thisfile_asf_videomedia_currentstream['image_width']; + $thisfile_video['streams'][$streamnumber]['resolution_y'] = $thisfile_asf_videomedia_currentstream['image_height']; + $thisfile_video['streams'][$streamnumber]['bits_per_sample'] = $thisfile_asf_videomedia_currentstream['format_data']['bits_per_pixel']; + break; + + default: + break; + } + } + } + + while ($this->ftell() < $info['avdataend']) { + $NextObjectDataHeader = $this->fread(24); + $offset = 0; + $NextObjectGUID = substr($NextObjectDataHeader, 0, 16); + $offset += 16; + $NextObjectGUIDtext = $this->BytestringToGUID($NextObjectGUID); + $NextObjectSize = getid3_lib::LittleEndian2Int(substr($NextObjectDataHeader, $offset, 8)); + $offset += 8; + + switch ($NextObjectGUID) { + case GETID3_ASF_Data_Object: + // Data Object: (mandatory, one only) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Data object - GETID3_ASF_Data_Object + // Object Size QWORD 64 // size of Data object, including 50 bytes of Data Object header. may be 0 if FilePropertiesObject.BroadcastFlag == 1 + // File ID GUID 128 // unique identifier. identical to File ID field in Header Object + // Total Data Packets QWORD 64 // number of Data Packet entries in Data Object. invalid if FilePropertiesObject.BroadcastFlag == 1 + // Reserved WORD 16 // hardcoded: 0x0101 + + // shortcut + $thisfile_asf['data_object'] = array(); + $thisfile_asf_dataobject = &$thisfile_asf['data_object']; + + $DataObjectData = $NextObjectDataHeader.$this->fread(50 - 24); + $offset = 24; + + $thisfile_asf_dataobject['objectid'] = $NextObjectGUID; + $thisfile_asf_dataobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_dataobject['objectsize'] = $NextObjectSize; + + $thisfile_asf_dataobject['fileid'] = substr($DataObjectData, $offset, 16); + $offset += 16; + $thisfile_asf_dataobject['fileid_guid'] = $this->BytestringToGUID($thisfile_asf_dataobject['fileid']); + $thisfile_asf_dataobject['total_data_packets'] = getid3_lib::LittleEndian2Int(substr($DataObjectData, $offset, 8)); + $offset += 8; + $thisfile_asf_dataobject['reserved'] = getid3_lib::LittleEndian2Int(substr($DataObjectData, $offset, 2)); + $offset += 2; + if ($thisfile_asf_dataobject['reserved'] != 0x0101) { + $this->warning('data_object.reserved ('.getid3_lib::PrintHexBytes($thisfile_asf_dataobject['reserved']).') does not match expected value of "0x0101"'); + //return false; + break; + } + + // Data Packets array of: variable // + // * Error Correction Flags BYTE 8 // + // * * Error Correction Data Length bits 4 // if Error Correction Length Type == 00, size of Error Correction Data in bytes, else hardcoded: 0000 + // * * Opaque Data Present bits 1 // + // * * Error Correction Length Type bits 2 // number of bits for size of the error correction data. hardcoded: 00 + // * * Error Correction Present bits 1 // If set, use Opaque Data Packet structure, else use Payload structure + // * Error Correction Data + + $info['avdataoffset'] = $this->ftell(); + $this->fseek(($thisfile_asf_dataobject['objectsize'] - 50), SEEK_CUR); // skip actual audio/video data + $info['avdataend'] = $this->ftell(); + break; + + case GETID3_ASF_Simple_Index_Object: + // Simple Index Object: (optional, recommended, one per video stream) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for Simple Index object - GETID3_ASF_Data_Object + // Object Size QWORD 64 // size of Simple Index object, including 56 bytes of Simple Index Object header + // File ID GUID 128 // unique identifier. may be zero or identical to File ID field in Data Object and Header Object + // Index Entry Time Interval QWORD 64 // interval between index entries in 100-nanosecond units + // Maximum Packet Count DWORD 32 // maximum packet count for all index entries + // Index Entries Count DWORD 32 // number of Index Entries structures + // Index Entries array of: variable // + // * Packet Number DWORD 32 // number of the Data Packet associated with this index entry + // * Packet Count WORD 16 // number of Data Packets to sent at this index entry + + // shortcut + $thisfile_asf['simple_index_object'] = array(); + $thisfile_asf_simpleindexobject = &$thisfile_asf['simple_index_object']; + + $SimpleIndexObjectData = $NextObjectDataHeader.$this->fread(56 - 24); + $offset = 24; + + $thisfile_asf_simpleindexobject['objectid'] = $NextObjectGUID; + $thisfile_asf_simpleindexobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_simpleindexobject['objectsize'] = $NextObjectSize; + + $thisfile_asf_simpleindexobject['fileid'] = substr($SimpleIndexObjectData, $offset, 16); + $offset += 16; + $thisfile_asf_simpleindexobject['fileid_guid'] = $this->BytestringToGUID($thisfile_asf_simpleindexobject['fileid']); + $thisfile_asf_simpleindexobject['index_entry_time_interval'] = getid3_lib::LittleEndian2Int(substr($SimpleIndexObjectData, $offset, 8)); + $offset += 8; + $thisfile_asf_simpleindexobject['maximum_packet_count'] = getid3_lib::LittleEndian2Int(substr($SimpleIndexObjectData, $offset, 4)); + $offset += 4; + $thisfile_asf_simpleindexobject['index_entries_count'] = getid3_lib::LittleEndian2Int(substr($SimpleIndexObjectData, $offset, 4)); + $offset += 4; + + $IndexEntriesData = $SimpleIndexObjectData.$this->fread(6 * $thisfile_asf_simpleindexobject['index_entries_count']); + for ($IndexEntriesCounter = 0; $IndexEntriesCounter < $thisfile_asf_simpleindexobject['index_entries_count']; $IndexEntriesCounter++) { + $thisfile_asf_simpleindexobject['index_entries'][$IndexEntriesCounter]['packet_number'] = getid3_lib::LittleEndian2Int(substr($IndexEntriesData, $offset, 4)); + $offset += 4; + $thisfile_asf_simpleindexobject['index_entries'][$IndexEntriesCounter]['packet_count'] = getid3_lib::LittleEndian2Int(substr($IndexEntriesData, $offset, 4)); + $offset += 2; + } + + break; + + case GETID3_ASF_Index_Object: + // 6.2 ASF top-level Index Object (optional but recommended when appropriate, 0 or 1) + // Field Name Field Type Size (bits) + // Object ID GUID 128 // GUID for the Index Object - GETID3_ASF_Index_Object + // Object Size QWORD 64 // Specifies the size, in bytes, of the Index Object, including at least 34 bytes of Index Object header + // Index Entry Time Interval DWORD 32 // Specifies the time interval between each index entry in ms. + // Index Specifiers Count WORD 16 // Specifies the number of Index Specifiers structures in this Index Object. + // Index Blocks Count DWORD 32 // Specifies the number of Index Blocks structures in this Index Object. + + // Index Entry Time Interval DWORD 32 // Specifies the time interval between index entries in milliseconds. This value cannot be 0. + // Index Specifiers Count WORD 16 // Specifies the number of entries in the Index Specifiers list. Valid values are 1 and greater. + // Index Specifiers array of: varies // + // * Stream Number WORD 16 // Specifies the stream number that the Index Specifiers refer to. Valid values are between 1 and 127. + // * Index Type WORD 16 // Specifies Index Type values as follows: + // 1 = Nearest Past Data Packet - indexes point to the data packet whose presentation time is closest to the index entry time. + // 2 = Nearest Past Media Object - indexes point to the closest data packet containing an entire object or first fragment of an object. + // 3 = Nearest Past Cleanpoint. - indexes point to the closest data packet containing an entire object (or first fragment of an object) that has the Cleanpoint Flag set. + // Nearest Past Cleanpoint is the most common type of index. + // Index Entry Count DWORD 32 // Specifies the number of Index Entries in the block. + // * Block Positions QWORD varies // Specifies a list of byte offsets of the beginnings of the blocks relative to the beginning of the first Data Packet (i.e., the beginning of the Data Object + 50 bytes). The number of entries in this list is specified by the value of the Index Specifiers Count field. The order of those byte offsets is tied to the order in which Index Specifiers are listed. + // * Index Entries array of: varies // + // * * Offsets DWORD varies // An offset value of 0xffffffff indicates an invalid offset value + + // shortcut + $thisfile_asf['asf_index_object'] = array(); + $thisfile_asf_asfindexobject = &$thisfile_asf['asf_index_object']; + + $ASFIndexObjectData = $NextObjectDataHeader.$this->fread(34 - 24); + $offset = 24; + + $thisfile_asf_asfindexobject['objectid'] = $NextObjectGUID; + $thisfile_asf_asfindexobject['objectid_guid'] = $NextObjectGUIDtext; + $thisfile_asf_asfindexobject['objectsize'] = $NextObjectSize; + + $thisfile_asf_asfindexobject['entry_time_interval'] = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 4)); + $offset += 4; + $thisfile_asf_asfindexobject['index_specifiers_count'] = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 2)); + $offset += 2; + $thisfile_asf_asfindexobject['index_blocks_count'] = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 4)); + $offset += 4; + + $ASFIndexObjectData .= $this->fread(4 * $thisfile_asf_asfindexobject['index_specifiers_count']); + for ($IndexSpecifiersCounter = 0; $IndexSpecifiersCounter < $thisfile_asf_asfindexobject['index_specifiers_count']; $IndexSpecifiersCounter++) { + $IndexSpecifierStreamNumber = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 2)); + $offset += 2; + $thisfile_asf_asfindexobject['index_specifiers'][$IndexSpecifiersCounter]['stream_number'] = $IndexSpecifierStreamNumber; + $thisfile_asf_asfindexobject['index_specifiers'][$IndexSpecifiersCounter]['index_type'] = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 2)); + $offset += 2; + $thisfile_asf_asfindexobject['index_specifiers'][$IndexSpecifiersCounter]['index_type_text'] = $this->ASFIndexObjectIndexTypeLookup($thisfile_asf_asfindexobject['index_specifiers'][$IndexSpecifiersCounter]['index_type']); + } + + $ASFIndexObjectData .= $this->fread(4); + $thisfile_asf_asfindexobject['index_entry_count'] = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 4)); + $offset += 4; + + $ASFIndexObjectData .= $this->fread(8 * $thisfile_asf_asfindexobject['index_specifiers_count']); + for ($IndexSpecifiersCounter = 0; $IndexSpecifiersCounter < $thisfile_asf_asfindexobject['index_specifiers_count']; $IndexSpecifiersCounter++) { + $thisfile_asf_asfindexobject['block_positions'][$IndexSpecifiersCounter] = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 8)); + $offset += 8; + } + + $ASFIndexObjectData .= $this->fread(4 * $thisfile_asf_asfindexobject['index_specifiers_count'] * $thisfile_asf_asfindexobject['index_entry_count']); + for ($IndexEntryCounter = 0; $IndexEntryCounter < $thisfile_asf_asfindexobject['index_entry_count']; $IndexEntryCounter++) { + for ($IndexSpecifiersCounter = 0; $IndexSpecifiersCounter < $thisfile_asf_asfindexobject['index_specifiers_count']; $IndexSpecifiersCounter++) { + $thisfile_asf_asfindexobject['offsets'][$IndexSpecifiersCounter][$IndexEntryCounter] = getid3_lib::LittleEndian2Int(substr($ASFIndexObjectData, $offset, 4)); + $offset += 4; + } + } + break; + + + default: + // Implementations shall ignore any standard or non-standard object that they do not know how to handle. + if ($this->GUIDname($NextObjectGUIDtext)) { + $this->warning('unhandled GUID "'.$this->GUIDname($NextObjectGUIDtext).'" {'.$NextObjectGUIDtext.'} in ASF body at offset '.($offset - 16 - 8)); + } else { + $this->warning('unknown GUID {'.$NextObjectGUIDtext.'} in ASF body at offset '.($this->ftell() - 16 - 8)); + } + $this->fseek(($NextObjectSize - 16 - 8), SEEK_CUR); + break; + } + } + + if (isset($thisfile_asf_codeclistobject['codec_entries']) && is_array($thisfile_asf_codeclistobject['codec_entries'])) { + foreach ($thisfile_asf_codeclistobject['codec_entries'] as $streamnumber => $streamdata) { + switch ($streamdata['information']) { + case 'WMV1': + case 'WMV2': + case 'WMV3': + case 'MSS1': + case 'MSS2': + case 'WMVA': + case 'WVC1': + case 'WMVP': + case 'WVP2': + $thisfile_video['dataformat'] = 'wmv'; + $info['mime_type'] = 'video/x-ms-wmv'; + break; + + case 'MP42': + case 'MP43': + case 'MP4S': + case 'mp4s': + $thisfile_video['dataformat'] = 'asf'; + $info['mime_type'] = 'video/x-ms-asf'; + break; + + default: + switch ($streamdata['type_raw']) { + case 1: + if (strstr($this->TrimConvert($streamdata['name']), 'Windows Media')) { + $thisfile_video['dataformat'] = 'wmv'; + if ($info['mime_type'] == 'video/x-ms-asf') { + $info['mime_type'] = 'video/x-ms-wmv'; + } + } + break; + + case 2: + if (strstr($this->TrimConvert($streamdata['name']), 'Windows Media')) { + $thisfile_audio['dataformat'] = 'wma'; + if ($info['mime_type'] == 'video/x-ms-asf') { + $info['mime_type'] = 'audio/x-ms-wma'; + } + } + break; + + } + break; + } + } + } + + switch (isset($thisfile_audio['codec']) ? $thisfile_audio['codec'] : '') { + case 'MPEG Layer-3': + $thisfile_audio['dataformat'] = 'mp3'; + break; + + default: + break; + } + + if (isset($thisfile_asf_codeclistobject['codec_entries'])) { + foreach ($thisfile_asf_codeclistobject['codec_entries'] as $streamnumber => $streamdata) { + switch ($streamdata['type_raw']) { + + case 1: // video + $thisfile_video['encoder'] = $this->TrimConvert($thisfile_asf_codeclistobject['codec_entries'][$streamnumber]['name']); + break; + + case 2: // audio + $thisfile_audio['encoder'] = $this->TrimConvert($thisfile_asf_codeclistobject['codec_entries'][$streamnumber]['name']); + + // AH 2003-10-01 + $thisfile_audio['encoder_options'] = $this->TrimConvert($thisfile_asf_codeclistobject['codec_entries'][0]['description']); + + $thisfile_audio['codec'] = $thisfile_audio['encoder']; + break; + + default: + $this->warning('Unknown streamtype: [codec_list_object][codec_entries]['.$streamnumber.'][type_raw] == '.$streamdata['type_raw']); + break; + + } + } + } + + if (isset($info['audio'])) { + $thisfile_audio['lossless'] = (isset($thisfile_audio['lossless']) ? $thisfile_audio['lossless'] : false); + $thisfile_audio['dataformat'] = (!empty($thisfile_audio['dataformat']) ? $thisfile_audio['dataformat'] : 'asf'); + } + if (!empty($thisfile_video['dataformat'])) { + $thisfile_video['lossless'] = (isset($thisfile_audio['lossless']) ? $thisfile_audio['lossless'] : false); + $thisfile_video['pixel_aspect_ratio'] = (isset($thisfile_audio['pixel_aspect_ratio']) ? $thisfile_audio['pixel_aspect_ratio'] : (float) 1); + $thisfile_video['dataformat'] = (!empty($thisfile_video['dataformat']) ? $thisfile_video['dataformat'] : 'asf'); + } + if (!empty($thisfile_video['streams'])) { + $thisfile_video['resolution_x'] = 0; + $thisfile_video['resolution_y'] = 0; + foreach ($thisfile_video['streams'] as $key => $valuearray) { + if (($valuearray['resolution_x'] > $thisfile_video['resolution_x']) || ($valuearray['resolution_y'] > $thisfile_video['resolution_y'])) { + $thisfile_video['resolution_x'] = $valuearray['resolution_x']; + $thisfile_video['resolution_y'] = $valuearray['resolution_y']; + } + } + } + $info['bitrate'] = (isset($thisfile_audio['bitrate']) ? $thisfile_audio['bitrate'] : 0) + (isset($thisfile_video['bitrate']) ? $thisfile_video['bitrate'] : 0); + + if ((!isset($info['playtime_seconds']) || ($info['playtime_seconds'] <= 0)) && ($info['bitrate'] > 0)) { + $info['playtime_seconds'] = ($info['filesize'] - $info['avdataoffset']) / ($info['bitrate'] / 8); + } + + return true; + } + + /** + * @param int $CodecListType + * + * @return string + */ + public static function codecListObjectTypeLookup($CodecListType) { + static $lookup = array( + 0x0001 => 'Video Codec', + 0x0002 => 'Audio Codec', + 0xFFFF => 'Unknown Codec' + ); + + return (isset($lookup[$CodecListType]) ? $lookup[$CodecListType] : 'Invalid Codec Type'); + } + + /** + * @return array + */ + public static function KnownGUIDs() { + static $GUIDarray = array( + 'GETID3_ASF_Extended_Stream_Properties_Object' => '14E6A5CB-C672-4332-8399-A96952065B5A', + 'GETID3_ASF_Padding_Object' => '1806D474-CADF-4509-A4BA-9AABCB96AAE8', + 'GETID3_ASF_Payload_Ext_Syst_Pixel_Aspect_Ratio' => '1B1EE554-F9EA-4BC8-821A-376B74E4C4B8', + 'GETID3_ASF_Script_Command_Object' => '1EFB1A30-0B62-11D0-A39B-00A0C90348F6', + 'GETID3_ASF_No_Error_Correction' => '20FB5700-5B55-11CF-A8FD-00805F5C442B', + 'GETID3_ASF_Content_Branding_Object' => '2211B3FA-BD23-11D2-B4B7-00A0C955FC6E', + 'GETID3_ASF_Content_Encryption_Object' => '2211B3FB-BD23-11D2-B4B7-00A0C955FC6E', + 'GETID3_ASF_Digital_Signature_Object' => '2211B3FC-BD23-11D2-B4B7-00A0C955FC6E', + 'GETID3_ASF_Extended_Content_Encryption_Object' => '298AE614-2622-4C17-B935-DAE07EE9289C', + 'GETID3_ASF_Simple_Index_Object' => '33000890-E5B1-11CF-89F4-00A0C90349CB', + 'GETID3_ASF_Degradable_JPEG_Media' => '35907DE0-E415-11CF-A917-00805F5C442B', + 'GETID3_ASF_Payload_Extension_System_Timecode' => '399595EC-8667-4E2D-8FDB-98814CE76C1E', + 'GETID3_ASF_Binary_Media' => '3AFB65E2-47EF-40F2-AC2C-70A90D71D343', + 'GETID3_ASF_Timecode_Index_Object' => '3CB73FD0-0C4A-4803-953D-EDF7B6228F0C', + 'GETID3_ASF_Metadata_Library_Object' => '44231C94-9498-49D1-A141-1D134E457054', + 'GETID3_ASF_Reserved_3' => '4B1ACBE3-100B-11D0-A39B-00A0C90348F6', + 'GETID3_ASF_Reserved_4' => '4CFEDB20-75F6-11CF-9C0F-00A0C90349CB', + 'GETID3_ASF_Command_Media' => '59DACFC0-59E6-11D0-A3AC-00A0C90348F6', + 'GETID3_ASF_Header_Extension_Object' => '5FBF03B5-A92E-11CF-8EE3-00C00C205365', + 'GETID3_ASF_Media_Object_Index_Parameters_Obj' => '6B203BAD-3F11-4E84-ACA8-D7613DE2CFA7', + 'GETID3_ASF_Header_Object' => '75B22630-668E-11CF-A6D9-00AA0062CE6C', + 'GETID3_ASF_Content_Description_Object' => '75B22633-668E-11CF-A6D9-00AA0062CE6C', + 'GETID3_ASF_Error_Correction_Object' => '75B22635-668E-11CF-A6D9-00AA0062CE6C', + 'GETID3_ASF_Data_Object' => '75B22636-668E-11CF-A6D9-00AA0062CE6C', + 'GETID3_ASF_Web_Stream_Media_Subtype' => '776257D4-C627-41CB-8F81-7AC7FF1C40CC', + 'GETID3_ASF_Stream_Bitrate_Properties_Object' => '7BF875CE-468D-11D1-8D82-006097C9A2B2', + 'GETID3_ASF_Language_List_Object' => '7C4346A9-EFE0-4BFC-B229-393EDE415C85', + 'GETID3_ASF_Codec_List_Object' => '86D15240-311D-11D0-A3A4-00A0C90348F6', + 'GETID3_ASF_Reserved_2' => '86D15241-311D-11D0-A3A4-00A0C90348F6', + 'GETID3_ASF_File_Properties_Object' => '8CABDCA1-A947-11CF-8EE4-00C00C205365', + 'GETID3_ASF_File_Transfer_Media' => '91BD222C-F21C-497A-8B6D-5AA86BFC0185', + 'GETID3_ASF_Old_RTP_Extension_Data' => '96800C63-4C94-11D1-837B-0080C7A37F95', + 'GETID3_ASF_Advanced_Mutual_Exclusion_Object' => 'A08649CF-4775-4670-8A16-6E35357566CD', + 'GETID3_ASF_Bandwidth_Sharing_Object' => 'A69609E6-517B-11D2-B6AF-00C04FD908E9', + 'GETID3_ASF_Reserved_1' => 'ABD3D211-A9BA-11cf-8EE6-00C00C205365', + 'GETID3_ASF_Bandwidth_Sharing_Exclusive' => 'AF6060AA-5197-11D2-B6AF-00C04FD908E9', + 'GETID3_ASF_Bandwidth_Sharing_Partial' => 'AF6060AB-5197-11D2-B6AF-00C04FD908E9', + 'GETID3_ASF_JFIF_Media' => 'B61BE100-5B4E-11CF-A8FD-00805F5C442B', + 'GETID3_ASF_Stream_Properties_Object' => 'B7DC0791-A9B7-11CF-8EE6-00C00C205365', + 'GETID3_ASF_Video_Media' => 'BC19EFC0-5B4D-11CF-A8FD-00805F5C442B', + 'GETID3_ASF_Audio_Spread' => 'BFC3CD50-618F-11CF-8BB2-00AA00B4E220', + 'GETID3_ASF_Metadata_Object' => 'C5F8CBEA-5BAF-4877-8467-AA8C44FA4CCA', + 'GETID3_ASF_Payload_Ext_Syst_Sample_Duration' => 'C6BD9450-867F-4907-83A3-C77921B733AD', + 'GETID3_ASF_Group_Mutual_Exclusion_Object' => 'D1465A40-5A79-4338-B71B-E36B8FD6C249', + 'GETID3_ASF_Extended_Content_Description_Object' => 'D2D0A440-E307-11D2-97F0-00A0C95EA850', + 'GETID3_ASF_Stream_Prioritization_Object' => 'D4FED15B-88D3-454F-81F0-ED5C45999E24', + 'GETID3_ASF_Payload_Ext_System_Content_Type' => 'D590DC20-07BC-436C-9CF7-F3BBFBF1A4DC', + 'GETID3_ASF_Old_File_Properties_Object' => 'D6E229D0-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_ASF_Header_Object' => 'D6E229D1-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_ASF_Data_Object' => 'D6E229D2-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Index_Object' => 'D6E229D3-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Stream_Properties_Object' => 'D6E229D4-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Content_Description_Object' => 'D6E229D5-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Script_Command_Object' => 'D6E229D6-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Marker_Object' => 'D6E229D7-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Component_Download_Object' => 'D6E229D8-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Stream_Group_Object' => 'D6E229D9-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Scalable_Object' => 'D6E229DA-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Prioritization_Object' => 'D6E229DB-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Bitrate_Mutual_Exclusion_Object' => 'D6E229DC-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Inter_Media_Dependency_Object' => 'D6E229DD-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Rating_Object' => 'D6E229DE-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Index_Parameters_Object' => 'D6E229DF-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Color_Table_Object' => 'D6E229E0-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Language_List_Object' => 'D6E229E1-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Audio_Media' => 'D6E229E2-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Video_Media' => 'D6E229E3-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Image_Media' => 'D6E229E4-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Timecode_Media' => 'D6E229E5-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Text_Media' => 'D6E229E6-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_MIDI_Media' => 'D6E229E7-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Command_Media' => 'D6E229E8-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_No_Error_Concealment' => 'D6E229EA-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Scrambled_Audio' => 'D6E229EB-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_No_Color_Table' => 'D6E229EC-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_SMPTE_Time' => 'D6E229ED-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_ASCII_Text' => 'D6E229EE-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Unicode_Text' => 'D6E229EF-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_HTML_Text' => 'D6E229F0-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_URL_Command' => 'D6E229F1-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Filename_Command' => 'D6E229F2-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_ACM_Codec' => 'D6E229F3-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_VCM_Codec' => 'D6E229F4-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_QuickTime_Codec' => 'D6E229F5-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_DirectShow_Transform_Filter' => 'D6E229F6-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_DirectShow_Rendering_Filter' => 'D6E229F7-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_No_Enhancement' => 'D6E229F8-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Unknown_Enhancement_Type' => 'D6E229F9-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Temporal_Enhancement' => 'D6E229FA-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Spatial_Enhancement' => 'D6E229FB-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Quality_Enhancement' => 'D6E229FC-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Number_of_Channels_Enhancement' => 'D6E229FD-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Frequency_Response_Enhancement' => 'D6E229FE-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Media_Object' => 'D6E229FF-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Mutex_Language' => 'D6E22A00-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Mutex_Bitrate' => 'D6E22A01-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Mutex_Unknown' => 'D6E22A02-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_ASF_Placeholder_Object' => 'D6E22A0E-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Old_Data_Unit_Extension_Object' => 'D6E22A0F-35DA-11D1-9034-00A0C90349BE', + 'GETID3_ASF_Web_Stream_Format' => 'DA1E6B13-8359-4050-B398-388E965BF00C', + 'GETID3_ASF_Payload_Ext_System_File_Name' => 'E165EC0E-19ED-45D7-B4A7-25CBD1E28E9B', + 'GETID3_ASF_Marker_Object' => 'F487CD01-A951-11CF-8EE6-00C00C205365', + 'GETID3_ASF_Timecode_Index_Parameters_Object' => 'F55E496D-9797-4B5D-8C8B-604DFE9BFB24', + 'GETID3_ASF_Audio_Media' => 'F8699E40-5B4D-11CF-A8FD-00805F5C442B', + 'GETID3_ASF_Media_Object_Index_Object' => 'FEB103F8-12AD-4C64-840F-2A1D2F7AD48C', + 'GETID3_ASF_Alt_Extended_Content_Encryption_Obj' => 'FF889EF1-ADEE-40DA-9E71-98704BB928CE', + 'GETID3_ASF_Index_Placeholder_Object' => 'D9AADE20-7C17-4F9C-BC28-8555DD98E2A2', // http://cpan.uwinnipeg.ca/htdocs/Audio-WMA/Audio/WMA.pm.html + 'GETID3_ASF_Compatibility_Object' => '26F18B5D-4584-47EC-9F5F-0E651F0452C9', // http://cpan.uwinnipeg.ca/htdocs/Audio-WMA/Audio/WMA.pm.html + ); + return $GUIDarray; + } + + /** + * @param string $GUIDstring + * + * @return string|false + */ + public static function GUIDname($GUIDstring) { + static $GUIDarray = array(); + if (empty($GUIDarray)) { + $GUIDarray = self::KnownGUIDs(); + } + return array_search($GUIDstring, $GUIDarray); + } + + /** + * @param int $id + * + * @return string + */ + public static function ASFIndexObjectIndexTypeLookup($id) { + static $ASFIndexObjectIndexTypeLookup = array(); + if (empty($ASFIndexObjectIndexTypeLookup)) { + $ASFIndexObjectIndexTypeLookup[1] = 'Nearest Past Data Packet'; + $ASFIndexObjectIndexTypeLookup[2] = 'Nearest Past Media Object'; + $ASFIndexObjectIndexTypeLookup[3] = 'Nearest Past Cleanpoint'; + } + return (isset($ASFIndexObjectIndexTypeLookup[$id]) ? $ASFIndexObjectIndexTypeLookup[$id] : 'invalid'); + } + + /** + * @param string $GUIDstring + * + * @return string + */ + public static function GUIDtoBytestring($GUIDstring) { + // Microsoft defines these 16-byte (128-bit) GUIDs in the strangest way: + // first 4 bytes are in little-endian order + // next 2 bytes are appended in little-endian order + // next 2 bytes are appended in little-endian order + // next 2 bytes are appended in big-endian order + // next 6 bytes are appended in big-endian order + + // AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp is stored as this 16-byte string: + // $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp + + $hexbytecharstring = chr(hexdec(substr($GUIDstring, 6, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 4, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 2, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 0, 2))); + + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 11, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 9, 2))); + + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 16, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 14, 2))); + + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 19, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 21, 2))); + + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 24, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 26, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 28, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 30, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 32, 2))); + $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 34, 2))); + + return $hexbytecharstring; + } + + /** + * @param string $Bytestring + * + * @return string + */ + public static function BytestringToGUID($Bytestring) { + $GUIDstring = str_pad(dechex(ord($Bytestring[3])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[2])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[1])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[0])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= '-'; + $GUIDstring .= str_pad(dechex(ord($Bytestring[5])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[4])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= '-'; + $GUIDstring .= str_pad(dechex(ord($Bytestring[7])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[6])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= '-'; + $GUIDstring .= str_pad(dechex(ord($Bytestring[8])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[9])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= '-'; + $GUIDstring .= str_pad(dechex(ord($Bytestring[10])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[11])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[12])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[13])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[14])), 2, '0', STR_PAD_LEFT); + $GUIDstring .= str_pad(dechex(ord($Bytestring[15])), 2, '0', STR_PAD_LEFT); + + return strtoupper($GUIDstring); + } + + /** + * @param int $FILETIME + * @param bool $round + * + * @return float|int + */ + public static function FILETIMEtoUNIXtime($FILETIME, $round=true) { + // FILETIME is a 64-bit unsigned integer representing + // the number of 100-nanosecond intervals since January 1, 1601 + // UNIX timestamp is number of seconds since January 1, 1970 + // 116444736000000000 = 10000000 * 60 * 60 * 24 * 365 * 369 + 89 leap days + if ($round) { + return intval(round(($FILETIME - 116444736000000000) / 10000000)); + } + return ($FILETIME - 116444736000000000) / 10000000; + } + + /** + * @param int $WMpictureType + * + * @return string + */ + public static function WMpictureTypeLookup($WMpictureType) { + static $lookup = null; + if ($lookup === null) { + $lookup = array( + 0x03 => 'Front Cover', + 0x04 => 'Back Cover', + 0x00 => 'User Defined', + 0x05 => 'Leaflet Page', + 0x06 => 'Media Label', + 0x07 => 'Lead Artist', + 0x08 => 'Artist', + 0x09 => 'Conductor', + 0x0A => 'Band', + 0x0B => 'Composer', + 0x0C => 'Lyricist', + 0x0D => 'Recording Location', + 0x0E => 'During Recording', + 0x0F => 'During Performance', + 0x10 => 'Video Screen Capture', + 0x12 => 'Illustration', + 0x13 => 'Band Logotype', + 0x14 => 'Publisher Logotype' + ); + $lookup = array_map(function($str) { + return getid3_lib::iconv_fallback('UTF-8', 'UTF-16LE', $str); + }, $lookup); + } + + return (isset($lookup[$WMpictureType]) ? $lookup[$WMpictureType] : ''); + } + + /** + * @param string $asf_header_extension_object_data + * @param int $unhandled_sections + * + * @return array + */ + public function HeaderExtensionObjectDataParse(&$asf_header_extension_object_data, &$unhandled_sections) { + // http://msdn.microsoft.com/en-us/library/bb643323.aspx + + $offset = 0; + $objectOffset = 0; + $HeaderExtensionObjectParsed = array(); + while ($objectOffset < strlen($asf_header_extension_object_data)) { + $offset = $objectOffset; + $thisObject = array(); + + $thisObject['guid'] = substr($asf_header_extension_object_data, $offset, 16); + $offset += 16; + $thisObject['guid_text'] = $this->BytestringToGUID($thisObject['guid']); + $thisObject['guid_name'] = $this->GUIDname($thisObject['guid_text']); + + $thisObject['size'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 8)); + $offset += 8; + if ($thisObject['size'] <= 0) { + break; + } + + switch ($thisObject['guid']) { + case GETID3_ASF_Extended_Stream_Properties_Object: + $thisObject['start_time'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 8)); + $offset += 8; + $thisObject['start_time_unix'] = $this->FILETIMEtoUNIXtime($thisObject['start_time']); + + $thisObject['end_time'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 8)); + $offset += 8; + $thisObject['end_time_unix'] = $this->FILETIMEtoUNIXtime($thisObject['end_time']); + + $thisObject['data_bitrate'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['buffer_size'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['initial_buffer_fullness'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['alternate_data_bitrate'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['alternate_buffer_size'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['alternate_initial_buffer_fullness'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['maximum_object_size'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['flags_raw'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + $thisObject['flags']['reliable'] = (bool) $thisObject['flags_raw'] & 0x00000001; + $thisObject['flags']['seekable'] = (bool) $thisObject['flags_raw'] & 0x00000002; + $thisObject['flags']['no_cleanpoints'] = (bool) $thisObject['flags_raw'] & 0x00000004; + $thisObject['flags']['resend_live_cleanpoints'] = (bool) $thisObject['flags_raw'] & 0x00000008; + + $thisObject['stream_number'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $thisObject['stream_language_id_index'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $thisObject['average_time_per_frame'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $thisObject['stream_name_count'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $thisObject['payload_extension_system_count'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + for ($i = 0; $i < $thisObject['stream_name_count']; $i++) { + $streamName = array(); + + $streamName['language_id_index'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $streamName['stream_name_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $streamName['stream_name'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, $streamName['stream_name_length'])); + $offset += $streamName['stream_name_length']; + + $thisObject['stream_names'][$i] = $streamName; + } + + for ($i = 0; $i < $thisObject['payload_extension_system_count']; $i++) { + $payloadExtensionSystem = array(); + + $payloadExtensionSystem['extension_system_id'] = substr($asf_header_extension_object_data, $offset, 16); + $offset += 16; + $payloadExtensionSystem['extension_system_id_text'] = $this->BytestringToGUID($payloadExtensionSystem['extension_system_id']); + + $payloadExtensionSystem['extension_system_size'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + if ($payloadExtensionSystem['extension_system_size'] <= 0) { + break 2; + } + + $payloadExtensionSystem['extension_system_info_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $payloadExtensionSystem['extension_system_info_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, $payloadExtensionSystem['extension_system_info_length'])); + $offset += $payloadExtensionSystem['extension_system_info_length']; + + $thisObject['payload_extension_systems'][$i] = $payloadExtensionSystem; + } + + break; + + case GETID3_ASF_Padding_Object: + // padding, skip it + break; + + case GETID3_ASF_Metadata_Object: + $thisObject['description_record_counts'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + for ($i = 0; $i < $thisObject['description_record_counts']; $i++) { + $descriptionRecord = array(); + + $descriptionRecord['reserved_1'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); // must be zero + $offset += 2; + + $descriptionRecord['stream_number'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $descriptionRecord['name_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $descriptionRecord['data_type'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + $descriptionRecord['data_type_text'] = self::metadataLibraryObjectDataTypeLookup($descriptionRecord['data_type']); + + $descriptionRecord['data_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $descriptionRecord['name'] = substr($asf_header_extension_object_data, $offset, $descriptionRecord['name_length']); + $offset += $descriptionRecord['name_length']; + + $descriptionRecord['data'] = substr($asf_header_extension_object_data, $offset, $descriptionRecord['data_length']); + $offset += $descriptionRecord['data_length']; + switch ($descriptionRecord['data_type']) { + case 0x0000: // Unicode string + break; + + case 0x0001: // BYTE array + // do nothing + break; + + case 0x0002: // BOOL + $descriptionRecord['data'] = (bool) getid3_lib::LittleEndian2Int($descriptionRecord['data']); + break; + + case 0x0003: // DWORD + case 0x0004: // QWORD + case 0x0005: // WORD + $descriptionRecord['data'] = getid3_lib::LittleEndian2Int($descriptionRecord['data']); + break; + + case 0x0006: // GUID + $descriptionRecord['data_text'] = $this->BytestringToGUID($descriptionRecord['data']); + break; + } + + $thisObject['description_record'][$i] = $descriptionRecord; + } + break; + + case GETID3_ASF_Language_List_Object: + $thisObject['language_id_record_counts'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + for ($i = 0; $i < $thisObject['language_id_record_counts']; $i++) { + $languageIDrecord = array(); + + $languageIDrecord['language_id_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 1)); + $offset += 1; + + $languageIDrecord['language_id'] = substr($asf_header_extension_object_data, $offset, $languageIDrecord['language_id_length']); + $offset += $languageIDrecord['language_id_length']; + + $thisObject['language_id_record'][$i] = $languageIDrecord; + } + break; + + case GETID3_ASF_Metadata_Library_Object: + $thisObject['description_records_count'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + for ($i = 0; $i < $thisObject['description_records_count']; $i++) { + $descriptionRecord = array(); + + $descriptionRecord['language_list_index'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $descriptionRecord['stream_number'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $descriptionRecord['name_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + + $descriptionRecord['data_type'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 2)); + $offset += 2; + $descriptionRecord['data_type_text'] = self::metadataLibraryObjectDataTypeLookup($descriptionRecord['data_type']); + + $descriptionRecord['data_length'] = getid3_lib::LittleEndian2Int(substr($asf_header_extension_object_data, $offset, 4)); + $offset += 4; + + $descriptionRecord['name'] = substr($asf_header_extension_object_data, $offset, $descriptionRecord['name_length']); + $offset += $descriptionRecord['name_length']; + + $descriptionRecord['data'] = substr($asf_header_extension_object_data, $offset, $descriptionRecord['data_length']); + $offset += $descriptionRecord['data_length']; + + if (preg_match('#^WM/Picture$#', str_replace("\x00", '', trim($descriptionRecord['name'])))) { + $WMpicture = $this->ASF_WMpicture($descriptionRecord['data']); + foreach ($WMpicture as $key => $value) { + $descriptionRecord['data'] = $WMpicture; + } + unset($WMpicture); + } + + $thisObject['description_record'][$i] = $descriptionRecord; + } + break; + + default: + $unhandled_sections++; + if ($this->GUIDname($thisObject['guid_text'])) { + $this->warning('unhandled Header Extension Object GUID "'.$this->GUIDname($thisObject['guid_text']).'" {'.$thisObject['guid_text'].'} at offset '.($offset - 16 - 8)); + } else { + $this->warning('unknown Header Extension Object GUID {'.$thisObject['guid_text'].'} in at offset '.($offset - 16 - 8)); + } + break; + } + $HeaderExtensionObjectParsed[] = $thisObject; + + $objectOffset += $thisObject['size']; + } + return $HeaderExtensionObjectParsed; + } + + /** + * @param int $id + * + * @return string + */ + public static function metadataLibraryObjectDataTypeLookup($id) { + static $lookup = array( + 0x0000 => 'Unicode string', // The data consists of a sequence of Unicode characters + 0x0001 => 'BYTE array', // The type of the data is implementation-specific + 0x0002 => 'BOOL', // The data is 2 bytes long and should be interpreted as a 16-bit unsigned integer. Only 0x0000 or 0x0001 are permitted values + 0x0003 => 'DWORD', // The data is 4 bytes long and should be interpreted as a 32-bit unsigned integer + 0x0004 => 'QWORD', // The data is 8 bytes long and should be interpreted as a 64-bit unsigned integer + 0x0005 => 'WORD', // The data is 2 bytes long and should be interpreted as a 16-bit unsigned integer + 0x0006 => 'GUID', // The data is 16 bytes long and should be interpreted as a 128-bit GUID + ); + return (isset($lookup[$id]) ? $lookup[$id] : 'invalid'); + } + + /** + * @param string $data + * + * @return array + */ + public function ASF_WMpicture(&$data) { + //typedef struct _WMPicture{ + // LPWSTR pwszMIMEType; + // BYTE bPictureType; + // LPWSTR pwszDescription; + // DWORD dwDataLen; + // BYTE* pbData; + //} WM_PICTURE; + + $WMpicture = array(); + + $offset = 0; + $WMpicture['image_type_id'] = getid3_lib::LittleEndian2Int(substr($data, $offset, 1)); + $offset += 1; + $WMpicture['image_type'] = self::WMpictureTypeLookup($WMpicture['image_type_id']); + $WMpicture['image_size'] = getid3_lib::LittleEndian2Int(substr($data, $offset, 4)); + $offset += 4; + + $WMpicture['image_mime'] = ''; + do { + $next_byte_pair = substr($data, $offset, 2); + $offset += 2; + $WMpicture['image_mime'] .= $next_byte_pair; + } while ($next_byte_pair !== "\x00\x00"); + + $WMpicture['image_description'] = ''; + do { + $next_byte_pair = substr($data, $offset, 2); + $offset += 2; + $WMpicture['image_description'] .= $next_byte_pair; + } while ($next_byte_pair !== "\x00\x00"); + + $WMpicture['dataoffset'] = $offset; + $WMpicture['data'] = substr($data, $offset); + + $imageinfo = array(); + $WMpicture['image_mime'] = ''; + $imagechunkcheck = getid3_lib::GetDataImageSize($WMpicture['data'], $imageinfo); + unset($imageinfo); + if (!empty($imagechunkcheck)) { + $WMpicture['image_mime'] = image_type_to_mime_type($imagechunkcheck[2]); + } + if (!isset($this->getid3->info['asf']['comments']['picture'])) { + $this->getid3->info['asf']['comments']['picture'] = array(); + } + $this->getid3->info['asf']['comments']['picture'][] = array('data'=>$WMpicture['data'], 'image_mime'=>$WMpicture['image_mime']); + + return $WMpicture; + } + + /** + * Remove terminator 00 00 and convert UTF-16LE to Latin-1. + * + * @param string $string + * + * @return string + */ + public static function TrimConvert($string) { + return trim(getid3_lib::iconv_fallback('UTF-16LE', 'ISO-8859-1', self::TrimTerm($string)), ' '); + } + + /** + * Remove terminator 00 00. + * + * @param string $string + * + * @return string + */ + public static function TrimTerm($string) { + // remove terminator, only if present (it should be, but...) + if (substr($string, -2) === "\x00\x00") { + $string = substr($string, 0, -2); + } + return $string; + } + +} diff --git a/projects/tests/tests/performance/ID3/module.audio-video.flv.php b/projects/tests/tests/performance/ID3/module.audio-video.flv.php new file mode 100644 index 00000000..7e684072 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio-video.flv.php @@ -0,0 +1,909 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio-video.flv.php // +// module for analyzing Shockwave Flash Video files // +// dependencies: NONE // +// // +///////////////////////////////////////////////////////////////// +// // +// FLV module by Seth Kaufman // +// // +// * version 0.1 (26 June 2005) // +// // +// * version 0.1.1 (15 July 2005) // +// minor modifications by James Heinrich // +// // +// * version 0.2 (22 February 2006) // +// Support for On2 VP6 codec and meta information // +// by Steve Webster // +// // +// * version 0.3 (15 June 2006) // +// Modified to not read entire file into memory // +// by James Heinrich // +// // +// * version 0.4 (07 December 2007) // +// Bugfixes for incorrectly parsed FLV dimensions // +// and incorrect parsing of onMetaTag // +// by Evgeny Moysevich // +// // +// * version 0.5 (21 May 2009) // +// Fixed parsing of audio tags and added additional codec // +// details. The duration is now read from onMetaTag (if // +// exists), rather than parsing whole file // +// by Nigel Barnes // +// // +// * version 0.6 (24 May 2009) // +// Better parsing of files with h264 video // +// by Evgeny Moysevich // +// // +// * version 0.6.1 (30 May 2011) // +// prevent infinite loops in expGolombUe() // +// // +// * version 0.7.0 (16 Jul 2013) // +// handle GETID3_FLV_VIDEO_VP6FLV_ALPHA // +// improved AVCSequenceParameterSetReader::readData() // +// by Xander Schouwerwou // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} + +define('GETID3_FLV_TAG_AUDIO', 8); +define('GETID3_FLV_TAG_VIDEO', 9); +define('GETID3_FLV_TAG_META', 18); + +define('GETID3_FLV_VIDEO_H263', 2); +define('GETID3_FLV_VIDEO_SCREEN', 3); +define('GETID3_FLV_VIDEO_VP6FLV', 4); +define('GETID3_FLV_VIDEO_VP6FLV_ALPHA', 5); +define('GETID3_FLV_VIDEO_SCREENV2', 6); +define('GETID3_FLV_VIDEO_H264', 7); + +define('H264_AVC_SEQUENCE_HEADER', 0); +define('H264_PROFILE_BASELINE', 66); +define('H264_PROFILE_MAIN', 77); +define('H264_PROFILE_EXTENDED', 88); +define('H264_PROFILE_HIGH', 100); +define('H264_PROFILE_HIGH10', 110); +define('H264_PROFILE_HIGH422', 122); +define('H264_PROFILE_HIGH444', 144); +define('H264_PROFILE_HIGH444_PREDICTIVE', 244); + +class getid3_flv extends getid3_handler +{ + const magic = 'FLV'; + + /** + * Break out of the loop if too many frames have been scanned; only scan this + * many if meta frame does not contain useful duration. + * + * @var int + */ + public $max_frames = 100000; + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + $this->fseek($info['avdataoffset']); + + $FLVdataLength = $info['avdataend'] - $info['avdataoffset']; + $FLVheader = $this->fread(5); + + $info['fileformat'] = 'flv'; + $info['flv']['header']['signature'] = substr($FLVheader, 0, 3); + $info['flv']['header']['version'] = getid3_lib::BigEndian2Int(substr($FLVheader, 3, 1)); + $TypeFlags = getid3_lib::BigEndian2Int(substr($FLVheader, 4, 1)); + + if ($info['flv']['header']['signature'] != self::magic) { + $this->error('Expecting "'.getid3_lib::PrintHexBytes(self::magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['flv']['header']['signature']).'"'); + unset($info['flv'], $info['fileformat']); + return false; + } + + $info['flv']['header']['hasAudio'] = (bool) ($TypeFlags & 0x04); + $info['flv']['header']['hasVideo'] = (bool) ($TypeFlags & 0x01); + + $FrameSizeDataLength = getid3_lib::BigEndian2Int($this->fread(4)); + $FLVheaderFrameLength = 9; + if ($FrameSizeDataLength > $FLVheaderFrameLength) { + $this->fseek($FrameSizeDataLength - $FLVheaderFrameLength, SEEK_CUR); + } + $Duration = 0; + $found_video = false; + $found_audio = false; + $found_meta = false; + $found_valid_meta_playtime = false; + $tagParseCount = 0; + $info['flv']['framecount'] = array('total'=>0, 'audio'=>0, 'video'=>0); + $flv_framecount = &$info['flv']['framecount']; + while ((($this->ftell() + 16) < $info['avdataend']) && (($tagParseCount++ <= $this->max_frames) || !$found_valid_meta_playtime)) { + $ThisTagHeader = $this->fread(16); + + $PreviousTagLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 0, 4)); + $TagType = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 4, 1)); + $DataLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 5, 3)); + $Timestamp = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 8, 3)); + $LastHeaderByte = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 15, 1)); + $NextOffset = $this->ftell() - 1 + $DataLength; + if ($Timestamp > $Duration) { + $Duration = $Timestamp; + } + + $flv_framecount['total']++; + switch ($TagType) { + case GETID3_FLV_TAG_AUDIO: + $flv_framecount['audio']++; + if (!$found_audio) { + $found_audio = true; + $info['flv']['audio']['audioFormat'] = ($LastHeaderByte >> 4) & 0x0F; + $info['flv']['audio']['audioRate'] = ($LastHeaderByte >> 2) & 0x03; + $info['flv']['audio']['audioSampleSize'] = ($LastHeaderByte >> 1) & 0x01; + $info['flv']['audio']['audioType'] = $LastHeaderByte & 0x01; + } + break; + + case GETID3_FLV_TAG_VIDEO: + $flv_framecount['video']++; + if (!$found_video) { + $found_video = true; + $info['flv']['video']['videoCodec'] = $LastHeaderByte & 0x07; + + $FLVvideoHeader = $this->fread(11); + + if ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H264) { + // this code block contributed by: moysevichØgmail*com + + $AVCPacketType = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 0, 1)); + if ($AVCPacketType == H264_AVC_SEQUENCE_HEADER) { + // read AVCDecoderConfigurationRecord + $configurationVersion = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 1)); + $AVCProfileIndication = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 1)); + $profile_compatibility = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 1)); + $lengthSizeMinusOne = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 1)); + $numOfSequenceParameterSets = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 8, 1)); + + if (($numOfSequenceParameterSets & 0x1F) != 0) { + // there is at least one SequenceParameterSet + // read size of the first SequenceParameterSet + //$spsSize = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 9, 2)); + $spsSize = getid3_lib::LittleEndian2Int(substr($FLVvideoHeader, 9, 2)); + // read the first SequenceParameterSet + $sps = $this->fread($spsSize); + if (strlen($sps) == $spsSize) { // make sure that whole SequenceParameterSet was red + $spsReader = new AVCSequenceParameterSetReader($sps); + $spsReader->readData(); + $info['video']['resolution_x'] = $spsReader->getWidth(); + $info['video']['resolution_y'] = $spsReader->getHeight(); + } + } + } + // end: moysevichØgmail*com + + } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H263) { + + $PictureSizeType = (getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 3, 2))) >> 7; + $PictureSizeType = $PictureSizeType & 0x0007; + $info['flv']['header']['videoSizeType'] = $PictureSizeType; + switch ($PictureSizeType) { + case 0: + //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2)); + //$PictureSizeEnc <<= 1; + //$info['video']['resolution_x'] = ($PictureSizeEnc & 0xFF00) >> 8; + //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2)); + //$PictureSizeEnc <<= 1; + //$info['video']['resolution_y'] = ($PictureSizeEnc & 0xFF00) >> 8; + + $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 2)) >> 7; + $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2)) >> 7; + $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFF; + $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFF; + break; + + case 1: + $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 3)) >> 7; + $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 3)) >> 7; + $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFFFF; + $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFFFF; + break; + + case 2: + $info['video']['resolution_x'] = 352; + $info['video']['resolution_y'] = 288; + break; + + case 3: + $info['video']['resolution_x'] = 176; + $info['video']['resolution_y'] = 144; + break; + + case 4: + $info['video']['resolution_x'] = 128; + $info['video']['resolution_y'] = 96; + break; + + case 5: + $info['video']['resolution_x'] = 320; + $info['video']['resolution_y'] = 240; + break; + + case 6: + $info['video']['resolution_x'] = 160; + $info['video']['resolution_y'] = 120; + break; + + default: + $info['video']['resolution_x'] = 0; + $info['video']['resolution_y'] = 0; + break; + + } + + } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_VP6FLV_ALPHA) { + + /* contributed by schouwerwouØgmail*com */ + if (!isset($info['video']['resolution_x'])) { // only when meta data isn't set + $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2)); + $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 2)); + $info['video']['resolution_x'] = ($PictureSizeEnc['x'] & 0xFF) << 3; + $info['video']['resolution_y'] = ($PictureSizeEnc['y'] & 0xFF) << 3; + } + /* end schouwerwouØgmail*com */ + + } + if (!empty($info['video']['resolution_x']) && !empty($info['video']['resolution_y'])) { + $info['video']['pixel_aspect_ratio'] = $info['video']['resolution_x'] / $info['video']['resolution_y']; + } + } + break; + + // Meta tag + case GETID3_FLV_TAG_META: + if (!$found_meta) { + $found_meta = true; + $this->fseek(-1, SEEK_CUR); + $datachunk = $this->fread($DataLength); + $AMFstream = new AMFStream($datachunk); + $reader = new AMFReader($AMFstream); + $eventName = $reader->readData(); + $info['flv']['meta'][$eventName] = $reader->readData(); + unset($reader); + + $copykeys = array('framerate'=>'frame_rate', 'width'=>'resolution_x', 'height'=>'resolution_y', 'audiodatarate'=>'bitrate', 'videodatarate'=>'bitrate'); + foreach ($copykeys as $sourcekey => $destkey) { + if (isset($info['flv']['meta']['onMetaData'][$sourcekey])) { + switch ($sourcekey) { + case 'width': + case 'height': + $info['video'][$destkey] = intval(round($info['flv']['meta']['onMetaData'][$sourcekey])); + break; + case 'audiodatarate': + $info['audio'][$destkey] = getid3_lib::CastAsInt(round($info['flv']['meta']['onMetaData'][$sourcekey] * 1000)); + break; + case 'videodatarate': + case 'frame_rate': + default: + $info['video'][$destkey] = $info['flv']['meta']['onMetaData'][$sourcekey]; + break; + } + } + } + if (!empty($info['flv']['meta']['onMetaData']['duration'])) { + $found_valid_meta_playtime = true; + } + } + break; + + default: + // noop + break; + } + $this->fseek($NextOffset); + } + + $info['playtime_seconds'] = $Duration / 1000; + if ($info['playtime_seconds'] > 0) { + $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; + } + + if ($info['flv']['header']['hasAudio']) { + $info['audio']['codec'] = self::audioFormatLookup($info['flv']['audio']['audioFormat']); + $info['audio']['sample_rate'] = self::audioRateLookup($info['flv']['audio']['audioRate']); + $info['audio']['bits_per_sample'] = self::audioBitDepthLookup($info['flv']['audio']['audioSampleSize']); + + $info['audio']['channels'] = $info['flv']['audio']['audioType'] + 1; // 0=mono,1=stereo + $info['audio']['lossless'] = ($info['flv']['audio']['audioFormat'] ? false : true); // 0=uncompressed + $info['audio']['dataformat'] = 'flv'; + } + if (!empty($info['flv']['header']['hasVideo'])) { + $info['video']['codec'] = self::videoCodecLookup($info['flv']['video']['videoCodec']); + $info['video']['dataformat'] = 'flv'; + $info['video']['lossless'] = false; + } + + // Set information from meta + if (!empty($info['flv']['meta']['onMetaData']['duration'])) { + $info['playtime_seconds'] = $info['flv']['meta']['onMetaData']['duration']; + $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; + } + if (isset($info['flv']['meta']['onMetaData']['audiocodecid'])) { + $info['audio']['codec'] = self::audioFormatLookup($info['flv']['meta']['onMetaData']['audiocodecid']); + } + if (isset($info['flv']['meta']['onMetaData']['videocodecid'])) { + $info['video']['codec'] = self::videoCodecLookup($info['flv']['meta']['onMetaData']['videocodecid']); + } + return true; + } + + /** + * @param int $id + * + * @return string|false + */ + public static function audioFormatLookup($id) { + static $lookup = array( + 0 => 'Linear PCM, platform endian', + 1 => 'ADPCM', + 2 => 'mp3', + 3 => 'Linear PCM, little endian', + 4 => 'Nellymoser 16kHz mono', + 5 => 'Nellymoser 8kHz mono', + 6 => 'Nellymoser', + 7 => 'G.711A-law logarithmic PCM', + 8 => 'G.711 mu-law logarithmic PCM', + 9 => 'reserved', + 10 => 'AAC', + 11 => 'Speex', + 12 => false, // unknown? + 13 => false, // unknown? + 14 => 'mp3 8kHz', + 15 => 'Device-specific sound', + ); + return (isset($lookup[$id]) ? $lookup[$id] : false); + } + + /** + * @param int $id + * + * @return int|false + */ + public static function audioRateLookup($id) { + static $lookup = array( + 0 => 5500, + 1 => 11025, + 2 => 22050, + 3 => 44100, + ); + return (isset($lookup[$id]) ? $lookup[$id] : false); + } + + /** + * @param int $id + * + * @return int|false + */ + public static function audioBitDepthLookup($id) { + static $lookup = array( + 0 => 8, + 1 => 16, + ); + return (isset($lookup[$id]) ? $lookup[$id] : false); + } + + /** + * @param int $id + * + * @return string|false + */ + public static function videoCodecLookup($id) { + static $lookup = array( + GETID3_FLV_VIDEO_H263 => 'Sorenson H.263', + GETID3_FLV_VIDEO_SCREEN => 'Screen video', + GETID3_FLV_VIDEO_VP6FLV => 'On2 VP6', + GETID3_FLV_VIDEO_VP6FLV_ALPHA => 'On2 VP6 with alpha channel', + GETID3_FLV_VIDEO_SCREENV2 => 'Screen video v2', + GETID3_FLV_VIDEO_H264 => 'Sorenson H.264', + ); + return (isset($lookup[$id]) ? $lookup[$id] : false); + } +} + +class AMFStream +{ + /** + * @var string + */ + public $bytes; + + /** + * @var int + */ + public $pos; + + /** + * @param string $bytes + */ + public function __construct(&$bytes) { + $this->bytes =& $bytes; + $this->pos = 0; + } + + /** + * @return int + */ + public function readByte() { // 8-bit + return ord(substr($this->bytes, $this->pos++, 1)); + } + + /** + * @return int + */ + public function readInt() { // 16-bit + return ($this->readByte() << 8) + $this->readByte(); + } + + /** + * @return int + */ + public function readLong() { // 32-bit + return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte(); + } + + /** + * @return float|false + */ + public function readDouble() { + return getid3_lib::BigEndian2Float($this->read(8)); + } + + /** + * @return string + */ + public function readUTF() { + $length = $this->readInt(); + return $this->read($length); + } + + /** + * @return string + */ + public function readLongUTF() { + $length = $this->readLong(); + return $this->read($length); + } + + /** + * @param int $length + * + * @return string + */ + public function read($length) { + $val = substr($this->bytes, $this->pos, $length); + $this->pos += $length; + return $val; + } + + /** + * @return int + */ + public function peekByte() { + $pos = $this->pos; + $val = $this->readByte(); + $this->pos = $pos; + return $val; + } + + /** + * @return int + */ + public function peekInt() { + $pos = $this->pos; + $val = $this->readInt(); + $this->pos = $pos; + return $val; + } + + /** + * @return int + */ + public function peekLong() { + $pos = $this->pos; + $val = $this->readLong(); + $this->pos = $pos; + return $val; + } + + /** + * @return float|false + */ + public function peekDouble() { + $pos = $this->pos; + $val = $this->readDouble(); + $this->pos = $pos; + return $val; + } + + /** + * @return string + */ + public function peekUTF() { + $pos = $this->pos; + $val = $this->readUTF(); + $this->pos = $pos; + return $val; + } + + /** + * @return string + */ + public function peekLongUTF() { + $pos = $this->pos; + $val = $this->readLongUTF(); + $this->pos = $pos; + return $val; + } +} + +class AMFReader +{ + /** + * @var AMFStream + */ + public $stream; + + /** + * @param AMFStream $stream + */ + public function __construct(AMFStream $stream) { + $this->stream = $stream; + } + + /** + * @return mixed + */ + public function readData() { + $value = null; + + $type = $this->stream->readByte(); + switch ($type) { + + // Double + case 0: + $value = $this->readDouble(); + break; + + // Boolean + case 1: + $value = $this->readBoolean(); + break; + + // String + case 2: + $value = $this->readString(); + break; + + // Object + case 3: + $value = $this->readObject(); + break; + + // null + case 6: + return null; + + // Mixed array + case 8: + $value = $this->readMixedArray(); + break; + + // Array + case 10: + $value = $this->readArray(); + break; + + // Date + case 11: + $value = $this->readDate(); + break; + + // Long string + case 13: + $value = $this->readLongString(); + break; + + // XML (handled as string) + case 15: + $value = $this->readXML(); + break; + + // Typed object (handled as object) + case 16: + $value = $this->readTypedObject(); + break; + + // Long string + default: + $value = '(unknown or unsupported data type)'; + break; + } + + return $value; + } + + /** + * @return float|false + */ + public function readDouble() { + return $this->stream->readDouble(); + } + + /** + * @return bool + */ + public function readBoolean() { + return $this->stream->readByte() == 1; + } + + /** + * @return string + */ + public function readString() { + return $this->stream->readUTF(); + } + + /** + * @return array + */ + public function readObject() { + // Get highest numerical index - ignored +// $highestIndex = $this->stream->readLong(); + + $data = array(); + $key = null; + + while ($key = $this->stream->readUTF()) { + $data[$key] = $this->readData(); + } + // Mixed array record ends with empty string (0x00 0x00) and 0x09 + if (($key == '') && ($this->stream->peekByte() == 0x09)) { + // Consume byte + $this->stream->readByte(); + } + return $data; + } + + /** + * @return array + */ + public function readMixedArray() { + // Get highest numerical index - ignored + $highestIndex = $this->stream->readLong(); + + $data = array(); + $key = null; + + while ($key = $this->stream->readUTF()) { + if (is_numeric($key)) { + $key = (int) $key; + } + $data[$key] = $this->readData(); + } + // Mixed array record ends with empty string (0x00 0x00) and 0x09 + if (($key == '') && ($this->stream->peekByte() == 0x09)) { + // Consume byte + $this->stream->readByte(); + } + + return $data; + } + + /** + * @return array + */ + public function readArray() { + $length = $this->stream->readLong(); + $data = array(); + + for ($i = 0; $i < $length; $i++) { + $data[] = $this->readData(); + } + return $data; + } + + /** + * @return float|false + */ + public function readDate() { + $timestamp = $this->stream->readDouble(); + $timezone = $this->stream->readInt(); + return $timestamp; + } + + /** + * @return string + */ + public function readLongString() { + return $this->stream->readLongUTF(); + } + + /** + * @return string + */ + public function readXML() { + return $this->stream->readLongUTF(); + } + + /** + * @return array + */ + public function readTypedObject() { + $className = $this->stream->readUTF(); + return $this->readObject(); + } +} + +class AVCSequenceParameterSetReader +{ + /** + * @var string + */ + public $sps; + public $start = 0; + public $currentBytes = 0; + public $currentBits = 0; + + /** + * @var int + */ + public $width; + + /** + * @var int + */ + public $height; + + /** + * @param string $sps + */ + public function __construct($sps) { + $this->sps = $sps; + } + + public function readData() { + $this->skipBits(8); + $this->skipBits(8); + $profile = $this->getBits(8); // read profile + if ($profile > 0) { + $this->skipBits(8); + $level_idc = $this->getBits(8); // level_idc + $this->expGolombUe(); // seq_parameter_set_id // sps + $this->expGolombUe(); // log2_max_frame_num_minus4 + $picOrderType = $this->expGolombUe(); // pic_order_cnt_type + if ($picOrderType == 0) { + $this->expGolombUe(); // log2_max_pic_order_cnt_lsb_minus4 + } elseif ($picOrderType == 1) { + $this->skipBits(1); // delta_pic_order_always_zero_flag + $this->expGolombSe(); // offset_for_non_ref_pic + $this->expGolombSe(); // offset_for_top_to_bottom_field + $num_ref_frames_in_pic_order_cnt_cycle = $this->expGolombUe(); // num_ref_frames_in_pic_order_cnt_cycle + for ($i = 0; $i < $num_ref_frames_in_pic_order_cnt_cycle; $i++) { + $this->expGolombSe(); // offset_for_ref_frame[ i ] + } + } + $this->expGolombUe(); // num_ref_frames + $this->skipBits(1); // gaps_in_frame_num_value_allowed_flag + $pic_width_in_mbs_minus1 = $this->expGolombUe(); // pic_width_in_mbs_minus1 + $pic_height_in_map_units_minus1 = $this->expGolombUe(); // pic_height_in_map_units_minus1 + + $frame_mbs_only_flag = $this->getBits(1); // frame_mbs_only_flag + if ($frame_mbs_only_flag == 0) { + $this->skipBits(1); // mb_adaptive_frame_field_flag + } + $this->skipBits(1); // direct_8x8_inference_flag + $frame_cropping_flag = $this->getBits(1); // frame_cropping_flag + + $frame_crop_left_offset = 0; + $frame_crop_right_offset = 0; + $frame_crop_top_offset = 0; + $frame_crop_bottom_offset = 0; + + if ($frame_cropping_flag) { + $frame_crop_left_offset = $this->expGolombUe(); // frame_crop_left_offset + $frame_crop_right_offset = $this->expGolombUe(); // frame_crop_right_offset + $frame_crop_top_offset = $this->expGolombUe(); // frame_crop_top_offset + $frame_crop_bottom_offset = $this->expGolombUe(); // frame_crop_bottom_offset + } + $this->skipBits(1); // vui_parameters_present_flag + // etc + + $this->width = (($pic_width_in_mbs_minus1 + 1) * 16) - ($frame_crop_left_offset * 2) - ($frame_crop_right_offset * 2); + $this->height = ((2 - $frame_mbs_only_flag) * ($pic_height_in_map_units_minus1 + 1) * 16) - ($frame_crop_top_offset * 2) - ($frame_crop_bottom_offset * 2); + } + } + + /** + * @param int $bits + */ + public function skipBits($bits) { + $newBits = $this->currentBits + $bits; + $this->currentBytes += (int)floor($newBits / 8); + $this->currentBits = $newBits % 8; + } + + /** + * @return int + */ + public function getBit() { + $result = (getid3_lib::BigEndian2Int(substr($this->sps, $this->currentBytes, 1)) >> (7 - $this->currentBits)) & 0x01; + $this->skipBits(1); + return $result; + } + + /** + * @param int $bits + * + * @return int + */ + public function getBits($bits) { + $result = 0; + for ($i = 0; $i < $bits; $i++) { + $result = ($result << 1) + $this->getBit(); + } + return $result; + } + + /** + * @return int + */ + public function expGolombUe() { + $significantBits = 0; + $bit = $this->getBit(); + while ($bit == 0) { + $significantBits++; + $bit = $this->getBit(); + + if ($significantBits > 31) { + // something is broken, this is an emergency escape to prevent infinite loops + return 0; + } + } + return (1 << $significantBits) + $this->getBits($significantBits) - 1; + } + + /** + * @return int + */ + public function expGolombSe() { + $result = $this->expGolombUe(); + if (($result & 0x01) == 0) { + return -($result >> 1); + } else { + return ($result + 1) >> 1; + } + } + + /** + * @return int + */ + public function getWidth() { + return $this->width; + } + + /** + * @return int + */ + public function getHeight() { + return $this->height; + } +} diff --git a/projects/tests/tests/performance/ID3/module.audio-video.matroska.php b/projects/tests/tests/performance/ID3/module.audio-video.matroska.php new file mode 100644 index 00000000..a2851085 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio-video.matroska.php @@ -0,0 +1,1922 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio-video.matriska.php // +// module for analyzing Matroska containers // +// dependencies: NONE // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} + +define('EBML_ID_CHAPTERS', 0x0043A770); // [10][43][A7][70] -- A system to define basic menus and partition data. For more detailed information, look at the Chapters Explanation. +define('EBML_ID_SEEKHEAD', 0x014D9B74); // [11][4D][9B][74] -- Contains the position of other level 1 elements. +define('EBML_ID_TAGS', 0x0254C367); // [12][54][C3][67] -- Element containing elements specific to Tracks/Chapters. A list of valid tags can be found . +define('EBML_ID_INFO', 0x0549A966); // [15][49][A9][66] -- Contains miscellaneous general information and statistics on the file. +define('EBML_ID_TRACKS', 0x0654AE6B); // [16][54][AE][6B] -- A top-level block of information with many tracks described. +define('EBML_ID_SEGMENT', 0x08538067); // [18][53][80][67] -- This element contains all other top-level (level 1) elements. Typically a Matroska file is composed of 1 segment. +define('EBML_ID_ATTACHMENTS', 0x0941A469); // [19][41][A4][69] -- Contain attached files. +define('EBML_ID_EBML', 0x0A45DFA3); // [1A][45][DF][A3] -- Set the EBML characteristics of the data to follow. Each EBML document has to start with this. +define('EBML_ID_CUES', 0x0C53BB6B); // [1C][53][BB][6B] -- A top-level element to speed seeking access. All entries are local to the segment. +define('EBML_ID_CLUSTER', 0x0F43B675); // [1F][43][B6][75] -- The lower level element containing the (monolithic) Block structure. +define('EBML_ID_LANGUAGE', 0x02B59C); // [22][B5][9C] -- Specifies the language of the track in the Matroska languages form. +define('EBML_ID_TRACKTIMECODESCALE', 0x03314F); // [23][31][4F] -- The scale to apply on this track to work at normal speed in relation with other tracks (mostly used to adjust video speed when the audio length differs). +define('EBML_ID_DEFAULTDURATION', 0x03E383); // [23][E3][83] -- Number of nanoseconds (i.e. not scaled) per frame. +define('EBML_ID_CODECNAME', 0x058688); // [25][86][88] -- A human-readable string specifying the codec. +define('EBML_ID_CODECDOWNLOADURL', 0x06B240); // [26][B2][40] -- A URL to download about the codec used. +define('EBML_ID_TIMECODESCALE', 0x0AD7B1); // [2A][D7][B1] -- Timecode scale in nanoseconds (1.000.000 means all timecodes in the segment are expressed in milliseconds). +define('EBML_ID_COLOURSPACE', 0x0EB524); // [2E][B5][24] -- Same value as in AVI (32 bits). +define('EBML_ID_GAMMAVALUE', 0x0FB523); // [2F][B5][23] -- Gamma Value. +define('EBML_ID_CODECSETTINGS', 0x1A9697); // [3A][96][97] -- A string describing the encoding setting used. +define('EBML_ID_CODECINFOURL', 0x1B4040); // [3B][40][40] -- A URL to find information about the codec used. +define('EBML_ID_PREVFILENAME', 0x1C83AB); // [3C][83][AB] -- An escaped filename corresponding to the previous segment. +define('EBML_ID_PREVUID', 0x1CB923); // [3C][B9][23] -- A unique ID to identify the previous chained segment (128 bits). +define('EBML_ID_NEXTFILENAME', 0x1E83BB); // [3E][83][BB] -- An escaped filename corresponding to the next segment. +define('EBML_ID_NEXTUID', 0x1EB923); // [3E][B9][23] -- A unique ID to identify the next chained segment (128 bits). +define('EBML_ID_CONTENTCOMPALGO', 0x0254); // [42][54] -- The compression algorithm used. Algorithms that have been specified so far are: +define('EBML_ID_CONTENTCOMPSETTINGS', 0x0255); // [42][55] -- Settings that might be needed by the decompressor. For Header Stripping (ContentCompAlgo=3), the bytes that were removed from the beggining of each frames of the track. +define('EBML_ID_DOCTYPE', 0x0282); // [42][82] -- A string that describes the type of document that follows this EBML header ('matroska' in our case). +define('EBML_ID_DOCTYPEREADVERSION', 0x0285); // [42][85] -- The minimum DocType version an interpreter has to support to read this file. +define('EBML_ID_EBMLVERSION', 0x0286); // [42][86] -- The version of EBML parser used to create the file. +define('EBML_ID_DOCTYPEVERSION', 0x0287); // [42][87] -- The version of DocType interpreter used to create the file. +define('EBML_ID_EBMLMAXIDLENGTH', 0x02F2); // [42][F2] -- The maximum length of the IDs you'll find in this file (4 or less in Matroska). +define('EBML_ID_EBMLMAXSIZELENGTH', 0x02F3); // [42][F3] -- The maximum length of the sizes you'll find in this file (8 or less in Matroska). This does not override the element size indicated at the beginning of an element. Elements that have an indicated size which is larger than what is allowed by EBMLMaxSizeLength shall be considered invalid. +define('EBML_ID_EBMLREADVERSION', 0x02F7); // [42][F7] -- The minimum EBML version a parser has to support to read this file. +define('EBML_ID_CHAPLANGUAGE', 0x037C); // [43][7C] -- The languages corresponding to the string, in the bibliographic ISO-639-2 form. +define('EBML_ID_CHAPCOUNTRY', 0x037E); // [43][7E] -- The countries corresponding to the string, same 2 octets as in Internet domains. +define('EBML_ID_SEGMENTFAMILY', 0x0444); // [44][44] -- A randomly generated unique ID that all segments related to each other must use (128 bits). +define('EBML_ID_DATEUTC', 0x0461); // [44][61] -- Date of the origin of timecode (value 0), i.e. production date. +define('EBML_ID_TAGLANGUAGE', 0x047A); // [44][7A] -- Specifies the language of the tag specified, in the Matroska languages form. +define('EBML_ID_TAGDEFAULT', 0x0484); // [44][84] -- Indication to know if this is the default/original language to use for the given tag. +define('EBML_ID_TAGBINARY', 0x0485); // [44][85] -- The values of the Tag if it is binary. Note that this cannot be used in the same SimpleTag as TagString. +define('EBML_ID_TAGSTRING', 0x0487); // [44][87] -- The value of the Tag. +define('EBML_ID_DURATION', 0x0489); // [44][89] -- Duration of the segment (based on TimecodeScale). +define('EBML_ID_CHAPPROCESSPRIVATE', 0x050D); // [45][0D] -- Some optional data attached to the ChapProcessCodecID information. For ChapProcessCodecID = 1, it is the "DVD level" equivalent. +define('EBML_ID_CHAPTERFLAGENABLED', 0x0598); // [45][98] -- Specify wether the chapter is enabled. It can be enabled/disabled by a Control Track. When disabled, the movie should skip all the content between the TimeStart and TimeEnd of this chapter. +define('EBML_ID_TAGNAME', 0x05A3); // [45][A3] -- The name of the Tag that is going to be stored. +define('EBML_ID_EDITIONENTRY', 0x05B9); // [45][B9] -- Contains all information about a segment edition. +define('EBML_ID_EDITIONUID', 0x05BC); // [45][BC] -- A unique ID to identify the edition. It's useful for tagging an edition. +define('EBML_ID_EDITIONFLAGHIDDEN', 0x05BD); // [45][BD] -- If an edition is hidden (1), it should not be available to the user interface (but still to Control Tracks). +define('EBML_ID_EDITIONFLAGDEFAULT', 0x05DB); // [45][DB] -- If a flag is set (1) the edition should be used as the default one. +define('EBML_ID_EDITIONFLAGORDERED', 0x05DD); // [45][DD] -- Specify if the chapters can be defined multiple times and the order to play them is enforced. +define('EBML_ID_FILEDATA', 0x065C); // [46][5C] -- The data of the file. +define('EBML_ID_FILEMIMETYPE', 0x0660); // [46][60] -- MIME type of the file. +define('EBML_ID_FILENAME', 0x066E); // [46][6E] -- Filename of the attached file. +define('EBML_ID_FILEREFERRAL', 0x0675); // [46][75] -- A binary value that a track/codec can refer to when the attachment is needed. +define('EBML_ID_FILEDESCRIPTION', 0x067E); // [46][7E] -- A human-friendly name for the attached file. +define('EBML_ID_FILEUID', 0x06AE); // [46][AE] -- Unique ID representing the file, as random as possible. +define('EBML_ID_CONTENTENCALGO', 0x07E1); // [47][E1] -- The encryption algorithm used. The value '0' means that the contents have not been encrypted but only signed. Predefined values: +define('EBML_ID_CONTENTENCKEYID', 0x07E2); // [47][E2] -- For public key algorithms this is the ID of the public key the data was encrypted with. +define('EBML_ID_CONTENTSIGNATURE', 0x07E3); // [47][E3] -- A cryptographic signature of the contents. +define('EBML_ID_CONTENTSIGKEYID', 0x07E4); // [47][E4] -- This is the ID of the private key the data was signed with. +define('EBML_ID_CONTENTSIGALGO', 0x07E5); // [47][E5] -- The algorithm used for the signature. A value of '0' means that the contents have not been signed but only encrypted. Predefined values: +define('EBML_ID_CONTENTSIGHASHALGO', 0x07E6); // [47][E6] -- The hash algorithm used for the signature. A value of '0' means that the contents have not been signed but only encrypted. Predefined values: +define('EBML_ID_MUXINGAPP', 0x0D80); // [4D][80] -- Muxing application or library ("libmatroska-0.4.3"). +define('EBML_ID_SEEK', 0x0DBB); // [4D][BB] -- Contains a single seek entry to an EBML element. +define('EBML_ID_CONTENTENCODINGORDER', 0x1031); // [50][31] -- Tells when this modification was used during encoding/muxing starting with 0 and counting upwards. The decoder/demuxer has to start with the highest order number it finds and work its way down. This value has to be unique over all ContentEncodingOrder elements in the segment. +define('EBML_ID_CONTENTENCODINGSCOPE', 0x1032); // [50][32] -- A bit field that describes which elements have been modified in this way. Values (big endian) can be OR'ed. Possible values: +define('EBML_ID_CONTENTENCODINGTYPE', 0x1033); // [50][33] -- A value describing what kind of transformation has been done. Possible values: +define('EBML_ID_CONTENTCOMPRESSION', 0x1034); // [50][34] -- Settings describing the compression used. Must be present if the value of ContentEncodingType is 0 and absent otherwise. Each block must be decompressable even if no previous block is available in order not to prevent seeking. +define('EBML_ID_CONTENTENCRYPTION', 0x1035); // [50][35] -- Settings describing the encryption used. Must be present if the value of ContentEncodingType is 1 and absent otherwise. +define('EBML_ID_CUEREFNUMBER', 0x135F); // [53][5F] -- Number of the referenced Block of Track X in the specified Cluster. +define('EBML_ID_NAME', 0x136E); // [53][6E] -- A human-readable track name. +define('EBML_ID_CUEBLOCKNUMBER', 0x1378); // [53][78] -- Number of the Block in the specified Cluster. +define('EBML_ID_TRACKOFFSET', 0x137F); // [53][7F] -- A value to add to the Block's Timecode. This can be used to adjust the playback offset of a track. +define('EBML_ID_SEEKID', 0x13AB); // [53][AB] -- The binary ID corresponding to the element name. +define('EBML_ID_SEEKPOSITION', 0x13AC); // [53][AC] -- The position of the element in the segment in octets (0 = first level 1 element). +define('EBML_ID_STEREOMODE', 0x13B8); // [53][B8] -- Stereo-3D video mode. +define('EBML_ID_OLDSTEREOMODE', 0x13B9); // [53][B9] -- Bogus StereoMode value used in old versions of libmatroska. DO NOT USE. (0: mono, 1: right eye, 2: left eye, 3: both eyes). +define('EBML_ID_PIXELCROPBOTTOM', 0x14AA); // [54][AA] -- The number of video pixels to remove at the bottom of the image (for HDTV content). +define('EBML_ID_DISPLAYWIDTH', 0x14B0); // [54][B0] -- Width of the video frames to display. +define('EBML_ID_DISPLAYUNIT', 0x14B2); // [54][B2] -- Type of the unit for DisplayWidth/Height (0: pixels, 1: centimeters, 2: inches). +define('EBML_ID_ASPECTRATIOTYPE', 0x14B3); // [54][B3] -- Specify the possible modifications to the aspect ratio (0: free resizing, 1: keep aspect ratio, 2: fixed). +define('EBML_ID_DISPLAYHEIGHT', 0x14BA); // [54][BA] -- Height of the video frames to display. +define('EBML_ID_PIXELCROPTOP', 0x14BB); // [54][BB] -- The number of video pixels to remove at the top of the image. +define('EBML_ID_PIXELCROPLEFT', 0x14CC); // [54][CC] -- The number of video pixels to remove on the left of the image. +define('EBML_ID_PIXELCROPRIGHT', 0x14DD); // [54][DD] -- The number of video pixels to remove on the right of the image. +define('EBML_ID_FLAGFORCED', 0x15AA); // [55][AA] -- Set if that track MUST be used during playback. There can be many forced track for a kind (audio, video or subs), the player should select the one which language matches the user preference or the default + forced track. Overlay MAY happen between a forced and non-forced track of the same kind. +define('EBML_ID_MAXBLOCKADDITIONID', 0x15EE); // [55][EE] -- The maximum value of BlockAddID. A value 0 means there is no BlockAdditions for this track. +define('EBML_ID_WRITINGAPP', 0x1741); // [57][41] -- Writing application ("mkvmerge-0.3.3"). +define('EBML_ID_CLUSTERSILENTTRACKS', 0x1854); // [58][54] -- The list of tracks that are not used in that part of the stream. It is useful when using overlay tracks on seeking. Then you should decide what track to use. +define('EBML_ID_CLUSTERSILENTTRACKNUMBER', 0x18D7); // [58][D7] -- One of the track number that are not used from now on in the stream. It could change later if not specified as silent in a further Cluster. +define('EBML_ID_ATTACHEDFILE', 0x21A7); // [61][A7] -- An attached file. +define('EBML_ID_CONTENTENCODING', 0x2240); // [62][40] -- Settings for one content encoding like compression or encryption. +define('EBML_ID_BITDEPTH', 0x2264); // [62][64] -- Bits per sample, mostly used for PCM. +define('EBML_ID_CODECPRIVATE', 0x23A2); // [63][A2] -- Private data only known to the codec. +define('EBML_ID_TARGETS', 0x23C0); // [63][C0] -- Contain all UIDs where the specified meta data apply. It is void to describe everything in the segment. +define('EBML_ID_CHAPTERPHYSICALEQUIV', 0x23C3); // [63][C3] -- Specify the physical equivalent of this ChapterAtom like "DVD" (60) or "SIDE" (50), see complete list of values. +define('EBML_ID_TAGCHAPTERUID', 0x23C4); // [63][C4] -- A unique ID to identify the Chapter(s) the tags belong to. If the value is 0 at this level, the tags apply to all chapters in the Segment. +define('EBML_ID_TAGTRACKUID', 0x23C5); // [63][C5] -- A unique ID to identify the Track(s) the tags belong to. If the value is 0 at this level, the tags apply to all tracks in the Segment. +define('EBML_ID_TAGATTACHMENTUID', 0x23C6); // [63][C6] -- A unique ID to identify the Attachment(s) the tags belong to. If the value is 0 at this level, the tags apply to all the attachments in the Segment. +define('EBML_ID_TAGEDITIONUID', 0x23C9); // [63][C9] -- A unique ID to identify the EditionEntry(s) the tags belong to. If the value is 0 at this level, the tags apply to all editions in the Segment. +define('EBML_ID_TARGETTYPE', 0x23CA); // [63][CA] -- An informational string that can be used to display the logical level of the target like "ALBUM", "TRACK", "MOVIE", "CHAPTER", etc (see TargetType). +define('EBML_ID_TRACKTRANSLATE', 0x2624); // [66][24] -- The track identification for the given Chapter Codec. +define('EBML_ID_TRACKTRANSLATETRACKID', 0x26A5); // [66][A5] -- The binary value used to represent this track in the chapter codec data. The format depends on the ChapProcessCodecID used. +define('EBML_ID_TRACKTRANSLATECODEC', 0x26BF); // [66][BF] -- The chapter codec using this ID (0: Matroska Script, 1: DVD-menu). +define('EBML_ID_TRACKTRANSLATEEDITIONUID', 0x26FC); // [66][FC] -- Specify an edition UID on which this translation applies. When not specified, it means for all editions found in the segment. +define('EBML_ID_SIMPLETAG', 0x27C8); // [67][C8] -- Contains general information about the target. +define('EBML_ID_TARGETTYPEVALUE', 0x28CA); // [68][CA] -- A number to indicate the logical level of the target (see TargetType). +define('EBML_ID_CHAPPROCESSCOMMAND', 0x2911); // [69][11] -- Contains all the commands associated to the Atom. +define('EBML_ID_CHAPPROCESSTIME', 0x2922); // [69][22] -- Defines when the process command should be handled (0: during the whole chapter, 1: before starting playback, 2: after playback of the chapter). +define('EBML_ID_CHAPTERTRANSLATE', 0x2924); // [69][24] -- A tuple of corresponding ID used by chapter codecs to represent this segment. +define('EBML_ID_CHAPPROCESSDATA', 0x2933); // [69][33] -- Contains the command information. The data should be interpreted depending on the ChapProcessCodecID value. For ChapProcessCodecID = 1, the data correspond to the binary DVD cell pre/post commands. +define('EBML_ID_CHAPPROCESS', 0x2944); // [69][44] -- Contains all the commands associated to the Atom. +define('EBML_ID_CHAPPROCESSCODECID', 0x2955); // [69][55] -- Contains the type of the codec used for the processing. A value of 0 means native Matroska processing (to be defined), a value of 1 means the DVD command set is used. More codec IDs can be added later. +define('EBML_ID_CHAPTERTRANSLATEID', 0x29A5); // [69][A5] -- The binary value used to represent this segment in the chapter codec data. The format depends on the ChapProcessCodecID used. +define('EBML_ID_CHAPTERTRANSLATECODEC', 0x29BF); // [69][BF] -- The chapter codec using this ID (0: Matroska Script, 1: DVD-menu). +define('EBML_ID_CHAPTERTRANSLATEEDITIONUID', 0x29FC); // [69][FC] -- Specify an edition UID on which this correspondance applies. When not specified, it means for all editions found in the segment. +define('EBML_ID_CONTENTENCODINGS', 0x2D80); // [6D][80] -- Settings for several content encoding mechanisms like compression or encryption. +define('EBML_ID_MINCACHE', 0x2DE7); // [6D][E7] -- The minimum number of frames a player should be able to cache during playback. If set to 0, the reference pseudo-cache system is not used. +define('EBML_ID_MAXCACHE', 0x2DF8); // [6D][F8] -- The maximum cache size required to store referenced frames in and the current frame. 0 means no cache is needed. +define('EBML_ID_CHAPTERSEGMENTUID', 0x2E67); // [6E][67] -- A segment to play in place of this chapter. Edition ChapterSegmentEditionUID should be used for this segment, otherwise no edition is used. +define('EBML_ID_CHAPTERSEGMENTEDITIONUID', 0x2EBC); // [6E][BC] -- The edition to play from the segment linked in ChapterSegmentUID. +define('EBML_ID_TRACKOVERLAY', 0x2FAB); // [6F][AB] -- Specify that this track is an overlay track for the Track specified (in the u-integer). That means when this track has a gap (see SilentTracks) the overlay track should be used instead. The order of multiple TrackOverlay matters, the first one is the one that should be used. If not found it should be the second, etc. +define('EBML_ID_TAG', 0x3373); // [73][73] -- Element containing elements specific to Tracks/Chapters. +define('EBML_ID_SEGMENTFILENAME', 0x3384); // [73][84] -- A filename corresponding to this segment. +define('EBML_ID_SEGMENTUID', 0x33A4); // [73][A4] -- A randomly generated unique ID to identify the current segment between many others (128 bits). +define('EBML_ID_CHAPTERUID', 0x33C4); // [73][C4] -- A unique ID to identify the Chapter. +define('EBML_ID_TRACKUID', 0x33C5); // [73][C5] -- A unique ID to identify the Track. This should be kept the same when making a direct stream copy of the Track to another file. +define('EBML_ID_ATTACHMENTLINK', 0x3446); // [74][46] -- The UID of an attachment that is used by this codec. +define('EBML_ID_CLUSTERBLOCKADDITIONS', 0x35A1); // [75][A1] -- Contain additional blocks to complete the main one. An EBML parser that has no knowledge of the Block structure could still see and use/skip these data. +define('EBML_ID_CHANNELPOSITIONS', 0x347B); // [7D][7B] -- Table of horizontal angles for each successive channel, see appendix. +define('EBML_ID_OUTPUTSAMPLINGFREQUENCY', 0x38B5); // [78][B5] -- Real output sampling frequency in Hz (used for SBR techniques). +define('EBML_ID_TITLE', 0x3BA9); // [7B][A9] -- General name of the segment. +define('EBML_ID_CHAPTERDISPLAY', 0x00); // [80] -- Contains all possible strings to use for the chapter display. +define('EBML_ID_TRACKTYPE', 0x03); // [83] -- A set of track types coded on 8 bits (1: video, 2: audio, 3: complex, 0x10: logo, 0x11: subtitle, 0x12: buttons, 0x20: control). +define('EBML_ID_CHAPSTRING', 0x05); // [85] -- Contains the string to use as the chapter atom. +define('EBML_ID_CODECID', 0x06); // [86] -- An ID corresponding to the codec, see the codec page for more info. +define('EBML_ID_FLAGDEFAULT', 0x08); // [88] -- Set if that track (audio, video or subs) SHOULD be used if no language found matches the user preference. +define('EBML_ID_CHAPTERTRACKNUMBER', 0x09); // [89] -- UID of the Track to apply this chapter too. In the absense of a control track, choosing this chapter will select the listed Tracks and deselect unlisted tracks. Absense of this element indicates that the Chapter should be applied to any currently used Tracks. +define('EBML_ID_CLUSTERSLICES', 0x0E); // [8E] -- Contains slices description. +define('EBML_ID_CHAPTERTRACK', 0x0F); // [8F] -- List of tracks on which the chapter applies. If this element is not present, all tracks apply +define('EBML_ID_CHAPTERTIMESTART', 0x11); // [91] -- Timecode of the start of Chapter (not scaled). +define('EBML_ID_CHAPTERTIMEEND', 0x12); // [92] -- Timecode of the end of Chapter (timecode excluded, not scaled). +define('EBML_ID_CUEREFTIME', 0x16); // [96] -- Timecode of the referenced Block. +define('EBML_ID_CUEREFCLUSTER', 0x17); // [97] -- Position of the Cluster containing the referenced Block. +define('EBML_ID_CHAPTERFLAGHIDDEN', 0x18); // [98] -- If a chapter is hidden (1), it should not be available to the user interface (but still to Control Tracks). +define('EBML_ID_FLAGINTERLACED', 0x1A); // [9A] -- Set if the video is interlaced. +define('EBML_ID_CLUSTERBLOCKDURATION', 0x1B); // [9B] -- The duration of the Block (based on TimecodeScale). This element is mandatory when DefaultDuration is set for the track. When not written and with no DefaultDuration, the value is assumed to be the difference between the timecode of this Block and the timecode of the next Block in "display" order (not coding order). This element can be useful at the end of a Track (as there is not other Block available), or when there is a break in a track like for subtitle tracks. +define('EBML_ID_FLAGLACING', 0x1C); // [9C] -- Set if the track may contain blocks using lacing. +define('EBML_ID_CHANNELS', 0x1F); // [9F] -- Numbers of channels in the track. +define('EBML_ID_CLUSTERBLOCKGROUP', 0x20); // [A0] -- Basic container of information containing a single Block or BlockVirtual, and information specific to that Block/VirtualBlock. +define('EBML_ID_CLUSTERBLOCK', 0x21); // [A1] -- Block containing the actual data to be rendered and a timecode relative to the Cluster Timecode. +define('EBML_ID_CLUSTERBLOCKVIRTUAL', 0x22); // [A2] -- A Block with no data. It must be stored in the stream at the place the real Block should be in display order. +define('EBML_ID_CLUSTERSIMPLEBLOCK', 0x23); // [A3] -- Similar to Block but without all the extra information, mostly used to reduced overhead when no extra feature is needed. +define('EBML_ID_CLUSTERCODECSTATE', 0x24); // [A4] -- The new codec state to use. Data interpretation is private to the codec. This information should always be referenced by a seek entry. +define('EBML_ID_CLUSTERBLOCKADDITIONAL', 0x25); // [A5] -- Interpreted by the codec as it wishes (using the BlockAddID). +define('EBML_ID_CLUSTERBLOCKMORE', 0x26); // [A6] -- Contain the BlockAdditional and some parameters. +define('EBML_ID_CLUSTERPOSITION', 0x27); // [A7] -- Position of the Cluster in the segment (0 in live broadcast streams). It might help to resynchronise offset on damaged streams. +define('EBML_ID_CODECDECODEALL', 0x2A); // [AA] -- The codec can decode potentially damaged data. +define('EBML_ID_CLUSTERPREVSIZE', 0x2B); // [AB] -- Size of the previous Cluster, in octets. Can be useful for backward playing. +define('EBML_ID_TRACKENTRY', 0x2E); // [AE] -- Describes a track with all elements. +define('EBML_ID_CLUSTERENCRYPTEDBLOCK', 0x2F); // [AF] -- Similar to SimpleBlock but the data inside the Block are Transformed (encrypt and/or signed). +define('EBML_ID_PIXELWIDTH', 0x30); // [B0] -- Width of the encoded video frames in pixels. +define('EBML_ID_CUETIME', 0x33); // [B3] -- Absolute timecode according to the segment time base. +define('EBML_ID_SAMPLINGFREQUENCY', 0x35); // [B5] -- Sampling frequency in Hz. +define('EBML_ID_CHAPTERATOM', 0x36); // [B6] -- Contains the atom information to use as the chapter atom (apply to all tracks). +define('EBML_ID_CUETRACKPOSITIONS', 0x37); // [B7] -- Contain positions for different tracks corresponding to the timecode. +define('EBML_ID_FLAGENABLED', 0x39); // [B9] -- Set if the track is used. +define('EBML_ID_PIXELHEIGHT', 0x3A); // [BA] -- Height of the encoded video frames in pixels. +define('EBML_ID_CUEPOINT', 0x3B); // [BB] -- Contains all information relative to a seek point in the segment. +define('EBML_ID_CRC32', 0x3F); // [BF] -- The CRC is computed on all the data of the Master element it's in, regardless of its position. It's recommended to put the CRC value at the beggining of the Master element for easier reading. All level 1 elements should include a CRC-32. +define('EBML_ID_CLUSTERBLOCKADDITIONID', 0x4B); // [CB] -- The ID of the BlockAdditional element (0 is the main Block). +define('EBML_ID_CLUSTERLACENUMBER', 0x4C); // [CC] -- The reverse number of the frame in the lace (0 is the last frame, 1 is the next to last, etc). While there are a few files in the wild with this element, it is no longer in use and has been deprecated. Being able to interpret this element is not required for playback. +define('EBML_ID_CLUSTERFRAMENUMBER', 0x4D); // [CD] -- The number of the frame to generate from this lace with this delay (allow you to generate many frames from the same Block/Frame). +define('EBML_ID_CLUSTERDELAY', 0x4E); // [CE] -- The (scaled) delay to apply to the element. +define('EBML_ID_CLUSTERDURATION', 0x4F); // [CF] -- The (scaled) duration to apply to the element. +define('EBML_ID_TRACKNUMBER', 0x57); // [D7] -- The track number as used in the Block Header (using more than 127 tracks is not encouraged, though the design allows an unlimited number). +define('EBML_ID_CUEREFERENCE', 0x5B); // [DB] -- The Clusters containing the required referenced Blocks. +define('EBML_ID_VIDEO', 0x60); // [E0] -- Video settings. +define('EBML_ID_AUDIO', 0x61); // [E1] -- Audio settings. +define('EBML_ID_CLUSTERTIMESLICE', 0x68); // [E8] -- Contains extra time information about the data contained in the Block. While there are a few files in the wild with this element, it is no longer in use and has been deprecated. Being able to interpret this element is not required for playback. +define('EBML_ID_CUECODECSTATE', 0x6A); // [EA] -- The position of the Codec State corresponding to this Cue element. 0 means that the data is taken from the initial Track Entry. +define('EBML_ID_CUEREFCODECSTATE', 0x6B); // [EB] -- The position of the Codec State corresponding to this referenced element. 0 means that the data is taken from the initial Track Entry. +define('EBML_ID_VOID', 0x6C); // [EC] -- Used to void damaged data, to avoid unexpected behaviors when using damaged data. The content is discarded. Also used to reserve space in a sub-element for later use. +define('EBML_ID_CLUSTERTIMECODE', 0x67); // [E7] -- Absolute timecode of the cluster (based on TimecodeScale). +define('EBML_ID_CLUSTERBLOCKADDID', 0x6E); // [EE] -- An ID to identify the BlockAdditional level. +define('EBML_ID_CUECLUSTERPOSITION', 0x71); // [F1] -- The position of the Cluster containing the required Block. +define('EBML_ID_CUETRACK', 0x77); // [F7] -- The track for which a position is given. +define('EBML_ID_CLUSTERREFERENCEPRIORITY', 0x7A); // [FA] -- This frame is referenced and has the specified cache priority. In cache only a frame of the same or higher priority can replace this frame. A value of 0 means the frame is not referenced. +define('EBML_ID_CLUSTERREFERENCEBLOCK', 0x7B); // [FB] -- Timecode of another frame used as a reference (ie: B or P frame). The timecode is relative to the block it's attached to. +define('EBML_ID_CLUSTERREFERENCEVIRTUAL', 0x7D); // [FD] -- Relative position of the data that should be in position of the virtual block. + + +/** +* @tutorial http://www.matroska.org/technical/specs/index.html +* +* @todo Rewrite EBML parser to reduce it's size and honor default element values +* @todo After rewrite implement stream size calculation, that will provide additional useful info and enable AAC/FLAC audio bitrate detection +*/ +class getid3_matroska extends getid3_handler +{ + /** + * If true, do not return information about CLUSTER chunks, since there's a lot of them + * and they're not usually useful [default: TRUE]. + * + * @var bool + */ + public static $hide_clusters = true; + + /** + * True to parse the whole file, not only header [default: FALSE]. + * + * @var bool + */ + public static $parse_whole_file = false; + + /* + * Private parser settings/placeholders. + */ + private $EBMLbuffer = ''; + private $EBMLbuffer_offset = 0; + private $EBMLbuffer_length = 0; + private $current_offset = 0; + private $unuseful_elements = array(EBML_ID_CRC32, EBML_ID_VOID); + + /** + * @return bool + */ + public function Analyze() + { + $info = &$this->getid3->info; + + // parse container + try { + $this->parseEBML($info); + } catch (Exception $e) { + $this->error('EBML parser: '.$e->getMessage()); + } + + // calculate playtime + if (isset($info['matroska']['info']) && is_array($info['matroska']['info'])) { + foreach ($info['matroska']['info'] as $key => $infoarray) { + if (isset($infoarray['Duration'])) { + // TimecodeScale is how many nanoseconds each Duration unit is + $info['playtime_seconds'] = $infoarray['Duration'] * ((isset($infoarray['TimecodeScale']) ? $infoarray['TimecodeScale'] : 1000000) / 1000000000); + break; + } + } + } + + // extract tags + if (isset($info['matroska']['tags']) && is_array($info['matroska']['tags'])) { + foreach ($info['matroska']['tags'] as $key => $infoarray) { + $this->ExtractCommentsSimpleTag($infoarray); + } + } + + // process tracks + if (isset($info['matroska']['tracks']['tracks']) && is_array($info['matroska']['tracks']['tracks'])) { + foreach ($info['matroska']['tracks']['tracks'] as $key => $trackarray) { + + $track_info = array(); + $track_info['dataformat'] = self::CodecIDtoCommonName($trackarray['CodecID']); + $track_info['default'] = (isset($trackarray['FlagDefault']) ? $trackarray['FlagDefault'] : true); + if (isset($trackarray['Name'])) { $track_info['name'] = $trackarray['Name']; } + + switch ($trackarray['TrackType']) { + + case 1: // Video + $track_info['resolution_x'] = $trackarray['PixelWidth']; + $track_info['resolution_y'] = $trackarray['PixelHeight']; + $track_info['display_unit'] = self::displayUnit(isset($trackarray['DisplayUnit']) ? $trackarray['DisplayUnit'] : 0); + $track_info['display_x'] = (isset($trackarray['DisplayWidth']) ? $trackarray['DisplayWidth'] : $trackarray['PixelWidth']); + $track_info['display_y'] = (isset($trackarray['DisplayHeight']) ? $trackarray['DisplayHeight'] : $trackarray['PixelHeight']); + + if (isset($trackarray['PixelCropBottom'])) { $track_info['crop_bottom'] = $trackarray['PixelCropBottom']; } + if (isset($trackarray['PixelCropTop'])) { $track_info['crop_top'] = $trackarray['PixelCropTop']; } + if (isset($trackarray['PixelCropLeft'])) { $track_info['crop_left'] = $trackarray['PixelCropLeft']; } + if (isset($trackarray['PixelCropRight'])) { $track_info['crop_right'] = $trackarray['PixelCropRight']; } + if (isset($trackarray['DefaultDuration'])) { $track_info['frame_rate'] = round(1000000000 / $trackarray['DefaultDuration'], 3); } + if (isset($trackarray['CodecName'])) { $track_info['codec'] = $trackarray['CodecName']; } + + switch ($trackarray['CodecID']) { + case 'V_MS/VFW/FOURCC': + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true); + + $parsed = getid3_riff::ParseBITMAPINFOHEADER($trackarray['CodecPrivate']); + $track_info['codec'] = getid3_riff::fourccLookup($parsed['fourcc']); + $info['matroska']['track_codec_parsed'][$trackarray['TrackNumber']] = $parsed; + break; + + /*case 'V_MPEG4/ISO/AVC': + $h264['profile'] = getid3_lib::BigEndian2Int(substr($trackarray['CodecPrivate'], 1, 1)); + $h264['level'] = getid3_lib::BigEndian2Int(substr($trackarray['CodecPrivate'], 3, 1)); + $rn = getid3_lib::BigEndian2Int(substr($trackarray['CodecPrivate'], 4, 1)); + $h264['NALUlength'] = ($rn & 3) + 1; + $rn = getid3_lib::BigEndian2Int(substr($trackarray['CodecPrivate'], 5, 1)); + $nsps = ($rn & 31); + $offset = 6; + for ($i = 0; $i < $nsps; $i ++) { + $length = getid3_lib::BigEndian2Int(substr($trackarray['CodecPrivate'], $offset, 2)); + $h264['SPS'][] = substr($trackarray['CodecPrivate'], $offset + 2, $length); + $offset += 2 + $length; + } + $npps = getid3_lib::BigEndian2Int(substr($trackarray['CodecPrivate'], $offset, 1)); + $offset += 1; + for ($i = 0; $i < $npps; $i ++) { + $length = getid3_lib::BigEndian2Int(substr($trackarray['CodecPrivate'], $offset, 2)); + $h264['PPS'][] = substr($trackarray['CodecPrivate'], $offset + 2, $length); + $offset += 2 + $length; + } + $info['matroska']['track_codec_parsed'][$trackarray['TrackNumber']] = $h264; + break;*/ + } + + $info['video']['streams'][$trackarray['TrackUID']] = $track_info; + break; + + case 2: // Audio + $track_info['sample_rate'] = (isset($trackarray['SamplingFrequency']) ? $trackarray['SamplingFrequency'] : 8000.0); + $track_info['channels'] = (isset($trackarray['Channels']) ? $trackarray['Channels'] : 1); + $track_info['language'] = (isset($trackarray['Language']) ? $trackarray['Language'] : 'eng'); + if (isset($trackarray['BitDepth'])) { $track_info['bits_per_sample'] = $trackarray['BitDepth']; } + if (isset($trackarray['CodecName'])) { $track_info['codec'] = $trackarray['CodecName']; } + + switch ($trackarray['CodecID']) { + case 'A_PCM/INT/LIT': + case 'A_PCM/INT/BIG': + $track_info['bitrate'] = $track_info['sample_rate'] * $track_info['channels'] * $trackarray['BitDepth']; + break; + + case 'A_AC3': + case 'A_EAC3': + case 'A_DTS': + case 'A_MPEG/L3': + case 'A_MPEG/L2': + case 'A_FLAC': + $module_dataformat = ($track_info['dataformat'] == 'mp2' ? 'mp3' : ($track_info['dataformat'] == 'eac3' ? 'ac3' : $track_info['dataformat'])); + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.'.$module_dataformat.'.php', __FILE__, true); + + if (!isset($info['matroska']['track_data_offsets'][$trackarray['TrackNumber']])) { + $this->warning('Unable to parse audio data ['.basename(__FILE__).':'.__LINE__.'] because $info[matroska][track_data_offsets]['.$trackarray['TrackNumber'].'] not set'); + break; + } + + // create temp instance + $getid3_temp = new getID3(); + if ($track_info['dataformat'] != 'flac') { + $getid3_temp->openfile($this->getid3->filename, $this->getid3->info['filesize'], $this->getid3->fp); + } + $getid3_temp->info['avdataoffset'] = $info['matroska']['track_data_offsets'][$trackarray['TrackNumber']]['offset']; + if ($track_info['dataformat'][0] == 'm' || $track_info['dataformat'] == 'flac') { + $getid3_temp->info['avdataend'] = $info['matroska']['track_data_offsets'][$trackarray['TrackNumber']]['offset'] + $info['matroska']['track_data_offsets'][$trackarray['TrackNumber']]['length']; + } + + // analyze + $class = 'getid3_'.$module_dataformat; + $header_data_key = $track_info['dataformat'][0] == 'm' ? 'mpeg' : $track_info['dataformat']; + $getid3_audio = new $class($getid3_temp, __CLASS__); + if ($track_info['dataformat'] == 'flac') { + $getid3_audio->AnalyzeString($trackarray['CodecPrivate']); + } + else { + $getid3_audio->Analyze(); + } + if (!empty($getid3_temp->info[$header_data_key])) { + $info['matroska']['track_codec_parsed'][$trackarray['TrackNumber']] = $getid3_temp->info[$header_data_key]; + if (isset($getid3_temp->info['audio']) && is_array($getid3_temp->info['audio'])) { + foreach ($getid3_temp->info['audio'] as $sub_key => $value) { + $track_info[$sub_key] = $value; + } + } + } + else { + $this->warning('Unable to parse audio data ['.basename(__FILE__).':'.__LINE__.'] because '.$class.'::Analyze() failed at offset '.$getid3_temp->info['avdataoffset']); + } + + // copy errors and warnings + if (!empty($getid3_temp->info['error'])) { + foreach ($getid3_temp->info['error'] as $newerror) { + $this->warning($class.'() says: ['.$newerror.']'); + } + } + if (!empty($getid3_temp->info['warning'])) { + foreach ($getid3_temp->info['warning'] as $newerror) { + $this->warning($class.'() says: ['.$newerror.']'); + } + } + unset($getid3_temp, $getid3_audio); + break; + + case 'A_AAC': + case 'A_AAC/MPEG2/LC': + case 'A_AAC/MPEG2/LC/SBR': + case 'A_AAC/MPEG4/LC': + case 'A_AAC/MPEG4/LC/SBR': + $this->warning($trackarray['CodecID'].' audio data contains no header, audio/video bitrates can\'t be calculated'); + break; + + case 'A_VORBIS': + if (!isset($trackarray['CodecPrivate'])) { + $this->warning('Unable to parse audio data ['.basename(__FILE__).':'.__LINE__.'] because CodecPrivate data not set'); + break; + } + $vorbis_offset = strpos($trackarray['CodecPrivate'], 'vorbis', 1); + if ($vorbis_offset === false) { + $this->warning('Unable to parse audio data ['.basename(__FILE__).':'.__LINE__.'] because CodecPrivate data does not contain "vorbis" keyword'); + break; + } + $vorbis_offset -= 1; + + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ogg.php', __FILE__, true); + + // create temp instance + $getid3_temp = new getID3(); + + // analyze + $getid3_ogg = new getid3_ogg($getid3_temp); + $oggpageinfo['page_seqno'] = 0; + $getid3_ogg->ParseVorbisPageHeader($trackarray['CodecPrivate'], $vorbis_offset, $oggpageinfo); + if (!empty($getid3_temp->info['ogg'])) { + $info['matroska']['track_codec_parsed'][$trackarray['TrackNumber']] = $getid3_temp->info['ogg']; + if (isset($getid3_temp->info['audio']) && is_array($getid3_temp->info['audio'])) { + foreach ($getid3_temp->info['audio'] as $sub_key => $value) { + $track_info[$sub_key] = $value; + } + } + } + + // copy errors and warnings + if (!empty($getid3_temp->info['error'])) { + foreach ($getid3_temp->info['error'] as $newerror) { + $this->warning('getid3_ogg() says: ['.$newerror.']'); + } + } + if (!empty($getid3_temp->info['warning'])) { + foreach ($getid3_temp->info['warning'] as $newerror) { + $this->warning('getid3_ogg() says: ['.$newerror.']'); + } + } + + if (!empty($getid3_temp->info['ogg']['bitrate_nominal'])) { + $track_info['bitrate'] = $getid3_temp->info['ogg']['bitrate_nominal']; + } + unset($getid3_temp, $getid3_ogg, $oggpageinfo, $vorbis_offset); + break; + + case 'A_MS/ACM': + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true); + + $parsed = getid3_riff::parseWAVEFORMATex($trackarray['CodecPrivate']); + foreach ($parsed as $sub_key => $value) { + if ($sub_key != 'raw') { + $track_info[$sub_key] = $value; + } + } + $info['matroska']['track_codec_parsed'][$trackarray['TrackNumber']] = $parsed; + break; + + default: + $this->warning('Unhandled audio type "'.(isset($trackarray['CodecID']) ? $trackarray['CodecID'] : '').'"'); + break; + } + + $info['audio']['streams'][$trackarray['TrackUID']] = $track_info; + break; + } + } + + if (!empty($info['video']['streams'])) { + $info['video'] = self::getDefaultStreamInfo($info['video']['streams']); + } + if (!empty($info['audio']['streams'])) { + $info['audio'] = self::getDefaultStreamInfo($info['audio']['streams']); + } + } + + // process attachments + if (isset($info['matroska']['attachments']) && $this->getid3->option_save_attachments !== getID3::ATTACHMENTS_NONE) { + foreach ($info['matroska']['attachments'] as $i => $entry) { + if (strpos($entry['FileMimeType'], 'image/') === 0 && !empty($entry['FileData'])) { + $info['matroska']['comments']['picture'][] = array('data' => $entry['FileData'], 'image_mime' => $entry['FileMimeType'], 'filename' => $entry['FileName']); + } + } + } + + // determine mime type + if (!empty($info['video']['streams'])) { + $info['mime_type'] = ($info['matroska']['doctype'] == 'webm' ? 'video/webm' : 'video/x-matroska'); + } elseif (!empty($info['audio']['streams'])) { + $info['mime_type'] = ($info['matroska']['doctype'] == 'webm' ? 'audio/webm' : 'audio/x-matroska'); + } elseif (isset($info['mime_type'])) { + unset($info['mime_type']); + } + + // use _STATISTICS_TAGS if available to set audio/video bitrates + if (!empty($info['matroska']['tags'])) { + $_STATISTICS_byTrackUID = array(); + foreach ($info['matroska']['tags'] as $key1 => $value1) { + if (!empty($value1['Targets']['TagTrackUID'][0]) && !empty($value1['SimpleTag'])) { + foreach ($value1['SimpleTag'] as $key2 => $value2) { + if (!empty($value2['TagName']) && isset($value2['TagString'])) { + $_STATISTICS_byTrackUID[$value1['Targets']['TagTrackUID'][0]][$value2['TagName']] = $value2['TagString']; + } + } + } + } + foreach (array('audio','video') as $avtype) { + if (!empty($info[$avtype]['streams'])) { + foreach ($info[$avtype]['streams'] as $trackUID => $trackdata) { + if (!isset($trackdata['bitrate']) && !empty($_STATISTICS_byTrackUID[$trackUID]['BPS'])) { + $info[$avtype]['streams'][$trackUID]['bitrate'] = (int) $_STATISTICS_byTrackUID[$trackUID]['BPS']; + @$info[$avtype]['bitrate'] += $info[$avtype]['streams'][$trackUID]['bitrate']; + } + } + } + } + } + + return true; + } + + /** + * @param array $info + */ + private function parseEBML(&$info) { + // http://www.matroska.org/technical/specs/index.html#EBMLBasics + $this->current_offset = $info['avdataoffset']; + + while ($this->getEBMLelement($top_element, $info['avdataend'])) { + switch ($top_element['id']) { + + case EBML_ID_EBML: + $info['matroska']['header']['offset'] = $top_element['offset']; + $info['matroska']['header']['length'] = $top_element['length']; + + while ($this->getEBMLelement($element_data, $top_element['end'], true)) { + switch ($element_data['id']) { + + case EBML_ID_EBMLVERSION: + case EBML_ID_EBMLREADVERSION: + case EBML_ID_EBMLMAXIDLENGTH: + case EBML_ID_EBMLMAXSIZELENGTH: + case EBML_ID_DOCTYPEVERSION: + case EBML_ID_DOCTYPEREADVERSION: + $element_data['data'] = getid3_lib::BigEndian2Int($element_data['data']); + break; + + case EBML_ID_DOCTYPE: + $element_data['data'] = getid3_lib::trimNullByte($element_data['data']); + $info['matroska']['doctype'] = $element_data['data']; + $info['fileformat'] = $element_data['data']; + break; + + default: + $this->unhandledElement('header', __LINE__, $element_data); + break; + } + + unset($element_data['offset'], $element_data['end']); + $info['matroska']['header']['elements'][] = $element_data; + } + break; + + case EBML_ID_SEGMENT: + $info['matroska']['segment'][0]['offset'] = $top_element['offset']; + $info['matroska']['segment'][0]['length'] = $top_element['length']; + + while ($this->getEBMLelement($element_data, $top_element['end'])) { + if ($element_data['id'] != EBML_ID_CLUSTER || !self::$hide_clusters) { // collect clusters only if required + $info['matroska']['segments'][] = $element_data; + } + switch ($element_data['id']) { + + case EBML_ID_SEEKHEAD: // Contains the position of other level 1 elements. + + while ($this->getEBMLelement($seek_entry, $element_data['end'])) { + switch ($seek_entry['id']) { + + case EBML_ID_SEEK: // Contains a single seek entry to an EBML element + while ($this->getEBMLelement($sub_seek_entry, $seek_entry['end'], true)) { + + switch ($sub_seek_entry['id']) { + + case EBML_ID_SEEKID: + $seek_entry['target_id'] = self::EBML2Int($sub_seek_entry['data']); + $seek_entry['target_name'] = self::EBMLidName($seek_entry['target_id']); + break; + + case EBML_ID_SEEKPOSITION: + $seek_entry['target_offset'] = $element_data['offset'] + getid3_lib::BigEndian2Int($sub_seek_entry['data']); + break; + + default: + $this->unhandledElement('seekhead.seek', __LINE__, $sub_seek_entry); } + break; + } + if (!isset($seek_entry['target_id'])) { + $this->warning('seek_entry[target_id] unexpectedly not set at '.$seek_entry['offset']); + break; + } + if (($seek_entry['target_id'] != EBML_ID_CLUSTER) || !self::$hide_clusters) { // collect clusters only if required + $info['matroska']['seek'][] = $seek_entry; + } + break; + + default: + $this->unhandledElement('seekhead', __LINE__, $seek_entry); + break; + } + } + break; + + case EBML_ID_TRACKS: // A top-level block of information with many tracks described. + $info['matroska']['tracks'] = $element_data; + + while ($this->getEBMLelement($track_entry, $element_data['end'])) { + switch ($track_entry['id']) { + + case EBML_ID_TRACKENTRY: //subelements: Describes a track with all elements. + + while ($this->getEBMLelement($subelement, $track_entry['end'], array(EBML_ID_VIDEO, EBML_ID_AUDIO, EBML_ID_CONTENTENCODINGS, EBML_ID_CODECPRIVATE))) { + switch ($subelement['id']) { + + case EBML_ID_TRACKUID: + $track_entry[$subelement['id_name']] = getid3_lib::PrintHexBytes($subelement['data'], true, false); + break; + case EBML_ID_TRACKNUMBER: + case EBML_ID_TRACKTYPE: + case EBML_ID_MINCACHE: + case EBML_ID_MAXCACHE: + case EBML_ID_MAXBLOCKADDITIONID: + case EBML_ID_DEFAULTDURATION: // nanoseconds per frame + $track_entry[$subelement['id_name']] = getid3_lib::BigEndian2Int($subelement['data']); + break; + + case EBML_ID_TRACKTIMECODESCALE: + $track_entry[$subelement['id_name']] = getid3_lib::BigEndian2Float($subelement['data']); + break; + + case EBML_ID_CODECID: + case EBML_ID_LANGUAGE: + case EBML_ID_NAME: + case EBML_ID_CODECNAME: + $track_entry[$subelement['id_name']] = getid3_lib::trimNullByte($subelement['data']); + break; + + case EBML_ID_CODECPRIVATE: + $track_entry[$subelement['id_name']] = $this->readEBMLelementData($subelement['length'], true); + break; + + case EBML_ID_FLAGENABLED: + case EBML_ID_FLAGDEFAULT: + case EBML_ID_FLAGFORCED: + case EBML_ID_FLAGLACING: + case EBML_ID_CODECDECODEALL: + $track_entry[$subelement['id_name']] = (bool) getid3_lib::BigEndian2Int($subelement['data']); + break; + + case EBML_ID_VIDEO: + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], true)) { + switch ($sub_subelement['id']) { + + case EBML_ID_PIXELWIDTH: + case EBML_ID_PIXELHEIGHT: + case EBML_ID_PIXELCROPBOTTOM: + case EBML_ID_PIXELCROPTOP: + case EBML_ID_PIXELCROPLEFT: + case EBML_ID_PIXELCROPRIGHT: + case EBML_ID_DISPLAYWIDTH: + case EBML_ID_DISPLAYHEIGHT: + case EBML_ID_DISPLAYUNIT: + case EBML_ID_ASPECTRATIOTYPE: + case EBML_ID_STEREOMODE: + case EBML_ID_OLDSTEREOMODE: + $track_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_FLAGINTERLACED: + $track_entry[$sub_subelement['id_name']] = (bool)getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_GAMMAVALUE: + $track_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Float($sub_subelement['data']); + break; + + case EBML_ID_COLOURSPACE: + $track_entry[$sub_subelement['id_name']] = getid3_lib::trimNullByte($sub_subelement['data']); + break; + + default: + $this->unhandledElement('track.video', __LINE__, $sub_subelement); + break; + } + } + break; + + case EBML_ID_AUDIO: + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], true)) { + switch ($sub_subelement['id']) { + + case EBML_ID_CHANNELS: + case EBML_ID_BITDEPTH: + $track_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_SAMPLINGFREQUENCY: + case EBML_ID_OUTPUTSAMPLINGFREQUENCY: + $track_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Float($sub_subelement['data']); + break; + + case EBML_ID_CHANNELPOSITIONS: + $track_entry[$sub_subelement['id_name']] = getid3_lib::trimNullByte($sub_subelement['data']); + break; + + default: + $this->unhandledElement('track.audio', __LINE__, $sub_subelement); + break; + } + } + break; + + case EBML_ID_CONTENTENCODINGS: + + while ($this->getEBMLelement($sub_subelement, $subelement['end'])) { + switch ($sub_subelement['id']) { + + case EBML_ID_CONTENTENCODING: + + while ($this->getEBMLelement($sub_sub_subelement, $sub_subelement['end'], array(EBML_ID_CONTENTCOMPRESSION, EBML_ID_CONTENTENCRYPTION))) { + switch ($sub_sub_subelement['id']) { + + case EBML_ID_CONTENTENCODINGORDER: + case EBML_ID_CONTENTENCODINGSCOPE: + case EBML_ID_CONTENTENCODINGTYPE: + $track_entry[$sub_subelement['id_name']][$sub_sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_sub_subelement['data']); + break; + + case EBML_ID_CONTENTCOMPRESSION: + + while ($this->getEBMLelement($sub_sub_sub_subelement, $sub_sub_subelement['end'], true)) { + switch ($sub_sub_sub_subelement['id']) { + + case EBML_ID_CONTENTCOMPALGO: + $track_entry[$sub_subelement['id_name']][$sub_sub_subelement['id_name']][$sub_sub_sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_sub_sub_subelement['data']); + break; + + case EBML_ID_CONTENTCOMPSETTINGS: + $track_entry[$sub_subelement['id_name']][$sub_sub_subelement['id_name']][$sub_sub_sub_subelement['id_name']] = $sub_sub_sub_subelement['data']; + break; + + default: + $this->unhandledElement('track.contentencodings.contentencoding.contentcompression', __LINE__, $sub_sub_sub_subelement); + break; + } + } + break; + + case EBML_ID_CONTENTENCRYPTION: + + while ($this->getEBMLelement($sub_sub_sub_subelement, $sub_sub_subelement['end'], true)) { + switch ($sub_sub_sub_subelement['id']) { + + case EBML_ID_CONTENTENCALGO: + case EBML_ID_CONTENTSIGALGO: + case EBML_ID_CONTENTSIGHASHALGO: + $track_entry[$sub_subelement['id_name']][$sub_sub_subelement['id_name']][$sub_sub_sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_sub_sub_subelement['data']); + break; + + case EBML_ID_CONTENTENCKEYID: + case EBML_ID_CONTENTSIGNATURE: + case EBML_ID_CONTENTSIGKEYID: + $track_entry[$sub_subelement['id_name']][$sub_sub_subelement['id_name']][$sub_sub_sub_subelement['id_name']] = $sub_sub_sub_subelement['data']; + break; + + default: + $this->unhandledElement('track.contentencodings.contentencoding.contentcompression', __LINE__, $sub_sub_sub_subelement); + break; + } + } + break; + + default: + $this->unhandledElement('track.contentencodings.contentencoding', __LINE__, $sub_sub_subelement); + break; + } + } + break; + + default: + $this->unhandledElement('track.contentencodings', __LINE__, $sub_subelement); + break; + } + } + break; + + default: + $this->unhandledElement('track', __LINE__, $subelement); + break; + } + } + + $info['matroska']['tracks']['tracks'][] = $track_entry; + break; + + default: + $this->unhandledElement('tracks', __LINE__, $track_entry); + break; + } + } + break; + + case EBML_ID_INFO: // Contains miscellaneous general information and statistics on the file. + $info_entry = array(); + + while ($this->getEBMLelement($subelement, $element_data['end'], true)) { + switch ($subelement['id']) { + + case EBML_ID_TIMECODESCALE: + $info_entry[$subelement['id_name']] = getid3_lib::BigEndian2Int($subelement['data']); + break; + + case EBML_ID_DURATION: + $info_entry[$subelement['id_name']] = getid3_lib::BigEndian2Float($subelement['data']); + break; + + case EBML_ID_DATEUTC: + $info_entry[$subelement['id_name']] = getid3_lib::BigEndian2Int($subelement['data']); + $info_entry[$subelement['id_name'].'_unix'] = self::EBMLdate2unix($info_entry[$subelement['id_name']]); + break; + + case EBML_ID_SEGMENTUID: + case EBML_ID_PREVUID: + case EBML_ID_NEXTUID: + $info_entry[$subelement['id_name']] = getid3_lib::trimNullByte($subelement['data']); + break; + + case EBML_ID_SEGMENTFAMILY: + $info_entry[$subelement['id_name']][] = getid3_lib::trimNullByte($subelement['data']); + break; + + case EBML_ID_SEGMENTFILENAME: + case EBML_ID_PREVFILENAME: + case EBML_ID_NEXTFILENAME: + case EBML_ID_TITLE: + case EBML_ID_MUXINGAPP: + case EBML_ID_WRITINGAPP: + $info_entry[$subelement['id_name']] = getid3_lib::trimNullByte($subelement['data']); + $info['matroska']['comments'][strtolower($subelement['id_name'])][] = $info_entry[$subelement['id_name']]; + break; + + case EBML_ID_CHAPTERTRANSLATE: + $chaptertranslate_entry = array(); + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], true)) { + switch ($sub_subelement['id']) { + + case EBML_ID_CHAPTERTRANSLATEEDITIONUID: + $chaptertranslate_entry[$sub_subelement['id_name']][] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_CHAPTERTRANSLATECODEC: + $chaptertranslate_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_CHAPTERTRANSLATEID: + $chaptertranslate_entry[$sub_subelement['id_name']] = getid3_lib::trimNullByte($sub_subelement['data']); + break; + + default: + $this->unhandledElement('info.chaptertranslate', __LINE__, $sub_subelement); + break; + } + } + $info_entry[$subelement['id_name']] = $chaptertranslate_entry; + break; + + default: + $this->unhandledElement('info', __LINE__, $subelement); + break; + } + } + $info['matroska']['info'][] = $info_entry; + break; + + case EBML_ID_CUES: // A top-level element to speed seeking access. All entries are local to the segment. Should be mandatory for non "live" streams. + if (self::$hide_clusters) { // do not parse cues if hide clusters is "ON" till they point to clusters anyway + $this->current_offset = $element_data['end']; + break; + } + $cues_entry = array(); + + while ($this->getEBMLelement($subelement, $element_data['end'])) { + switch ($subelement['id']) { + + case EBML_ID_CUEPOINT: + $cuepoint_entry = array(); + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], array(EBML_ID_CUETRACKPOSITIONS))) { + switch ($sub_subelement['id']) { + + case EBML_ID_CUETRACKPOSITIONS: + $cuetrackpositions_entry = array(); + + while ($this->getEBMLelement($sub_sub_subelement, $sub_subelement['end'], true)) { + switch ($sub_sub_subelement['id']) { + + case EBML_ID_CUETRACK: + case EBML_ID_CUECLUSTERPOSITION: + case EBML_ID_CUEBLOCKNUMBER: + case EBML_ID_CUECODECSTATE: + $cuetrackpositions_entry[$sub_sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_sub_subelement['data']); + break; + + default: + $this->unhandledElement('cues.cuepoint.cuetrackpositions', __LINE__, $sub_sub_subelement); + break; + } + } + $cuepoint_entry[$sub_subelement['id_name']][] = $cuetrackpositions_entry; + break; + + case EBML_ID_CUETIME: + $cuepoint_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + default: + $this->unhandledElement('cues.cuepoint', __LINE__, $sub_subelement); + break; + } + } + $cues_entry[] = $cuepoint_entry; + break; + + default: + $this->unhandledElement('cues', __LINE__, $subelement); + break; + } + } + $info['matroska']['cues'] = $cues_entry; + break; + + case EBML_ID_TAGS: // Element containing elements specific to Tracks/Chapters. + $tags_entry = array(); + + while ($this->getEBMLelement($subelement, $element_data['end'], false)) { + switch ($subelement['id']) { + + case EBML_ID_TAG: + $tag_entry = array(); + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], false)) { + switch ($sub_subelement['id']) { + + case EBML_ID_TARGETS: + $targets_entry = array(); + + while ($this->getEBMLelement($sub_sub_subelement, $sub_subelement['end'], true)) { + switch ($sub_sub_subelement['id']) { + + case EBML_ID_TARGETTYPEVALUE: + $targets_entry[$sub_sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_sub_subelement['data']); + $targets_entry[strtolower($sub_sub_subelement['id_name']).'_long'] = self::TargetTypeValue($targets_entry[$sub_sub_subelement['id_name']]); + break; + + case EBML_ID_TARGETTYPE: + $targets_entry[$sub_sub_subelement['id_name']] = $sub_sub_subelement['data']; + break; + + case EBML_ID_TAGTRACKUID: + case EBML_ID_TAGEDITIONUID: + case EBML_ID_TAGCHAPTERUID: + case EBML_ID_TAGATTACHMENTUID: + $targets_entry[$sub_sub_subelement['id_name']][] = getid3_lib::PrintHexBytes($sub_sub_subelement['data'], true, false); + break; + + default: + $this->unhandledElement('tags.tag.targets', __LINE__, $sub_sub_subelement); + break; + } + } + $tag_entry[$sub_subelement['id_name']] = $targets_entry; + break; + + case EBML_ID_SIMPLETAG: + $tag_entry[$sub_subelement['id_name']][] = $this->HandleEMBLSimpleTag($sub_subelement['end']); + break; + + default: + $this->unhandledElement('tags.tag', __LINE__, $sub_subelement); + break; + } + } + $tags_entry[] = $tag_entry; + break; + + default: + $this->unhandledElement('tags', __LINE__, $subelement); + break; + } + } + $info['matroska']['tags'] = $tags_entry; + break; + + case EBML_ID_ATTACHMENTS: // Contain attached files. + + while ($this->getEBMLelement($subelement, $element_data['end'])) { + switch ($subelement['id']) { + + case EBML_ID_ATTACHEDFILE: + $attachedfile_entry = array(); + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], array(EBML_ID_FILEDATA))) { + switch ($sub_subelement['id']) { + + case EBML_ID_FILEDESCRIPTION: + case EBML_ID_FILENAME: + case EBML_ID_FILEMIMETYPE: + $attachedfile_entry[$sub_subelement['id_name']] = $sub_subelement['data']; + break; + + case EBML_ID_FILEDATA: + $attachedfile_entry['data_offset'] = $this->current_offset; + $attachedfile_entry['data_length'] = $sub_subelement['length']; + + $attachedfile_entry[$sub_subelement['id_name']] = $this->saveAttachment( + $attachedfile_entry['FileName'], + $attachedfile_entry['data_offset'], + $attachedfile_entry['data_length']); + + $this->current_offset = $sub_subelement['end']; + break; + + case EBML_ID_FILEUID: + $attachedfile_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + default: + $this->unhandledElement('attachments.attachedfile', __LINE__, $sub_subelement); + break; + } + } + $info['matroska']['attachments'][] = $attachedfile_entry; + break; + + default: + $this->unhandledElement('attachments', __LINE__, $subelement); + break; + } + } + break; + + case EBML_ID_CHAPTERS: + + while ($this->getEBMLelement($subelement, $element_data['end'])) { + switch ($subelement['id']) { + + case EBML_ID_EDITIONENTRY: + $editionentry_entry = array(); + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], array(EBML_ID_CHAPTERATOM))) { + switch ($sub_subelement['id']) { + + case EBML_ID_EDITIONUID: + $editionentry_entry[$sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_EDITIONFLAGHIDDEN: + case EBML_ID_EDITIONFLAGDEFAULT: + case EBML_ID_EDITIONFLAGORDERED: + $editionentry_entry[$sub_subelement['id_name']] = (bool)getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_CHAPTERATOM: + $chapteratom_entry = array(); + + while ($this->getEBMLelement($sub_sub_subelement, $sub_subelement['end'], array(EBML_ID_CHAPTERTRACK, EBML_ID_CHAPTERDISPLAY))) { + switch ($sub_sub_subelement['id']) { + + case EBML_ID_CHAPTERSEGMENTUID: + case EBML_ID_CHAPTERSEGMENTEDITIONUID: + $chapteratom_entry[$sub_sub_subelement['id_name']] = $sub_sub_subelement['data']; + break; + + case EBML_ID_CHAPTERFLAGENABLED: + case EBML_ID_CHAPTERFLAGHIDDEN: + $chapteratom_entry[$sub_sub_subelement['id_name']] = (bool)getid3_lib::BigEndian2Int($sub_sub_subelement['data']); + break; + + case EBML_ID_CHAPTERUID: + case EBML_ID_CHAPTERTIMESTART: + case EBML_ID_CHAPTERTIMEEND: + $chapteratom_entry[$sub_sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_sub_subelement['data']); + break; + + case EBML_ID_CHAPTERTRACK: + $chaptertrack_entry = array(); + + while ($this->getEBMLelement($sub_sub_sub_subelement, $sub_sub_subelement['end'], true)) { + switch ($sub_sub_sub_subelement['id']) { + + case EBML_ID_CHAPTERTRACKNUMBER: + $chaptertrack_entry[$sub_sub_sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_sub_sub_subelement['data']); + break; + + default: + $this->unhandledElement('chapters.editionentry.chapteratom.chaptertrack', __LINE__, $sub_sub_sub_subelement); + break; + } + } + $chapteratom_entry[$sub_sub_subelement['id_name']][] = $chaptertrack_entry; + break; + + case EBML_ID_CHAPTERDISPLAY: + $chapterdisplay_entry = array(); + + while ($this->getEBMLelement($sub_sub_sub_subelement, $sub_sub_subelement['end'], true)) { + switch ($sub_sub_sub_subelement['id']) { + + case EBML_ID_CHAPSTRING: + case EBML_ID_CHAPLANGUAGE: + case EBML_ID_CHAPCOUNTRY: + $chapterdisplay_entry[$sub_sub_sub_subelement['id_name']] = $sub_sub_sub_subelement['data']; + break; + + default: + $this->unhandledElement('chapters.editionentry.chapteratom.chapterdisplay', __LINE__, $sub_sub_sub_subelement); + break; + } + } + $chapteratom_entry[$sub_sub_subelement['id_name']][] = $chapterdisplay_entry; + break; + + default: + $this->unhandledElement('chapters.editionentry.chapteratom', __LINE__, $sub_sub_subelement); + break; + } + } + $editionentry_entry[$sub_subelement['id_name']][] = $chapteratom_entry; + break; + + default: + $this->unhandledElement('chapters.editionentry', __LINE__, $sub_subelement); + break; + } + } + $info['matroska']['chapters'][] = $editionentry_entry; + break; + + default: + $this->unhandledElement('chapters', __LINE__, $subelement); + break; + } + } + break; + + case EBML_ID_CLUSTER: // The lower level element containing the (monolithic) Block structure. + $cluster_entry = array(); + + while ($this->getEBMLelement($subelement, $element_data['end'], array(EBML_ID_CLUSTERSILENTTRACKS, EBML_ID_CLUSTERBLOCKGROUP, EBML_ID_CLUSTERSIMPLEBLOCK))) { + switch ($subelement['id']) { + + case EBML_ID_CLUSTERTIMECODE: + case EBML_ID_CLUSTERPOSITION: + case EBML_ID_CLUSTERPREVSIZE: + $cluster_entry[$subelement['id_name']] = getid3_lib::BigEndian2Int($subelement['data']); + break; + + case EBML_ID_CLUSTERSILENTTRACKS: + $cluster_silent_tracks = array(); + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], true)) { + switch ($sub_subelement['id']) { + + case EBML_ID_CLUSTERSILENTTRACKNUMBER: + $cluster_silent_tracks[] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + default: + $this->unhandledElement('cluster.silenttracks', __LINE__, $sub_subelement); + break; + } + } + $cluster_entry[$subelement['id_name']][] = $cluster_silent_tracks; + break; + + case EBML_ID_CLUSTERBLOCKGROUP: + $cluster_block_group = array('offset' => $this->current_offset); + + while ($this->getEBMLelement($sub_subelement, $subelement['end'], array(EBML_ID_CLUSTERBLOCK))) { + switch ($sub_subelement['id']) { + + case EBML_ID_CLUSTERBLOCK: + $cluster_block_group[$sub_subelement['id_name']] = $this->HandleEMBLClusterBlock($sub_subelement, EBML_ID_CLUSTERBLOCK, $info); + break; + + case EBML_ID_CLUSTERREFERENCEPRIORITY: // unsigned-int + case EBML_ID_CLUSTERBLOCKDURATION: // unsigned-int + $cluster_block_group[$sub_subelement['id_name']] = getid3_lib::BigEndian2Int($sub_subelement['data']); + break; + + case EBML_ID_CLUSTERREFERENCEBLOCK: // signed-int + $cluster_block_group[$sub_subelement['id_name']][] = getid3_lib::BigEndian2Int($sub_subelement['data'], false, true); + break; + + case EBML_ID_CLUSTERCODECSTATE: + $cluster_block_group[$sub_subelement['id_name']] = getid3_lib::trimNullByte($sub_subelement['data']); + break; + + default: + $this->unhandledElement('clusters.blockgroup', __LINE__, $sub_subelement); + break; + } + } + $cluster_entry[$subelement['id_name']][] = $cluster_block_group; + break; + + case EBML_ID_CLUSTERSIMPLEBLOCK: + $cluster_entry[$subelement['id_name']][] = $this->HandleEMBLClusterBlock($subelement, EBML_ID_CLUSTERSIMPLEBLOCK, $info); + break; + + default: + $this->unhandledElement('cluster', __LINE__, $subelement); + break; + } + $this->current_offset = $subelement['end']; + } + if (!self::$hide_clusters) { + $info['matroska']['cluster'][] = $cluster_entry; + } + + // check to see if all the data we need exists already, if so, break out of the loop + if (!self::$parse_whole_file) { + if (isset($info['matroska']['info']) && is_array($info['matroska']['info'])) { + if (isset($info['matroska']['tracks']['tracks']) && is_array($info['matroska']['tracks']['tracks'])) { + if (count($info['matroska']['track_data_offsets']) == count($info['matroska']['tracks']['tracks'])) { + return; + } + } + } + } + break; + + default: + $this->unhandledElement('segment', __LINE__, $element_data); + break; + } + } + break; + + default: + $this->unhandledElement('root', __LINE__, $top_element); + break; + } + } + } + + /** + * @param int $min_data + * + * @return bool + */ + private function EnsureBufferHasEnoughData($min_data=1024) { + if (($this->current_offset - $this->EBMLbuffer_offset) >= ($this->EBMLbuffer_length - $min_data)) { + $read_bytes = max($min_data, $this->getid3->fread_buffer_size()); + + try { + $this->fseek($this->current_offset); + $this->EBMLbuffer_offset = $this->current_offset; + $this->EBMLbuffer = $this->fread($read_bytes); + $this->EBMLbuffer_length = strlen($this->EBMLbuffer); + } catch (getid3_exception $e) { + $this->warning('EBML parser: '.$e->getMessage()); + return false; + } + + if ($this->EBMLbuffer_length == 0 && $this->feof()) { + return $this->error('EBML parser: ran out of file at offset '.$this->current_offset); + } + } + return true; + } + + /** + * @return int|float|false + */ + private function readEBMLint() { + $actual_offset = $this->current_offset - $this->EBMLbuffer_offset; + + // get length of integer + $first_byte_int = ord($this->EBMLbuffer[$actual_offset]); + if (0x80 & $first_byte_int) { + $length = 1; + } elseif (0x40 & $first_byte_int) { + $length = 2; + } elseif (0x20 & $first_byte_int) { + $length = 3; + } elseif (0x10 & $first_byte_int) { + $length = 4; + } elseif (0x08 & $first_byte_int) { + $length = 5; + } elseif (0x04 & $first_byte_int) { + $length = 6; + } elseif (0x02 & $first_byte_int) { + $length = 7; + } elseif (0x01 & $first_byte_int) { + $length = 8; + } else { + throw new Exception('invalid EBML integer (leading 0x00) at '.$this->current_offset); + } + + // read + $int_value = self::EBML2Int(substr($this->EBMLbuffer, $actual_offset, $length)); + $this->current_offset += $length; + + return $int_value; + } + + /** + * @param int $length + * @param bool $check_buffer + * + * @return string|false + */ + private function readEBMLelementData($length, $check_buffer=false) { + if ($check_buffer && !$this->EnsureBufferHasEnoughData($length)) { + return false; + } + $data = substr($this->EBMLbuffer, $this->current_offset - $this->EBMLbuffer_offset, $length); + $this->current_offset += $length; + return $data; + } + + /** + * @param array $element + * @param int $parent_end + * @param array|bool $get_data + * + * @return bool + */ + private function getEBMLelement(&$element, $parent_end, $get_data=false) { + if ($this->current_offset >= $parent_end) { + return false; + } + + if (!$this->EnsureBufferHasEnoughData()) { + $this->current_offset = PHP_INT_MAX; // do not exit parser right now, allow to finish current loop to gather maximum information + return false; + } + + $element = array(); + + // set offset + $element['offset'] = $this->current_offset; + + // get ID + $element['id'] = $this->readEBMLint(); + + // get name + $element['id_name'] = self::EBMLidName($element['id']); + + // get length + $element['length'] = $this->readEBMLint(); + + // get end offset + $element['end'] = $this->current_offset + $element['length']; + + // get raw data + $dont_parse = (in_array($element['id'], $this->unuseful_elements) || $element['id_name'] == dechex($element['id'])); + if (($get_data === true || (is_array($get_data) && !in_array($element['id'], $get_data))) && !$dont_parse) { + $element['data'] = $this->readEBMLelementData($element['length'], $element); + } + + return true; + } + + /** + * @param string $type + * @param int $line + * @param array $element + */ + private function unhandledElement($type, $line, $element) { + // warn only about unknown and missed elements, not about unuseful + if (!in_array($element['id'], $this->unuseful_elements)) { + $this->warning('Unhandled '.$type.' element ['.basename(__FILE__).':'.$line.'] ('.$element['id'].'::'.$element['id_name'].' ['.$element['length'].' bytes]) at '.$element['offset']); + } + + // increase offset for unparsed elements + if (!isset($element['data'])) { + $this->current_offset = $element['end']; + } + } + + /** + * @param array $SimpleTagArray + * + * @return bool + */ + private function ExtractCommentsSimpleTag($SimpleTagArray) { + if (!empty($SimpleTagArray['SimpleTag'])) { + foreach ($SimpleTagArray['SimpleTag'] as $SimpleTagKey => $SimpleTagData) { + if (!empty($SimpleTagData['TagName']) && !empty($SimpleTagData['TagString'])) { + $this->getid3->info['matroska']['comments'][strtolower($SimpleTagData['TagName'])][] = $SimpleTagData['TagString']; + } + if (!empty($SimpleTagData['SimpleTag'])) { + $this->ExtractCommentsSimpleTag($SimpleTagData); + } + } + } + + return true; + } + + /** + * @param int $parent_end + * + * @return array + */ + private function HandleEMBLSimpleTag($parent_end) { + $simpletag_entry = array(); + + while ($this->getEBMLelement($element, $parent_end, array(EBML_ID_SIMPLETAG))) { + switch ($element['id']) { + + case EBML_ID_TAGNAME: + case EBML_ID_TAGLANGUAGE: + case EBML_ID_TAGSTRING: + case EBML_ID_TAGBINARY: + $simpletag_entry[$element['id_name']] = $element['data']; + break; + + case EBML_ID_SIMPLETAG: + $simpletag_entry[$element['id_name']][] = $this->HandleEMBLSimpleTag($element['end']); + break; + + case EBML_ID_TAGDEFAULT: + $simpletag_entry[$element['id_name']] = (bool)getid3_lib::BigEndian2Int($element['data']); + break; + + default: + $this->unhandledElement('tag.simpletag', __LINE__, $element); + break; + } + } + + return $simpletag_entry; + } + + /** + * @param array $element + * @param int $block_type + * @param array $info + * + * @return array + */ + private function HandleEMBLClusterBlock($element, $block_type, &$info) { + // http://www.matroska.org/technical/specs/index.html#block_structure + // http://www.matroska.org/technical/specs/index.html#simpleblock_structure + + $block_data = array(); + $block_data['tracknumber'] = $this->readEBMLint(); + $block_data['timecode'] = getid3_lib::BigEndian2Int($this->readEBMLelementData(2), false, true); + $block_data['flags_raw'] = getid3_lib::BigEndian2Int($this->readEBMLelementData(1)); + + if ($block_type == EBML_ID_CLUSTERSIMPLEBLOCK) { + $block_data['flags']['keyframe'] = (($block_data['flags_raw'] & 0x80) >> 7); + //$block_data['flags']['reserved1'] = (($block_data['flags_raw'] & 0x70) >> 4); + } + else { + //$block_data['flags']['reserved1'] = (($block_data['flags_raw'] & 0xF0) >> 4); + } + $block_data['flags']['invisible'] = (bool)(($block_data['flags_raw'] & 0x08) >> 3); + $block_data['flags']['lacing'] = (($block_data['flags_raw'] & 0x06) >> 1); // 00=no lacing; 01=Xiph lacing; 11=EBML lacing; 10=fixed-size lacing + if ($block_type == EBML_ID_CLUSTERSIMPLEBLOCK) { + $block_data['flags']['discardable'] = (($block_data['flags_raw'] & 0x01)); + } + else { + //$block_data['flags']['reserved2'] = (($block_data['flags_raw'] & 0x01) >> 0); + } + $block_data['flags']['lacing_type'] = self::BlockLacingType($block_data['flags']['lacing']); + + // Lace (when lacing bit is set) + if ($block_data['flags']['lacing'] > 0) { + $block_data['lace_frames'] = getid3_lib::BigEndian2Int($this->readEBMLelementData(1)) + 1; // Number of frames in the lace-1 (uint8) + if ($block_data['flags']['lacing'] != 0x02) { + for ($i = 1; $i < $block_data['lace_frames']; $i ++) { // Lace-coded size of each frame of the lace, except for the last one (multiple uint8). *This is not used with Fixed-size lacing as it is calculated automatically from (total size of lace) / (number of frames in lace). + if ($block_data['flags']['lacing'] == 0x03) { // EBML lacing + $block_data['lace_frames_size'][$i] = $this->readEBMLint(); // TODO: read size correctly, calc size for the last frame. For now offsets are deteminded OK with readEBMLint() and that's the most important thing. + } + else { // Xiph lacing + $block_data['lace_frames_size'][$i] = 0; + do { + $size = getid3_lib::BigEndian2Int($this->readEBMLelementData(1)); + $block_data['lace_frames_size'][$i] += $size; + } + while ($size == 255); + } + } + if ($block_data['flags']['lacing'] == 0x01) { // calc size of the last frame only for Xiph lacing, till EBML sizes are now anyway determined incorrectly + $block_data['lace_frames_size'][] = $element['end'] - $this->current_offset - array_sum($block_data['lace_frames_size']); + } + } + } + + if (!isset($info['matroska']['track_data_offsets'][$block_data['tracknumber']])) { + $info['matroska']['track_data_offsets'][$block_data['tracknumber']]['offset'] = $this->current_offset; + $info['matroska']['track_data_offsets'][$block_data['tracknumber']]['length'] = $element['end'] - $this->current_offset; + //$info['matroska']['track_data_offsets'][$block_data['tracknumber']]['total_length'] = 0; + } + //$info['matroska']['track_data_offsets'][$block_data['tracknumber']]['total_length'] += $info['matroska']['track_data_offsets'][$block_data['tracknumber']]['length']; + //$info['matroska']['track_data_offsets'][$block_data['tracknumber']]['duration'] = $block_data['timecode'] * ((isset($info['matroska']['info'][0]['TimecodeScale']) ? $info['matroska']['info'][0]['TimecodeScale'] : 1000000) / 1000000000); + + // set offset manually + $this->current_offset = $element['end']; + + return $block_data; + } + + /** + * @param string $EBMLstring + * + * @return int|float|false + */ + private static function EBML2Int($EBMLstring) { + // http://matroska.org/specs/ + + // Element ID coded with an UTF-8 like system: + // 1xxx xxxx - Class A IDs (2^7 -2 possible values) (base 0x8X) + // 01xx xxxx xxxx xxxx - Class B IDs (2^14-2 possible values) (base 0x4X 0xXX) + // 001x xxxx xxxx xxxx xxxx xxxx - Class C IDs (2^21-2 possible values) (base 0x2X 0xXX 0xXX) + // 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx - Class D IDs (2^28-2 possible values) (base 0x1X 0xXX 0xXX 0xXX) + // Values with all x at 0 and 1 are reserved (hence the -2). + + // Data size, in octets, is also coded with an UTF-8 like system : + // 1xxx xxxx - value 0 to 2^7-2 + // 01xx xxxx xxxx xxxx - value 0 to 2^14-2 + // 001x xxxx xxxx xxxx xxxx xxxx - value 0 to 2^21-2 + // 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^28-2 + // 0000 1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^35-2 + // 0000 01xx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^42-2 + // 0000 001x xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^49-2 + // 0000 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^56-2 + + $first_byte_int = ord($EBMLstring[0]); + if (0x80 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x7F); + } elseif (0x40 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x3F); + } elseif (0x20 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x1F); + } elseif (0x10 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x0F); + } elseif (0x08 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x07); + } elseif (0x04 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x03); + } elseif (0x02 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x01); + } elseif (0x01 & $first_byte_int) { + $EBMLstring[0] = chr($first_byte_int & 0x00); + } + + return getid3_lib::BigEndian2Int($EBMLstring); + } + + /** + * @param int $EBMLdatestamp + * + * @return float + */ + private static function EBMLdate2unix($EBMLdatestamp) { + // Date - signed 8 octets integer in nanoseconds with 0 indicating the precise beginning of the millennium (at 2001-01-01T00:00:00,000000000 UTC) + // 978307200 == mktime(0, 0, 0, 1, 1, 2001) == January 1, 2001 12:00:00am UTC + return round(($EBMLdatestamp / 1000000000) + 978307200); + } + + /** + * @param int $target_type + * + * @return string|int + */ + public static function TargetTypeValue($target_type) { + // http://www.matroska.org/technical/specs/tagging/index.html + static $TargetTypeValue = array(); + if (empty($TargetTypeValue)) { + $TargetTypeValue[10] = 'A: ~ V:shot'; // the lowest hierarchy found in music or movies + $TargetTypeValue[20] = 'A:subtrack/part/movement ~ V:scene'; // corresponds to parts of a track for audio (like a movement) + $TargetTypeValue[30] = 'A:track/song ~ V:chapter'; // the common parts of an album or a movie + $TargetTypeValue[40] = 'A:part/session ~ V:part/session'; // when an album or episode has different logical parts + $TargetTypeValue[50] = 'A:album/opera/concert ~ V:movie/episode/concert'; // the most common grouping level of music and video (equals to an episode for TV series) + $TargetTypeValue[60] = 'A:edition/issue/volume/opus ~ V:season/sequel/volume'; // a list of lower levels grouped together + $TargetTypeValue[70] = 'A:collection ~ V:collection'; // the high hierarchy consisting of many different lower items + } + return (isset($TargetTypeValue[$target_type]) ? $TargetTypeValue[$target_type] : $target_type); + } + + /** + * @param int $lacingtype + * + * @return string|int + */ + public static function BlockLacingType($lacingtype) { + // http://matroska.org/technical/specs/index.html#block_structure + static $BlockLacingType = array(); + if (empty($BlockLacingType)) { + $BlockLacingType[0x00] = 'no lacing'; + $BlockLacingType[0x01] = 'Xiph lacing'; + $BlockLacingType[0x02] = 'fixed-size lacing'; + $BlockLacingType[0x03] = 'EBML lacing'; + } + return (isset($BlockLacingType[$lacingtype]) ? $BlockLacingType[$lacingtype] : $lacingtype); + } + + /** + * @param string $codecid + * + * @return string + */ + public static function CodecIDtoCommonName($codecid) { + // http://www.matroska.org/technical/specs/codecid/index.html + static $CodecIDlist = array(); + if (empty($CodecIDlist)) { + $CodecIDlist['A_AAC'] = 'aac'; + $CodecIDlist['A_AAC/MPEG2/LC'] = 'aac'; + $CodecIDlist['A_AC3'] = 'ac3'; + $CodecIDlist['A_EAC3'] = 'eac3'; + $CodecIDlist['A_DTS'] = 'dts'; + $CodecIDlist['A_FLAC'] = 'flac'; + $CodecIDlist['A_MPEG/L1'] = 'mp1'; + $CodecIDlist['A_MPEG/L2'] = 'mp2'; + $CodecIDlist['A_MPEG/L3'] = 'mp3'; + $CodecIDlist['A_PCM/INT/LIT'] = 'pcm'; // PCM Integer Little Endian + $CodecIDlist['A_PCM/INT/BIG'] = 'pcm'; // PCM Integer Big Endian + $CodecIDlist['A_QUICKTIME/QDMC'] = 'quicktime'; // Quicktime: QDesign Music + $CodecIDlist['A_QUICKTIME/QDM2'] = 'quicktime'; // Quicktime: QDesign Music v2 + $CodecIDlist['A_VORBIS'] = 'vorbis'; + $CodecIDlist['V_MPEG1'] = 'mpeg'; + $CodecIDlist['V_THEORA'] = 'theora'; + $CodecIDlist['V_REAL/RV40'] = 'real'; + $CodecIDlist['V_REAL/RV10'] = 'real'; + $CodecIDlist['V_REAL/RV20'] = 'real'; + $CodecIDlist['V_REAL/RV30'] = 'real'; + $CodecIDlist['V_QUICKTIME'] = 'quicktime'; // Quicktime + $CodecIDlist['V_MPEG4/ISO/AP'] = 'mpeg4'; + $CodecIDlist['V_MPEG4/ISO/ASP'] = 'mpeg4'; + $CodecIDlist['V_MPEG4/ISO/AVC'] = 'h264'; + $CodecIDlist['V_MPEG4/ISO/SP'] = 'mpeg4'; + $CodecIDlist['V_VP8'] = 'vp8'; + $CodecIDlist['V_MS/VFW/FOURCC'] = 'vcm'; // Microsoft (TM) Video Codec Manager (VCM) + $CodecIDlist['A_MS/ACM'] = 'acm'; // Microsoft (TM) Audio Codec Manager (ACM) + } + return (isset($CodecIDlist[$codecid]) ? $CodecIDlist[$codecid] : $codecid); + } + + /** + * @param int $value + * + * @return string + */ + private static function EBMLidName($value) { + static $EBMLidList = array(); + if (empty($EBMLidList)) { + $EBMLidList[EBML_ID_ASPECTRATIOTYPE] = 'AspectRatioType'; + $EBMLidList[EBML_ID_ATTACHEDFILE] = 'AttachedFile'; + $EBMLidList[EBML_ID_ATTACHMENTLINK] = 'AttachmentLink'; + $EBMLidList[EBML_ID_ATTACHMENTS] = 'Attachments'; + $EBMLidList[EBML_ID_AUDIO] = 'Audio'; + $EBMLidList[EBML_ID_BITDEPTH] = 'BitDepth'; + $EBMLidList[EBML_ID_CHANNELPOSITIONS] = 'ChannelPositions'; + $EBMLidList[EBML_ID_CHANNELS] = 'Channels'; + $EBMLidList[EBML_ID_CHAPCOUNTRY] = 'ChapCountry'; + $EBMLidList[EBML_ID_CHAPLANGUAGE] = 'ChapLanguage'; + $EBMLidList[EBML_ID_CHAPPROCESS] = 'ChapProcess'; + $EBMLidList[EBML_ID_CHAPPROCESSCODECID] = 'ChapProcessCodecID'; + $EBMLidList[EBML_ID_CHAPPROCESSCOMMAND] = 'ChapProcessCommand'; + $EBMLidList[EBML_ID_CHAPPROCESSDATA] = 'ChapProcessData'; + $EBMLidList[EBML_ID_CHAPPROCESSPRIVATE] = 'ChapProcessPrivate'; + $EBMLidList[EBML_ID_CHAPPROCESSTIME] = 'ChapProcessTime'; + $EBMLidList[EBML_ID_CHAPSTRING] = 'ChapString'; + $EBMLidList[EBML_ID_CHAPTERATOM] = 'ChapterAtom'; + $EBMLidList[EBML_ID_CHAPTERDISPLAY] = 'ChapterDisplay'; + $EBMLidList[EBML_ID_CHAPTERFLAGENABLED] = 'ChapterFlagEnabled'; + $EBMLidList[EBML_ID_CHAPTERFLAGHIDDEN] = 'ChapterFlagHidden'; + $EBMLidList[EBML_ID_CHAPTERPHYSICALEQUIV] = 'ChapterPhysicalEquiv'; + $EBMLidList[EBML_ID_CHAPTERS] = 'Chapters'; + $EBMLidList[EBML_ID_CHAPTERSEGMENTEDITIONUID] = 'ChapterSegmentEditionUID'; + $EBMLidList[EBML_ID_CHAPTERSEGMENTUID] = 'ChapterSegmentUID'; + $EBMLidList[EBML_ID_CHAPTERTIMEEND] = 'ChapterTimeEnd'; + $EBMLidList[EBML_ID_CHAPTERTIMESTART] = 'ChapterTimeStart'; + $EBMLidList[EBML_ID_CHAPTERTRACK] = 'ChapterTrack'; + $EBMLidList[EBML_ID_CHAPTERTRACKNUMBER] = 'ChapterTrackNumber'; + $EBMLidList[EBML_ID_CHAPTERTRANSLATE] = 'ChapterTranslate'; + $EBMLidList[EBML_ID_CHAPTERTRANSLATECODEC] = 'ChapterTranslateCodec'; + $EBMLidList[EBML_ID_CHAPTERTRANSLATEEDITIONUID] = 'ChapterTranslateEditionUID'; + $EBMLidList[EBML_ID_CHAPTERTRANSLATEID] = 'ChapterTranslateID'; + $EBMLidList[EBML_ID_CHAPTERUID] = 'ChapterUID'; + $EBMLidList[EBML_ID_CLUSTER] = 'Cluster'; + $EBMLidList[EBML_ID_CLUSTERBLOCK] = 'ClusterBlock'; + $EBMLidList[EBML_ID_CLUSTERBLOCKADDID] = 'ClusterBlockAddID'; + $EBMLidList[EBML_ID_CLUSTERBLOCKADDITIONAL] = 'ClusterBlockAdditional'; + $EBMLidList[EBML_ID_CLUSTERBLOCKADDITIONID] = 'ClusterBlockAdditionID'; + $EBMLidList[EBML_ID_CLUSTERBLOCKADDITIONS] = 'ClusterBlockAdditions'; + $EBMLidList[EBML_ID_CLUSTERBLOCKDURATION] = 'ClusterBlockDuration'; + $EBMLidList[EBML_ID_CLUSTERBLOCKGROUP] = 'ClusterBlockGroup'; + $EBMLidList[EBML_ID_CLUSTERBLOCKMORE] = 'ClusterBlockMore'; + $EBMLidList[EBML_ID_CLUSTERBLOCKVIRTUAL] = 'ClusterBlockVirtual'; + $EBMLidList[EBML_ID_CLUSTERCODECSTATE] = 'ClusterCodecState'; + $EBMLidList[EBML_ID_CLUSTERDELAY] = 'ClusterDelay'; + $EBMLidList[EBML_ID_CLUSTERDURATION] = 'ClusterDuration'; + $EBMLidList[EBML_ID_CLUSTERENCRYPTEDBLOCK] = 'ClusterEncryptedBlock'; + $EBMLidList[EBML_ID_CLUSTERFRAMENUMBER] = 'ClusterFrameNumber'; + $EBMLidList[EBML_ID_CLUSTERLACENUMBER] = 'ClusterLaceNumber'; + $EBMLidList[EBML_ID_CLUSTERPOSITION] = 'ClusterPosition'; + $EBMLidList[EBML_ID_CLUSTERPREVSIZE] = 'ClusterPrevSize'; + $EBMLidList[EBML_ID_CLUSTERREFERENCEBLOCK] = 'ClusterReferenceBlock'; + $EBMLidList[EBML_ID_CLUSTERREFERENCEPRIORITY] = 'ClusterReferencePriority'; + $EBMLidList[EBML_ID_CLUSTERREFERENCEVIRTUAL] = 'ClusterReferenceVirtual'; + $EBMLidList[EBML_ID_CLUSTERSILENTTRACKNUMBER] = 'ClusterSilentTrackNumber'; + $EBMLidList[EBML_ID_CLUSTERSILENTTRACKS] = 'ClusterSilentTracks'; + $EBMLidList[EBML_ID_CLUSTERSIMPLEBLOCK] = 'ClusterSimpleBlock'; + $EBMLidList[EBML_ID_CLUSTERTIMECODE] = 'ClusterTimecode'; + $EBMLidList[EBML_ID_CLUSTERTIMESLICE] = 'ClusterTimeSlice'; + $EBMLidList[EBML_ID_CODECDECODEALL] = 'CodecDecodeAll'; + $EBMLidList[EBML_ID_CODECDOWNLOADURL] = 'CodecDownloadURL'; + $EBMLidList[EBML_ID_CODECID] = 'CodecID'; + $EBMLidList[EBML_ID_CODECINFOURL] = 'CodecInfoURL'; + $EBMLidList[EBML_ID_CODECNAME] = 'CodecName'; + $EBMLidList[EBML_ID_CODECPRIVATE] = 'CodecPrivate'; + $EBMLidList[EBML_ID_CODECSETTINGS] = 'CodecSettings'; + $EBMLidList[EBML_ID_COLOURSPACE] = 'ColourSpace'; + $EBMLidList[EBML_ID_CONTENTCOMPALGO] = 'ContentCompAlgo'; + $EBMLidList[EBML_ID_CONTENTCOMPRESSION] = 'ContentCompression'; + $EBMLidList[EBML_ID_CONTENTCOMPSETTINGS] = 'ContentCompSettings'; + $EBMLidList[EBML_ID_CONTENTENCALGO] = 'ContentEncAlgo'; + $EBMLidList[EBML_ID_CONTENTENCKEYID] = 'ContentEncKeyID'; + $EBMLidList[EBML_ID_CONTENTENCODING] = 'ContentEncoding'; + $EBMLidList[EBML_ID_CONTENTENCODINGORDER] = 'ContentEncodingOrder'; + $EBMLidList[EBML_ID_CONTENTENCODINGS] = 'ContentEncodings'; + $EBMLidList[EBML_ID_CONTENTENCODINGSCOPE] = 'ContentEncodingScope'; + $EBMLidList[EBML_ID_CONTENTENCODINGTYPE] = 'ContentEncodingType'; + $EBMLidList[EBML_ID_CONTENTENCRYPTION] = 'ContentEncryption'; + $EBMLidList[EBML_ID_CONTENTSIGALGO] = 'ContentSigAlgo'; + $EBMLidList[EBML_ID_CONTENTSIGHASHALGO] = 'ContentSigHashAlgo'; + $EBMLidList[EBML_ID_CONTENTSIGKEYID] = 'ContentSigKeyID'; + $EBMLidList[EBML_ID_CONTENTSIGNATURE] = 'ContentSignature'; + $EBMLidList[EBML_ID_CRC32] = 'CRC32'; + $EBMLidList[EBML_ID_CUEBLOCKNUMBER] = 'CueBlockNumber'; + $EBMLidList[EBML_ID_CUECLUSTERPOSITION] = 'CueClusterPosition'; + $EBMLidList[EBML_ID_CUECODECSTATE] = 'CueCodecState'; + $EBMLidList[EBML_ID_CUEPOINT] = 'CuePoint'; + $EBMLidList[EBML_ID_CUEREFCLUSTER] = 'CueRefCluster'; + $EBMLidList[EBML_ID_CUEREFCODECSTATE] = 'CueRefCodecState'; + $EBMLidList[EBML_ID_CUEREFERENCE] = 'CueReference'; + $EBMLidList[EBML_ID_CUEREFNUMBER] = 'CueRefNumber'; + $EBMLidList[EBML_ID_CUEREFTIME] = 'CueRefTime'; + $EBMLidList[EBML_ID_CUES] = 'Cues'; + $EBMLidList[EBML_ID_CUETIME] = 'CueTime'; + $EBMLidList[EBML_ID_CUETRACK] = 'CueTrack'; + $EBMLidList[EBML_ID_CUETRACKPOSITIONS] = 'CueTrackPositions'; + $EBMLidList[EBML_ID_DATEUTC] = 'DateUTC'; + $EBMLidList[EBML_ID_DEFAULTDURATION] = 'DefaultDuration'; + $EBMLidList[EBML_ID_DISPLAYHEIGHT] = 'DisplayHeight'; + $EBMLidList[EBML_ID_DISPLAYUNIT] = 'DisplayUnit'; + $EBMLidList[EBML_ID_DISPLAYWIDTH] = 'DisplayWidth'; + $EBMLidList[EBML_ID_DOCTYPE] = 'DocType'; + $EBMLidList[EBML_ID_DOCTYPEREADVERSION] = 'DocTypeReadVersion'; + $EBMLidList[EBML_ID_DOCTYPEVERSION] = 'DocTypeVersion'; + $EBMLidList[EBML_ID_DURATION] = 'Duration'; + $EBMLidList[EBML_ID_EBML] = 'EBML'; + $EBMLidList[EBML_ID_EBMLMAXIDLENGTH] = 'EBMLMaxIDLength'; + $EBMLidList[EBML_ID_EBMLMAXSIZELENGTH] = 'EBMLMaxSizeLength'; + $EBMLidList[EBML_ID_EBMLREADVERSION] = 'EBMLReadVersion'; + $EBMLidList[EBML_ID_EBMLVERSION] = 'EBMLVersion'; + $EBMLidList[EBML_ID_EDITIONENTRY] = 'EditionEntry'; + $EBMLidList[EBML_ID_EDITIONFLAGDEFAULT] = 'EditionFlagDefault'; + $EBMLidList[EBML_ID_EDITIONFLAGHIDDEN] = 'EditionFlagHidden'; + $EBMLidList[EBML_ID_EDITIONFLAGORDERED] = 'EditionFlagOrdered'; + $EBMLidList[EBML_ID_EDITIONUID] = 'EditionUID'; + $EBMLidList[EBML_ID_FILEDATA] = 'FileData'; + $EBMLidList[EBML_ID_FILEDESCRIPTION] = 'FileDescription'; + $EBMLidList[EBML_ID_FILEMIMETYPE] = 'FileMimeType'; + $EBMLidList[EBML_ID_FILENAME] = 'FileName'; + $EBMLidList[EBML_ID_FILEREFERRAL] = 'FileReferral'; + $EBMLidList[EBML_ID_FILEUID] = 'FileUID'; + $EBMLidList[EBML_ID_FLAGDEFAULT] = 'FlagDefault'; + $EBMLidList[EBML_ID_FLAGENABLED] = 'FlagEnabled'; + $EBMLidList[EBML_ID_FLAGFORCED] = 'FlagForced'; + $EBMLidList[EBML_ID_FLAGINTERLACED] = 'FlagInterlaced'; + $EBMLidList[EBML_ID_FLAGLACING] = 'FlagLacing'; + $EBMLidList[EBML_ID_GAMMAVALUE] = 'GammaValue'; + $EBMLidList[EBML_ID_INFO] = 'Info'; + $EBMLidList[EBML_ID_LANGUAGE] = 'Language'; + $EBMLidList[EBML_ID_MAXBLOCKADDITIONID] = 'MaxBlockAdditionID'; + $EBMLidList[EBML_ID_MAXCACHE] = 'MaxCache'; + $EBMLidList[EBML_ID_MINCACHE] = 'MinCache'; + $EBMLidList[EBML_ID_MUXINGAPP] = 'MuxingApp'; + $EBMLidList[EBML_ID_NAME] = 'Name'; + $EBMLidList[EBML_ID_NEXTFILENAME] = 'NextFilename'; + $EBMLidList[EBML_ID_NEXTUID] = 'NextUID'; + $EBMLidList[EBML_ID_OUTPUTSAMPLINGFREQUENCY] = 'OutputSamplingFrequency'; + $EBMLidList[EBML_ID_PIXELCROPBOTTOM] = 'PixelCropBottom'; + $EBMLidList[EBML_ID_PIXELCROPLEFT] = 'PixelCropLeft'; + $EBMLidList[EBML_ID_PIXELCROPRIGHT] = 'PixelCropRight'; + $EBMLidList[EBML_ID_PIXELCROPTOP] = 'PixelCropTop'; + $EBMLidList[EBML_ID_PIXELHEIGHT] = 'PixelHeight'; + $EBMLidList[EBML_ID_PIXELWIDTH] = 'PixelWidth'; + $EBMLidList[EBML_ID_PREVFILENAME] = 'PrevFilename'; + $EBMLidList[EBML_ID_PREVUID] = 'PrevUID'; + $EBMLidList[EBML_ID_SAMPLINGFREQUENCY] = 'SamplingFrequency'; + $EBMLidList[EBML_ID_SEEK] = 'Seek'; + $EBMLidList[EBML_ID_SEEKHEAD] = 'SeekHead'; + $EBMLidList[EBML_ID_SEEKID] = 'SeekID'; + $EBMLidList[EBML_ID_SEEKPOSITION] = 'SeekPosition'; + $EBMLidList[EBML_ID_SEGMENT] = 'Segment'; + $EBMLidList[EBML_ID_SEGMENTFAMILY] = 'SegmentFamily'; + $EBMLidList[EBML_ID_SEGMENTFILENAME] = 'SegmentFilename'; + $EBMLidList[EBML_ID_SEGMENTUID] = 'SegmentUID'; + $EBMLidList[EBML_ID_SIMPLETAG] = 'SimpleTag'; + $EBMLidList[EBML_ID_CLUSTERSLICES] = 'ClusterSlices'; + $EBMLidList[EBML_ID_STEREOMODE] = 'StereoMode'; + $EBMLidList[EBML_ID_OLDSTEREOMODE] = 'OldStereoMode'; + $EBMLidList[EBML_ID_TAG] = 'Tag'; + $EBMLidList[EBML_ID_TAGATTACHMENTUID] = 'TagAttachmentUID'; + $EBMLidList[EBML_ID_TAGBINARY] = 'TagBinary'; + $EBMLidList[EBML_ID_TAGCHAPTERUID] = 'TagChapterUID'; + $EBMLidList[EBML_ID_TAGDEFAULT] = 'TagDefault'; + $EBMLidList[EBML_ID_TAGEDITIONUID] = 'TagEditionUID'; + $EBMLidList[EBML_ID_TAGLANGUAGE] = 'TagLanguage'; + $EBMLidList[EBML_ID_TAGNAME] = 'TagName'; + $EBMLidList[EBML_ID_TAGTRACKUID] = 'TagTrackUID'; + $EBMLidList[EBML_ID_TAGS] = 'Tags'; + $EBMLidList[EBML_ID_TAGSTRING] = 'TagString'; + $EBMLidList[EBML_ID_TARGETS] = 'Targets'; + $EBMLidList[EBML_ID_TARGETTYPE] = 'TargetType'; + $EBMLidList[EBML_ID_TARGETTYPEVALUE] = 'TargetTypeValue'; + $EBMLidList[EBML_ID_TIMECODESCALE] = 'TimecodeScale'; + $EBMLidList[EBML_ID_TITLE] = 'Title'; + $EBMLidList[EBML_ID_TRACKENTRY] = 'TrackEntry'; + $EBMLidList[EBML_ID_TRACKNUMBER] = 'TrackNumber'; + $EBMLidList[EBML_ID_TRACKOFFSET] = 'TrackOffset'; + $EBMLidList[EBML_ID_TRACKOVERLAY] = 'TrackOverlay'; + $EBMLidList[EBML_ID_TRACKS] = 'Tracks'; + $EBMLidList[EBML_ID_TRACKTIMECODESCALE] = 'TrackTimecodeScale'; + $EBMLidList[EBML_ID_TRACKTRANSLATE] = 'TrackTranslate'; + $EBMLidList[EBML_ID_TRACKTRANSLATECODEC] = 'TrackTranslateCodec'; + $EBMLidList[EBML_ID_TRACKTRANSLATEEDITIONUID] = 'TrackTranslateEditionUID'; + $EBMLidList[EBML_ID_TRACKTRANSLATETRACKID] = 'TrackTranslateTrackID'; + $EBMLidList[EBML_ID_TRACKTYPE] = 'TrackType'; + $EBMLidList[EBML_ID_TRACKUID] = 'TrackUID'; + $EBMLidList[EBML_ID_VIDEO] = 'Video'; + $EBMLidList[EBML_ID_VOID] = 'Void'; + $EBMLidList[EBML_ID_WRITINGAPP] = 'WritingApp'; + } + + return (isset($EBMLidList[$value]) ? $EBMLidList[$value] : dechex($value)); + } + + /** + * @param int $value + * + * @return string + */ + public static function displayUnit($value) { + // http://www.matroska.org/technical/specs/index.html#DisplayUnit + static $units = array( + 0 => 'pixels', + 1 => 'centimeters', + 2 => 'inches', + 3 => 'Display Aspect Ratio'); + + return (isset($units[$value]) ? $units[$value] : 'unknown'); + } + + /** + * @param array $streams + * + * @return array + */ + private static function getDefaultStreamInfo($streams) + { + $stream = array(); + foreach (array_reverse($streams) as $stream) { + if ($stream['default']) { + break; + } + } + + $unset = array('default', 'name'); + foreach ($unset as $u) { + if (isset($stream[$u])) { + unset($stream[$u]); + } + } + + $info = $stream; + $info['streams'] = $streams; + + return $info; + } + +} diff --git a/projects/tests/tests/performance/ID3/module.audio-video.quicktime.php b/projects/tests/tests/performance/ID3/module.audio-video.quicktime.php new file mode 100644 index 00000000..1cf1c059 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio-video.quicktime.php @@ -0,0 +1,3100 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio-video.quicktime.php // +// module for analyzing Quicktime and MP3-in-MP4 files // +// dependencies: module.audio.mp3.php // +// dependencies: module.tag.id3v2.php // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.mp3.php', __FILE__, true); +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true); // needed for ISO 639-2 language code lookup + +class getid3_quicktime extends getid3_handler +{ + + public $ReturnAtomData = true; + public $ParseAllPossibleAtoms = false; + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + $info['fileformat'] = 'quicktime'; + $info['quicktime']['hinting'] = false; + $info['quicktime']['controller'] = 'standard'; // may be overridden if 'ctyp' atom is present + + $this->fseek($info['avdataoffset']); + + $offset = 0; + $atomcounter = 0; + $atom_data_read_buffer_size = $info['php_memory_limit'] ? round($info['php_memory_limit'] / 4) : $this->getid3->option_fread_buffer_size * 1024; // set read buffer to 25% of PHP memory limit (if one is specified), otherwise use option_fread_buffer_size [default: 32MB] + while ($offset < $info['avdataend']) { + if (!getid3_lib::intValueSupported($offset)) { + $this->error('Unable to parse atom at offset '.$offset.' because beyond '.round(PHP_INT_MAX / 1073741824).'GB limit of PHP filesystem functions'); + break; + } + $this->fseek($offset); + $AtomHeader = $this->fread(8); + + $atomsize = getid3_lib::BigEndian2Int(substr($AtomHeader, 0, 4)); + $atomname = substr($AtomHeader, 4, 4); + + // 64-bit MOV patch by jlegateØktnc*com + if ($atomsize == 1) { + $atomsize = getid3_lib::BigEndian2Int($this->fread(8)); + } + + if (($offset + $atomsize) > $info['avdataend']) { + $info['quicktime'][$atomname]['name'] = $atomname; + $info['quicktime'][$atomname]['size'] = $atomsize; + $info['quicktime'][$atomname]['offset'] = $offset; + $this->error('Atom at offset '.$offset.' claims to go beyond end-of-file (length: '.$atomsize.' bytes)'); + return false; + } + if ($atomsize == 0) { + // Furthermore, for historical reasons the list of atoms is optionally + // terminated by a 32-bit integer set to 0. If you are writing a program + // to read user data atoms, you should allow for the terminating 0. + $info['quicktime'][$atomname]['name'] = $atomname; + $info['quicktime'][$atomname]['size'] = $atomsize; + $info['quicktime'][$atomname]['offset'] = $offset; + break; + } + + $atomHierarchy = array(); + $parsedAtomData = $this->QuicktimeParseAtom($atomname, $atomsize, $this->fread(min($atomsize, $atom_data_read_buffer_size)), $offset, $atomHierarchy, $this->ParseAllPossibleAtoms); + $parsedAtomData['name'] = $atomname; + $parsedAtomData['size'] = $atomsize; + $parsedAtomData['offset'] = $offset; + if (in_array($atomname, array('uuid'))) { + @$info['quicktime'][$atomname][] = $parsedAtomData; + } else { + $info['quicktime'][$atomname] = $parsedAtomData; + } + + $offset += $atomsize; + $atomcounter++; + } + + if (!empty($info['avdataend_tmp'])) { + // this value is assigned to a temp value and then erased because + // otherwise any atoms beyond the 'mdat' atom would not get parsed + $info['avdataend'] = $info['avdataend_tmp']; + unset($info['avdataend_tmp']); + } + + if (!empty($info['quicktime']['comments']['chapters']) && is_array($info['quicktime']['comments']['chapters']) && (count($info['quicktime']['comments']['chapters']) > 0)) { + $durations = $this->quicktime_time_to_sample_table($info); + for ($i = 0; $i < count($info['quicktime']['comments']['chapters']); $i++) { + $bookmark = array(); + $bookmark['title'] = $info['quicktime']['comments']['chapters'][$i]; + if (isset($durations[$i])) { + $bookmark['duration_sample'] = $durations[$i]['sample_duration']; + if ($i > 0) { + $bookmark['start_sample'] = $info['quicktime']['bookmarks'][($i - 1)]['start_sample'] + $info['quicktime']['bookmarks'][($i - 1)]['duration_sample']; + } else { + $bookmark['start_sample'] = 0; + } + if ($time_scale = $this->quicktime_bookmark_time_scale($info)) { + $bookmark['duration_seconds'] = $bookmark['duration_sample'] / $time_scale; + $bookmark['start_seconds'] = $bookmark['start_sample'] / $time_scale; + } + } + $info['quicktime']['bookmarks'][] = $bookmark; + } + } + + if (isset($info['quicktime']['temp_meta_key_names'])) { + unset($info['quicktime']['temp_meta_key_names']); + } + + if (!empty($info['quicktime']['comments']['location.ISO6709'])) { + // https://en.wikipedia.org/wiki/ISO_6709 + foreach ($info['quicktime']['comments']['location.ISO6709'] as $ISO6709string) { + $ISO6709parsed = array('latitude'=>false, 'longitude'=>false, 'altitude'=>false); + if (preg_match('#^([\\+\\-])([0-9]{2}|[0-9]{4}|[0-9]{6})(\\.[0-9]+)?([\\+\\-])([0-9]{3}|[0-9]{5}|[0-9]{7})(\\.[0-9]+)?(([\\+\\-])([0-9]{3}|[0-9]{5}|[0-9]{7})(\\.[0-9]+)?)?/$#', $ISO6709string, $matches)) { + // phpcs:ignore PHPCompatibility.Lists.AssignmentOrder.Affected + @list($dummy, $lat_sign, $lat_deg, $lat_deg_dec, $lon_sign, $lon_deg, $lon_deg_dec, $dummy, $alt_sign, $alt_deg, $alt_deg_dec) = $matches; + + if (strlen($lat_deg) == 2) { // [+-]DD.D + $ISO6709parsed['latitude'] = (($lat_sign == '-') ? -1 : 1) * floatval(ltrim($lat_deg, '0').$lat_deg_dec); + } elseif (strlen($lat_deg) == 4) { // [+-]DDMM.M + $ISO6709parsed['latitude'] = (($lat_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lat_deg, 0, 2), '0')) + floatval(ltrim(substr($lat_deg, 2, 2), '0').$lat_deg_dec / 60); + } elseif (strlen($lat_deg) == 6) { // [+-]DDMMSS.S + $ISO6709parsed['latitude'] = (($lat_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lat_deg, 0, 2), '0')) + floatval(ltrim(substr($lat_deg, 2, 2), '0') / 60) + floatval(ltrim(substr($lat_deg, 4, 2), '0').$lat_deg_dec / 3600); + } + + if (strlen($lon_deg) == 3) { // [+-]DDD.D + $ISO6709parsed['longitude'] = (($lon_sign == '-') ? -1 : 1) * floatval(ltrim($lon_deg, '0').$lon_deg_dec); + } elseif (strlen($lon_deg) == 5) { // [+-]DDDMM.M + $ISO6709parsed['longitude'] = (($lon_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lon_deg, 0, 2), '0')) + floatval(ltrim(substr($lon_deg, 2, 2), '0').$lon_deg_dec / 60); + } elseif (strlen($lon_deg) == 7) { // [+-]DDDMMSS.S + $ISO6709parsed['longitude'] = (($lon_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lon_deg, 0, 2), '0')) + floatval(ltrim(substr($lon_deg, 2, 2), '0') / 60) + floatval(ltrim(substr($lon_deg, 4, 2), '0').$lon_deg_dec / 3600); + } + + if (strlen($alt_deg) == 3) { // [+-]DDD.D + $ISO6709parsed['altitude'] = (($alt_sign == '-') ? -1 : 1) * floatval(ltrim($alt_deg, '0').$alt_deg_dec); + } elseif (strlen($alt_deg) == 5) { // [+-]DDDMM.M + $ISO6709parsed['altitude'] = (($alt_sign == '-') ? -1 : 1) * floatval(ltrim(substr($alt_deg, 0, 2), '0')) + floatval(ltrim(substr($alt_deg, 2, 2), '0').$alt_deg_dec / 60); + } elseif (strlen($alt_deg) == 7) { // [+-]DDDMMSS.S + $ISO6709parsed['altitude'] = (($alt_sign == '-') ? -1 : 1) * floatval(ltrim(substr($alt_deg, 0, 2), '0')) + floatval(ltrim(substr($alt_deg, 2, 2), '0') / 60) + floatval(ltrim(substr($alt_deg, 4, 2), '0').$alt_deg_dec / 3600); + } + + foreach (array('latitude', 'longitude', 'altitude') as $key) { + if ($ISO6709parsed[$key] !== false) { + $value = (($lat_sign == '-') ? -1 : 1) * floatval($ISO6709parsed[$key]); + if (!isset($info['quicktime']['comments']['gps_'.$key]) || !in_array($value, $info['quicktime']['comments']['gps_'.$key])) { + @$info['quicktime']['comments']['gps_'.$key][] = (($lat_sign == '-') ? -1 : 1) * floatval($ISO6709parsed[$key]); + } + } + } + } + if ($ISO6709parsed['latitude'] === false) { + $this->warning('location.ISO6709 string not parsed correctly: "'.$ISO6709string.'", please submit as a bug'); + } + break; + } + } + + if (!isset($info['bitrate']) && isset($info['playtime_seconds'])) { + $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; + } + if (isset($info['bitrate']) && !isset($info['audio']['bitrate']) && !isset($info['quicktime']['video'])) { + $info['audio']['bitrate'] = $info['bitrate']; + } + if (!empty($info['bitrate']) && !empty($info['audio']['bitrate']) && empty($info['video']['bitrate']) && !empty($info['video']['frame_rate']) && !empty($info['video']['resolution_x']) && ($info['bitrate'] > $info['audio']['bitrate'])) { + $info['video']['bitrate'] = $info['bitrate'] - $info['audio']['bitrate']; + } + if (!empty($info['playtime_seconds']) && !isset($info['video']['frame_rate']) && !empty($info['quicktime']['stts_framecount'])) { + foreach ($info['quicktime']['stts_framecount'] as $key => $samples_count) { + $samples_per_second = $samples_count / $info['playtime_seconds']; + if ($samples_per_second > 240) { + // has to be audio samples + } else { + $info['video']['frame_rate'] = $samples_per_second; + break; + } + } + } + if ($info['audio']['dataformat'] == 'mp4') { + $info['fileformat'] = 'mp4'; + if (empty($info['video']['resolution_x'])) { + $info['mime_type'] = 'audio/mp4'; + unset($info['video']['dataformat']); + } else { + $info['mime_type'] = 'video/mp4'; + } + } + + if (!$this->ReturnAtomData) { + unset($info['quicktime']['moov']); + } + + if (empty($info['audio']['dataformat']) && !empty($info['quicktime']['audio'])) { + $info['audio']['dataformat'] = 'quicktime'; + } + if (empty($info['video']['dataformat']) && !empty($info['quicktime']['video'])) { + $info['video']['dataformat'] = 'quicktime'; + } + if (isset($info['video']) && ($info['mime_type'] == 'audio/mp4') && empty($info['video']['resolution_x']) && empty($info['video']['resolution_y'])) { + unset($info['video']); + } + + return true; + } + + /** + * @param string $atomname + * @param int $atomsize + * @param string $atom_data + * @param int $baseoffset + * @param array $atomHierarchy + * @param bool $ParseAllPossibleAtoms + * + * @return array|false + */ + public function QuicktimeParseAtom($atomname, $atomsize, $atom_data, $baseoffset, &$atomHierarchy, $ParseAllPossibleAtoms) { + // http://developer.apple.com/techpubs/quicktime/qtdevdocs/APIREF/INDEX/atomalphaindex.htm + // https://code.google.com/p/mp4v2/wiki/iTunesMetadata + + $info = &$this->getid3->info; + + $atom_parent = end($atomHierarchy); // not array_pop($atomHierarchy); see https://www.getid3.org/phpBB3/viewtopic.php?t=1717 + array_push($atomHierarchy, $atomname); + $atom_structure = array(); + $atom_structure['hierarchy'] = implode(' ', $atomHierarchy); + $atom_structure['name'] = $atomname; + $atom_structure['size'] = $atomsize; + $atom_structure['offset'] = $baseoffset; + if (substr($atomname, 0, 3) == "\x00\x00\x00") { + // https://github.com/JamesHeinrich/getID3/issues/139 + $atomname = getid3_lib::BigEndian2Int($atomname); + $atom_structure['name'] = $atomname; + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms); + } else { + switch ($atomname) { + case 'moov': // MOVie container atom + case 'trak': // TRAcK container atom + case 'clip': // CLIPping container atom + case 'matt': // track MATTe container atom + case 'edts': // EDiTS container atom + case 'tref': // Track REFerence container atom + case 'mdia': // MeDIA container atom + case 'minf': // Media INFormation container atom + case 'dinf': // Data INFormation container atom + case 'nmhd': // Null Media HeaDer container atom + case 'udta': // User DaTA container atom + case 'cmov': // Compressed MOVie container atom + case 'rmra': // Reference Movie Record Atom + case 'rmda': // Reference Movie Descriptor Atom + case 'gmhd': // Generic Media info HeaDer atom (seen on QTVR) + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms); + break; + + case 'ilst': // Item LiST container atom + if ($atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms)) { + // some "ilst" atoms contain data atoms that have a numeric name, and the data is far more accessible if the returned array is compacted + $allnumericnames = true; + foreach ($atom_structure['subatoms'] as $subatomarray) { + if (!is_integer($subatomarray['name']) || (count($subatomarray['subatoms']) != 1)) { + $allnumericnames = false; + break; + } + } + if ($allnumericnames) { + $newData = array(); + foreach ($atom_structure['subatoms'] as $subatomarray) { + foreach ($subatomarray['subatoms'] as $newData_subatomarray) { + unset($newData_subatomarray['hierarchy'], $newData_subatomarray['name']); + $newData[$subatomarray['name']] = $newData_subatomarray; + break; + } + } + $atom_structure['data'] = $newData; + unset($atom_structure['subatoms']); + } + } + break; + + case 'stbl': // Sample TaBLe container atom + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms); + $isVideo = false; + $framerate = 0; + $framecount = 0; + foreach ($atom_structure['subatoms'] as $key => $value_array) { + if (isset($value_array['sample_description_table'])) { + foreach ($value_array['sample_description_table'] as $key2 => $value_array2) { + if (isset($value_array2['data_format'])) { + switch ($value_array2['data_format']) { + case 'avc1': + case 'mp4v': + // video data + $isVideo = true; + break; + case 'mp4a': + // audio data + break; + } + } + } + } elseif (isset($value_array['time_to_sample_table'])) { + foreach ($value_array['time_to_sample_table'] as $key2 => $value_array2) { + if (isset($value_array2['sample_count']) && isset($value_array2['sample_duration']) && ($value_array2['sample_duration'] > 0)) { + $framerate = round($info['quicktime']['time_scale'] / $value_array2['sample_duration'], 3); + $framecount = $value_array2['sample_count']; + } + } + } + } + if ($isVideo && $framerate) { + $info['quicktime']['video']['frame_rate'] = $framerate; + $info['video']['frame_rate'] = $info['quicktime']['video']['frame_rate']; + } + if ($isVideo && $framecount) { + $info['quicktime']['video']['frame_count'] = $framecount; + } + break; + + + case "\xA9".'alb': // ALBum + case "\xA9".'ART': // + case "\xA9".'art': // ARTist + case "\xA9".'aut': // + case "\xA9".'cmt': // CoMmenT + case "\xA9".'com': // COMposer + case "\xA9".'cpy': // + case "\xA9".'day': // content created year + case "\xA9".'dir': // + case "\xA9".'ed1': // + case "\xA9".'ed2': // + case "\xA9".'ed3': // + case "\xA9".'ed4': // + case "\xA9".'ed5': // + case "\xA9".'ed6': // + case "\xA9".'ed7': // + case "\xA9".'ed8': // + case "\xA9".'ed9': // + case "\xA9".'enc': // + case "\xA9".'fmt': // + case "\xA9".'gen': // GENre + case "\xA9".'grp': // GRouPing + case "\xA9".'hst': // + case "\xA9".'inf': // + case "\xA9".'lyr': // LYRics + case "\xA9".'mak': // + case "\xA9".'mod': // + case "\xA9".'nam': // full NAMe + case "\xA9".'ope': // + case "\xA9".'PRD': // + case "\xA9".'prf': // + case "\xA9".'req': // + case "\xA9".'src': // + case "\xA9".'swr': // + case "\xA9".'too': // encoder + case "\xA9".'trk': // TRacK + case "\xA9".'url': // + case "\xA9".'wrn': // + case "\xA9".'wrt': // WRiTer + case '----': // itunes specific + case 'aART': // Album ARTist + case 'akID': // iTunes store account type + case 'apID': // Purchase Account + case 'atID': // + case 'catg': // CaTeGory + case 'cmID': // + case 'cnID': // + case 'covr': // COVeR artwork + case 'cpil': // ComPILation + case 'cprt': // CoPyRighT + case 'desc': // DESCription + case 'disk': // DISK number + case 'egid': // Episode Global ID + case 'geID': // + case 'gnre': // GeNRE + case 'hdvd': // HD ViDeo + case 'keyw': // KEYWord + case 'ldes': // Long DEScription + case 'pcst': // PodCaST + case 'pgap': // GAPless Playback + case 'plID': // + case 'purd': // PURchase Date + case 'purl': // Podcast URL + case 'rati': // + case 'rndu': // + case 'rpdu': // + case 'rtng': // RaTiNG + case 'sfID': // iTunes store country + case 'soaa': // SOrt Album Artist + case 'soal': // SOrt ALbum + case 'soar': // SOrt ARtist + case 'soco': // SOrt COmposer + case 'sonm': // SOrt NaMe + case 'sosn': // SOrt Show Name + case 'stik': // + case 'tmpo': // TeMPO (BPM) + case 'trkn': // TRacK Number + case 'tven': // tvEpisodeID + case 'tves': // TV EpiSode + case 'tvnn': // TV Network Name + case 'tvsh': // TV SHow Name + case 'tvsn': // TV SeasoN + if ($atom_parent == 'udta') { + // User data atom handler + $atom_structure['data_length'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 2)); + $atom_structure['language_id'] = getid3_lib::BigEndian2Int(substr($atom_data, 2, 2)); + $atom_structure['data'] = substr($atom_data, 4); + + $atom_structure['language'] = $this->QuicktimeLanguageLookup($atom_structure['language_id']); + if (empty($info['comments']['language']) || (!in_array($atom_structure['language'], $info['comments']['language']))) { + $info['comments']['language'][] = $atom_structure['language']; + } + } else { + // Apple item list box atom handler + $atomoffset = 0; + if (substr($atom_data, 2, 2) == "\x10\xB5") { + // not sure what it means, but observed on iPhone4 data. + // Each $atom_data has 2 bytes of datasize, plus 0x10B5, then data + while ($atomoffset < strlen($atom_data)) { + $boxsmallsize = getid3_lib::BigEndian2Int(substr($atom_data, $atomoffset, 2)); + $boxsmalltype = substr($atom_data, $atomoffset + 2, 2); + $boxsmalldata = substr($atom_data, $atomoffset + 4, $boxsmallsize); + if ($boxsmallsize <= 1) { + $this->warning('Invalid QuickTime atom smallbox size "'.$boxsmallsize.'" in atom "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $atomname).'" at offset: '.($atom_structure['offset'] + $atomoffset)); + $atom_structure['data'] = null; + $atomoffset = strlen($atom_data); + break; + } + switch ($boxsmalltype) { + case "\x10\xB5": + $atom_structure['data'] = $boxsmalldata; + break; + default: + $this->warning('Unknown QuickTime smallbox type: "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $boxsmalltype).'" ('.trim(getid3_lib::PrintHexBytes($boxsmalltype)).') at offset '.$baseoffset); + $atom_structure['data'] = $atom_data; + break; + } + $atomoffset += (4 + $boxsmallsize); + } + } else { + while ($atomoffset < strlen($atom_data)) { + $boxsize = getid3_lib::BigEndian2Int(substr($atom_data, $atomoffset, 4)); + $boxtype = substr($atom_data, $atomoffset + 4, 4); + $boxdata = substr($atom_data, $atomoffset + 8, $boxsize - 8); + if ($boxsize <= 1) { + $this->warning('Invalid QuickTime atom box size "'.$boxsize.'" in atom "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $atomname).'" at offset: '.($atom_structure['offset'] + $atomoffset)); + $atom_structure['data'] = null; + $atomoffset = strlen($atom_data); + break; + } + $atomoffset += $boxsize; + + switch ($boxtype) { + case 'mean': + case 'name': + $atom_structure[$boxtype] = substr($boxdata, 4); + break; + + case 'data': + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($boxdata, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($boxdata, 1, 3)); + switch ($atom_structure['flags_raw']) { + case 0: // data flag + case 21: // tmpo/cpil flag + switch ($atomname) { + case 'cpil': + case 'hdvd': + case 'pcst': + case 'pgap': + // 8-bit integer (boolean) + $atom_structure['data'] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 1)); + break; + + case 'tmpo': + // 16-bit integer + $atom_structure['data'] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 2)); + break; + + case 'disk': + case 'trkn': + // binary + $num = getid3_lib::BigEndian2Int(substr($boxdata, 10, 2)); + $num_total = getid3_lib::BigEndian2Int(substr($boxdata, 12, 2)); + $atom_structure['data'] = empty($num) ? '' : $num; + $atom_structure['data'] .= empty($num_total) ? '' : '/'.$num_total; + break; + + case 'gnre': + // enum + $GenreID = getid3_lib::BigEndian2Int(substr($boxdata, 8, 4)); + $atom_structure['data'] = getid3_id3v1::LookupGenreName($GenreID - 1); + break; + + case 'rtng': + // 8-bit integer + $atom_structure[$atomname] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 1)); + $atom_structure['data'] = $this->QuicktimeContentRatingLookup($atom_structure[$atomname]); + break; + + case 'stik': + // 8-bit integer (enum) + $atom_structure[$atomname] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 1)); + $atom_structure['data'] = $this->QuicktimeSTIKLookup($atom_structure[$atomname]); + break; + + case 'sfID': + // 32-bit integer + $atom_structure[$atomname] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 4)); + $atom_structure['data'] = $this->QuicktimeStoreFrontCodeLookup($atom_structure[$atomname]); + break; + + case 'egid': + case 'purl': + $atom_structure['data'] = substr($boxdata, 8); + break; + + case 'plID': + // 64-bit integer + $atom_structure['data'] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 8)); + break; + + case 'covr': + $atom_structure['data'] = substr($boxdata, 8); + // not a foolproof check, but better than nothing + if (preg_match('#^\\xFF\\xD8\\xFF#', $atom_structure['data'])) { + $atom_structure['image_mime'] = 'image/jpeg'; + } elseif (preg_match('#^\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A#', $atom_structure['data'])) { + $atom_structure['image_mime'] = 'image/png'; + } elseif (preg_match('#^GIF#', $atom_structure['data'])) { + $atom_structure['image_mime'] = 'image/gif'; + } + $info['quicktime']['comments']['picture'][] = array('image_mime'=>$atom_structure['image_mime'], 'data'=>$atom_structure['data'], 'description'=>'cover'); + break; + + case 'atID': + case 'cnID': + case 'geID': + case 'tves': + case 'tvsn': + default: + // 32-bit integer + $atom_structure['data'] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 4)); + } + break; + + case 1: // text flag + case 13: // image flag + default: + $atom_structure['data'] = substr($boxdata, 8); + if ($atomname == 'covr') { + // not a foolproof check, but better than nothing + if (preg_match('#^\\xFF\\xD8\\xFF#', $atom_structure['data'])) { + $atom_structure['image_mime'] = 'image/jpeg'; + } elseif (preg_match('#^\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A#', $atom_structure['data'])) { + $atom_structure['image_mime'] = 'image/png'; + } elseif (preg_match('#^GIF#', $atom_structure['data'])) { + $atom_structure['image_mime'] = 'image/gif'; + } + $info['quicktime']['comments']['picture'][] = array('image_mime'=>$atom_structure['image_mime'], 'data'=>$atom_structure['data'], 'description'=>'cover'); + } + break; + + } + break; + + default: + $this->warning('Unknown QuickTime box type: "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $boxtype).'" ('.trim(getid3_lib::PrintHexBytes($boxtype)).') at offset '.$baseoffset); + $atom_structure['data'] = $atom_data; + + } + } + } + } + $this->CopyToAppropriateCommentsSection($atomname, $atom_structure['data'], $atom_structure['name']); + break; + + + case 'play': // auto-PLAY atom + $atom_structure['autoplay'] = (bool) getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + + $info['quicktime']['autoplay'] = $atom_structure['autoplay']; + break; + + + case 'WLOC': // Window LOCation atom + $atom_structure['location_x'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 2)); + $atom_structure['location_y'] = getid3_lib::BigEndian2Int(substr($atom_data, 2, 2)); + break; + + + case 'LOOP': // LOOPing atom + case 'SelO': // play SELection Only atom + case 'AllF': // play ALL Frames atom + $atom_structure['data'] = getid3_lib::BigEndian2Int($atom_data); + break; + + + case 'name': // + case 'MCPS': // Media Cleaner PRo + case '@PRM': // adobe PReMiere version + case '@PRQ': // adobe PRemiere Quicktime version + $atom_structure['data'] = $atom_data; + break; + + + case 'cmvd': // Compressed MooV Data atom + // Code by ubergeekØubergeek*tv based on information from + // http://developer.apple.com/quicktime/icefloe/dispatch012.html + $atom_structure['unCompressedSize'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 4)); + + $CompressedFileData = substr($atom_data, 4); + if ($UncompressedHeader = @gzuncompress($CompressedFileData)) { + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($UncompressedHeader, 0, $atomHierarchy, $ParseAllPossibleAtoms); + } else { + $this->warning('Error decompressing compressed MOV atom at offset '.$atom_structure['offset']); + } + break; + + + case 'dcom': // Data COMpression atom + $atom_structure['compression_id'] = $atom_data; + $atom_structure['compression_text'] = $this->QuicktimeDCOMLookup($atom_data); + break; + + + case 'rdrf': // Reference movie Data ReFerence atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); + $atom_structure['flags']['internal_data'] = (bool) ($atom_structure['flags_raw'] & 0x000001); + + $atom_structure['reference_type_name'] = substr($atom_data, 4, 4); + $atom_structure['reference_length'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 4)); + switch ($atom_structure['reference_type_name']) { + case 'url ': + $atom_structure['url'] = $this->NoNullString(substr($atom_data, 12)); + break; + + case 'alis': + $atom_structure['file_alias'] = substr($atom_data, 12); + break; + + case 'rsrc': + $atom_structure['resource_alias'] = substr($atom_data, 12); + break; + + default: + $atom_structure['data'] = substr($atom_data, 12); + break; + } + break; + + + case 'rmqu': // Reference Movie QUality atom + $atom_structure['movie_quality'] = getid3_lib::BigEndian2Int($atom_data); + break; + + + case 'rmcs': // Reference Movie Cpu Speed atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['cpu_speed_rating'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); + break; + + + case 'rmvc': // Reference Movie Version Check atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['gestalt_selector'] = substr($atom_data, 4, 4); + $atom_structure['gestalt_value_mask'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 4)); + $atom_structure['gestalt_value'] = getid3_lib::BigEndian2Int(substr($atom_data, 12, 4)); + $atom_structure['gestalt_check_type'] = getid3_lib::BigEndian2Int(substr($atom_data, 14, 2)); + break; + + + case 'rmcd': // Reference Movie Component check atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['component_type'] = substr($atom_data, 4, 4); + $atom_structure['component_subtype'] = substr($atom_data, 8, 4); + $atom_structure['component_manufacturer'] = substr($atom_data, 12, 4); + $atom_structure['component_flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 16, 4)); + $atom_structure['component_flags_mask'] = getid3_lib::BigEndian2Int(substr($atom_data, 20, 4)); + $atom_structure['component_min_version'] = getid3_lib::BigEndian2Int(substr($atom_data, 24, 4)); + break; + + + case 'rmdr': // Reference Movie Data Rate atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['data_rate'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + + $atom_structure['data_rate_bps'] = $atom_structure['data_rate'] * 10; + break; + + + case 'rmla': // Reference Movie Language Atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['language_id'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); + + $atom_structure['language'] = $this->QuicktimeLanguageLookup($atom_structure['language_id']); + if (empty($info['comments']['language']) || (!in_array($atom_structure['language'], $info['comments']['language']))) { + $info['comments']['language'][] = $atom_structure['language']; + } + break; + + + case 'ptv ': // Print To Video - defines a movie's full screen mode + // http://developer.apple.com/documentation/QuickTime/APIREF/SOURCESIV/at_ptv-_pg.htm + $atom_structure['display_size_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 2)); + $atom_structure['reserved_1'] = getid3_lib::BigEndian2Int(substr($atom_data, 2, 2)); // hardcoded: 0x0000 + $atom_structure['reserved_2'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); // hardcoded: 0x0000 + $atom_structure['slide_show_flag'] = getid3_lib::BigEndian2Int(substr($atom_data, 6, 1)); + $atom_structure['play_on_open_flag'] = getid3_lib::BigEndian2Int(substr($atom_data, 7, 1)); + + $atom_structure['flags']['play_on_open'] = (bool) $atom_structure['play_on_open_flag']; + $atom_structure['flags']['slide_show'] = (bool) $atom_structure['slide_show_flag']; + + $ptv_lookup[0] = 'normal'; + $ptv_lookup[1] = 'double'; + $ptv_lookup[2] = 'half'; + $ptv_lookup[3] = 'full'; + $ptv_lookup[4] = 'current'; + if (isset($ptv_lookup[$atom_structure['display_size_raw']])) { + $atom_structure['display_size'] = $ptv_lookup[$atom_structure['display_size_raw']]; + } else { + $this->warning('unknown "ptv " display constant ('.$atom_structure['display_size_raw'].')'); + } + break; + + + case 'stsd': // Sample Table Sample Description atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + + // see: https://github.com/JamesHeinrich/getID3/issues/111 + // Some corrupt files have been known to have high bits set in the number_entries field + // This field shouldn't really need to be 32-bits, values stores are likely in the range 1-100000 + // Workaround: mask off the upper byte and throw a warning if it's nonzero + if ($atom_structure['number_entries'] > 0x000FFFFF) { + if ($atom_structure['number_entries'] > 0x00FFFFFF) { + $this->warning('"stsd" atom contains improbably large number_entries (0x'.getid3_lib::PrintHexBytes(substr($atom_data, 4, 4), true, false).' = '.$atom_structure['number_entries'].'), probably in error. Ignoring upper byte and interpreting this as 0x'.getid3_lib::PrintHexBytes(substr($atom_data, 5, 3), true, false).' = '.($atom_structure['number_entries'] & 0x00FFFFFF)); + $atom_structure['number_entries'] = ($atom_structure['number_entries'] & 0x00FFFFFF); + } else { + $this->warning('"stsd" atom contains improbably large number_entries (0x'.getid3_lib::PrintHexBytes(substr($atom_data, 4, 4), true, false).' = '.$atom_structure['number_entries'].'), probably in error. Please report this to info@getid3.org referencing bug report #111'); + } + } + + $stsdEntriesDataOffset = 8; + for ($i = 0; $i < $atom_structure['number_entries']; $i++) { + $atom_structure['sample_description_table'][$i]['size'] = getid3_lib::BigEndian2Int(substr($atom_data, $stsdEntriesDataOffset, 4)); + $stsdEntriesDataOffset += 4; + $atom_structure['sample_description_table'][$i]['data_format'] = substr($atom_data, $stsdEntriesDataOffset, 4); + $stsdEntriesDataOffset += 4; + $atom_structure['sample_description_table'][$i]['reserved'] = getid3_lib::BigEndian2Int(substr($atom_data, $stsdEntriesDataOffset, 6)); + $stsdEntriesDataOffset += 6; + $atom_structure['sample_description_table'][$i]['reference_index'] = getid3_lib::BigEndian2Int(substr($atom_data, $stsdEntriesDataOffset, 2)); + $stsdEntriesDataOffset += 2; + $atom_structure['sample_description_table'][$i]['data'] = substr($atom_data, $stsdEntriesDataOffset, ($atom_structure['sample_description_table'][$i]['size'] - 4 - 4 - 6 - 2)); + $stsdEntriesDataOffset += ($atom_structure['sample_description_table'][$i]['size'] - 4 - 4 - 6 - 2); + + if (substr($atom_structure['sample_description_table'][$i]['data'], 1, 54) == 'application/octet-stream;type=com.parrot.videometadata') { + // special handling for apparently-malformed (TextMetaDataSampleEntry?) data for some version of Parrot drones + $atom_structure['sample_description_table'][$i]['parrot_frame_metadata']['mime_type'] = substr($atom_structure['sample_description_table'][$i]['data'], 1, 55); + $atom_structure['sample_description_table'][$i]['parrot_frame_metadata']['metadata_version'] = (int) substr($atom_structure['sample_description_table'][$i]['data'], 55, 1); + unset($atom_structure['sample_description_table'][$i]['data']); +$this->warning('incomplete/incorrect handling of "stsd" with Parrot metadata in this version of getID3() ['.$this->getid3->version().']'); + continue; + } + + $atom_structure['sample_description_table'][$i]['encoder_version'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 0, 2)); + $atom_structure['sample_description_table'][$i]['encoder_revision'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 2, 2)); + $atom_structure['sample_description_table'][$i]['encoder_vendor'] = substr($atom_structure['sample_description_table'][$i]['data'], 4, 4); + + switch ($atom_structure['sample_description_table'][$i]['encoder_vendor']) { + + case "\x00\x00\x00\x00": + // audio tracks + $atom_structure['sample_description_table'][$i]['audio_channels'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 8, 2)); + $atom_structure['sample_description_table'][$i]['audio_bit_depth'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 10, 2)); + $atom_structure['sample_description_table'][$i]['audio_compression_id'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 12, 2)); + $atom_structure['sample_description_table'][$i]['audio_packet_size'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 14, 2)); + $atom_structure['sample_description_table'][$i]['audio_sample_rate'] = getid3_lib::FixedPoint16_16(substr($atom_structure['sample_description_table'][$i]['data'], 16, 4)); + + // video tracks + // http://developer.apple.com/library/mac/#documentation/QuickTime/QTFF/QTFFChap3/qtff3.html + $atom_structure['sample_description_table'][$i]['temporal_quality'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 8, 4)); + $atom_structure['sample_description_table'][$i]['spatial_quality'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 12, 4)); + $atom_structure['sample_description_table'][$i]['width'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 16, 2)); + $atom_structure['sample_description_table'][$i]['height'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 18, 2)); + $atom_structure['sample_description_table'][$i]['resolution_x'] = getid3_lib::FixedPoint16_16(substr($atom_structure['sample_description_table'][$i]['data'], 24, 4)); + $atom_structure['sample_description_table'][$i]['resolution_y'] = getid3_lib::FixedPoint16_16(substr($atom_structure['sample_description_table'][$i]['data'], 28, 4)); + $atom_structure['sample_description_table'][$i]['data_size'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 32, 4)); + $atom_structure['sample_description_table'][$i]['frame_count'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 36, 2)); + $atom_structure['sample_description_table'][$i]['compressor_name'] = substr($atom_structure['sample_description_table'][$i]['data'], 38, 4); + $atom_structure['sample_description_table'][$i]['pixel_depth'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 42, 2)); + $atom_structure['sample_description_table'][$i]['color_table_id'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 44, 2)); + + switch ($atom_structure['sample_description_table'][$i]['data_format']) { + case '2vuY': + case 'avc1': + case 'cvid': + case 'dvc ': + case 'dvcp': + case 'gif ': + case 'h263': + case 'jpeg': + case 'kpcd': + case 'mjpa': + case 'mjpb': + case 'mp4v': + case 'png ': + case 'raw ': + case 'rle ': + case 'rpza': + case 'smc ': + case 'SVQ1': + case 'SVQ3': + case 'tiff': + case 'v210': + case 'v216': + case 'v308': + case 'v408': + case 'v410': + case 'yuv2': + $info['fileformat'] = 'mp4'; + $info['video']['fourcc'] = $atom_structure['sample_description_table'][$i]['data_format']; + if ($this->QuicktimeVideoCodecLookup($info['video']['fourcc'])) { + $info['video']['fourcc_lookup'] = $this->QuicktimeVideoCodecLookup($info['video']['fourcc']); + } + + // https://www.getid3.org/phpBB3/viewtopic.php?t=1550 + //if ((!empty($atom_structure['sample_description_table'][$i]['width']) && !empty($atom_structure['sample_description_table'][$i]['width'])) && (empty($info['video']['resolution_x']) || empty($info['video']['resolution_y']) || (number_format($info['video']['resolution_x'], 6) != number_format(round($info['video']['resolution_x']), 6)) || (number_format($info['video']['resolution_y'], 6) != number_format(round($info['video']['resolution_y']), 6)))) { // ugly check for floating point numbers + if (!empty($atom_structure['sample_description_table'][$i]['width']) && !empty($atom_structure['sample_description_table'][$i]['height'])) { + // assume that values stored here are more important than values stored in [tkhd] atom + $info['video']['resolution_x'] = $atom_structure['sample_description_table'][$i]['width']; + $info['video']['resolution_y'] = $atom_structure['sample_description_table'][$i]['height']; + $info['quicktime']['video']['resolution_x'] = $info['video']['resolution_x']; + $info['quicktime']['video']['resolution_y'] = $info['video']['resolution_y']; + } + break; + + case 'qtvr': + $info['video']['dataformat'] = 'quicktimevr'; + break; + + case 'mp4a': + default: + $info['quicktime']['audio']['codec'] = $this->QuicktimeAudioCodecLookup($atom_structure['sample_description_table'][$i]['data_format']); + $info['quicktime']['audio']['sample_rate'] = $atom_structure['sample_description_table'][$i]['audio_sample_rate']; + $info['quicktime']['audio']['channels'] = $atom_structure['sample_description_table'][$i]['audio_channels']; + $info['quicktime']['audio']['bit_depth'] = $atom_structure['sample_description_table'][$i]['audio_bit_depth']; + $info['audio']['codec'] = $info['quicktime']['audio']['codec']; + $info['audio']['sample_rate'] = $info['quicktime']['audio']['sample_rate']; + $info['audio']['channels'] = $info['quicktime']['audio']['channels']; + $info['audio']['bits_per_sample'] = $info['quicktime']['audio']['bit_depth']; + switch ($atom_structure['sample_description_table'][$i]['data_format']) { + case 'raw ': // PCM + case 'alac': // Apple Lossless Audio Codec + case 'sowt': // signed/two's complement (Little Endian) + case 'twos': // signed/two's complement (Big Endian) + case 'in24': // 24-bit Integer + case 'in32': // 32-bit Integer + case 'fl32': // 32-bit Floating Point + case 'fl64': // 64-bit Floating Point + $info['audio']['lossless'] = $info['quicktime']['audio']['lossless'] = true; + $info['audio']['bitrate'] = $info['quicktime']['audio']['bitrate'] = $info['audio']['channels'] * $info['audio']['bits_per_sample'] * $info['audio']['sample_rate']; + break; + default: + $info['audio']['lossless'] = false; + break; + } + break; + } + break; + + default: + switch ($atom_structure['sample_description_table'][$i]['data_format']) { + case 'mp4s': + $info['fileformat'] = 'mp4'; + break; + + default: + // video atom + $atom_structure['sample_description_table'][$i]['video_temporal_quality'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 8, 4)); + $atom_structure['sample_description_table'][$i]['video_spatial_quality'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 12, 4)); + $atom_structure['sample_description_table'][$i]['video_frame_width'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 16, 2)); + $atom_structure['sample_description_table'][$i]['video_frame_height'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 18, 2)); + $atom_structure['sample_description_table'][$i]['video_resolution_x'] = getid3_lib::FixedPoint16_16(substr($atom_structure['sample_description_table'][$i]['data'], 20, 4)); + $atom_structure['sample_description_table'][$i]['video_resolution_y'] = getid3_lib::FixedPoint16_16(substr($atom_structure['sample_description_table'][$i]['data'], 24, 4)); + $atom_structure['sample_description_table'][$i]['video_data_size'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 28, 4)); + $atom_structure['sample_description_table'][$i]['video_frame_count'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 32, 2)); + $atom_structure['sample_description_table'][$i]['video_encoder_name_len'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 34, 1)); + $atom_structure['sample_description_table'][$i]['video_encoder_name'] = substr($atom_structure['sample_description_table'][$i]['data'], 35, $atom_structure['sample_description_table'][$i]['video_encoder_name_len']); + $atom_structure['sample_description_table'][$i]['video_pixel_color_depth'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 66, 2)); + $atom_structure['sample_description_table'][$i]['video_color_table_id'] = getid3_lib::BigEndian2Int(substr($atom_structure['sample_description_table'][$i]['data'], 68, 2)); + + $atom_structure['sample_description_table'][$i]['video_pixel_color_type'] = (($atom_structure['sample_description_table'][$i]['video_pixel_color_depth'] > 32) ? 'grayscale' : 'color'); + $atom_structure['sample_description_table'][$i]['video_pixel_color_name'] = $this->QuicktimeColorNameLookup($atom_structure['sample_description_table'][$i]['video_pixel_color_depth']); + + if ($atom_structure['sample_description_table'][$i]['video_pixel_color_name'] != 'invalid') { + $info['quicktime']['video']['codec_fourcc'] = $atom_structure['sample_description_table'][$i]['data_format']; + $info['quicktime']['video']['codec_fourcc_lookup'] = $this->QuicktimeVideoCodecLookup($atom_structure['sample_description_table'][$i]['data_format']); + $info['quicktime']['video']['codec'] = (($atom_structure['sample_description_table'][$i]['video_encoder_name_len'] > 0) ? $atom_structure['sample_description_table'][$i]['video_encoder_name'] : $atom_structure['sample_description_table'][$i]['data_format']); + $info['quicktime']['video']['color_depth'] = $atom_structure['sample_description_table'][$i]['video_pixel_color_depth']; + $info['quicktime']['video']['color_depth_name'] = $atom_structure['sample_description_table'][$i]['video_pixel_color_name']; + + $info['video']['codec'] = $info['quicktime']['video']['codec']; + $info['video']['bits_per_sample'] = $info['quicktime']['video']['color_depth']; + } + $info['video']['lossless'] = false; + $info['video']['pixel_aspect_ratio'] = (float) 1; + break; + } + break; + } + switch (strtolower($atom_structure['sample_description_table'][$i]['data_format'])) { + case 'mp4a': + $info['audio']['dataformat'] = 'mp4'; + $info['quicktime']['audio']['codec'] = 'mp4'; + break; + + case '3ivx': + case '3iv1': + case '3iv2': + $info['video']['dataformat'] = '3ivx'; + break; + + case 'xvid': + $info['video']['dataformat'] = 'xvid'; + break; + + case 'mp4v': + $info['video']['dataformat'] = 'mpeg4'; + break; + + case 'divx': + case 'div1': + case 'div2': + case 'div3': + case 'div4': + case 'div5': + case 'div6': + $info['video']['dataformat'] = 'divx'; + break; + + default: + // do nothing + break; + } + unset($atom_structure['sample_description_table'][$i]['data']); + } + break; + + + case 'stts': // Sample Table Time-to-Sample atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $sttsEntriesDataOffset = 8; + //$FrameRateCalculatorArray = array(); + $frames_count = 0; + + $max_stts_entries_to_scan = ($info['php_memory_limit'] ? min(floor($this->getid3->memory_limit / 10000), $atom_structure['number_entries']) : $atom_structure['number_entries']); + if ($max_stts_entries_to_scan < $atom_structure['number_entries']) { + $this->warning('QuickTime atom "stts" has '.$atom_structure['number_entries'].' but only scanning the first '.$max_stts_entries_to_scan.' entries due to limited PHP memory available ('.floor($this->getid3->memory_limit / 1048576).'MB).'); + } + for ($i = 0; $i < $max_stts_entries_to_scan; $i++) { + $atom_structure['time_to_sample_table'][$i]['sample_count'] = getid3_lib::BigEndian2Int(substr($atom_data, $sttsEntriesDataOffset, 4)); + $sttsEntriesDataOffset += 4; + $atom_structure['time_to_sample_table'][$i]['sample_duration'] = getid3_lib::BigEndian2Int(substr($atom_data, $sttsEntriesDataOffset, 4)); + $sttsEntriesDataOffset += 4; + + $frames_count += $atom_structure['time_to_sample_table'][$i]['sample_count']; + + // THIS SECTION REPLACED WITH CODE IN "stbl" ATOM + //if (!empty($info['quicktime']['time_scale']) && ($atom_structure['time_to_sample_table'][$i]['sample_duration'] > 0)) { + // $stts_new_framerate = $info['quicktime']['time_scale'] / $atom_structure['time_to_sample_table'][$i]['sample_duration']; + // if ($stts_new_framerate <= 60) { + // // some atoms have durations of "1" giving a very large framerate, which probably is not right + // $info['video']['frame_rate'] = max($info['video']['frame_rate'], $stts_new_framerate); + // } + //} + // + //$FrameRateCalculatorArray[($info['quicktime']['time_scale'] / $atom_structure['time_to_sample_table'][$i]['sample_duration'])] += $atom_structure['time_to_sample_table'][$i]['sample_count']; + } + $info['quicktime']['stts_framecount'][] = $frames_count; + //$sttsFramesTotal = 0; + //$sttsSecondsTotal = 0; + //foreach ($FrameRateCalculatorArray as $frames_per_second => $frame_count) { + // if (($frames_per_second > 60) || ($frames_per_second < 1)) { + // // not video FPS information, probably audio information + // $sttsFramesTotal = 0; + // $sttsSecondsTotal = 0; + // break; + // } + // $sttsFramesTotal += $frame_count; + // $sttsSecondsTotal += $frame_count / $frames_per_second; + //} + //if (($sttsFramesTotal > 0) && ($sttsSecondsTotal > 0)) { + // if (($sttsFramesTotal / $sttsSecondsTotal) > $info['video']['frame_rate']) { + // $info['video']['frame_rate'] = $sttsFramesTotal / $sttsSecondsTotal; + // } + //} + break; + + + case 'stss': // Sample Table Sync Sample (key frames) atom + if ($ParseAllPossibleAtoms) { + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $stssEntriesDataOffset = 8; + for ($i = 0; $i < $atom_structure['number_entries']; $i++) { + $atom_structure['time_to_sample_table'][$i] = getid3_lib::BigEndian2Int(substr($atom_data, $stssEntriesDataOffset, 4)); + $stssEntriesDataOffset += 4; + } + } + break; + + + case 'stsc': // Sample Table Sample-to-Chunk atom + if ($ParseAllPossibleAtoms) { + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $stscEntriesDataOffset = 8; + for ($i = 0; $i < $atom_structure['number_entries']; $i++) { + $atom_structure['sample_to_chunk_table'][$i]['first_chunk'] = getid3_lib::BigEndian2Int(substr($atom_data, $stscEntriesDataOffset, 4)); + $stscEntriesDataOffset += 4; + $atom_structure['sample_to_chunk_table'][$i]['samples_per_chunk'] = getid3_lib::BigEndian2Int(substr($atom_data, $stscEntriesDataOffset, 4)); + $stscEntriesDataOffset += 4; + $atom_structure['sample_to_chunk_table'][$i]['sample_description'] = getid3_lib::BigEndian2Int(substr($atom_data, $stscEntriesDataOffset, 4)); + $stscEntriesDataOffset += 4; + } + } + break; + + + case 'stsz': // Sample Table SiZe atom + if ($ParseAllPossibleAtoms) { + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['sample_size'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 4)); + $stszEntriesDataOffset = 12; + if ($atom_structure['sample_size'] == 0) { + for ($i = 0; $i < $atom_structure['number_entries']; $i++) { + $atom_structure['sample_size_table'][$i] = getid3_lib::BigEndian2Int(substr($atom_data, $stszEntriesDataOffset, 4)); + $stszEntriesDataOffset += 4; + } + } + } + break; + + + case 'stco': // Sample Table Chunk Offset atom +// if (true) { + if ($ParseAllPossibleAtoms) { + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $stcoEntriesDataOffset = 8; + for ($i = 0; $i < $atom_structure['number_entries']; $i++) { + $atom_structure['chunk_offset_table'][$i] = getid3_lib::BigEndian2Int(substr($atom_data, $stcoEntriesDataOffset, 4)); + $stcoEntriesDataOffset += 4; + } + } + break; + + + case 'co64': // Chunk Offset 64-bit (version of "stco" that supports > 2GB files) + if ($ParseAllPossibleAtoms) { + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $stcoEntriesDataOffset = 8; + for ($i = 0; $i < $atom_structure['number_entries']; $i++) { + $atom_structure['chunk_offset_table'][$i] = getid3_lib::BigEndian2Int(substr($atom_data, $stcoEntriesDataOffset, 8)); + $stcoEntriesDataOffset += 8; + } + } + break; + + + case 'dref': // Data REFerence atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $drefDataOffset = 8; + for ($i = 0; $i < $atom_structure['number_entries']; $i++) { + $atom_structure['data_references'][$i]['size'] = getid3_lib::BigEndian2Int(substr($atom_data, $drefDataOffset, 4)); + $drefDataOffset += 4; + $atom_structure['data_references'][$i]['type'] = substr($atom_data, $drefDataOffset, 4); + $drefDataOffset += 4; + $atom_structure['data_references'][$i]['version'] = getid3_lib::BigEndian2Int(substr($atom_data, $drefDataOffset, 1)); + $drefDataOffset += 1; + $atom_structure['data_references'][$i]['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, $drefDataOffset, 3)); // hardcoded: 0x0000 + $drefDataOffset += 3; + $atom_structure['data_references'][$i]['data'] = substr($atom_data, $drefDataOffset, ($atom_structure['data_references'][$i]['size'] - 4 - 4 - 1 - 3)); + $drefDataOffset += ($atom_structure['data_references'][$i]['size'] - 4 - 4 - 1 - 3); + + $atom_structure['data_references'][$i]['flags']['self_reference'] = (bool) ($atom_structure['data_references'][$i]['flags_raw'] & 0x001); + } + break; + + + case 'gmin': // base Media INformation atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['graphics_mode'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); + $atom_structure['opcolor_red'] = getid3_lib::BigEndian2Int(substr($atom_data, 6, 2)); + $atom_structure['opcolor_green'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 2)); + $atom_structure['opcolor_blue'] = getid3_lib::BigEndian2Int(substr($atom_data, 10, 2)); + $atom_structure['balance'] = getid3_lib::BigEndian2Int(substr($atom_data, 12, 2)); + $atom_structure['reserved'] = getid3_lib::BigEndian2Int(substr($atom_data, 14, 2)); + break; + + + case 'smhd': // Sound Media information HeaDer atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['balance'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); + $atom_structure['reserved'] = getid3_lib::BigEndian2Int(substr($atom_data, 6, 2)); + break; + + + case 'vmhd': // Video Media information HeaDer atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); + $atom_structure['graphics_mode'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); + $atom_structure['opcolor_red'] = getid3_lib::BigEndian2Int(substr($atom_data, 6, 2)); + $atom_structure['opcolor_green'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 2)); + $atom_structure['opcolor_blue'] = getid3_lib::BigEndian2Int(substr($atom_data, 10, 2)); + + $atom_structure['flags']['no_lean_ahead'] = (bool) ($atom_structure['flags_raw'] & 0x001); + break; + + + case 'hdlr': // HanDLeR reference atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['component_type'] = substr($atom_data, 4, 4); + $atom_structure['component_subtype'] = substr($atom_data, 8, 4); + $atom_structure['component_manufacturer'] = substr($atom_data, 12, 4); + $atom_structure['component_flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 16, 4)); + $atom_structure['component_flags_mask'] = getid3_lib::BigEndian2Int(substr($atom_data, 20, 4)); + $atom_structure['component_name'] = $this->MaybePascal2String(substr($atom_data, 24)); + + if (($atom_structure['component_subtype'] == 'STpn') && ($atom_structure['component_manufacturer'] == 'zzzz')) { + $info['video']['dataformat'] = 'quicktimevr'; + } + break; + + + case 'mdhd': // MeDia HeaDer atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['creation_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $atom_structure['modify_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 4)); + $atom_structure['time_scale'] = getid3_lib::BigEndian2Int(substr($atom_data, 12, 4)); + $atom_structure['duration'] = getid3_lib::BigEndian2Int(substr($atom_data, 16, 4)); + $atom_structure['language_id'] = getid3_lib::BigEndian2Int(substr($atom_data, 20, 2)); + $atom_structure['quality'] = getid3_lib::BigEndian2Int(substr($atom_data, 22, 2)); + + if ($atom_structure['time_scale'] == 0) { + $this->error('Corrupt Quicktime file: mdhd.time_scale == zero'); + return false; + } + $info['quicktime']['time_scale'] = ((isset($info['quicktime']['time_scale']) && ($info['quicktime']['time_scale'] < 1000)) ? max($info['quicktime']['time_scale'], $atom_structure['time_scale']) : $atom_structure['time_scale']); + + $atom_structure['creation_time_unix'] = getid3_lib::DateMac2Unix($atom_structure['creation_time']); + $atom_structure['modify_time_unix'] = getid3_lib::DateMac2Unix($atom_structure['modify_time']); + $atom_structure['playtime_seconds'] = $atom_structure['duration'] / $atom_structure['time_scale']; + $atom_structure['language'] = $this->QuicktimeLanguageLookup($atom_structure['language_id']); + if (empty($info['comments']['language']) || (!in_array($atom_structure['language'], $info['comments']['language']))) { + $info['comments']['language'][] = $atom_structure['language']; + } + $info['quicktime']['timestamps_unix']['create'][$atom_structure['hierarchy']] = $atom_structure['creation_time_unix']; + $info['quicktime']['timestamps_unix']['modify'][$atom_structure['hierarchy']] = $atom_structure['modify_time_unix']; + break; + + + case 'pnot': // Preview atom + $atom_structure['modification_date'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 4)); // "standard Macintosh format" + $atom_structure['version_number'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); // hardcoded: 0x00 + $atom_structure['atom_type'] = substr($atom_data, 6, 4); // usually: 'PICT' + $atom_structure['atom_index'] = getid3_lib::BigEndian2Int(substr($atom_data, 10, 2)); // usually: 0x01 + + $atom_structure['modification_date_unix'] = getid3_lib::DateMac2Unix($atom_structure['modification_date']); + $info['quicktime']['timestamps_unix']['modify'][$atom_structure['hierarchy']] = $atom_structure['modification_date_unix']; + break; + + + case 'crgn': // Clipping ReGioN atom + $atom_structure['region_size'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 2)); // The Region size, Region boundary box, + $atom_structure['boundary_box'] = getid3_lib::BigEndian2Int(substr($atom_data, 2, 8)); // and Clipping region data fields + $atom_structure['clipping_data'] = substr($atom_data, 10); // constitute a QuickDraw region. + break; + + + case 'load': // track LOAD settings atom + $atom_structure['preload_start_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 4)); + $atom_structure['preload_duration'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $atom_structure['preload_flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 4)); + $atom_structure['default_hints_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 12, 4)); + + $atom_structure['default_hints']['double_buffer'] = (bool) ($atom_structure['default_hints_raw'] & 0x0020); + $atom_structure['default_hints']['high_quality'] = (bool) ($atom_structure['default_hints_raw'] & 0x0100); + break; + + + case 'tmcd': // TiMe CoDe atom + case 'chap': // CHAPter list atom + case 'sync': // SYNChronization atom + case 'scpt': // tranSCriPT atom + case 'ssrc': // non-primary SouRCe atom + for ($i = 0; $i < strlen($atom_data); $i += 4) { + @$atom_structure['track_id'][] = getid3_lib::BigEndian2Int(substr($atom_data, $i, 4)); + } + break; + + + case 'elst': // Edit LiST atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['number_entries'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + for ($i = 0; $i < $atom_structure['number_entries']; $i++ ) { + $atom_structure['edit_list'][$i]['track_duration'] = getid3_lib::BigEndian2Int(substr($atom_data, 8 + ($i * 12) + 0, 4)); + $atom_structure['edit_list'][$i]['media_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 8 + ($i * 12) + 4, 4)); + $atom_structure['edit_list'][$i]['media_rate'] = getid3_lib::FixedPoint16_16(substr($atom_data, 8 + ($i * 12) + 8, 4)); + } + break; + + + case 'kmat': // compressed MATte atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); // hardcoded: 0x0000 + $atom_structure['matte_data_raw'] = substr($atom_data, 4); + break; + + + case 'ctab': // Color TABle atom + $atom_structure['color_table_seed'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 4)); // hardcoded: 0x00000000 + $atom_structure['color_table_flags'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 2)); // hardcoded: 0x8000 + $atom_structure['color_table_size'] = getid3_lib::BigEndian2Int(substr($atom_data, 6, 2)) + 1; + for ($colortableentry = 0; $colortableentry < $atom_structure['color_table_size']; $colortableentry++) { + $atom_structure['color_table'][$colortableentry]['alpha'] = getid3_lib::BigEndian2Int(substr($atom_data, 8 + ($colortableentry * 8) + 0, 2)); + $atom_structure['color_table'][$colortableentry]['red'] = getid3_lib::BigEndian2Int(substr($atom_data, 8 + ($colortableentry * 8) + 2, 2)); + $atom_structure['color_table'][$colortableentry]['green'] = getid3_lib::BigEndian2Int(substr($atom_data, 8 + ($colortableentry * 8) + 4, 2)); + $atom_structure['color_table'][$colortableentry]['blue'] = getid3_lib::BigEndian2Int(substr($atom_data, 8 + ($colortableentry * 8) + 6, 2)); + } + break; + + + case 'mvhd': // MoVie HeaDer atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); + $atom_structure['creation_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $atom_structure['modify_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 4)); + $atom_structure['time_scale'] = getid3_lib::BigEndian2Int(substr($atom_data, 12, 4)); + $atom_structure['duration'] = getid3_lib::BigEndian2Int(substr($atom_data, 16, 4)); + $atom_structure['preferred_rate'] = getid3_lib::FixedPoint16_16(substr($atom_data, 20, 4)); + $atom_structure['preferred_volume'] = getid3_lib::FixedPoint8_8(substr($atom_data, 24, 2)); + $atom_structure['reserved'] = substr($atom_data, 26, 10); + $atom_structure['matrix_a'] = getid3_lib::FixedPoint16_16(substr($atom_data, 36, 4)); + $atom_structure['matrix_b'] = getid3_lib::FixedPoint16_16(substr($atom_data, 40, 4)); + $atom_structure['matrix_u'] = getid3_lib::FixedPoint2_30(substr($atom_data, 44, 4)); + $atom_structure['matrix_c'] = getid3_lib::FixedPoint16_16(substr($atom_data, 48, 4)); + $atom_structure['matrix_d'] = getid3_lib::FixedPoint16_16(substr($atom_data, 52, 4)); + $atom_structure['matrix_v'] = getid3_lib::FixedPoint2_30(substr($atom_data, 56, 4)); + $atom_structure['matrix_x'] = getid3_lib::FixedPoint16_16(substr($atom_data, 60, 4)); + $atom_structure['matrix_y'] = getid3_lib::FixedPoint16_16(substr($atom_data, 64, 4)); + $atom_structure['matrix_w'] = getid3_lib::FixedPoint2_30(substr($atom_data, 68, 4)); + $atom_structure['preview_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 72, 4)); + $atom_structure['preview_duration'] = getid3_lib::BigEndian2Int(substr($atom_data, 76, 4)); + $atom_structure['poster_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 80, 4)); + $atom_structure['selection_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 84, 4)); + $atom_structure['selection_duration'] = getid3_lib::BigEndian2Int(substr($atom_data, 88, 4)); + $atom_structure['current_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 92, 4)); + $atom_structure['next_track_id'] = getid3_lib::BigEndian2Int(substr($atom_data, 96, 4)); + + if ($atom_structure['time_scale'] == 0) { + $this->error('Corrupt Quicktime file: mvhd.time_scale == zero'); + return false; + } + $atom_structure['creation_time_unix'] = getid3_lib::DateMac2Unix($atom_structure['creation_time']); + $atom_structure['modify_time_unix'] = getid3_lib::DateMac2Unix($atom_structure['modify_time']); + $info['quicktime']['timestamps_unix']['create'][$atom_structure['hierarchy']] = $atom_structure['creation_time_unix']; + $info['quicktime']['timestamps_unix']['modify'][$atom_structure['hierarchy']] = $atom_structure['modify_time_unix']; + $info['quicktime']['time_scale'] = ((isset($info['quicktime']['time_scale']) && ($info['quicktime']['time_scale'] < 1000)) ? max($info['quicktime']['time_scale'], $atom_structure['time_scale']) : $atom_structure['time_scale']); + $info['quicktime']['display_scale'] = $atom_structure['matrix_a']; + $info['playtime_seconds'] = $atom_structure['duration'] / $atom_structure['time_scale']; + break; + + + case 'tkhd': // TracK HeaDer atom + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); + $atom_structure['creation_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $atom_structure['modify_time'] = getid3_lib::BigEndian2Int(substr($atom_data, 8, 4)); + $atom_structure['trackid'] = getid3_lib::BigEndian2Int(substr($atom_data, 12, 4)); + $atom_structure['reserved1'] = getid3_lib::BigEndian2Int(substr($atom_data, 16, 4)); + $atom_structure['duration'] = getid3_lib::BigEndian2Int(substr($atom_data, 20, 4)); + $atom_structure['reserved2'] = getid3_lib::BigEndian2Int(substr($atom_data, 24, 8)); + $atom_structure['layer'] = getid3_lib::BigEndian2Int(substr($atom_data, 32, 2)); + $atom_structure['alternate_group'] = getid3_lib::BigEndian2Int(substr($atom_data, 34, 2)); + $atom_structure['volume'] = getid3_lib::FixedPoint8_8(substr($atom_data, 36, 2)); + $atom_structure['reserved3'] = getid3_lib::BigEndian2Int(substr($atom_data, 38, 2)); + // http://developer.apple.com/library/mac/#documentation/QuickTime/RM/MovieBasics/MTEditing/K-Chapter/11MatrixFunctions.html + // http://developer.apple.com/library/mac/#documentation/QuickTime/qtff/QTFFChap4/qtff4.html#//apple_ref/doc/uid/TP40000939-CH206-18737 + $atom_structure['matrix_a'] = getid3_lib::FixedPoint16_16(substr($atom_data, 40, 4)); + $atom_structure['matrix_b'] = getid3_lib::FixedPoint16_16(substr($atom_data, 44, 4)); + $atom_structure['matrix_u'] = getid3_lib::FixedPoint2_30(substr($atom_data, 48, 4)); + $atom_structure['matrix_c'] = getid3_lib::FixedPoint16_16(substr($atom_data, 52, 4)); + $atom_structure['matrix_d'] = getid3_lib::FixedPoint16_16(substr($atom_data, 56, 4)); + $atom_structure['matrix_v'] = getid3_lib::FixedPoint2_30(substr($atom_data, 60, 4)); + $atom_structure['matrix_x'] = getid3_lib::FixedPoint16_16(substr($atom_data, 64, 4)); + $atom_structure['matrix_y'] = getid3_lib::FixedPoint16_16(substr($atom_data, 68, 4)); + $atom_structure['matrix_w'] = getid3_lib::FixedPoint2_30(substr($atom_data, 72, 4)); + $atom_structure['width'] = getid3_lib::FixedPoint16_16(substr($atom_data, 76, 4)); + $atom_structure['height'] = getid3_lib::FixedPoint16_16(substr($atom_data, 80, 4)); + $atom_structure['flags']['enabled'] = (bool) ($atom_structure['flags_raw'] & 0x0001); + $atom_structure['flags']['in_movie'] = (bool) ($atom_structure['flags_raw'] & 0x0002); + $atom_structure['flags']['in_preview'] = (bool) ($atom_structure['flags_raw'] & 0x0004); + $atom_structure['flags']['in_poster'] = (bool) ($atom_structure['flags_raw'] & 0x0008); + $atom_structure['creation_time_unix'] = getid3_lib::DateMac2Unix($atom_structure['creation_time']); + $atom_structure['modify_time_unix'] = getid3_lib::DateMac2Unix($atom_structure['modify_time']); + $info['quicktime']['timestamps_unix']['create'][$atom_structure['hierarchy']] = $atom_structure['creation_time_unix']; + $info['quicktime']['timestamps_unix']['modify'][$atom_structure['hierarchy']] = $atom_structure['modify_time_unix']; + + // https://www.getid3.org/phpBB3/viewtopic.php?t=1908 + // attempt to compute rotation from matrix values + // 2017-Dec-28: uncertain if 90/270 are correctly oriented; values returned by FixedPoint16_16 should perhaps be -1 instead of 65535(?) + $matrixRotation = 0; + switch ($atom_structure['matrix_a'].':'.$atom_structure['matrix_b'].':'.$atom_structure['matrix_c'].':'.$atom_structure['matrix_d']) { + case '1:0:0:1': $matrixRotation = 0; break; + case '0:1:65535:0': $matrixRotation = 90; break; + case '65535:0:0:65535': $matrixRotation = 180; break; + case '0:65535:1:0': $matrixRotation = 270; break; + default: break; + } + + // https://www.getid3.org/phpBB3/viewtopic.php?t=2468 + // The rotation matrix can appear in the Quicktime file multiple times, at least once for each track, + // and it's possible that only the video track (or, in theory, one of the video tracks) is flagged as + // rotated while the other tracks (e.g. audio) is tagged as rotation=0 (behavior noted on iPhone 8 Plus) + // The correct solution would be to check if the TrackID associated with the rotation matrix is indeed + // a video track (or the main video track) and only set the rotation then, but since information about + // what track is what is not trivially there to be examined, the lazy solution is to set the rotation + // if it is found to be nonzero, on the assumption that tracks that don't need it will have rotation set + // to zero (and be effectively ignored) and the video track will have rotation set correctly, which will + // either be zero and automatically correct, or nonzero and be set correctly. + if (!isset($info['video']['rotate']) || (($info['video']['rotate'] == 0) && ($matrixRotation > 0))) { + $info['quicktime']['video']['rotate'] = $info['video']['rotate'] = $matrixRotation; + } + + if ($atom_structure['flags']['enabled'] == 1) { + if (!isset($info['video']['resolution_x']) || !isset($info['video']['resolution_y'])) { + $info['video']['resolution_x'] = $atom_structure['width']; + $info['video']['resolution_y'] = $atom_structure['height']; + } + $info['video']['resolution_x'] = max($info['video']['resolution_x'], $atom_structure['width']); + $info['video']['resolution_y'] = max($info['video']['resolution_y'], $atom_structure['height']); + $info['quicktime']['video']['resolution_x'] = $info['video']['resolution_x']; + $info['quicktime']['video']['resolution_y'] = $info['video']['resolution_y']; + } else { + // see: https://www.getid3.org/phpBB3/viewtopic.php?t=1295 + //if (isset($info['video']['resolution_x'])) { unset($info['video']['resolution_x']); } + //if (isset($info['video']['resolution_y'])) { unset($info['video']['resolution_y']); } + //if (isset($info['quicktime']['video'])) { unset($info['quicktime']['video']); } + } + break; + + + case 'iods': // Initial Object DeScriptor atom + // http://www.koders.com/c/fid1FAB3E762903DC482D8A246D4A4BF9F28E049594.aspx?s=windows.h + // http://libquicktime.sourcearchive.com/documentation/1.0.2plus-pdebian/iods_8c-source.html + $offset = 0; + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 3)); + $offset += 3; + $atom_structure['mp4_iod_tag'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + $atom_structure['length'] = $this->quicktime_read_mp4_descr_length($atom_data, $offset); + //$offset already adjusted by quicktime_read_mp4_descr_length() + $atom_structure['object_descriptor_id'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 2)); + $offset += 2; + $atom_structure['od_profile_level'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + $atom_structure['scene_profile_level'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + $atom_structure['audio_profile_id'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + $atom_structure['video_profile_id'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + $atom_structure['graphics_profile_level'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + + $atom_structure['num_iods_tracks'] = ($atom_structure['length'] - 7) / 6; // 6 bytes would only be right if all tracks use 1-byte length fields + for ($i = 0; $i < $atom_structure['num_iods_tracks']; $i++) { + $atom_structure['track'][$i]['ES_ID_IncTag'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 1)); + $offset += 1; + $atom_structure['track'][$i]['length'] = $this->quicktime_read_mp4_descr_length($atom_data, $offset); + //$offset already adjusted by quicktime_read_mp4_descr_length() + $atom_structure['track'][$i]['track_id'] = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 4)); + $offset += 4; + } + + $atom_structure['audio_profile_name'] = $this->QuicktimeIODSaudioProfileName($atom_structure['audio_profile_id']); + $atom_structure['video_profile_name'] = $this->QuicktimeIODSvideoProfileName($atom_structure['video_profile_id']); + break; + + case 'ftyp': // FileTYPe (?) atom (for MP4 it seems) + $atom_structure['signature'] = substr($atom_data, 0, 4); + $atom_structure['unknown_1'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $atom_structure['fourcc'] = substr($atom_data, 8, 4); + break; + + case 'mdat': // Media DATa atom + // 'mdat' contains the actual data for the audio/video, possibly also subtitles + + /* due to lack of known documentation, this is a kludge implementation. If you know of documentation on how mdat is properly structed, please send it to info@getid3.org */ + + // first, skip any 'wide' padding, and second 'mdat' header (with specified size of zero?) + $mdat_offset = 0; + while (true) { + if (substr($atom_data, $mdat_offset, 8) == "\x00\x00\x00\x08".'wide') { + $mdat_offset += 8; + } elseif (substr($atom_data, $mdat_offset, 8) == "\x00\x00\x00\x00".'mdat') { + $mdat_offset += 8; + } else { + break; + } + } + if (substr($atom_data, $mdat_offset, 4) == 'GPRO') { + $GOPRO_chunk_length = getid3_lib::LittleEndian2Int(substr($atom_data, $mdat_offset + 4, 4)); + $GOPRO_offset = 8; + $atom_structure['GPRO']['raw'] = substr($atom_data, $mdat_offset + 8, $GOPRO_chunk_length - 8); + $atom_structure['GPRO']['firmware'] = substr($atom_structure['GPRO']['raw'], 0, 15); + $atom_structure['GPRO']['unknown1'] = substr($atom_structure['GPRO']['raw'], 15, 16); + $atom_structure['GPRO']['unknown2'] = substr($atom_structure['GPRO']['raw'], 31, 32); + $atom_structure['GPRO']['unknown3'] = substr($atom_structure['GPRO']['raw'], 63, 16); + $atom_structure['GPRO']['camera'] = substr($atom_structure['GPRO']['raw'], 79, 32); + $info['quicktime']['camera']['model'] = rtrim($atom_structure['GPRO']['camera'], "\x00"); + } + + // check to see if it looks like chapter titles, in the form of unterminated strings with a leading 16-bit size field + while (($mdat_offset < (strlen($atom_data) - 8)) + && ($chapter_string_length = getid3_lib::BigEndian2Int(substr($atom_data, $mdat_offset, 2))) + && ($chapter_string_length < 1000) + && ($chapter_string_length <= (strlen($atom_data) - $mdat_offset - 2)) + && preg_match('#^([\x00-\xFF]{2})([\x20-\xFF]+)$#', substr($atom_data, $mdat_offset, $chapter_string_length + 2), $chapter_matches)) { + list($dummy, $chapter_string_length_hex, $chapter_string) = $chapter_matches; + $mdat_offset += (2 + $chapter_string_length); + @$info['quicktime']['comments']['chapters'][] = $chapter_string; + + // "encd" atom specifies encoding. In theory could be anything, almost always UTF-8, but may be UTF-16 with BOM (not currently handled) + if (substr($atom_data, $mdat_offset, 12) == "\x00\x00\x00\x0C\x65\x6E\x63\x64\x00\x00\x01\x00") { // UTF-8 + $mdat_offset += 12; + } + } + + if (($atomsize > 8) && (!isset($info['avdataend_tmp']) || ($info['quicktime'][$atomname]['size'] > ($info['avdataend_tmp'] - $info['avdataoffset'])))) { + + $info['avdataoffset'] = $atom_structure['offset'] + 8; // $info['quicktime'][$atomname]['offset'] + 8; + $OldAVDataEnd = $info['avdataend']; + $info['avdataend'] = $atom_structure['offset'] + $atom_structure['size']; // $info['quicktime'][$atomname]['offset'] + $info['quicktime'][$atomname]['size']; + + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, $this->getid3->info['filesize'], $this->getid3->fp); + $getid3_temp->info['avdataoffset'] = $info['avdataoffset']; + $getid3_temp->info['avdataend'] = $info['avdataend']; + $getid3_mp3 = new getid3_mp3($getid3_temp); + if ($getid3_mp3->MPEGaudioHeaderValid($getid3_mp3->MPEGaudioHeaderDecode($this->fread(4)))) { + $getid3_mp3->getOnlyMPEGaudioInfo($getid3_temp->info['avdataoffset'], false); + if (!empty($getid3_temp->info['warning'])) { + foreach ($getid3_temp->info['warning'] as $value) { + $this->warning($value); + } + } + if (!empty($getid3_temp->info['mpeg'])) { + $info['mpeg'] = $getid3_temp->info['mpeg']; + if (isset($info['mpeg']['audio'])) { + $info['audio']['dataformat'] = 'mp3'; + $info['audio']['codec'] = (!empty($info['mpeg']['audio']['encoder']) ? $info['mpeg']['audio']['encoder'] : (!empty($info['mpeg']['audio']['codec']) ? $info['mpeg']['audio']['codec'] : (!empty($info['mpeg']['audio']['LAME']) ? 'LAME' :'mp3'))); + $info['audio']['sample_rate'] = $info['mpeg']['audio']['sample_rate']; + $info['audio']['channels'] = $info['mpeg']['audio']['channels']; + $info['audio']['bitrate'] = $info['mpeg']['audio']['bitrate']; + $info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']); + $info['bitrate'] = $info['audio']['bitrate']; + } + } + } + unset($getid3_mp3, $getid3_temp); + $info['avdataend'] = $OldAVDataEnd; + unset($OldAVDataEnd); + + } + + unset($mdat_offset, $chapter_string_length, $chapter_matches); + break; + + case 'free': // FREE space atom + case 'skip': // SKIP atom + case 'wide': // 64-bit expansion placeholder atom + // 'free', 'skip' and 'wide' are just padding, contains no useful data at all + + // When writing QuickTime files, it is sometimes necessary to update an atom's size. + // It is impossible to update a 32-bit atom to a 64-bit atom since the 32-bit atom + // is only 8 bytes in size, and the 64-bit atom requires 16 bytes. Therefore, QuickTime + // puts an 8-byte placeholder atom before any atoms it may have to update the size of. + // In this way, if the atom needs to be converted from a 32-bit to a 64-bit atom, the + // placeholder atom can be overwritten to obtain the necessary 8 extra bytes. + // The placeholder atom has a type of kWideAtomPlaceholderType ( 'wide' ). + break; + + + case 'nsav': // NoSAVe atom + // http://developer.apple.com/technotes/tn/tn2038.html + $atom_structure['data'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 4)); + break; + + case 'ctyp': // Controller TYPe atom (seen on QTVR) + // http://homepages.slingshot.co.nz/~helmboy/quicktime/formats/qtm-layout.txt + // some controller names are: + // 0x00 + 'std' for linear movie + // 'none' for no controls + $atom_structure['ctyp'] = substr($atom_data, 0, 4); + $info['quicktime']['controller'] = $atom_structure['ctyp']; + switch ($atom_structure['ctyp']) { + case 'qtvr': + $info['video']['dataformat'] = 'quicktimevr'; + break; + } + break; + + case 'pano': // PANOrama track (seen on QTVR) + $atom_structure['pano'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 4)); + break; + + case 'hint': // HINT track + case 'hinf': // + case 'hinv': // + case 'hnti': // + $info['quicktime']['hinting'] = true; + break; + + case 'imgt': // IMaGe Track reference (kQTVRImageTrackRefType) (seen on QTVR) + for ($i = 0; $i < ($atom_structure['size'] - 8); $i += 4) { + $atom_structure['imgt'][] = getid3_lib::BigEndian2Int(substr($atom_data, $i, 4)); + } + break; + + + // Observed-but-not-handled atom types are just listed here to prevent warnings being generated + case 'FXTC': // Something to do with Adobe After Effects (?) + case 'PrmA': + case 'code': + case 'FIEL': // this is NOT "fiel" (Field Ordering) as describe here: http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/chapter_4_section_2.html + case 'tapt': // TrackApertureModeDimensionsAID - http://developer.apple.com/documentation/QuickTime/Reference/QT7-1_Update_Reference/Constants/Constants.html + // tapt seems to be used to compute the video size [https://www.getid3.org/phpBB3/viewtopic.php?t=838] + // * http://lists.apple.com/archives/quicktime-api/2006/Aug/msg00014.html + // * http://handbrake.fr/irclogs/handbrake-dev/handbrake-dev20080128_pg2.html + case 'ctts':// STCompositionOffsetAID - http://developer.apple.com/documentation/QuickTime/Reference/QTRef_Constants/Reference/reference.html + case 'cslg':// STCompositionShiftLeastGreatestAID - http://developer.apple.com/documentation/QuickTime/Reference/QTRef_Constants/Reference/reference.html + case 'sdtp':// STSampleDependencyAID - http://developer.apple.com/documentation/QuickTime/Reference/QTRef_Constants/Reference/reference.html + case 'stps':// STPartialSyncSampleAID - http://developer.apple.com/documentation/QuickTime/Reference/QTRef_Constants/Reference/reference.html + //$atom_structure['data'] = $atom_data; + break; + + case "\xA9".'xyz': // GPS latitude+longitude+altitude + $atom_structure['data'] = $atom_data; + if (preg_match('#([\\+\\-][0-9\\.]+)([\\+\\-][0-9\\.]+)([\\+\\-][0-9\\.]+)?/$#i', $atom_data, $matches)) { + @list($all, $latitude, $longitude, $altitude) = $matches; + $info['quicktime']['comments']['gps_latitude'][] = floatval($latitude); + $info['quicktime']['comments']['gps_longitude'][] = floatval($longitude); + if (!empty($altitude)) { + $info['quicktime']['comments']['gps_altitude'][] = floatval($altitude); + } + } else { + $this->warning('QuickTime atom "©xyz" data does not match expected data pattern at offset '.$baseoffset.'. Please report as getID3() bug.'); + } + break; + + case 'NCDT': + // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html + // Nikon-specific QuickTime tags found in the NCDT atom of MOV videos from some Nikon cameras such as the Coolpix S8000 and D5100 + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 4, $atomHierarchy, $ParseAllPossibleAtoms); + break; + case 'NCTH': // Nikon Camera THumbnail image + case 'NCVW': // Nikon Camera preVieW image + // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html + if (preg_match('/^\xFF\xD8\xFF/', $atom_data)) { + $atom_structure['data'] = $atom_data; + $atom_structure['image_mime'] = 'image/jpeg'; + $atom_structure['description'] = (($atomname == 'NCTH') ? 'Nikon Camera Thumbnail Image' : (($atomname == 'NCVW') ? 'Nikon Camera Preview Image' : 'Nikon preview image')); + $info['quicktime']['comments']['picture'][] = array('image_mime'=>$atom_structure['image_mime'], 'data'=>$atom_data, 'description'=>$atom_structure['description']); + } + break; + case 'NCTG': // Nikon - http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html#NCTG + $atom_structure['data'] = $this->QuicktimeParseNikonNCTG($atom_data); + break; + case 'NCHD': // Nikon:MakerNoteVersion - http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html + case 'NCDB': // Nikon - http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html + case 'CNCV': // Canon:CompressorVersion - http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Canon.html + $atom_structure['data'] = $atom_data; + break; + + case "\x00\x00\x00\x00": + // some kind of metacontainer, may contain a big data dump such as: + // mdta keys \005 mdtacom.apple.quicktime.make (mdtacom.apple.quicktime.creationdate ,mdtacom.apple.quicktime.location.ISO6709 $mdtacom.apple.quicktime.software !mdtacom.apple.quicktime.model ilst \01D \001 \015data \001DE\010Apple 0 \002 (data \001DE\0102011-05-11T17:54:04+0200 2 \003 *data \001DE\010+52.4936+013.3897+040.247/ \01D \004 \015data \001DE\0104.3.1 \005 \018data \001DE\010iPhone 4 + // http://www.geocities.com/xhelmboyx/quicktime/formats/qti-layout.txt + + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom(substr($atom_data, 4), $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms); + //$atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms); + break; + + case 'meta': // METAdata atom + // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html + + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms); + break; + + case 'data': // metaDATA atom + static $metaDATAkey = 1; // real ugly, but so is the QuickTime structure that stores keys and values in different multinested locations that are hard to relate to each other + // seems to be 2 bytes language code (ASCII), 2 bytes unknown (set to 0x10B5 in sample I have), remainder is useful data + $atom_structure['language'] = substr($atom_data, 4 + 0, 2); + $atom_structure['unknown'] = getid3_lib::BigEndian2Int(substr($atom_data, 4 + 2, 2)); + $atom_structure['data'] = substr($atom_data, 4 + 4); + $atom_structure['key_name'] = @$info['quicktime']['temp_meta_key_names'][$metaDATAkey++]; + + if ($atom_structure['key_name'] && $atom_structure['data']) { + @$info['quicktime']['comments'][str_replace('com.apple.quicktime.', '', $atom_structure['key_name'])][] = $atom_structure['data']; + } + break; + + case 'keys': // KEYS that may be present in the metadata atom. + // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html#//apple_ref/doc/uid/TP40000939-CH1-SW21 + // The metadata item keys atom holds a list of the metadata keys that may be present in the metadata atom. + // This list is indexed starting with 1; 0 is a reserved index value. The metadata item keys atom is a full atom with an atom type of "keys". + $atom_structure['version'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 1)); + $atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($atom_data, 1, 3)); + $atom_structure['entry_count'] = getid3_lib::BigEndian2Int(substr($atom_data, 4, 4)); + $keys_atom_offset = 8; + for ($i = 1; $i <= $atom_structure['entry_count']; $i++) { + $atom_structure['keys'][$i]['key_size'] = getid3_lib::BigEndian2Int(substr($atom_data, $keys_atom_offset + 0, 4)); + $atom_structure['keys'][$i]['key_namespace'] = substr($atom_data, $keys_atom_offset + 4, 4); + $atom_structure['keys'][$i]['key_value'] = substr($atom_data, $keys_atom_offset + 8, $atom_structure['keys'][$i]['key_size'] - 8); + $keys_atom_offset += $atom_structure['keys'][$i]['key_size']; // key_size includes the 4+4 bytes for key_size and key_namespace + + $info['quicktime']['temp_meta_key_names'][$i] = $atom_structure['keys'][$i]['key_value']; + } + break; + + case 'uuid': // user-defined atom often seen containing XML data, also used for potentially many other purposes, only a few specifically handled by getID3 (e.g. 360fly spatial data) + //Get the UUID ID in first 16 bytes + $uuid_bytes_read = unpack('H8time_low/H4time_mid/H4time_hi/H4clock_seq_hi/H12clock_seq_low', substr($atom_data, 0, 16)); + $atom_structure['uuid_field_id'] = implode('-', $uuid_bytes_read); + + switch ($atom_structure['uuid_field_id']) { // http://fileformats.archiveteam.org/wiki/Boxes/atoms_format#UUID_boxes + + case '0537cdab-9d0c-4431-a72a-fa561f2a113e': // Exif - http://fileformats.archiveteam.org/wiki/Exif + case '2c4c0100-8504-40b9-a03e-562148d6dfeb': // Photoshop Image Resources - http://fileformats.archiveteam.org/wiki/Photoshop_Image_Resources + case '33c7a4d2-b81d-4723-a0ba-f1a3e097ad38': // IPTC-IIM - http://fileformats.archiveteam.org/wiki/IPTC-IIM + case '8974dbce-7be7-4c51-84f9-7148f9882554': // PIFF Track Encryption Box - http://fileformats.archiveteam.org/wiki/Protected_Interoperable_File_Format + case '96a9f1f1-dc98-402d-a7ae-d68e34451809': // GeoJP2 World File Box - http://fileformats.archiveteam.org/wiki/GeoJP2 + case 'a2394f52-5a9b-4f14-a244-6c427c648df4': // PIFF Sample Encryption Box - http://fileformats.archiveteam.org/wiki/Protected_Interoperable_File_Format + case 'b14bf8bd-083d-4b43-a5ae-8cd7d5a6ce03': // GeoJP2 GeoTIFF Box - http://fileformats.archiveteam.org/wiki/GeoJP2 + case 'd08a4f18-10f3-4a82-b6c8-32d8aba183d3': // PIFF Protection System Specific Header Box - http://fileformats.archiveteam.org/wiki/Protected_Interoperable_File_Format + $this->warning('Unhandled (but recognized) "uuid" atom identified by "'.$atom_structure['uuid_field_id'].'" at offset '.$atom_structure['offset'].' ('.strlen($atom_data).' bytes)'); + break; + + case 'be7acfcb-97a9-42e8-9c71-999491e3afac': // XMP data (in XML format) + $atom_structure['xml'] = substr($atom_data, 16, strlen($atom_data) - 16 - 8); // 16 bytes for UUID, 8 bytes header(?) + break; + + case 'efe1589a-bb77-49ef-8095-27759eb1dc6f': // 360fly data + /* 360fly code in this block by Paul Lewis 2019-Oct-31 */ + /* Sensor Timestamps need to be calculated using the recordings base time at ['quicktime']['moov']['subatoms'][0]['creation_time_unix']. */ + $atom_structure['title'] = '360Fly Sensor Data'; + + //Get the UUID HEADER data + $uuid_bytes_read = unpack('vheader_size/vheader_version/vtimescale/vhardware_version/x/x/x/x/x/x/x/x/x/x/x/x/x/x/x/x/', substr($atom_data, 16, 32)); + $atom_structure['uuid_header'] = $uuid_bytes_read; + + $start_byte = 48; + $atom_SENSOR_data = substr($atom_data, $start_byte); + $atom_structure['sensor_data']['data_type'] = array( + 'fusion_count' => 0, // ID 250 + 'fusion_data' => array(), + 'accel_count' => 0, // ID 1 + 'accel_data' => array(), + 'gyro_count' => 0, // ID 2 + 'gyro_data' => array(), + 'magno_count' => 0, // ID 3 + 'magno_data' => array(), + 'gps_count' => 0, // ID 5 + 'gps_data' => array(), + 'rotation_count' => 0, // ID 6 + 'rotation_data' => array(), + 'unknown_count' => 0, // ID ?? + 'unknown_data' => array(), + 'debug_list' => '', // Used to debug variables stored as comma delimited strings + ); + $debug_structure['debug_items'] = array(); + // Can start loop here to decode all sensor data in 32 Byte chunks: + foreach (str_split($atom_SENSOR_data, 32) as $sensor_key => $sensor_data) { + // This gets me a data_type code to work out what data is in the next 31 bytes. + $sensor_data_type = substr($sensor_data, 0, 1); + $sensor_data_content = substr($sensor_data, 1); + $uuid_bytes_read = unpack('C*', $sensor_data_type); + $sensor_data_array = array(); + switch ($uuid_bytes_read[1]) { + case 250: + $atom_structure['sensor_data']['data_type']['fusion_count']++; + $uuid_bytes_read = unpack('cmode/Jtimestamp/Gyaw/Gpitch/Groll/x*', $sensor_data_content); + $sensor_data_array['mode'] = $uuid_bytes_read['mode']; + $sensor_data_array['timestamp'] = $uuid_bytes_read['timestamp']; + $sensor_data_array['yaw'] = $uuid_bytes_read['yaw']; + $sensor_data_array['pitch'] = $uuid_bytes_read['pitch']; + $sensor_data_array['roll'] = $uuid_bytes_read['roll']; + array_push($atom_structure['sensor_data']['data_type']['fusion_data'], $sensor_data_array); + break; + case 1: + $atom_structure['sensor_data']['data_type']['accel_count']++; + $uuid_bytes_read = unpack('cmode/Jtimestamp/Gyaw/Gpitch/Groll/x*', $sensor_data_content); + $sensor_data_array['mode'] = $uuid_bytes_read['mode']; + $sensor_data_array['timestamp'] = $uuid_bytes_read['timestamp']; + $sensor_data_array['yaw'] = $uuid_bytes_read['yaw']; + $sensor_data_array['pitch'] = $uuid_bytes_read['pitch']; + $sensor_data_array['roll'] = $uuid_bytes_read['roll']; + array_push($atom_structure['sensor_data']['data_type']['accel_data'], $sensor_data_array); + break; + case 2: + $atom_structure['sensor_data']['data_type']['gyro_count']++; + $uuid_bytes_read = unpack('cmode/Jtimestamp/Gyaw/Gpitch/Groll/x*', $sensor_data_content); + $sensor_data_array['mode'] = $uuid_bytes_read['mode']; + $sensor_data_array['timestamp'] = $uuid_bytes_read['timestamp']; + $sensor_data_array['yaw'] = $uuid_bytes_read['yaw']; + $sensor_data_array['pitch'] = $uuid_bytes_read['pitch']; + $sensor_data_array['roll'] = $uuid_bytes_read['roll']; + array_push($atom_structure['sensor_data']['data_type']['gyro_data'], $sensor_data_array); + break; + case 3: + $atom_structure['sensor_data']['data_type']['magno_count']++; + $uuid_bytes_read = unpack('cmode/Jtimestamp/Gmagx/Gmagy/Gmagz/x*', $sensor_data_content); + $sensor_data_array['mode'] = $uuid_bytes_read['mode']; + $sensor_data_array['timestamp'] = $uuid_bytes_read['timestamp']; + $sensor_data_array['magx'] = $uuid_bytes_read['magx']; + $sensor_data_array['magy'] = $uuid_bytes_read['magy']; + $sensor_data_array['magz'] = $uuid_bytes_read['magz']; + array_push($atom_structure['sensor_data']['data_type']['magno_data'], $sensor_data_array); + break; + case 5: + $atom_structure['sensor_data']['data_type']['gps_count']++; + $uuid_bytes_read = unpack('cmode/Jtimestamp/Glat/Glon/Galt/Gspeed/nbearing/nacc/x*', $sensor_data_content); + $sensor_data_array['mode'] = $uuid_bytes_read['mode']; + $sensor_data_array['timestamp'] = $uuid_bytes_read['timestamp']; + $sensor_data_array['lat'] = $uuid_bytes_read['lat']; + $sensor_data_array['lon'] = $uuid_bytes_read['lon']; + $sensor_data_array['alt'] = $uuid_bytes_read['alt']; + $sensor_data_array['speed'] = $uuid_bytes_read['speed']; + $sensor_data_array['bearing'] = $uuid_bytes_read['bearing']; + $sensor_data_array['acc'] = $uuid_bytes_read['acc']; + array_push($atom_structure['sensor_data']['data_type']['gps_data'], $sensor_data_array); + //array_push($debug_structure['debug_items'], $uuid_bytes_read['timestamp']); + break; + case 6: + $atom_structure['sensor_data']['data_type']['rotation_count']++; + $uuid_bytes_read = unpack('cmode/Jtimestamp/Grotx/Groty/Grotz/x*', $sensor_data_content); + $sensor_data_array['mode'] = $uuid_bytes_read['mode']; + $sensor_data_array['timestamp'] = $uuid_bytes_read['timestamp']; + $sensor_data_array['rotx'] = $uuid_bytes_read['rotx']; + $sensor_data_array['roty'] = $uuid_bytes_read['roty']; + $sensor_data_array['rotz'] = $uuid_bytes_read['rotz']; + array_push($atom_structure['sensor_data']['data_type']['rotation_data'], $sensor_data_array); + break; + default: + $atom_structure['sensor_data']['data_type']['unknown_count']++; + break; + } + } + //if (isset($debug_structure['debug_items']) && count($debug_structure['debug_items']) > 0) { + // $atom_structure['sensor_data']['data_type']['debug_list'] = implode(',', $debug_structure['debug_items']); + //} else { + $atom_structure['sensor_data']['data_type']['debug_list'] = 'No debug items in list!'; + //} + break; + + default: + $this->warning('Unhandled "uuid" atom identified by "'.$atom_structure['uuid_field_id'].'" at offset '.$atom_structure['offset'].' ('.strlen($atom_data).' bytes)'); + } + break; + + case 'gps ': + // https://dashcamtalk.com/forum/threads/script-to-extract-gps-data-from-novatek-mp4.20808/page-2#post-291730 + // The 'gps ' contains simple look up table made up of 8byte rows, that point to the 'free' atoms that contains the actual GPS data. + // The first row is version/metadata/notsure, I skip that. + // The following rows consist of 4byte address (absolute) and 4byte size (0x1000), these point to the GPS data in the file. + + $GPS_rowsize = 8; // 4 bytes for offset, 4 bytes for size + if (strlen($atom_data) > 0) { + if ((strlen($atom_data) % $GPS_rowsize) == 0) { + $atom_structure['gps_toc'] = array(); + foreach (str_split($atom_data, $GPS_rowsize) as $counter => $datapair) { + $atom_structure['gps_toc'][] = unpack('Noffset/Nsize', substr($atom_data, $counter * $GPS_rowsize, $GPS_rowsize)); + } + + $atom_structure['gps_entries'] = array(); + $previous_offset = $this->ftell(); + foreach ($atom_structure['gps_toc'] as $key => $gps_pointer) { + if ($key == 0) { + // "The first row is version/metadata/notsure, I skip that." + continue; + } + $this->fseek($gps_pointer['offset']); + $GPS_free_data = $this->fread($gps_pointer['size']); + + /* + // 2017-05-10: I see some of the data, notably the Hour-Minute-Second, but cannot reconcile the rest of the data. However, the NMEA "GPRMC" line is there and relatively easy to parse, so I'm using that instead + + // https://dashcamtalk.com/forum/threads/script-to-extract-gps-data-from-novatek-mp4.20808/page-2#post-291730 + // The structure of the GPS data atom (the 'free' atoms mentioned above) is following: + // hour,minute,second,year,month,day,active,latitude_b,longitude_b,unknown2,latitude,longitude,speed = struct.unpack_from(' 90) ? 1900 : 2000); // complete lack of foresight: datestamps are stored with 2-digit years, take best guess + $GPS_this_GPRMC['timestamp'] = $year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$second.$ms; + + $GPS_this_GPRMC['active'] = ($GPS_this_GPRMC['raw']['status'] == 'A'); // A=Active,V=Void + + foreach (array('latitude','longitude') as $latlon) { + preg_match('#^([0-9]{1,3})([0-9]{2}\\.[0-9]+)$#', $GPS_this_GPRMC['raw'][$latlon], $matches); + list($dummy, $deg, $min) = $matches; + $GPS_this_GPRMC[$latlon] = $deg + ($min / 60); + } + $GPS_this_GPRMC['latitude'] *= (($GPS_this_GPRMC['raw']['latitude_direction'] == 'S') ? -1 : 1); + $GPS_this_GPRMC['longitude'] *= (($GPS_this_GPRMC['raw']['longitude_direction'] == 'W') ? -1 : 1); + + $GPS_this_GPRMC['heading'] = $GPS_this_GPRMC['raw']['angle']; + $GPS_this_GPRMC['speed_knot'] = $GPS_this_GPRMC['raw']['knots']; + $GPS_this_GPRMC['speed_kmh'] = $GPS_this_GPRMC['raw']['knots'] * 1.852; + if ($GPS_this_GPRMC['raw']['variation']) { + $GPS_this_GPRMC['variation'] = $GPS_this_GPRMC['raw']['variation']; + $GPS_this_GPRMC['variation'] *= (($GPS_this_GPRMC['raw']['variation_direction'] == 'W') ? -1 : 1); + } + + $atom_structure['gps_entries'][$key] = $GPS_this_GPRMC; + + @$info['quicktime']['gps_track'][$GPS_this_GPRMC['timestamp']] = array( + 'latitude' => (float) $GPS_this_GPRMC['latitude'], + 'longitude' => (float) $GPS_this_GPRMC['longitude'], + 'speed_kmh' => (float) $GPS_this_GPRMC['speed_kmh'], + 'heading' => (float) $GPS_this_GPRMC['heading'], + ); + + } else { + $this->warning('Unhandled GPS format in "free" atom at offset '.$gps_pointer['offset']); + } + } + $this->fseek($previous_offset); + + } else { + $this->warning('QuickTime atom "'.$atomname.'" is not mod-8 bytes long ('.$atomsize.' bytes) at offset '.$baseoffset); + } + } else { + $this->warning('QuickTime atom "'.$atomname.'" is zero bytes long at offset '.$baseoffset); + } + break; + + case 'loci':// 3GP location (El Loco) + $loffset = 0; + $info['quicktime']['comments']['gps_flags'] = array( getid3_lib::BigEndian2Int(substr($atom_data, 0, 4))); + $info['quicktime']['comments']['gps_lang'] = array( getid3_lib::BigEndian2Int(substr($atom_data, 4, 2))); + $info['quicktime']['comments']['gps_location'] = array( $this->LociString(substr($atom_data, 6), $loffset)); + $loci_data = substr($atom_data, 6 + $loffset); + $info['quicktime']['comments']['gps_role'] = array( getid3_lib::BigEndian2Int(substr($loci_data, 0, 1))); + $info['quicktime']['comments']['gps_longitude'] = array(getid3_lib::FixedPoint16_16(substr($loci_data, 1, 4))); + $info['quicktime']['comments']['gps_latitude'] = array(getid3_lib::FixedPoint16_16(substr($loci_data, 5, 4))); + $info['quicktime']['comments']['gps_altitude'] = array(getid3_lib::FixedPoint16_16(substr($loci_data, 9, 4))); + $info['quicktime']['comments']['gps_body'] = array( $this->LociString(substr($loci_data, 13 ), $loffset)); + $info['quicktime']['comments']['gps_notes'] = array( $this->LociString(substr($loci_data, 13 + $loffset), $loffset)); + break; + + case 'chpl': // CHaPter List + // https://www.adobe.com/content/dam/Adobe/en/devnet/flv/pdfs/video_file_format_spec_v10.pdf + $chpl_version = getid3_lib::BigEndian2Int(substr($atom_data, 4, 1)); // Expected to be 0 + $chpl_flags = getid3_lib::BigEndian2Int(substr($atom_data, 5, 3)); // Reserved, set to 0 + $chpl_count = getid3_lib::BigEndian2Int(substr($atom_data, 8, 1)); + $chpl_offset = 9; + for ($i = 0; $i < $chpl_count; $i++) { + if (($chpl_offset + 9) >= strlen($atom_data)) { + $this->warning('QuickTime chapter '.$i.' extends beyond end of "chpl" atom'); + break; + } + $info['quicktime']['chapters'][$i]['timestamp'] = getid3_lib::BigEndian2Int(substr($atom_data, $chpl_offset, 8)) / 10000000; // timestamps are stored as 100-nanosecond units + $chpl_offset += 8; + $chpl_title_size = getid3_lib::BigEndian2Int(substr($atom_data, $chpl_offset, 1)); + $chpl_offset += 1; + $info['quicktime']['chapters'][$i]['title'] = substr($atom_data, $chpl_offset, $chpl_title_size); + $chpl_offset += $chpl_title_size; + } + break; + + case 'FIRM': // FIRMware version(?), seen on GoPro Hero4 + $info['quicktime']['camera']['firmware'] = $atom_data; + break; + + case 'CAME': // FIRMware version(?), seen on GoPro Hero4 + $info['quicktime']['camera']['serial_hash'] = unpack('H*', $atom_data); + break; + + case 'dscp': + case 'rcif': + // https://www.getid3.org/phpBB3/viewtopic.php?t=1908 + if (substr($atom_data, 0, 7) == "\x00\x00\x00\x00\x55\xC4".'{') { + if ($json_decoded = @json_decode(rtrim(substr($atom_data, 6), "\x00"), true)) { + $info['quicktime']['camera'][$atomname] = $json_decoded; + if (($atomname == 'rcif') && isset($info['quicktime']['camera']['rcif']['wxcamera']['rotate'])) { + $info['video']['rotate'] = $info['quicktime']['video']['rotate'] = $info['quicktime']['camera']['rcif']['wxcamera']['rotate']; + } + } else { + $this->warning('Failed to JSON decode atom "'.$atomname.'"'); + $atom_structure['data'] = $atom_data; + } + unset($json_decoded); + } else { + $this->warning('Expecting 55 C4 7B at start of atom "'.$atomname.'", found '.getid3_lib::PrintHexBytes(substr($atom_data, 4, 3)).' instead'); + $atom_structure['data'] = $atom_data; + } + break; + + case 'frea': + // https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Kodak.html#frea + // may contain "scra" (PreviewImage) and/or "thma" (ThumbnailImage) + $atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 4, $atomHierarchy, $ParseAllPossibleAtoms); + break; + case 'tima': // subatom to "frea" + // no idea what this does, the one sample file I've seen has a value of 0x00000027 + $atom_structure['data'] = $atom_data; + break; + case 'ver ': // subatom to "frea" + // some kind of version number, the one sample file I've seen has a value of "3.00.073" + $atom_structure['data'] = $atom_data; + break; + case 'thma': // subatom to "frea" -- "ThumbnailImage" + // https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Kodak.html#frea + if (strlen($atom_data) > 0) { + $info['quicktime']['comments']['picture'][] = array('data'=>$atom_data, 'image_mime'=>'image/jpeg', 'description'=>'ThumbnailImage'); + } + break; + case 'scra': // subatom to "frea" -- "PreviewImage" + // https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Kodak.html#frea + // but the only sample file I've seen has no useful data here + if (strlen($atom_data) > 0) { + $info['quicktime']['comments']['picture'][] = array('data'=>$atom_data, 'image_mime'=>'image/jpeg', 'description'=>'PreviewImage'); + } + break; + + case 'cdsc': // timed metadata reference + // A QuickTime movie can contain none, one, or several timed metadata tracks. Timed metadata tracks can refer to multiple tracks. + // Metadata tracks are linked to the tracks they describe using a track-reference of type 'cdsc'. The metadata track holds the 'cdsc' track reference. + $atom_structure['track_number'] = getid3_lib::BigEndian2Int($atom_data); + break; + + default: + $this->warning('Unknown QuickTime atom type: "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $atomname).'" ('.trim(getid3_lib::PrintHexBytes($atomname)).'), '.$atomsize.' bytes at offset '.$baseoffset); + $atom_structure['data'] = $atom_data; + break; + } + } + array_pop($atomHierarchy); + return $atom_structure; + } + + /** + * @param string $atom_data + * @param int $baseoffset + * @param array $atomHierarchy + * @param bool $ParseAllPossibleAtoms + * + * @return array|false + */ + public function QuicktimeParseContainerAtom($atom_data, $baseoffset, &$atomHierarchy, $ParseAllPossibleAtoms) { + $atom_structure = false; + $subatomoffset = 0; + $subatomcounter = 0; + if ((strlen($atom_data) == 4) && (getid3_lib::BigEndian2Int($atom_data) == 0x00000000)) { + return false; + } + while ($subatomoffset < strlen($atom_data)) { + $subatomsize = getid3_lib::BigEndian2Int(substr($atom_data, $subatomoffset + 0, 4)); + $subatomname = substr($atom_data, $subatomoffset + 4, 4); + $subatomdata = substr($atom_data, $subatomoffset + 8, $subatomsize - 8); + if ($subatomsize == 0) { + // Furthermore, for historical reasons the list of atoms is optionally + // terminated by a 32-bit integer set to 0. If you are writing a program + // to read user data atoms, you should allow for the terminating 0. + if (strlen($atom_data) > 12) { + $subatomoffset += 4; + continue; + } + return $atom_structure; + } + if (strlen($subatomdata) < ($subatomsize - 8)) { + // we don't have enough data to decode the subatom. + // this may be because we are refusing to parse large subatoms, or it may be because this atom had its size set too large + // so we passed in the start of a following atom incorrectly? + return $atom_structure; + } + $atom_structure[$subatomcounter++] = $this->QuicktimeParseAtom($subatomname, $subatomsize, $subatomdata, $baseoffset + $subatomoffset, $atomHierarchy, $ParseAllPossibleAtoms); + $subatomoffset += $subatomsize; + } + return $atom_structure; + } + + /** + * @param string $data + * @param int $offset + * + * @return int + */ + public function quicktime_read_mp4_descr_length($data, &$offset) { + // http://libquicktime.sourcearchive.com/documentation/2:1.0.2plus-pdebian-2build1/esds_8c-source.html + $num_bytes = 0; + $length = 0; + do { + $b = ord(substr($data, $offset++, 1)); + $length = ($length << 7) | ($b & 0x7F); + } while (($b & 0x80) && ($num_bytes++ < 4)); + return $length; + } + + /** + * @param int $languageid + * + * @return string + */ + public function QuicktimeLanguageLookup($languageid) { + // http://developer.apple.com/library/mac/#documentation/QuickTime/QTFF/QTFFChap4/qtff4.html#//apple_ref/doc/uid/TP40000939-CH206-34353 + static $QuicktimeLanguageLookup = array(); + if (empty($QuicktimeLanguageLookup)) { + $QuicktimeLanguageLookup[0] = 'English'; + $QuicktimeLanguageLookup[1] = 'French'; + $QuicktimeLanguageLookup[2] = 'German'; + $QuicktimeLanguageLookup[3] = 'Italian'; + $QuicktimeLanguageLookup[4] = 'Dutch'; + $QuicktimeLanguageLookup[5] = 'Swedish'; + $QuicktimeLanguageLookup[6] = 'Spanish'; + $QuicktimeLanguageLookup[7] = 'Danish'; + $QuicktimeLanguageLookup[8] = 'Portuguese'; + $QuicktimeLanguageLookup[9] = 'Norwegian'; + $QuicktimeLanguageLookup[10] = 'Hebrew'; + $QuicktimeLanguageLookup[11] = 'Japanese'; + $QuicktimeLanguageLookup[12] = 'Arabic'; + $QuicktimeLanguageLookup[13] = 'Finnish'; + $QuicktimeLanguageLookup[14] = 'Greek'; + $QuicktimeLanguageLookup[15] = 'Icelandic'; + $QuicktimeLanguageLookup[16] = 'Maltese'; + $QuicktimeLanguageLookup[17] = 'Turkish'; + $QuicktimeLanguageLookup[18] = 'Croatian'; + $QuicktimeLanguageLookup[19] = 'Chinese (Traditional)'; + $QuicktimeLanguageLookup[20] = 'Urdu'; + $QuicktimeLanguageLookup[21] = 'Hindi'; + $QuicktimeLanguageLookup[22] = 'Thai'; + $QuicktimeLanguageLookup[23] = 'Korean'; + $QuicktimeLanguageLookup[24] = 'Lithuanian'; + $QuicktimeLanguageLookup[25] = 'Polish'; + $QuicktimeLanguageLookup[26] = 'Hungarian'; + $QuicktimeLanguageLookup[27] = 'Estonian'; + $QuicktimeLanguageLookup[28] = 'Lettish'; + $QuicktimeLanguageLookup[28] = 'Latvian'; + $QuicktimeLanguageLookup[29] = 'Saamisk'; + $QuicktimeLanguageLookup[29] = 'Lappish'; + $QuicktimeLanguageLookup[30] = 'Faeroese'; + $QuicktimeLanguageLookup[31] = 'Farsi'; + $QuicktimeLanguageLookup[31] = 'Persian'; + $QuicktimeLanguageLookup[32] = 'Russian'; + $QuicktimeLanguageLookup[33] = 'Chinese (Simplified)'; + $QuicktimeLanguageLookup[34] = 'Flemish'; + $QuicktimeLanguageLookup[35] = 'Irish'; + $QuicktimeLanguageLookup[36] = 'Albanian'; + $QuicktimeLanguageLookup[37] = 'Romanian'; + $QuicktimeLanguageLookup[38] = 'Czech'; + $QuicktimeLanguageLookup[39] = 'Slovak'; + $QuicktimeLanguageLookup[40] = 'Slovenian'; + $QuicktimeLanguageLookup[41] = 'Yiddish'; + $QuicktimeLanguageLookup[42] = 'Serbian'; + $QuicktimeLanguageLookup[43] = 'Macedonian'; + $QuicktimeLanguageLookup[44] = 'Bulgarian'; + $QuicktimeLanguageLookup[45] = 'Ukrainian'; + $QuicktimeLanguageLookup[46] = 'Byelorussian'; + $QuicktimeLanguageLookup[47] = 'Uzbek'; + $QuicktimeLanguageLookup[48] = 'Kazakh'; + $QuicktimeLanguageLookup[49] = 'Azerbaijani'; + $QuicktimeLanguageLookup[50] = 'AzerbaijanAr'; + $QuicktimeLanguageLookup[51] = 'Armenian'; + $QuicktimeLanguageLookup[52] = 'Georgian'; + $QuicktimeLanguageLookup[53] = 'Moldavian'; + $QuicktimeLanguageLookup[54] = 'Kirghiz'; + $QuicktimeLanguageLookup[55] = 'Tajiki'; + $QuicktimeLanguageLookup[56] = 'Turkmen'; + $QuicktimeLanguageLookup[57] = 'Mongolian'; + $QuicktimeLanguageLookup[58] = 'MongolianCyr'; + $QuicktimeLanguageLookup[59] = 'Pashto'; + $QuicktimeLanguageLookup[60] = 'Kurdish'; + $QuicktimeLanguageLookup[61] = 'Kashmiri'; + $QuicktimeLanguageLookup[62] = 'Sindhi'; + $QuicktimeLanguageLookup[63] = 'Tibetan'; + $QuicktimeLanguageLookup[64] = 'Nepali'; + $QuicktimeLanguageLookup[65] = 'Sanskrit'; + $QuicktimeLanguageLookup[66] = 'Marathi'; + $QuicktimeLanguageLookup[67] = 'Bengali'; + $QuicktimeLanguageLookup[68] = 'Assamese'; + $QuicktimeLanguageLookup[69] = 'Gujarati'; + $QuicktimeLanguageLookup[70] = 'Punjabi'; + $QuicktimeLanguageLookup[71] = 'Oriya'; + $QuicktimeLanguageLookup[72] = 'Malayalam'; + $QuicktimeLanguageLookup[73] = 'Kannada'; + $QuicktimeLanguageLookup[74] = 'Tamil'; + $QuicktimeLanguageLookup[75] = 'Telugu'; + $QuicktimeLanguageLookup[76] = 'Sinhalese'; + $QuicktimeLanguageLookup[77] = 'Burmese'; + $QuicktimeLanguageLookup[78] = 'Khmer'; + $QuicktimeLanguageLookup[79] = 'Lao'; + $QuicktimeLanguageLookup[80] = 'Vietnamese'; + $QuicktimeLanguageLookup[81] = 'Indonesian'; + $QuicktimeLanguageLookup[82] = 'Tagalog'; + $QuicktimeLanguageLookup[83] = 'MalayRoman'; + $QuicktimeLanguageLookup[84] = 'MalayArabic'; + $QuicktimeLanguageLookup[85] = 'Amharic'; + $QuicktimeLanguageLookup[86] = 'Tigrinya'; + $QuicktimeLanguageLookup[87] = 'Galla'; + $QuicktimeLanguageLookup[87] = 'Oromo'; + $QuicktimeLanguageLookup[88] = 'Somali'; + $QuicktimeLanguageLookup[89] = 'Swahili'; + $QuicktimeLanguageLookup[90] = 'Ruanda'; + $QuicktimeLanguageLookup[91] = 'Rundi'; + $QuicktimeLanguageLookup[92] = 'Chewa'; + $QuicktimeLanguageLookup[93] = 'Malagasy'; + $QuicktimeLanguageLookup[94] = 'Esperanto'; + $QuicktimeLanguageLookup[128] = 'Welsh'; + $QuicktimeLanguageLookup[129] = 'Basque'; + $QuicktimeLanguageLookup[130] = 'Catalan'; + $QuicktimeLanguageLookup[131] = 'Latin'; + $QuicktimeLanguageLookup[132] = 'Quechua'; + $QuicktimeLanguageLookup[133] = 'Guarani'; + $QuicktimeLanguageLookup[134] = 'Aymara'; + $QuicktimeLanguageLookup[135] = 'Tatar'; + $QuicktimeLanguageLookup[136] = 'Uighur'; + $QuicktimeLanguageLookup[137] = 'Dzongkha'; + $QuicktimeLanguageLookup[138] = 'JavaneseRom'; + $QuicktimeLanguageLookup[32767] = 'Unspecified'; + } + if (($languageid > 138) && ($languageid < 32767)) { + /* + ISO Language Codes - http://www.loc.gov/standards/iso639-2/php/code_list.php + Because the language codes specified by ISO 639-2/T are three characters long, they must be packed to fit into a 16-bit field. + The packing algorithm must map each of the three characters, which are always lowercase, into a 5-bit integer and then concatenate + these integers into the least significant 15 bits of a 16-bit integer, leaving the 16-bit integer's most significant bit set to zero. + + One algorithm for performing this packing is to treat each ISO character as a 16-bit integer. Subtract 0x60 from the first character + and multiply by 2^10 (0x400), subtract 0x60 from the second character and multiply by 2^5 (0x20), subtract 0x60 from the third character, + and add the three 16-bit values. This will result in a single 16-bit value with the three codes correctly packed into the 15 least + significant bits and the most significant bit set to zero. + */ + $iso_language_id = ''; + $iso_language_id .= chr((($languageid & 0x7C00) >> 10) + 0x60); + $iso_language_id .= chr((($languageid & 0x03E0) >> 5) + 0x60); + $iso_language_id .= chr((($languageid & 0x001F) >> 0) + 0x60); + $QuicktimeLanguageLookup[$languageid] = getid3_id3v2::LanguageLookup($iso_language_id); + } + return (isset($QuicktimeLanguageLookup[$languageid]) ? $QuicktimeLanguageLookup[$languageid] : 'invalid'); + } + + /** + * @param string $codecid + * + * @return string + */ + public function QuicktimeVideoCodecLookup($codecid) { + static $QuicktimeVideoCodecLookup = array(); + if (empty($QuicktimeVideoCodecLookup)) { + $QuicktimeVideoCodecLookup['.SGI'] = 'SGI'; + $QuicktimeVideoCodecLookup['3IV1'] = '3ivx MPEG-4 v1'; + $QuicktimeVideoCodecLookup['3IV2'] = '3ivx MPEG-4 v2'; + $QuicktimeVideoCodecLookup['3IVX'] = '3ivx MPEG-4'; + $QuicktimeVideoCodecLookup['8BPS'] = 'Planar RGB'; + $QuicktimeVideoCodecLookup['avc1'] = 'H.264/MPEG-4 AVC'; + $QuicktimeVideoCodecLookup['avr '] = 'AVR-JPEG'; + $QuicktimeVideoCodecLookup['b16g'] = '16Gray'; + $QuicktimeVideoCodecLookup['b32a'] = '32AlphaGray'; + $QuicktimeVideoCodecLookup['b48r'] = '48RGB'; + $QuicktimeVideoCodecLookup['b64a'] = '64ARGB'; + $QuicktimeVideoCodecLookup['base'] = 'Base'; + $QuicktimeVideoCodecLookup['clou'] = 'Cloud'; + $QuicktimeVideoCodecLookup['cmyk'] = 'CMYK'; + $QuicktimeVideoCodecLookup['cvid'] = 'Cinepak'; + $QuicktimeVideoCodecLookup['dmb1'] = 'OpenDML JPEG'; + $QuicktimeVideoCodecLookup['dvc '] = 'DVC-NTSC'; + $QuicktimeVideoCodecLookup['dvcp'] = 'DVC-PAL'; + $QuicktimeVideoCodecLookup['dvpn'] = 'DVCPro-NTSC'; + $QuicktimeVideoCodecLookup['dvpp'] = 'DVCPro-PAL'; + $QuicktimeVideoCodecLookup['fire'] = 'Fire'; + $QuicktimeVideoCodecLookup['flic'] = 'FLC'; + $QuicktimeVideoCodecLookup['gif '] = 'GIF'; + $QuicktimeVideoCodecLookup['h261'] = 'H261'; + $QuicktimeVideoCodecLookup['h263'] = 'H263'; + $QuicktimeVideoCodecLookup['IV41'] = 'Indeo4'; + $QuicktimeVideoCodecLookup['jpeg'] = 'JPEG'; + $QuicktimeVideoCodecLookup['kpcd'] = 'PhotoCD'; + $QuicktimeVideoCodecLookup['mjpa'] = 'Motion JPEG-A'; + $QuicktimeVideoCodecLookup['mjpb'] = 'Motion JPEG-B'; + $QuicktimeVideoCodecLookup['msvc'] = 'Microsoft Video1'; + $QuicktimeVideoCodecLookup['myuv'] = 'MPEG YUV420'; + $QuicktimeVideoCodecLookup['path'] = 'Vector'; + $QuicktimeVideoCodecLookup['png '] = 'PNG'; + $QuicktimeVideoCodecLookup['PNTG'] = 'MacPaint'; + $QuicktimeVideoCodecLookup['qdgx'] = 'QuickDrawGX'; + $QuicktimeVideoCodecLookup['qdrw'] = 'QuickDraw'; + $QuicktimeVideoCodecLookup['raw '] = 'RAW'; + $QuicktimeVideoCodecLookup['ripl'] = 'WaterRipple'; + $QuicktimeVideoCodecLookup['rpza'] = 'Video'; + $QuicktimeVideoCodecLookup['smc '] = 'Graphics'; + $QuicktimeVideoCodecLookup['SVQ1'] = 'Sorenson Video 1'; + $QuicktimeVideoCodecLookup['SVQ1'] = 'Sorenson Video 3'; + $QuicktimeVideoCodecLookup['syv9'] = 'Sorenson YUV9'; + $QuicktimeVideoCodecLookup['tga '] = 'Targa'; + $QuicktimeVideoCodecLookup['tiff'] = 'TIFF'; + $QuicktimeVideoCodecLookup['WRAW'] = 'Windows RAW'; + $QuicktimeVideoCodecLookup['WRLE'] = 'BMP'; + $QuicktimeVideoCodecLookup['y420'] = 'YUV420'; + $QuicktimeVideoCodecLookup['yuv2'] = 'ComponentVideo'; + $QuicktimeVideoCodecLookup['yuvs'] = 'ComponentVideoUnsigned'; + $QuicktimeVideoCodecLookup['yuvu'] = 'ComponentVideoSigned'; + } + return (isset($QuicktimeVideoCodecLookup[$codecid]) ? $QuicktimeVideoCodecLookup[$codecid] : ''); + } + + /** + * @param string $codecid + * + * @return mixed|string + */ + public function QuicktimeAudioCodecLookup($codecid) { + static $QuicktimeAudioCodecLookup = array(); + if (empty($QuicktimeAudioCodecLookup)) { + $QuicktimeAudioCodecLookup['.mp3'] = 'Fraunhofer MPEG Layer-III alias'; + $QuicktimeAudioCodecLookup['aac '] = 'ISO/IEC 14496-3 AAC'; + $QuicktimeAudioCodecLookup['agsm'] = 'Apple GSM 10:1'; + $QuicktimeAudioCodecLookup['alac'] = 'Apple Lossless Audio Codec'; + $QuicktimeAudioCodecLookup['alaw'] = 'A-law 2:1'; + $QuicktimeAudioCodecLookup['conv'] = 'Sample Format'; + $QuicktimeAudioCodecLookup['dvca'] = 'DV'; + $QuicktimeAudioCodecLookup['dvi '] = 'DV 4:1'; + $QuicktimeAudioCodecLookup['eqal'] = 'Frequency Equalizer'; + $QuicktimeAudioCodecLookup['fl32'] = '32-bit Floating Point'; + $QuicktimeAudioCodecLookup['fl64'] = '64-bit Floating Point'; + $QuicktimeAudioCodecLookup['ima4'] = 'Interactive Multimedia Association 4:1'; + $QuicktimeAudioCodecLookup['in24'] = '24-bit Integer'; + $QuicktimeAudioCodecLookup['in32'] = '32-bit Integer'; + $QuicktimeAudioCodecLookup['lpc '] = 'LPC 23:1'; + $QuicktimeAudioCodecLookup['MAC3'] = 'Macintosh Audio Compression/Expansion (MACE) 3:1'; + $QuicktimeAudioCodecLookup['MAC6'] = 'Macintosh Audio Compression/Expansion (MACE) 6:1'; + $QuicktimeAudioCodecLookup['mixb'] = '8-bit Mixer'; + $QuicktimeAudioCodecLookup['mixw'] = '16-bit Mixer'; + $QuicktimeAudioCodecLookup['mp4a'] = 'ISO/IEC 14496-3 AAC'; + $QuicktimeAudioCodecLookup['MS'."\x00\x02"] = 'Microsoft ADPCM'; + $QuicktimeAudioCodecLookup['MS'."\x00\x11"] = 'DV IMA'; + $QuicktimeAudioCodecLookup['MS'."\x00\x55"] = 'Fraunhofer MPEG Layer III'; + $QuicktimeAudioCodecLookup['NONE'] = 'No Encoding'; + $QuicktimeAudioCodecLookup['Qclp'] = 'Qualcomm PureVoice'; + $QuicktimeAudioCodecLookup['QDM2'] = 'QDesign Music 2'; + $QuicktimeAudioCodecLookup['QDMC'] = 'QDesign Music 1'; + $QuicktimeAudioCodecLookup['ratb'] = '8-bit Rate'; + $QuicktimeAudioCodecLookup['ratw'] = '16-bit Rate'; + $QuicktimeAudioCodecLookup['raw '] = 'raw PCM'; + $QuicktimeAudioCodecLookup['sour'] = 'Sound Source'; + $QuicktimeAudioCodecLookup['sowt'] = 'signed/two\'s complement (Little Endian)'; + $QuicktimeAudioCodecLookup['str1'] = 'Iomega MPEG layer II'; + $QuicktimeAudioCodecLookup['str2'] = 'Iomega MPEG *layer II'; + $QuicktimeAudioCodecLookup['str3'] = 'Iomega MPEG **layer II'; + $QuicktimeAudioCodecLookup['str4'] = 'Iomega MPEG ***layer II'; + $QuicktimeAudioCodecLookup['twos'] = 'signed/two\'s complement (Big Endian)'; + $QuicktimeAudioCodecLookup['ulaw'] = 'mu-law 2:1'; + } + return (isset($QuicktimeAudioCodecLookup[$codecid]) ? $QuicktimeAudioCodecLookup[$codecid] : ''); + } + + /** + * @param string $compressionid + * + * @return string + */ + public function QuicktimeDCOMLookup($compressionid) { + static $QuicktimeDCOMLookup = array(); + if (empty($QuicktimeDCOMLookup)) { + $QuicktimeDCOMLookup['zlib'] = 'ZLib Deflate'; + $QuicktimeDCOMLookup['adec'] = 'Apple Compression'; + } + return (isset($QuicktimeDCOMLookup[$compressionid]) ? $QuicktimeDCOMLookup[$compressionid] : ''); + } + + /** + * @param int $colordepthid + * + * @return string + */ + public function QuicktimeColorNameLookup($colordepthid) { + static $QuicktimeColorNameLookup = array(); + if (empty($QuicktimeColorNameLookup)) { + $QuicktimeColorNameLookup[1] = '2-color (monochrome)'; + $QuicktimeColorNameLookup[2] = '4-color'; + $QuicktimeColorNameLookup[4] = '16-color'; + $QuicktimeColorNameLookup[8] = '256-color'; + $QuicktimeColorNameLookup[16] = 'thousands (16-bit color)'; + $QuicktimeColorNameLookup[24] = 'millions (24-bit color)'; + $QuicktimeColorNameLookup[32] = 'millions+ (32-bit color)'; + $QuicktimeColorNameLookup[33] = 'black & white'; + $QuicktimeColorNameLookup[34] = '4-gray'; + $QuicktimeColorNameLookup[36] = '16-gray'; + $QuicktimeColorNameLookup[40] = '256-gray'; + } + return (isset($QuicktimeColorNameLookup[$colordepthid]) ? $QuicktimeColorNameLookup[$colordepthid] : 'invalid'); + } + + /** + * @param int $stik + * + * @return string + */ + public function QuicktimeSTIKLookup($stik) { + static $QuicktimeSTIKLookup = array(); + if (empty($QuicktimeSTIKLookup)) { + $QuicktimeSTIKLookup[0] = 'Movie'; + $QuicktimeSTIKLookup[1] = 'Normal'; + $QuicktimeSTIKLookup[2] = 'Audiobook'; + $QuicktimeSTIKLookup[5] = 'Whacked Bookmark'; + $QuicktimeSTIKLookup[6] = 'Music Video'; + $QuicktimeSTIKLookup[9] = 'Short Film'; + $QuicktimeSTIKLookup[10] = 'TV Show'; + $QuicktimeSTIKLookup[11] = 'Booklet'; + $QuicktimeSTIKLookup[14] = 'Ringtone'; + $QuicktimeSTIKLookup[21] = 'Podcast'; + } + return (isset($QuicktimeSTIKLookup[$stik]) ? $QuicktimeSTIKLookup[$stik] : 'invalid'); + } + + /** + * @param int $audio_profile_id + * + * @return string + */ + public function QuicktimeIODSaudioProfileName($audio_profile_id) { + static $QuicktimeIODSaudioProfileNameLookup = array(); + if (empty($QuicktimeIODSaudioProfileNameLookup)) { + $QuicktimeIODSaudioProfileNameLookup = array( + 0x00 => 'ISO Reserved (0x00)', + 0x01 => 'Main Audio Profile @ Level 1', + 0x02 => 'Main Audio Profile @ Level 2', + 0x03 => 'Main Audio Profile @ Level 3', + 0x04 => 'Main Audio Profile @ Level 4', + 0x05 => 'Scalable Audio Profile @ Level 1', + 0x06 => 'Scalable Audio Profile @ Level 2', + 0x07 => 'Scalable Audio Profile @ Level 3', + 0x08 => 'Scalable Audio Profile @ Level 4', + 0x09 => 'Speech Audio Profile @ Level 1', + 0x0A => 'Speech Audio Profile @ Level 2', + 0x0B => 'Synthetic Audio Profile @ Level 1', + 0x0C => 'Synthetic Audio Profile @ Level 2', + 0x0D => 'Synthetic Audio Profile @ Level 3', + 0x0E => 'High Quality Audio Profile @ Level 1', + 0x0F => 'High Quality Audio Profile @ Level 2', + 0x10 => 'High Quality Audio Profile @ Level 3', + 0x11 => 'High Quality Audio Profile @ Level 4', + 0x12 => 'High Quality Audio Profile @ Level 5', + 0x13 => 'High Quality Audio Profile @ Level 6', + 0x14 => 'High Quality Audio Profile @ Level 7', + 0x15 => 'High Quality Audio Profile @ Level 8', + 0x16 => 'Low Delay Audio Profile @ Level 1', + 0x17 => 'Low Delay Audio Profile @ Level 2', + 0x18 => 'Low Delay Audio Profile @ Level 3', + 0x19 => 'Low Delay Audio Profile @ Level 4', + 0x1A => 'Low Delay Audio Profile @ Level 5', + 0x1B => 'Low Delay Audio Profile @ Level 6', + 0x1C => 'Low Delay Audio Profile @ Level 7', + 0x1D => 'Low Delay Audio Profile @ Level 8', + 0x1E => 'Natural Audio Profile @ Level 1', + 0x1F => 'Natural Audio Profile @ Level 2', + 0x20 => 'Natural Audio Profile @ Level 3', + 0x21 => 'Natural Audio Profile @ Level 4', + 0x22 => 'Mobile Audio Internetworking Profile @ Level 1', + 0x23 => 'Mobile Audio Internetworking Profile @ Level 2', + 0x24 => 'Mobile Audio Internetworking Profile @ Level 3', + 0x25 => 'Mobile Audio Internetworking Profile @ Level 4', + 0x26 => 'Mobile Audio Internetworking Profile @ Level 5', + 0x27 => 'Mobile Audio Internetworking Profile @ Level 6', + 0x28 => 'AAC Profile @ Level 1', + 0x29 => 'AAC Profile @ Level 2', + 0x2A => 'AAC Profile @ Level 4', + 0x2B => 'AAC Profile @ Level 5', + 0x2C => 'High Efficiency AAC Profile @ Level 2', + 0x2D => 'High Efficiency AAC Profile @ Level 3', + 0x2E => 'High Efficiency AAC Profile @ Level 4', + 0x2F => 'High Efficiency AAC Profile @ Level 5', + 0xFE => 'Not part of MPEG-4 audio profiles', + 0xFF => 'No audio capability required', + ); + } + return (isset($QuicktimeIODSaudioProfileNameLookup[$audio_profile_id]) ? $QuicktimeIODSaudioProfileNameLookup[$audio_profile_id] : 'ISO Reserved / User Private'); + } + + /** + * @param int $video_profile_id + * + * @return string + */ + public function QuicktimeIODSvideoProfileName($video_profile_id) { + static $QuicktimeIODSvideoProfileNameLookup = array(); + if (empty($QuicktimeIODSvideoProfileNameLookup)) { + $QuicktimeIODSvideoProfileNameLookup = array( + 0x00 => 'Reserved (0x00) Profile', + 0x01 => 'Simple Profile @ Level 1', + 0x02 => 'Simple Profile @ Level 2', + 0x03 => 'Simple Profile @ Level 3', + 0x08 => 'Simple Profile @ Level 0', + 0x10 => 'Simple Scalable Profile @ Level 0', + 0x11 => 'Simple Scalable Profile @ Level 1', + 0x12 => 'Simple Scalable Profile @ Level 2', + 0x15 => 'AVC/H264 Profile', + 0x21 => 'Core Profile @ Level 1', + 0x22 => 'Core Profile @ Level 2', + 0x32 => 'Main Profile @ Level 2', + 0x33 => 'Main Profile @ Level 3', + 0x34 => 'Main Profile @ Level 4', + 0x42 => 'N-bit Profile @ Level 2', + 0x51 => 'Scalable Texture Profile @ Level 1', + 0x61 => 'Simple Face Animation Profile @ Level 1', + 0x62 => 'Simple Face Animation Profile @ Level 2', + 0x63 => 'Simple FBA Profile @ Level 1', + 0x64 => 'Simple FBA Profile @ Level 2', + 0x71 => 'Basic Animated Texture Profile @ Level 1', + 0x72 => 'Basic Animated Texture Profile @ Level 2', + 0x81 => 'Hybrid Profile @ Level 1', + 0x82 => 'Hybrid Profile @ Level 2', + 0x91 => 'Advanced Real Time Simple Profile @ Level 1', + 0x92 => 'Advanced Real Time Simple Profile @ Level 2', + 0x93 => 'Advanced Real Time Simple Profile @ Level 3', + 0x94 => 'Advanced Real Time Simple Profile @ Level 4', + 0xA1 => 'Core Scalable Profile @ Level1', + 0xA2 => 'Core Scalable Profile @ Level2', + 0xA3 => 'Core Scalable Profile @ Level3', + 0xB1 => 'Advanced Coding Efficiency Profile @ Level 1', + 0xB2 => 'Advanced Coding Efficiency Profile @ Level 2', + 0xB3 => 'Advanced Coding Efficiency Profile @ Level 3', + 0xB4 => 'Advanced Coding Efficiency Profile @ Level 4', + 0xC1 => 'Advanced Core Profile @ Level 1', + 0xC2 => 'Advanced Core Profile @ Level 2', + 0xD1 => 'Advanced Scalable Texture @ Level1', + 0xD2 => 'Advanced Scalable Texture @ Level2', + 0xE1 => 'Simple Studio Profile @ Level 1', + 0xE2 => 'Simple Studio Profile @ Level 2', + 0xE3 => 'Simple Studio Profile @ Level 3', + 0xE4 => 'Simple Studio Profile @ Level 4', + 0xE5 => 'Core Studio Profile @ Level 1', + 0xE6 => 'Core Studio Profile @ Level 2', + 0xE7 => 'Core Studio Profile @ Level 3', + 0xE8 => 'Core Studio Profile @ Level 4', + 0xF0 => 'Advanced Simple Profile @ Level 0', + 0xF1 => 'Advanced Simple Profile @ Level 1', + 0xF2 => 'Advanced Simple Profile @ Level 2', + 0xF3 => 'Advanced Simple Profile @ Level 3', + 0xF4 => 'Advanced Simple Profile @ Level 4', + 0xF5 => 'Advanced Simple Profile @ Level 5', + 0xF7 => 'Advanced Simple Profile @ Level 3b', + 0xF8 => 'Fine Granularity Scalable Profile @ Level 0', + 0xF9 => 'Fine Granularity Scalable Profile @ Level 1', + 0xFA => 'Fine Granularity Scalable Profile @ Level 2', + 0xFB => 'Fine Granularity Scalable Profile @ Level 3', + 0xFC => 'Fine Granularity Scalable Profile @ Level 4', + 0xFD => 'Fine Granularity Scalable Profile @ Level 5', + 0xFE => 'Not part of MPEG-4 Visual profiles', + 0xFF => 'No visual capability required', + ); + } + return (isset($QuicktimeIODSvideoProfileNameLookup[$video_profile_id]) ? $QuicktimeIODSvideoProfileNameLookup[$video_profile_id] : 'ISO Reserved Profile'); + } + + /** + * @param int $rtng + * + * @return string + */ + public function QuicktimeContentRatingLookup($rtng) { + static $QuicktimeContentRatingLookup = array(); + if (empty($QuicktimeContentRatingLookup)) { + $QuicktimeContentRatingLookup[0] = 'None'; + $QuicktimeContentRatingLookup[2] = 'Clean'; + $QuicktimeContentRatingLookup[4] = 'Explicit'; + } + return (isset($QuicktimeContentRatingLookup[$rtng]) ? $QuicktimeContentRatingLookup[$rtng] : 'invalid'); + } + + /** + * @param int $akid + * + * @return string + */ + public function QuicktimeStoreAccountTypeLookup($akid) { + static $QuicktimeStoreAccountTypeLookup = array(); + if (empty($QuicktimeStoreAccountTypeLookup)) { + $QuicktimeStoreAccountTypeLookup[0] = 'iTunes'; + $QuicktimeStoreAccountTypeLookup[1] = 'AOL'; + } + return (isset($QuicktimeStoreAccountTypeLookup[$akid]) ? $QuicktimeStoreAccountTypeLookup[$akid] : 'invalid'); + } + + /** + * @param int $sfid + * + * @return string + */ + public function QuicktimeStoreFrontCodeLookup($sfid) { + static $QuicktimeStoreFrontCodeLookup = array(); + if (empty($QuicktimeStoreFrontCodeLookup)) { + $QuicktimeStoreFrontCodeLookup[143460] = 'Australia'; + $QuicktimeStoreFrontCodeLookup[143445] = 'Austria'; + $QuicktimeStoreFrontCodeLookup[143446] = 'Belgium'; + $QuicktimeStoreFrontCodeLookup[143455] = 'Canada'; + $QuicktimeStoreFrontCodeLookup[143458] = 'Denmark'; + $QuicktimeStoreFrontCodeLookup[143447] = 'Finland'; + $QuicktimeStoreFrontCodeLookup[143442] = 'France'; + $QuicktimeStoreFrontCodeLookup[143443] = 'Germany'; + $QuicktimeStoreFrontCodeLookup[143448] = 'Greece'; + $QuicktimeStoreFrontCodeLookup[143449] = 'Ireland'; + $QuicktimeStoreFrontCodeLookup[143450] = 'Italy'; + $QuicktimeStoreFrontCodeLookup[143462] = 'Japan'; + $QuicktimeStoreFrontCodeLookup[143451] = 'Luxembourg'; + $QuicktimeStoreFrontCodeLookup[143452] = 'Netherlands'; + $QuicktimeStoreFrontCodeLookup[143461] = 'New Zealand'; + $QuicktimeStoreFrontCodeLookup[143457] = 'Norway'; + $QuicktimeStoreFrontCodeLookup[143453] = 'Portugal'; + $QuicktimeStoreFrontCodeLookup[143454] = 'Spain'; + $QuicktimeStoreFrontCodeLookup[143456] = 'Sweden'; + $QuicktimeStoreFrontCodeLookup[143459] = 'Switzerland'; + $QuicktimeStoreFrontCodeLookup[143444] = 'United Kingdom'; + $QuicktimeStoreFrontCodeLookup[143441] = 'United States'; + } + return (isset($QuicktimeStoreFrontCodeLookup[$sfid]) ? $QuicktimeStoreFrontCodeLookup[$sfid] : 'invalid'); + } + + /** + * @param string $atom_data + * + * @return array + */ + public function QuicktimeParseNikonNCTG($atom_data) { + // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html#NCTG + // Nikon-specific QuickTime tags found in the NCDT atom of MOV videos from some Nikon cameras such as the Coolpix S8000 and D5100 + // Data is stored as records of: + // * 4 bytes record type + // * 2 bytes size of data field type: + // 0x0001 = flag (size field *= 1-byte) + // 0x0002 = char (size field *= 1-byte) + // 0x0003 = DWORD+ (size field *= 2-byte), values are stored CDAB + // 0x0004 = QWORD+ (size field *= 4-byte), values are stored EFGHABCD + // 0x0005 = float (size field *= 8-byte), values are stored aaaabbbb where value is aaaa/bbbb; possibly multiple sets of values appended together + // 0x0007 = bytes (size field *= 1-byte), values are stored as ?????? + // 0x0008 = ????? (size field *= 2-byte), values are stored as ?????? + // * 2 bytes data size field + // * ? bytes data (string data may be null-padded; datestamp fields are in the format "2011:05:25 20:24:15") + // all integers are stored BigEndian + + $NCTGtagName = array( + 0x00000001 => 'Make', + 0x00000002 => 'Model', + 0x00000003 => 'Software', + 0x00000011 => 'CreateDate', + 0x00000012 => 'DateTimeOriginal', + 0x00000013 => 'FrameCount', + 0x00000016 => 'FrameRate', + 0x00000022 => 'FrameWidth', + 0x00000023 => 'FrameHeight', + 0x00000032 => 'AudioChannels', + 0x00000033 => 'AudioBitsPerSample', + 0x00000034 => 'AudioSampleRate', + 0x02000001 => 'MakerNoteVersion', + 0x02000005 => 'WhiteBalance', + 0x0200000b => 'WhiteBalanceFineTune', + 0x0200001e => 'ColorSpace', + 0x02000023 => 'PictureControlData', + 0x02000024 => 'WorldTime', + 0x02000032 => 'UnknownInfo', + 0x02000083 => 'LensType', + 0x02000084 => 'Lens', + ); + + $offset = 0; + $data = null; + $datalength = strlen($atom_data); + $parsed = array(); + while ($offset < $datalength) { + $record_type = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 4)); $offset += 4; + $data_size_type = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 2)); $offset += 2; + $data_size = getid3_lib::BigEndian2Int(substr($atom_data, $offset, 2)); $offset += 2; + switch ($data_size_type) { + case 0x0001: // 0x0001 = flag (size field *= 1-byte) + $data = getid3_lib::BigEndian2Int(substr($atom_data, $offset, $data_size * 1)); + $offset += ($data_size * 1); + break; + case 0x0002: // 0x0002 = char (size field *= 1-byte) + $data = substr($atom_data, $offset, $data_size * 1); + $offset += ($data_size * 1); + $data = rtrim($data, "\x00"); + break; + case 0x0003: // 0x0003 = DWORD+ (size field *= 2-byte), values are stored CDAB + $data = ''; + for ($i = $data_size - 1; $i >= 0; $i--) { + $data .= substr($atom_data, $offset + ($i * 2), 2); + } + $data = getid3_lib::BigEndian2Int($data); + $offset += ($data_size * 2); + break; + case 0x0004: // 0x0004 = QWORD+ (size field *= 4-byte), values are stored EFGHABCD + $data = ''; + for ($i = $data_size - 1; $i >= 0; $i--) { + $data .= substr($atom_data, $offset + ($i * 4), 4); + } + $data = getid3_lib::BigEndian2Int($data); + $offset += ($data_size * 4); + break; + case 0x0005: // 0x0005 = float (size field *= 8-byte), values are stored aaaabbbb where value is aaaa/bbbb; possibly multiple sets of values appended together + $data = array(); + for ($i = 0; $i < $data_size; $i++) { + $numerator = getid3_lib::BigEndian2Int(substr($atom_data, $offset + ($i * 8) + 0, 4)); + $denomninator = getid3_lib::BigEndian2Int(substr($atom_data, $offset + ($i * 8) + 4, 4)); + if ($denomninator == 0) { + $data[$i] = false; + } else { + $data[$i] = (double) $numerator / $denomninator; + } + } + $offset += (8 * $data_size); + if (count($data) == 1) { + $data = $data[0]; + } + break; + case 0x0007: // 0x0007 = bytes (size field *= 1-byte), values are stored as ?????? + $data = substr($atom_data, $offset, $data_size * 1); + $offset += ($data_size * 1); + break; + case 0x0008: // 0x0008 = ????? (size field *= 2-byte), values are stored as ?????? + $data = substr($atom_data, $offset, $data_size * 2); + $offset += ($data_size * 2); + break; + default: + echo 'QuicktimeParseNikonNCTG()::unknown $data_size_type: '.$data_size_type.'
    '; + break 2; + } + + switch ($record_type) { + case 0x00000011: // CreateDate + case 0x00000012: // DateTimeOriginal + $data = strtotime($data); + break; + case 0x0200001e: // ColorSpace + switch ($data) { + case 1: + $data = 'sRGB'; + break; + case 2: + $data = 'Adobe RGB'; + break; + } + break; + case 0x02000023: // PictureControlData + $PictureControlAdjust = array(0=>'default', 1=>'quick', 2=>'full'); + $FilterEffect = array(0x80=>'off', 0x81=>'yellow', 0x82=>'orange', 0x83=>'red', 0x84=>'green', 0xff=>'n/a'); + $ToningEffect = array(0x80=>'b&w', 0x81=>'sepia', 0x82=>'cyanotype', 0x83=>'red', 0x84=>'yellow', 0x85=>'green', 0x86=>'blue-green', 0x87=>'blue', 0x88=>'purple-blue', 0x89=>'red-purple', 0xff=>'n/a'); + $data = array( + 'PictureControlVersion' => substr($data, 0, 4), + 'PictureControlName' => rtrim(substr($data, 4, 20), "\x00"), + 'PictureControlBase' => rtrim(substr($data, 24, 20), "\x00"), + //'?' => substr($data, 44, 4), + 'PictureControlAdjust' => $PictureControlAdjust[ord(substr($data, 48, 1))], + 'PictureControlQuickAdjust' => ord(substr($data, 49, 1)), + 'Sharpness' => ord(substr($data, 50, 1)), + 'Contrast' => ord(substr($data, 51, 1)), + 'Brightness' => ord(substr($data, 52, 1)), + 'Saturation' => ord(substr($data, 53, 1)), + 'HueAdjustment' => ord(substr($data, 54, 1)), + 'FilterEffect' => $FilterEffect[ord(substr($data, 55, 1))], + 'ToningEffect' => $ToningEffect[ord(substr($data, 56, 1))], + 'ToningSaturation' => ord(substr($data, 57, 1)), + ); + break; + case 0x02000024: // WorldTime + // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html#WorldTime + // timezone is stored as offset from GMT in minutes + $timezone = getid3_lib::BigEndian2Int(substr($data, 0, 2)); + if ($timezone & 0x8000) { + $timezone = 0 - (0x10000 - $timezone); + } + $timezone /= 60; + + $dst = (bool) getid3_lib::BigEndian2Int(substr($data, 2, 1)); + switch (getid3_lib::BigEndian2Int(substr($data, 3, 1))) { + case 2: + $datedisplayformat = 'D/M/Y'; break; + case 1: + $datedisplayformat = 'M/D/Y'; break; + case 0: + default: + $datedisplayformat = 'Y/M/D'; break; + } + + $data = array('timezone'=>floatval($timezone), 'dst'=>$dst, 'display'=>$datedisplayformat); + break; + case 0x02000083: // LensType + $data = array( + //'_' => $data, + 'mf' => (bool) ($data & 0x01), + 'd' => (bool) ($data & 0x02), + 'g' => (bool) ($data & 0x04), + 'vr' => (bool) ($data & 0x08), + ); + break; + } + $tag_name = (isset($NCTGtagName[$record_type]) ? $NCTGtagName[$record_type] : '0x'.str_pad(dechex($record_type), 8, '0', STR_PAD_LEFT)); + $parsed[$tag_name] = $data; + } + return $parsed; + } + + /** + * @param string $keyname + * @param string|array $data + * @param string $boxname + * + * @return bool + */ + public function CopyToAppropriateCommentsSection($keyname, $data, $boxname='') { + static $handyatomtranslatorarray = array(); + if (empty($handyatomtranslatorarray)) { + // http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt + // http://www.geocities.com/xhelmboyx/quicktime/formats/mp4-layout.txt + // http://atomicparsley.sourceforge.net/mpeg-4files.html + // https://code.google.com/p/mp4v2/wiki/iTunesMetadata + $handyatomtranslatorarray["\xA9".'alb'] = 'album'; // iTunes 4.0 + $handyatomtranslatorarray["\xA9".'ART'] = 'artist'; + $handyatomtranslatorarray["\xA9".'art'] = 'artist'; // iTunes 4.0 + $handyatomtranslatorarray["\xA9".'aut'] = 'author'; + $handyatomtranslatorarray["\xA9".'cmt'] = 'comment'; // iTunes 4.0 + $handyatomtranslatorarray["\xA9".'com'] = 'comment'; + $handyatomtranslatorarray["\xA9".'cpy'] = 'copyright'; + $handyatomtranslatorarray["\xA9".'day'] = 'creation_date'; // iTunes 4.0 + $handyatomtranslatorarray["\xA9".'dir'] = 'director'; + $handyatomtranslatorarray["\xA9".'ed1'] = 'edit1'; + $handyatomtranslatorarray["\xA9".'ed2'] = 'edit2'; + $handyatomtranslatorarray["\xA9".'ed3'] = 'edit3'; + $handyatomtranslatorarray["\xA9".'ed4'] = 'edit4'; + $handyatomtranslatorarray["\xA9".'ed5'] = 'edit5'; + $handyatomtranslatorarray["\xA9".'ed6'] = 'edit6'; + $handyatomtranslatorarray["\xA9".'ed7'] = 'edit7'; + $handyatomtranslatorarray["\xA9".'ed8'] = 'edit8'; + $handyatomtranslatorarray["\xA9".'ed9'] = 'edit9'; + $handyatomtranslatorarray["\xA9".'enc'] = 'encoded_by'; + $handyatomtranslatorarray["\xA9".'fmt'] = 'format'; + $handyatomtranslatorarray["\xA9".'gen'] = 'genre'; // iTunes 4.0 + $handyatomtranslatorarray["\xA9".'grp'] = 'grouping'; // iTunes 4.2 + $handyatomtranslatorarray["\xA9".'hst'] = 'host_computer'; + $handyatomtranslatorarray["\xA9".'inf'] = 'information'; + $handyatomtranslatorarray["\xA9".'lyr'] = 'lyrics'; // iTunes 5.0 + $handyatomtranslatorarray["\xA9".'mak'] = 'make'; + $handyatomtranslatorarray["\xA9".'mod'] = 'model'; + $handyatomtranslatorarray["\xA9".'nam'] = 'title'; // iTunes 4.0 + $handyatomtranslatorarray["\xA9".'ope'] = 'composer'; + $handyatomtranslatorarray["\xA9".'prd'] = 'producer'; + $handyatomtranslatorarray["\xA9".'PRD'] = 'product'; + $handyatomtranslatorarray["\xA9".'prf'] = 'performers'; + $handyatomtranslatorarray["\xA9".'req'] = 'system_requirements'; + $handyatomtranslatorarray["\xA9".'src'] = 'source_credit'; + $handyatomtranslatorarray["\xA9".'swr'] = 'software'; + $handyatomtranslatorarray["\xA9".'too'] = 'encoding_tool'; // iTunes 4.0 + $handyatomtranslatorarray["\xA9".'trk'] = 'track_number'; + $handyatomtranslatorarray["\xA9".'url'] = 'url'; + $handyatomtranslatorarray["\xA9".'wrn'] = 'warning'; + $handyatomtranslatorarray["\xA9".'wrt'] = 'composer'; + $handyatomtranslatorarray['aART'] = 'album_artist'; + $handyatomtranslatorarray['apID'] = 'purchase_account'; + $handyatomtranslatorarray['catg'] = 'category'; // iTunes 4.9 + $handyatomtranslatorarray['covr'] = 'picture'; // iTunes 4.0 + $handyatomtranslatorarray['cpil'] = 'compilation'; // iTunes 4.0 + $handyatomtranslatorarray['cprt'] = 'copyright'; // iTunes 4.0? + $handyatomtranslatorarray['desc'] = 'description'; // iTunes 5.0 + $handyatomtranslatorarray['disk'] = 'disc_number'; // iTunes 4.0 + $handyatomtranslatorarray['egid'] = 'episode_guid'; // iTunes 4.9 + $handyatomtranslatorarray['gnre'] = 'genre'; // iTunes 4.0 + $handyatomtranslatorarray['hdvd'] = 'hd_video'; // iTunes 4.0 + $handyatomtranslatorarray['ldes'] = 'description_long'; // + $handyatomtranslatorarray['keyw'] = 'keyword'; // iTunes 4.9 + $handyatomtranslatorarray['pcst'] = 'podcast'; // iTunes 4.9 + $handyatomtranslatorarray['pgap'] = 'gapless_playback'; // iTunes 7.0 + $handyatomtranslatorarray['purd'] = 'purchase_date'; // iTunes 6.0.2 + $handyatomtranslatorarray['purl'] = 'podcast_url'; // iTunes 4.9 + $handyatomtranslatorarray['rtng'] = 'rating'; // iTunes 4.0 + $handyatomtranslatorarray['soaa'] = 'sort_album_artist'; // + $handyatomtranslatorarray['soal'] = 'sort_album'; // + $handyatomtranslatorarray['soar'] = 'sort_artist'; // + $handyatomtranslatorarray['soco'] = 'sort_composer'; // + $handyatomtranslatorarray['sonm'] = 'sort_title'; // + $handyatomtranslatorarray['sosn'] = 'sort_show'; // + $handyatomtranslatorarray['stik'] = 'stik'; // iTunes 4.9 + $handyatomtranslatorarray['tmpo'] = 'bpm'; // iTunes 4.0 + $handyatomtranslatorarray['trkn'] = 'track_number'; // iTunes 4.0 + $handyatomtranslatorarray['tven'] = 'tv_episode_id'; // + $handyatomtranslatorarray['tves'] = 'tv_episode'; // iTunes 6.0 + $handyatomtranslatorarray['tvnn'] = 'tv_network_name'; // iTunes 6.0 + $handyatomtranslatorarray['tvsh'] = 'tv_show_name'; // iTunes 6.0 + $handyatomtranslatorarray['tvsn'] = 'tv_season'; // iTunes 6.0 + + // boxnames: + /* + $handyatomtranslatorarray['iTunSMPB'] = 'iTunSMPB'; + $handyatomtranslatorarray['iTunNORM'] = 'iTunNORM'; + $handyatomtranslatorarray['Encoding Params'] = 'Encoding Params'; + $handyatomtranslatorarray['replaygain_track_gain'] = 'replaygain_track_gain'; + $handyatomtranslatorarray['replaygain_track_peak'] = 'replaygain_track_peak'; + $handyatomtranslatorarray['replaygain_track_minmax'] = 'replaygain_track_minmax'; + $handyatomtranslatorarray['MusicIP PUID'] = 'MusicIP PUID'; + $handyatomtranslatorarray['MusicBrainz Artist Id'] = 'MusicBrainz Artist Id'; + $handyatomtranslatorarray['MusicBrainz Album Id'] = 'MusicBrainz Album Id'; + $handyatomtranslatorarray['MusicBrainz Album Artist Id'] = 'MusicBrainz Album Artist Id'; + $handyatomtranslatorarray['MusicBrainz Track Id'] = 'MusicBrainz Track Id'; + $handyatomtranslatorarray['MusicBrainz Disc Id'] = 'MusicBrainz Disc Id'; + + // http://age.hobba.nl/audio/tag_frame_reference.html + $handyatomtranslatorarray['PLAY_COUNTER'] = 'play_counter'; // Foobar2000 - https://www.getid3.org/phpBB3/viewtopic.php?t=1355 + $handyatomtranslatorarray['MEDIATYPE'] = 'mediatype'; // Foobar2000 - https://www.getid3.org/phpBB3/viewtopic.php?t=1355 + */ + } + $info = &$this->getid3->info; + $comment_key = ''; + if ($boxname && ($boxname != $keyname)) { + $comment_key = (isset($handyatomtranslatorarray[$boxname]) ? $handyatomtranslatorarray[$boxname] : $boxname); + } elseif (isset($handyatomtranslatorarray[$keyname])) { + $comment_key = $handyatomtranslatorarray[$keyname]; + } + if ($comment_key) { + if ($comment_key == 'picture') { + // already copied directly into [comments][picture] elsewhere, do not re-copy here + return true; + } + $gooddata = array($data); + if ($comment_key == 'genre') { + // some other taggers separate multiple genres with semicolon, e.g. "Heavy Metal;Thrash Metal;Metal" + $gooddata = explode(';', $data); + } + foreach ($gooddata as $data) { + if (!empty($info['quicktime']['comments'][$comment_key]) && in_array($data, $info['quicktime']['comments'][$comment_key], true)) { + // avoid duplicate copies of identical data + continue; + } + $info['quicktime']['comments'][$comment_key][] = $data; + } + } + return true; + } + + /** + * @param string $lstring + * @param int $count + * + * @return string + */ + public function LociString($lstring, &$count) { + // Loci strings are UTF-8 or UTF-16 and null (x00/x0000) terminated. UTF-16 has a BOM + // Also need to return the number of bytes the string occupied so additional fields can be extracted + $len = strlen($lstring); + if ($len == 0) { + $count = 0; + return ''; + } + if ($lstring[0] == "\x00") { + $count = 1; + return ''; + } + // check for BOM + if (($len > 2) && ((($lstring[0] == "\xFE") && ($lstring[1] == "\xFF")) || (($lstring[0] == "\xFF") && ($lstring[1] == "\xFE")))) { + // UTF-16 + if (preg_match('/(.*)\x00/', $lstring, $lmatches)) { + $count = strlen($lmatches[1]) * 2 + 2; //account for 2 byte characters and trailing \x0000 + return getid3_lib::iconv_fallback_utf16_utf8($lmatches[1]); + } else { + return ''; + } + } + // UTF-8 + if (preg_match('/(.*)\x00/', $lstring, $lmatches)) { + $count = strlen($lmatches[1]) + 1; //account for trailing \x00 + return $lmatches[1]; + } + return ''; + } + + /** + * @param string $nullterminatedstring + * + * @return string + */ + public function NoNullString($nullterminatedstring) { + // remove the single null terminator on null terminated strings + if (substr($nullterminatedstring, strlen($nullterminatedstring) - 1, 1) === "\x00") { + return substr($nullterminatedstring, 0, strlen($nullterminatedstring) - 1); + } + return $nullterminatedstring; + } + + /** + * @param string $pascalstring + * + * @return string + */ + public function Pascal2String($pascalstring) { + // Pascal strings have 1 unsigned byte at the beginning saying how many chars (1-255) are in the string + return substr($pascalstring, 1); + } + + /** + * @param string $pascalstring + * + * @return string + */ + public function MaybePascal2String($pascalstring) { + // Pascal strings have 1 unsigned byte at the beginning saying how many chars (1-255) are in the string + // Check if string actually is in this format or written incorrectly, straight string, or null-terminated string + if (ord(substr($pascalstring, 0, 1)) == (strlen($pascalstring) - 1)) { + return substr($pascalstring, 1); + } elseif (substr($pascalstring, -1, 1) == "\x00") { + // appears to be null-terminated instead of Pascal-style + return substr($pascalstring, 0, -1); + } + return $pascalstring; + } + + + /** + * Helper functions for m4b audiobook chapters + * code by Steffen Hartmann 2015-Nov-08. + * + * @param array $info + * @param string $tag + * @param string $history + * @param array $result + */ + public function search_tag_by_key($info, $tag, $history, &$result) { + foreach ($info as $key => $value) { + $key_history = $history.'/'.$key; + if ($key === $tag) { + $result[] = array($key_history, $info); + } else { + if (is_array($value)) { + $this->search_tag_by_key($value, $tag, $key_history, $result); + } + } + } + } + + /** + * @param array $info + * @param string $k + * @param string $v + * @param string $history + * @param array $result + */ + public function search_tag_by_pair($info, $k, $v, $history, &$result) { + foreach ($info as $key => $value) { + $key_history = $history.'/'.$key; + if (($key === $k) && ($value === $v)) { + $result[] = array($key_history, $info); + } else { + if (is_array($value)) { + $this->search_tag_by_pair($value, $k, $v, $key_history, $result); + } + } + } + } + + /** + * @param array $info + * + * @return array + */ + public function quicktime_time_to_sample_table($info) { + $res = array(); + $this->search_tag_by_pair($info['quicktime']['moov'], 'name', 'stbl', 'quicktime/moov', $res); + foreach ($res as $value) { + $stbl_res = array(); + $this->search_tag_by_pair($value[1], 'data_format', 'text', $value[0], $stbl_res); + if (count($stbl_res) > 0) { + $stts_res = array(); + $this->search_tag_by_key($value[1], 'time_to_sample_table', $value[0], $stts_res); + if (count($stts_res) > 0) { + return $stts_res[0][1]['time_to_sample_table']; + } + } + } + return array(); + } + + /** + * @param array $info + * + * @return int + */ + public function quicktime_bookmark_time_scale($info) { + $time_scale = ''; + $ts_prefix_len = 0; + $res = array(); + $this->search_tag_by_pair($info['quicktime']['moov'], 'name', 'stbl', 'quicktime/moov', $res); + foreach ($res as $value) { + $stbl_res = array(); + $this->search_tag_by_pair($value[1], 'data_format', 'text', $value[0], $stbl_res); + if (count($stbl_res) > 0) { + $ts_res = array(); + $this->search_tag_by_key($info['quicktime']['moov'], 'time_scale', 'quicktime/moov', $ts_res); + foreach ($ts_res as $sub_value) { + $prefix = substr($sub_value[0], 0, -12); + if ((substr($stbl_res[0][0], 0, strlen($prefix)) === $prefix) && ($ts_prefix_len < strlen($prefix))) { + $time_scale = $sub_value[1]['time_scale']; + $ts_prefix_len = strlen($prefix); + } + } + } + } + return $time_scale; + } + /* + // END helper functions for m4b audiobook chapters + */ + + +} diff --git a/projects/tests/tests/performance/ID3/module.audio-video.riff.php b/projects/tests/tests/performance/ID3/module.audio-video.riff.php new file mode 100644 index 00000000..cdf55338 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio-video.riff.php @@ -0,0 +1,2791 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio-video.riff.php // +// module for analyzing RIFF files // +// multiple formats supported by this module: // +// Wave, AVI, AIFF/AIFC, (MP3,AC3)/RIFF, Wavpack v3, 8SVX // +// dependencies: module.audio.mp3.php // +// module.audio.ac3.php // +// module.audio.dts.php // +// /// +///////////////////////////////////////////////////////////////// + +/** +* @todo Parse AC-3/DTS audio inside WAVE correctly +* @todo Rewrite RIFF parser totally +*/ + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.mp3.php', __FILE__, true); +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ac3.php', __FILE__, true); +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.dts.php', __FILE__, true); + +class getid3_riff extends getid3_handler +{ + protected $container = 'riff'; // default + + /** + * @return bool + * + * @throws getid3_exception + */ + public function Analyze() { + $info = &$this->getid3->info; + + // initialize these values to an empty array, otherwise they default to NULL + // and you can't append array values to a NULL value + $info['riff'] = array('raw'=>array()); + + // Shortcuts + $thisfile_riff = &$info['riff']; + $thisfile_riff_raw = &$thisfile_riff['raw']; + $thisfile_audio = &$info['audio']; + $thisfile_video = &$info['video']; + $thisfile_audio_dataformat = &$thisfile_audio['dataformat']; + $thisfile_riff_audio = &$thisfile_riff['audio']; + $thisfile_riff_video = &$thisfile_riff['video']; + $thisfile_riff_WAVE = array(); + + $Original['avdataoffset'] = $info['avdataoffset']; + $Original['avdataend'] = $info['avdataend']; + + $this->fseek($info['avdataoffset']); + $RIFFheader = $this->fread(12); + $offset = $this->ftell(); + $RIFFtype = substr($RIFFheader, 0, 4); + $RIFFsize = substr($RIFFheader, 4, 4); + $RIFFsubtype = substr($RIFFheader, 8, 4); + + switch ($RIFFtype) { + + case 'FORM': // AIFF, AIFC + //$info['fileformat'] = 'aiff'; + $this->container = 'aiff'; + $thisfile_riff['header_size'] = $this->EitherEndian2Int($RIFFsize); + $thisfile_riff[$RIFFsubtype] = $this->ParseRIFF($offset, ($offset + $thisfile_riff['header_size'] - 4)); + break; + + case 'RIFF': // AVI, WAV, etc + case 'SDSS': // SDSS is identical to RIFF, just renamed. Used by SmartSound QuickTracks (www.smartsound.com) + case 'RMP3': // RMP3 is identical to RIFF, just renamed. Used by [unknown program] when creating RIFF-MP3s + //$info['fileformat'] = 'riff'; + $this->container = 'riff'; + $thisfile_riff['header_size'] = $this->EitherEndian2Int($RIFFsize); + if ($RIFFsubtype == 'RMP3') { + // RMP3 is identical to WAVE, just renamed. Used by [unknown program] when creating RIFF-MP3s + $RIFFsubtype = 'WAVE'; + } + if ($RIFFsubtype != 'AMV ') { + // AMV files are RIFF-AVI files with parts of the spec deliberately broken, such as chunk size fields hardcoded to zero (because players known in hardware that these fields are always a certain size + // Handled separately in ParseRIFFAMV() + $thisfile_riff[$RIFFsubtype] = $this->ParseRIFF($offset, ($offset + $thisfile_riff['header_size'] - 4)); + } + if (($info['avdataend'] - $info['filesize']) == 1) { + // LiteWave appears to incorrectly *not* pad actual output file + // to nearest WORD boundary so may appear to be short by one + // byte, in which case - skip warning + $info['avdataend'] = $info['filesize']; + } + + $nextRIFFoffset = $Original['avdataoffset'] + 8 + $thisfile_riff['header_size']; // 8 = "RIFF" + 32-bit offset + while ($nextRIFFoffset < min($info['filesize'], $info['avdataend'])) { + try { + $this->fseek($nextRIFFoffset); + } catch (getid3_exception $e) { + if ($e->getCode() == 10) { + //$this->warning('RIFF parser: '.$e->getMessage()); + $this->error('AVI extends beyond '.round(PHP_INT_MAX / 1073741824).'GB and PHP filesystem functions cannot read that far, playtime may be wrong'); + $this->warning('[avdataend] value may be incorrect, multiple AVIX chunks may be present'); + break; + } else { + throw $e; + } + } + $nextRIFFheader = $this->fread(12); + if ($nextRIFFoffset == ($info['avdataend'] - 1)) { + if (substr($nextRIFFheader, 0, 1) == "\x00") { + // RIFF padded to WORD boundary, we're actually already at the end + break; + } + } + $nextRIFFheaderID = substr($nextRIFFheader, 0, 4); + $nextRIFFsize = $this->EitherEndian2Int(substr($nextRIFFheader, 4, 4)); + $nextRIFFtype = substr($nextRIFFheader, 8, 4); + $chunkdata = array(); + $chunkdata['offset'] = $nextRIFFoffset + 8; + $chunkdata['size'] = $nextRIFFsize; + $nextRIFFoffset = $chunkdata['offset'] + $chunkdata['size']; + + switch ($nextRIFFheaderID) { + case 'RIFF': + $chunkdata['chunks'] = $this->ParseRIFF($chunkdata['offset'] + 4, $nextRIFFoffset); + if (!isset($thisfile_riff[$nextRIFFtype])) { + $thisfile_riff[$nextRIFFtype] = array(); + } + $thisfile_riff[$nextRIFFtype][] = $chunkdata; + break; + + case 'AMV ': + unset($info['riff']); + $info['amv'] = $this->ParseRIFFAMV($chunkdata['offset'] + 4, $nextRIFFoffset); + break; + + case 'JUNK': + // ignore + $thisfile_riff[$nextRIFFheaderID][] = $chunkdata; + break; + + case 'IDVX': + $info['divxtag']['comments'] = self::ParseDIVXTAG($this->fread($chunkdata['size'])); + break; + + default: + if ($info['filesize'] == ($chunkdata['offset'] - 8 + 128)) { + $DIVXTAG = $nextRIFFheader.$this->fread(128 - 12); + if (substr($DIVXTAG, -7) == 'DIVXTAG') { + // DIVXTAG is supposed to be inside an IDVX chunk in a LIST chunk, but some bad encoders just slap it on the end of a file + $this->warning('Found wrongly-structured DIVXTAG at offset '.($this->ftell() - 128).', parsing anyway'); + $info['divxtag']['comments'] = self::ParseDIVXTAG($DIVXTAG); + break 2; + } + } + $this->warning('Expecting "RIFF|JUNK|IDVX" at '.$nextRIFFoffset.', found "'.$nextRIFFheaderID.'" ('.getid3_lib::PrintHexBytes($nextRIFFheaderID).') - skipping rest of file'); + break 2; + + } + + } + if ($RIFFsubtype == 'WAVE') { + $thisfile_riff_WAVE = &$thisfile_riff['WAVE']; + } + break; + + default: + $this->error('Cannot parse RIFF (this is maybe not a RIFF / WAV / AVI file?) - expecting "FORM|RIFF|SDSS|RMP3" found "'.$RIFFsubtype.'" instead'); + //unset($info['fileformat']); + return false; + } + + $streamindex = 0; + switch ($RIFFsubtype) { + + // http://en.wikipedia.org/wiki/Wav + case 'WAVE': + $info['fileformat'] = 'wav'; + + if (empty($thisfile_audio['bitrate_mode'])) { + $thisfile_audio['bitrate_mode'] = 'cbr'; + } + if (empty($thisfile_audio_dataformat)) { + $thisfile_audio_dataformat = 'wav'; + } + + if (isset($thisfile_riff_WAVE['data'][0]['offset'])) { + $info['avdataoffset'] = $thisfile_riff_WAVE['data'][0]['offset'] + 8; + $info['avdataend'] = $info['avdataoffset'] + $thisfile_riff_WAVE['data'][0]['size']; + } + if (isset($thisfile_riff_WAVE['fmt '][0]['data'])) { + + $thisfile_riff_audio[$streamindex] = self::parseWAVEFORMATex($thisfile_riff_WAVE['fmt '][0]['data']); + $thisfile_audio['wformattag'] = $thisfile_riff_audio[$streamindex]['raw']['wFormatTag']; + if (!isset($thisfile_riff_audio[$streamindex]['bitrate']) || ($thisfile_riff_audio[$streamindex]['bitrate'] == 0)) { + $this->error('Corrupt RIFF file: bitrate_audio == zero'); + return false; + } + $thisfile_riff_raw['fmt '] = $thisfile_riff_audio[$streamindex]['raw']; + unset($thisfile_riff_audio[$streamindex]['raw']); + $thisfile_audio['streams'][$streamindex] = $thisfile_riff_audio[$streamindex]; + + $thisfile_audio = (array) getid3_lib::array_merge_noclobber($thisfile_audio, $thisfile_riff_audio[$streamindex]); + if (substr($thisfile_audio['codec'], 0, strlen('unknown: 0x')) == 'unknown: 0x') { + $this->warning('Audio codec = '.$thisfile_audio['codec']); + } + $thisfile_audio['bitrate'] = $thisfile_riff_audio[$streamindex]['bitrate']; + + if (empty($info['playtime_seconds'])) { // may already be set (e.g. DTS-WAV) + $info['playtime_seconds'] = (float) ((($info['avdataend'] - $info['avdataoffset']) * 8) / $thisfile_audio['bitrate']); + } + + $thisfile_audio['lossless'] = false; + if (isset($thisfile_riff_WAVE['data'][0]['offset']) && isset($thisfile_riff_raw['fmt ']['wFormatTag'])) { + switch ($thisfile_riff_raw['fmt ']['wFormatTag']) { + + case 0x0001: // PCM + $thisfile_audio['lossless'] = true; + break; + + case 0x2000: // AC-3 + $thisfile_audio_dataformat = 'ac3'; + break; + + default: + // do nothing + break; + + } + } + $thisfile_audio['streams'][$streamindex]['wformattag'] = $thisfile_audio['wformattag']; + $thisfile_audio['streams'][$streamindex]['bitrate_mode'] = $thisfile_audio['bitrate_mode']; + $thisfile_audio['streams'][$streamindex]['lossless'] = $thisfile_audio['lossless']; + $thisfile_audio['streams'][$streamindex]['dataformat'] = $thisfile_audio_dataformat; + } + + if (isset($thisfile_riff_WAVE['rgad'][0]['data'])) { + + // shortcuts + $rgadData = &$thisfile_riff_WAVE['rgad'][0]['data']; + $thisfile_riff_raw['rgad'] = array('track'=>array(), 'album'=>array()); + $thisfile_riff_raw_rgad = &$thisfile_riff_raw['rgad']; + $thisfile_riff_raw_rgad_track = &$thisfile_riff_raw_rgad['track']; + $thisfile_riff_raw_rgad_album = &$thisfile_riff_raw_rgad['album']; + + $thisfile_riff_raw_rgad['fPeakAmplitude'] = getid3_lib::LittleEndian2Float(substr($rgadData, 0, 4)); + $thisfile_riff_raw_rgad['nRadioRgAdjust'] = $this->EitherEndian2Int(substr($rgadData, 4, 2)); + $thisfile_riff_raw_rgad['nAudiophileRgAdjust'] = $this->EitherEndian2Int(substr($rgadData, 6, 2)); + + $nRadioRgAdjustBitstring = str_pad(getid3_lib::Dec2Bin($thisfile_riff_raw_rgad['nRadioRgAdjust']), 16, '0', STR_PAD_LEFT); + $nAudiophileRgAdjustBitstring = str_pad(getid3_lib::Dec2Bin($thisfile_riff_raw_rgad['nAudiophileRgAdjust']), 16, '0', STR_PAD_LEFT); + $thisfile_riff_raw_rgad_track['name'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 0, 3)); + $thisfile_riff_raw_rgad_track['originator'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 3, 3)); + $thisfile_riff_raw_rgad_track['signbit'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 6, 1)); + $thisfile_riff_raw_rgad_track['adjustment'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 7, 9)); + $thisfile_riff_raw_rgad_album['name'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 0, 3)); + $thisfile_riff_raw_rgad_album['originator'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 3, 3)); + $thisfile_riff_raw_rgad_album['signbit'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 6, 1)); + $thisfile_riff_raw_rgad_album['adjustment'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 7, 9)); + + $thisfile_riff['rgad']['peakamplitude'] = $thisfile_riff_raw_rgad['fPeakAmplitude']; + if (($thisfile_riff_raw_rgad_track['name'] != 0) && ($thisfile_riff_raw_rgad_track['originator'] != 0)) { + $thisfile_riff['rgad']['track']['name'] = getid3_lib::RGADnameLookup($thisfile_riff_raw_rgad_track['name']); + $thisfile_riff['rgad']['track']['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_riff_raw_rgad_track['originator']); + $thisfile_riff['rgad']['track']['adjustment'] = getid3_lib::RGADadjustmentLookup($thisfile_riff_raw_rgad_track['adjustment'], $thisfile_riff_raw_rgad_track['signbit']); + } + if (($thisfile_riff_raw_rgad_album['name'] != 0) && ($thisfile_riff_raw_rgad_album['originator'] != 0)) { + $thisfile_riff['rgad']['album']['name'] = getid3_lib::RGADnameLookup($thisfile_riff_raw_rgad_album['name']); + $thisfile_riff['rgad']['album']['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_riff_raw_rgad_album['originator']); + $thisfile_riff['rgad']['album']['adjustment'] = getid3_lib::RGADadjustmentLookup($thisfile_riff_raw_rgad_album['adjustment'], $thisfile_riff_raw_rgad_album['signbit']); + } + } + + if (isset($thisfile_riff_WAVE['fact'][0]['data'])) { + $thisfile_riff_raw['fact']['NumberOfSamples'] = $this->EitherEndian2Int(substr($thisfile_riff_WAVE['fact'][0]['data'], 0, 4)); + + // This should be a good way of calculating exact playtime, + // but some sample files have had incorrect number of samples, + // so cannot use this method + + // if (!empty($thisfile_riff_raw['fmt ']['nSamplesPerSec'])) { + // $info['playtime_seconds'] = (float) $thisfile_riff_raw['fact']['NumberOfSamples'] / $thisfile_riff_raw['fmt ']['nSamplesPerSec']; + // } + } + if (!empty($thisfile_riff_raw['fmt ']['nAvgBytesPerSec'])) { + $thisfile_audio['bitrate'] = getid3_lib::CastAsInt($thisfile_riff_raw['fmt ']['nAvgBytesPerSec'] * 8); + } + + if (isset($thisfile_riff_WAVE['bext'][0]['data'])) { + // shortcut + $thisfile_riff_WAVE_bext_0 = &$thisfile_riff_WAVE['bext'][0]; + + $thisfile_riff_WAVE_bext_0['title'] = trim(substr($thisfile_riff_WAVE_bext_0['data'], 0, 256)); + $thisfile_riff_WAVE_bext_0['author'] = trim(substr($thisfile_riff_WAVE_bext_0['data'], 256, 32)); + $thisfile_riff_WAVE_bext_0['reference'] = trim(substr($thisfile_riff_WAVE_bext_0['data'], 288, 32)); + $thisfile_riff_WAVE_bext_0['origin_date'] = substr($thisfile_riff_WAVE_bext_0['data'], 320, 10); + $thisfile_riff_WAVE_bext_0['origin_time'] = substr($thisfile_riff_WAVE_bext_0['data'], 330, 8); + $thisfile_riff_WAVE_bext_0['time_reference'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_bext_0['data'], 338, 8)); + $thisfile_riff_WAVE_bext_0['bwf_version'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_bext_0['data'], 346, 1)); + $thisfile_riff_WAVE_bext_0['reserved'] = substr($thisfile_riff_WAVE_bext_0['data'], 347, 254); + $thisfile_riff_WAVE_bext_0['coding_history'] = explode("\r\n", trim(substr($thisfile_riff_WAVE_bext_0['data'], 601))); + if (preg_match('#^([0-9]{4}).([0-9]{2}).([0-9]{2})$#', $thisfile_riff_WAVE_bext_0['origin_date'], $matches_bext_date)) { + if (preg_match('#^([0-9]{2}).([0-9]{2}).([0-9]{2})$#', $thisfile_riff_WAVE_bext_0['origin_time'], $matches_bext_time)) { + list($dummy, $bext_timestamp['year'], $bext_timestamp['month'], $bext_timestamp['day']) = $matches_bext_date; + list($dummy, $bext_timestamp['hour'], $bext_timestamp['minute'], $bext_timestamp['second']) = $matches_bext_time; + $thisfile_riff_WAVE_bext_0['origin_date_unix'] = gmmktime($bext_timestamp['hour'], $bext_timestamp['minute'], $bext_timestamp['second'], $bext_timestamp['month'], $bext_timestamp['day'], $bext_timestamp['year']); + } else { + $this->warning('RIFF.WAVE.BEXT.origin_time is invalid'); + } + } else { + $this->warning('RIFF.WAVE.BEXT.origin_date is invalid'); + } + $thisfile_riff['comments']['author'][] = $thisfile_riff_WAVE_bext_0['author']; + $thisfile_riff['comments']['title'][] = $thisfile_riff_WAVE_bext_0['title']; + } + + if (isset($thisfile_riff_WAVE['MEXT'][0]['data'])) { + // shortcut + $thisfile_riff_WAVE_MEXT_0 = &$thisfile_riff_WAVE['MEXT'][0]; + + $thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 0, 2)); + $thisfile_riff_WAVE_MEXT_0['flags']['homogenous'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0001); + if ($thisfile_riff_WAVE_MEXT_0['flags']['homogenous']) { + $thisfile_riff_WAVE_MEXT_0['flags']['padding'] = ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0002) ? false : true; + $thisfile_riff_WAVE_MEXT_0['flags']['22_or_44'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0004); + $thisfile_riff_WAVE_MEXT_0['flags']['free_format'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0008); + + $thisfile_riff_WAVE_MEXT_0['nominal_frame_size'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 2, 2)); + } + $thisfile_riff_WAVE_MEXT_0['anciliary_data_length'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 6, 2)); + $thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 8, 2)); + $thisfile_riff_WAVE_MEXT_0['flags']['anciliary_data_left'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] & 0x0001); + $thisfile_riff_WAVE_MEXT_0['flags']['anciliary_data_free'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] & 0x0002); + $thisfile_riff_WAVE_MEXT_0['flags']['anciliary_data_right'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] & 0x0004); + } + + if (isset($thisfile_riff_WAVE['cart'][0]['data'])) { + // shortcut + $thisfile_riff_WAVE_cart_0 = &$thisfile_riff_WAVE['cart'][0]; + + $thisfile_riff_WAVE_cart_0['version'] = substr($thisfile_riff_WAVE_cart_0['data'], 0, 4); + $thisfile_riff_WAVE_cart_0['title'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 4, 64)); + $thisfile_riff_WAVE_cart_0['artist'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 68, 64)); + $thisfile_riff_WAVE_cart_0['cut_id'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 132, 64)); + $thisfile_riff_WAVE_cart_0['client_id'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 196, 64)); + $thisfile_riff_WAVE_cart_0['category'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 260, 64)); + $thisfile_riff_WAVE_cart_0['classification'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 324, 64)); + $thisfile_riff_WAVE_cart_0['out_cue'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 388, 64)); + $thisfile_riff_WAVE_cart_0['start_date'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 452, 10)); + $thisfile_riff_WAVE_cart_0['start_time'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 462, 8)); + $thisfile_riff_WAVE_cart_0['end_date'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 470, 10)); + $thisfile_riff_WAVE_cart_0['end_time'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 480, 8)); + $thisfile_riff_WAVE_cart_0['producer_app_id'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 488, 64)); + $thisfile_riff_WAVE_cart_0['producer_app_version'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 552, 64)); + $thisfile_riff_WAVE_cart_0['user_defined_text'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 616, 64)); + $thisfile_riff_WAVE_cart_0['zero_db_reference'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_cart_0['data'], 680, 4), true); + for ($i = 0; $i < 8; $i++) { + $thisfile_riff_WAVE_cart_0['post_time'][$i]['usage_fourcc'] = substr($thisfile_riff_WAVE_cart_0['data'], 684 + ($i * 8), 4); + $thisfile_riff_WAVE_cart_0['post_time'][$i]['timer_value'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_cart_0['data'], 684 + ($i * 8) + 4, 4)); + } + $thisfile_riff_WAVE_cart_0['url'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 748, 1024)); + $thisfile_riff_WAVE_cart_0['tag_text'] = explode("\r\n", trim(substr($thisfile_riff_WAVE_cart_0['data'], 1772))); + $thisfile_riff['comments']['tag_text'][] = substr($thisfile_riff_WAVE_cart_0['data'], 1772); + + $thisfile_riff['comments']['artist'][] = $thisfile_riff_WAVE_cart_0['artist']; + $thisfile_riff['comments']['title'][] = $thisfile_riff_WAVE_cart_0['title']; + } + + if (isset($thisfile_riff_WAVE['SNDM'][0]['data'])) { + // SoundMiner metadata + + // shortcuts + $thisfile_riff_WAVE_SNDM_0 = &$thisfile_riff_WAVE['SNDM'][0]; + $thisfile_riff_WAVE_SNDM_0_data = &$thisfile_riff_WAVE_SNDM_0['data']; + $SNDM_startoffset = 0; + $SNDM_endoffset = $thisfile_riff_WAVE_SNDM_0['size']; + + while ($SNDM_startoffset < $SNDM_endoffset) { + $SNDM_thisTagOffset = 0; + $SNDM_thisTagSize = getid3_lib::BigEndian2Int(substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 4)); + $SNDM_thisTagOffset += 4; + $SNDM_thisTagKey = substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 4); + $SNDM_thisTagOffset += 4; + $SNDM_thisTagDataSize = getid3_lib::BigEndian2Int(substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 2)); + $SNDM_thisTagOffset += 2; + $SNDM_thisTagDataFlags = getid3_lib::BigEndian2Int(substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 2)); + $SNDM_thisTagOffset += 2; + $SNDM_thisTagDataText = substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, $SNDM_thisTagDataSize); + $SNDM_thisTagOffset += $SNDM_thisTagDataSize; + + if ($SNDM_thisTagSize != (4 + 4 + 2 + 2 + $SNDM_thisTagDataSize)) { + $this->warning('RIFF.WAVE.SNDM.data contains tag not expected length (expected: '.$SNDM_thisTagSize.', found: '.(4 + 4 + 2 + 2 + $SNDM_thisTagDataSize).') at offset '.$SNDM_startoffset.' (file offset '.($thisfile_riff_WAVE_SNDM_0['offset'] + $SNDM_startoffset).')'); + break; + } elseif ($SNDM_thisTagSize <= 0) { + $this->warning('RIFF.WAVE.SNDM.data contains zero-size tag at offset '.$SNDM_startoffset.' (file offset '.($thisfile_riff_WAVE_SNDM_0['offset'] + $SNDM_startoffset).')'); + break; + } + $SNDM_startoffset += $SNDM_thisTagSize; + + $thisfile_riff_WAVE_SNDM_0['parsed_raw'][$SNDM_thisTagKey] = $SNDM_thisTagDataText; + if ($parsedkey = self::waveSNDMtagLookup($SNDM_thisTagKey)) { + $thisfile_riff_WAVE_SNDM_0['parsed'][$parsedkey] = $SNDM_thisTagDataText; + } else { + $this->warning('RIFF.WAVE.SNDM contains unknown tag "'.$SNDM_thisTagKey.'" at offset '.$SNDM_startoffset.' (file offset '.($thisfile_riff_WAVE_SNDM_0['offset'] + $SNDM_startoffset).')'); + } + } + + $tagmapping = array( + 'tracktitle'=>'title', + 'category' =>'genre', + 'cdtitle' =>'album', + ); + foreach ($tagmapping as $fromkey => $tokey) { + if (isset($thisfile_riff_WAVE_SNDM_0['parsed'][$fromkey])) { + $thisfile_riff['comments'][$tokey][] = $thisfile_riff_WAVE_SNDM_0['parsed'][$fromkey]; + } + } + } + + if (isset($thisfile_riff_WAVE['iXML'][0]['data'])) { + // requires functions simplexml_load_string and get_object_vars + if ($parsedXML = getid3_lib::XML2array($thisfile_riff_WAVE['iXML'][0]['data'])) { + $thisfile_riff_WAVE['iXML'][0]['parsed'] = $parsedXML; + if (isset($parsedXML['SPEED']['MASTER_SPEED'])) { + @list($numerator, $denominator) = explode('/', $parsedXML['SPEED']['MASTER_SPEED']); + $thisfile_riff_WAVE['iXML'][0]['master_speed'] = $numerator / ($denominator ? $denominator : 1000); + } + if (isset($parsedXML['SPEED']['TIMECODE_RATE'])) { + @list($numerator, $denominator) = explode('/', $parsedXML['SPEED']['TIMECODE_RATE']); + $thisfile_riff_WAVE['iXML'][0]['timecode_rate'] = $numerator / ($denominator ? $denominator : 1000); + } + if (isset($parsedXML['SPEED']['TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_LO']) && !empty($parsedXML['SPEED']['TIMESTAMP_SAMPLE_RATE']) && !empty($thisfile_riff_WAVE['iXML'][0]['timecode_rate'])) { + $samples_since_midnight = floatval(ltrim($parsedXML['SPEED']['TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_HI'].$parsedXML['SPEED']['TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_LO'], '0')); + $timestamp_sample_rate = (is_array($parsedXML['SPEED']['TIMESTAMP_SAMPLE_RATE']) ? max($parsedXML['SPEED']['TIMESTAMP_SAMPLE_RATE']) : $parsedXML['SPEED']['TIMESTAMP_SAMPLE_RATE']); // XML could possibly contain more than one TIMESTAMP_SAMPLE_RATE tag, returning as array instead of integer [why? does it make sense? perhaps doesn't matter but getID3 needs to deal with it] - see https://github.com/JamesHeinrich/getID3/issues/105 + $thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] = $samples_since_midnight / $timestamp_sample_rate; + $h = floor( $thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] / 3600); + $m = floor(($thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] - ($h * 3600)) / 60); + $s = floor( $thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] - ($h * 3600) - ($m * 60)); + $f = ($thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] - ($h * 3600) - ($m * 60) - $s) * $thisfile_riff_WAVE['iXML'][0]['timecode_rate']; + $thisfile_riff_WAVE['iXML'][0]['timecode_string'] = sprintf('%02d:%02d:%02d:%05.2f', $h, $m, $s, $f); + $thisfile_riff_WAVE['iXML'][0]['timecode_string_round'] = sprintf('%02d:%02d:%02d:%02d', $h, $m, $s, round($f)); + unset($samples_since_midnight, $timestamp_sample_rate, $h, $m, $s, $f); + } + unset($parsedXML); + } + } + + + + if (!isset($thisfile_audio['bitrate']) && isset($thisfile_riff_audio[$streamindex]['bitrate'])) { + $thisfile_audio['bitrate'] = $thisfile_riff_audio[$streamindex]['bitrate']; + $info['playtime_seconds'] = (float) ((($info['avdataend'] - $info['avdataoffset']) * 8) / $thisfile_audio['bitrate']); + } + + if (!empty($info['wavpack'])) { + $thisfile_audio_dataformat = 'wavpack'; + $thisfile_audio['bitrate_mode'] = 'vbr'; + $thisfile_audio['encoder'] = 'WavPack v'.$info['wavpack']['version']; + + // Reset to the way it was - RIFF parsing will have messed this up + $info['avdataend'] = $Original['avdataend']; + $thisfile_audio['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; + + $this->fseek($info['avdataoffset'] - 44); + $RIFFdata = $this->fread(44); + $OrignalRIFFheaderSize = getid3_lib::LittleEndian2Int(substr($RIFFdata, 4, 4)) + 8; + $OrignalRIFFdataSize = getid3_lib::LittleEndian2Int(substr($RIFFdata, 40, 4)) + 44; + + if ($OrignalRIFFheaderSize > $OrignalRIFFdataSize) { + $info['avdataend'] -= ($OrignalRIFFheaderSize - $OrignalRIFFdataSize); + $this->fseek($info['avdataend']); + $RIFFdata .= $this->fread($OrignalRIFFheaderSize - $OrignalRIFFdataSize); + } + + // move the data chunk after all other chunks (if any) + // so that the RIFF parser doesn't see EOF when trying + // to skip over the data chunk + $RIFFdata = substr($RIFFdata, 0, 36).substr($RIFFdata, 44).substr($RIFFdata, 36, 8); + $getid3_riff = new getid3_riff($this->getid3); + $getid3_riff->ParseRIFFdata($RIFFdata); + unset($getid3_riff); + } + + if (isset($thisfile_riff_raw['fmt ']['wFormatTag'])) { + switch ($thisfile_riff_raw['fmt ']['wFormatTag']) { + case 0x0001: // PCM + if (!empty($info['ac3'])) { + // Dolby Digital WAV files masquerade as PCM-WAV, but they're not + $thisfile_audio['wformattag'] = 0x2000; + $thisfile_audio['codec'] = self::wFormatTagLookup($thisfile_audio['wformattag']); + $thisfile_audio['lossless'] = false; + $thisfile_audio['bitrate'] = $info['ac3']['bitrate']; + $thisfile_audio['sample_rate'] = $info['ac3']['sample_rate']; + } + if (!empty($info['dts'])) { + // Dolby DTS files masquerade as PCM-WAV, but they're not + $thisfile_audio['wformattag'] = 0x2001; + $thisfile_audio['codec'] = self::wFormatTagLookup($thisfile_audio['wformattag']); + $thisfile_audio['lossless'] = false; + $thisfile_audio['bitrate'] = $info['dts']['bitrate']; + $thisfile_audio['sample_rate'] = $info['dts']['sample_rate']; + } + break; + case 0x08AE: // ClearJump LiteWave + $thisfile_audio['bitrate_mode'] = 'vbr'; + $thisfile_audio_dataformat = 'litewave'; + + //typedef struct tagSLwFormat { + // WORD m_wCompFormat; // low byte defines compression method, high byte is compression flags + // DWORD m_dwScale; // scale factor for lossy compression + // DWORD m_dwBlockSize; // number of samples in encoded blocks + // WORD m_wQuality; // alias for the scale factor + // WORD m_wMarkDistance; // distance between marks in bytes + // WORD m_wReserved; + // + // //following paramters are ignored if CF_FILESRC is not set + // DWORD m_dwOrgSize; // original file size in bytes + // WORD m_bFactExists; // indicates if 'fact' chunk exists in the original file + // DWORD m_dwRiffChunkSize; // riff chunk size in the original file + // + // PCMWAVEFORMAT m_OrgWf; // original wave format + // }SLwFormat, *PSLwFormat; + + // shortcut + $thisfile_riff['litewave']['raw'] = array(); + $riff_litewave = &$thisfile_riff['litewave']; + $riff_litewave_raw = &$riff_litewave['raw']; + + $flags = array( + 'compression_method' => 1, + 'compression_flags' => 1, + 'm_dwScale' => 4, + 'm_dwBlockSize' => 4, + 'm_wQuality' => 2, + 'm_wMarkDistance' => 2, + 'm_wReserved' => 2, + 'm_dwOrgSize' => 4, + 'm_bFactExists' => 2, + 'm_dwRiffChunkSize' => 4, + ); + $litewave_offset = 18; + foreach ($flags as $flag => $length) { + $riff_litewave_raw[$flag] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE['fmt '][0]['data'], $litewave_offset, $length)); + $litewave_offset += $length; + } + + //$riff_litewave['quality_factor'] = intval(round((2000 - $riff_litewave_raw['m_dwScale']) / 20)); + $riff_litewave['quality_factor'] = $riff_litewave_raw['m_wQuality']; + + $riff_litewave['flags']['raw_source'] = ($riff_litewave_raw['compression_flags'] & 0x01) ? false : true; + $riff_litewave['flags']['vbr_blocksize'] = ($riff_litewave_raw['compression_flags'] & 0x02) ? false : true; + $riff_litewave['flags']['seekpoints'] = (bool) ($riff_litewave_raw['compression_flags'] & 0x04); + + $thisfile_audio['lossless'] = (($riff_litewave_raw['m_wQuality'] == 100) ? true : false); + $thisfile_audio['encoder_options'] = '-q'.$riff_litewave['quality_factor']; + break; + + default: + break; + } + } + if ($info['avdataend'] > $info['filesize']) { + switch (!empty($thisfile_audio_dataformat) ? $thisfile_audio_dataformat : '') { + case 'wavpack': // WavPack + case 'lpac': // LPAC + case 'ofr': // OptimFROG + case 'ofs': // OptimFROG DualStream + // lossless compressed audio formats that keep original RIFF headers - skip warning + break; + + case 'litewave': + if (($info['avdataend'] - $info['filesize']) == 1) { + // LiteWave appears to incorrectly *not* pad actual output file + // to nearest WORD boundary so may appear to be short by one + // byte, in which case - skip warning + } else { + // Short by more than one byte, throw warning + $this->warning('Probably truncated file - expecting '.$thisfile_riff[$RIFFsubtype]['data'][0]['size'].' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' (short by '.($thisfile_riff[$RIFFsubtype]['data'][0]['size'] - ($info['filesize'] - $info['avdataoffset'])).' bytes)'); + $info['avdataend'] = $info['filesize']; + } + break; + + default: + if ((($info['avdataend'] - $info['filesize']) == 1) && (($thisfile_riff[$RIFFsubtype]['data'][0]['size'] % 2) == 0) && ((($info['filesize'] - $info['avdataoffset']) % 2) == 1)) { + // output file appears to be incorrectly *not* padded to nearest WORD boundary + // Output less severe warning + $this->warning('File should probably be padded to nearest WORD boundary, but it is not (expecting '.$thisfile_riff[$RIFFsubtype]['data'][0]['size'].' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' therefore short by '.($thisfile_riff[$RIFFsubtype]['data'][0]['size'] - ($info['filesize'] - $info['avdataoffset'])).' bytes)'); + $info['avdataend'] = $info['filesize']; + } else { + // Short by more than one byte, throw warning + $this->warning('Probably truncated file - expecting '.$thisfile_riff[$RIFFsubtype]['data'][0]['size'].' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' (short by '.($thisfile_riff[$RIFFsubtype]['data'][0]['size'] - ($info['filesize'] - $info['avdataoffset'])).' bytes)'); + $info['avdataend'] = $info['filesize']; + } + break; + } + } + if (!empty($info['mpeg']['audio']['LAME']['audio_bytes'])) { + if ((($info['avdataend'] - $info['avdataoffset']) - $info['mpeg']['audio']['LAME']['audio_bytes']) == 1) { + $info['avdataend']--; + $this->warning('Extra null byte at end of MP3 data assumed to be RIFF padding and therefore ignored'); + } + } + if (isset($thisfile_audio_dataformat) && ($thisfile_audio_dataformat == 'ac3')) { + unset($thisfile_audio['bits_per_sample']); + if (!empty($info['ac3']['bitrate']) && ($info['ac3']['bitrate'] != $thisfile_audio['bitrate'])) { + $thisfile_audio['bitrate'] = $info['ac3']['bitrate']; + } + } + break; + + // http://en.wikipedia.org/wiki/Audio_Video_Interleave + case 'AVI ': + $info['fileformat'] = 'avi'; + $info['mime_type'] = 'video/avi'; + + $thisfile_video['bitrate_mode'] = 'vbr'; // maybe not, but probably + $thisfile_video['dataformat'] = 'avi'; + + $thisfile_riff_video_current = array(); + + if (isset($thisfile_riff[$RIFFsubtype]['movi']['offset'])) { + $info['avdataoffset'] = $thisfile_riff[$RIFFsubtype]['movi']['offset'] + 8; + if (isset($thisfile_riff['AVIX'])) { + $info['avdataend'] = $thisfile_riff['AVIX'][(count($thisfile_riff['AVIX']) - 1)]['chunks']['movi']['offset'] + $thisfile_riff['AVIX'][(count($thisfile_riff['AVIX']) - 1)]['chunks']['movi']['size']; + } else { + $info['avdataend'] = $thisfile_riff['AVI ']['movi']['offset'] + $thisfile_riff['AVI ']['movi']['size']; + } + if ($info['avdataend'] > $info['filesize']) { + $this->warning('Probably truncated file - expecting '.($info['avdataend'] - $info['avdataoffset']).' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' (short by '.($info['avdataend'] - $info['filesize']).' bytes)'); + $info['avdataend'] = $info['filesize']; + } + } + + if (isset($thisfile_riff['AVI ']['hdrl']['strl']['indx'])) { + //$bIndexType = array( + // 0x00 => 'AVI_INDEX_OF_INDEXES', + // 0x01 => 'AVI_INDEX_OF_CHUNKS', + // 0x80 => 'AVI_INDEX_IS_DATA', + //); + //$bIndexSubtype = array( + // 0x01 => array( + // 0x01 => 'AVI_INDEX_2FIELD', + // ), + //); + foreach ($thisfile_riff['AVI ']['hdrl']['strl']['indx'] as $streamnumber => $steamdataarray) { + $ahsisd = &$thisfile_riff['AVI ']['hdrl']['strl']['indx'][$streamnumber]['data']; + + $thisfile_riff_raw['indx'][$streamnumber]['wLongsPerEntry'] = $this->EitherEndian2Int(substr($ahsisd, 0, 2)); + $thisfile_riff_raw['indx'][$streamnumber]['bIndexSubType'] = $this->EitherEndian2Int(substr($ahsisd, 2, 1)); + $thisfile_riff_raw['indx'][$streamnumber]['bIndexType'] = $this->EitherEndian2Int(substr($ahsisd, 3, 1)); + $thisfile_riff_raw['indx'][$streamnumber]['nEntriesInUse'] = $this->EitherEndian2Int(substr($ahsisd, 4, 4)); + $thisfile_riff_raw['indx'][$streamnumber]['dwChunkId'] = substr($ahsisd, 8, 4); + $thisfile_riff_raw['indx'][$streamnumber]['dwReserved'] = $this->EitherEndian2Int(substr($ahsisd, 12, 4)); + + //$thisfile_riff_raw['indx'][$streamnumber]['bIndexType_name'] = $bIndexType[$thisfile_riff_raw['indx'][$streamnumber]['bIndexType']]; + //$thisfile_riff_raw['indx'][$streamnumber]['bIndexSubType_name'] = $bIndexSubtype[$thisfile_riff_raw['indx'][$streamnumber]['bIndexType']][$thisfile_riff_raw['indx'][$streamnumber]['bIndexSubType']]; + + unset($ahsisd); + } + } + if (isset($thisfile_riff['AVI ']['hdrl']['avih'][$streamindex]['data'])) { + $avihData = $thisfile_riff['AVI ']['hdrl']['avih'][$streamindex]['data']; + + // shortcut + $thisfile_riff_raw['avih'] = array(); + $thisfile_riff_raw_avih = &$thisfile_riff_raw['avih']; + + $thisfile_riff_raw_avih['dwMicroSecPerFrame'] = $this->EitherEndian2Int(substr($avihData, 0, 4)); // frame display rate (or 0L) + if ($thisfile_riff_raw_avih['dwMicroSecPerFrame'] == 0) { + $this->error('Corrupt RIFF file: avih.dwMicroSecPerFrame == zero'); + return false; + } + + $flags = array( + 'dwMaxBytesPerSec', // max. transfer rate + 'dwPaddingGranularity', // pad to multiples of this size; normally 2K. + 'dwFlags', // the ever-present flags + 'dwTotalFrames', // # frames in file + 'dwInitialFrames', // + 'dwStreams', // + 'dwSuggestedBufferSize', // + 'dwWidth', // + 'dwHeight', // + 'dwScale', // + 'dwRate', // + 'dwStart', // + 'dwLength', // + ); + $avih_offset = 4; + foreach ($flags as $flag) { + $thisfile_riff_raw_avih[$flag] = $this->EitherEndian2Int(substr($avihData, $avih_offset, 4)); + $avih_offset += 4; + } + + $flags = array( + 'hasindex' => 0x00000010, + 'mustuseindex' => 0x00000020, + 'interleaved' => 0x00000100, + 'trustcktype' => 0x00000800, + 'capturedfile' => 0x00010000, + 'copyrighted' => 0x00020010, + ); + foreach ($flags as $flag => $value) { + $thisfile_riff_raw_avih['flags'][$flag] = (bool) ($thisfile_riff_raw_avih['dwFlags'] & $value); + } + + // shortcut + $thisfile_riff_video[$streamindex] = array(); + /** @var array $thisfile_riff_video_current */ + $thisfile_riff_video_current = &$thisfile_riff_video[$streamindex]; + + if ($thisfile_riff_raw_avih['dwWidth'] > 0) { + $thisfile_riff_video_current['frame_width'] = $thisfile_riff_raw_avih['dwWidth']; + $thisfile_video['resolution_x'] = $thisfile_riff_video_current['frame_width']; + } + if ($thisfile_riff_raw_avih['dwHeight'] > 0) { + $thisfile_riff_video_current['frame_height'] = $thisfile_riff_raw_avih['dwHeight']; + $thisfile_video['resolution_y'] = $thisfile_riff_video_current['frame_height']; + } + if ($thisfile_riff_raw_avih['dwTotalFrames'] > 0) { + $thisfile_riff_video_current['total_frames'] = $thisfile_riff_raw_avih['dwTotalFrames']; + $thisfile_video['total_frames'] = $thisfile_riff_video_current['total_frames']; + } + + $thisfile_riff_video_current['frame_rate'] = round(1000000 / $thisfile_riff_raw_avih['dwMicroSecPerFrame'], 3); + $thisfile_video['frame_rate'] = $thisfile_riff_video_current['frame_rate']; + } + if (isset($thisfile_riff['AVI ']['hdrl']['strl']['strh'][0]['data'])) { + if (is_array($thisfile_riff['AVI ']['hdrl']['strl']['strh'])) { + for ($i = 0; $i < count($thisfile_riff['AVI ']['hdrl']['strl']['strh']); $i++) { + if (isset($thisfile_riff['AVI ']['hdrl']['strl']['strh'][$i]['data'])) { + $strhData = $thisfile_riff['AVI ']['hdrl']['strl']['strh'][$i]['data']; + $strhfccType = substr($strhData, 0, 4); + + if (isset($thisfile_riff['AVI ']['hdrl']['strl']['strf'][$i]['data'])) { + $strfData = $thisfile_riff['AVI ']['hdrl']['strl']['strf'][$i]['data']; + + // shortcut + $thisfile_riff_raw_strf_strhfccType_streamindex = &$thisfile_riff_raw['strf'][$strhfccType][$streamindex]; + + switch ($strhfccType) { + case 'auds': + $thisfile_audio['bitrate_mode'] = 'cbr'; + $thisfile_audio_dataformat = 'wav'; + if (isset($thisfile_riff_audio) && is_array($thisfile_riff_audio)) { + $streamindex = count($thisfile_riff_audio); + } + + $thisfile_riff_audio[$streamindex] = self::parseWAVEFORMATex($strfData); + $thisfile_audio['wformattag'] = $thisfile_riff_audio[$streamindex]['raw']['wFormatTag']; + + // shortcut + $thisfile_audio['streams'][$streamindex] = $thisfile_riff_audio[$streamindex]; + $thisfile_audio_streams_currentstream = &$thisfile_audio['streams'][$streamindex]; + + if ($thisfile_audio_streams_currentstream['bits_per_sample'] == 0) { + unset($thisfile_audio_streams_currentstream['bits_per_sample']); + } + $thisfile_audio_streams_currentstream['wformattag'] = $thisfile_audio_streams_currentstream['raw']['wFormatTag']; + unset($thisfile_audio_streams_currentstream['raw']); + + // shortcut + $thisfile_riff_raw['strf'][$strhfccType][$streamindex] = $thisfile_riff_audio[$streamindex]['raw']; + + unset($thisfile_riff_audio[$streamindex]['raw']); + $thisfile_audio = getid3_lib::array_merge_noclobber($thisfile_audio, $thisfile_riff_audio[$streamindex]); + + $thisfile_audio['lossless'] = false; + switch ($thisfile_riff_raw_strf_strhfccType_streamindex['wFormatTag']) { + case 0x0001: // PCM + $thisfile_audio_dataformat = 'wav'; + $thisfile_audio['lossless'] = true; + break; + + case 0x0050: // MPEG Layer 2 or Layer 1 + $thisfile_audio_dataformat = 'mp2'; // Assume Layer-2 + break; + + case 0x0055: // MPEG Layer 3 + $thisfile_audio_dataformat = 'mp3'; + break; + + case 0x00FF: // AAC + $thisfile_audio_dataformat = 'aac'; + break; + + case 0x0161: // Windows Media v7 / v8 / v9 + case 0x0162: // Windows Media Professional v9 + case 0x0163: // Windows Media Lossess v9 + $thisfile_audio_dataformat = 'wma'; + break; + + case 0x2000: // AC-3 + $thisfile_audio_dataformat = 'ac3'; + break; + + case 0x2001: // DTS + $thisfile_audio_dataformat = 'dts'; + break; + + default: + $thisfile_audio_dataformat = 'wav'; + break; + } + $thisfile_audio_streams_currentstream['dataformat'] = $thisfile_audio_dataformat; + $thisfile_audio_streams_currentstream['lossless'] = $thisfile_audio['lossless']; + $thisfile_audio_streams_currentstream['bitrate_mode'] = $thisfile_audio['bitrate_mode']; + break; + + + case 'iavs': + case 'vids': + // shortcut + $thisfile_riff_raw['strh'][$i] = array(); + $thisfile_riff_raw_strh_current = &$thisfile_riff_raw['strh'][$i]; + + $thisfile_riff_raw_strh_current['fccType'] = substr($strhData, 0, 4); // same as $strhfccType; + $thisfile_riff_raw_strh_current['fccHandler'] = substr($strhData, 4, 4); + $thisfile_riff_raw_strh_current['dwFlags'] = $this->EitherEndian2Int(substr($strhData, 8, 4)); // Contains AVITF_* flags + $thisfile_riff_raw_strh_current['wPriority'] = $this->EitherEndian2Int(substr($strhData, 12, 2)); + $thisfile_riff_raw_strh_current['wLanguage'] = $this->EitherEndian2Int(substr($strhData, 14, 2)); + $thisfile_riff_raw_strh_current['dwInitialFrames'] = $this->EitherEndian2Int(substr($strhData, 16, 4)); + $thisfile_riff_raw_strh_current['dwScale'] = $this->EitherEndian2Int(substr($strhData, 20, 4)); + $thisfile_riff_raw_strh_current['dwRate'] = $this->EitherEndian2Int(substr($strhData, 24, 4)); + $thisfile_riff_raw_strh_current['dwStart'] = $this->EitherEndian2Int(substr($strhData, 28, 4)); + $thisfile_riff_raw_strh_current['dwLength'] = $this->EitherEndian2Int(substr($strhData, 32, 4)); + $thisfile_riff_raw_strh_current['dwSuggestedBufferSize'] = $this->EitherEndian2Int(substr($strhData, 36, 4)); + $thisfile_riff_raw_strh_current['dwQuality'] = $this->EitherEndian2Int(substr($strhData, 40, 4)); + $thisfile_riff_raw_strh_current['dwSampleSize'] = $this->EitherEndian2Int(substr($strhData, 44, 4)); + $thisfile_riff_raw_strh_current['rcFrame'] = $this->EitherEndian2Int(substr($strhData, 48, 4)); + + $thisfile_riff_video_current['codec'] = self::fourccLookup($thisfile_riff_raw_strh_current['fccHandler']); + $thisfile_video['fourcc'] = $thisfile_riff_raw_strh_current['fccHandler']; + if (!$thisfile_riff_video_current['codec'] && isset($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']) && self::fourccLookup($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc'])) { + $thisfile_riff_video_current['codec'] = self::fourccLookup($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']); + $thisfile_video['fourcc'] = $thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']; + } + $thisfile_video['codec'] = $thisfile_riff_video_current['codec']; + $thisfile_video['pixel_aspect_ratio'] = (float) 1; + switch ($thisfile_riff_raw_strh_current['fccHandler']) { + case 'HFYU': // Huffman Lossless Codec + case 'IRAW': // Intel YUV Uncompressed + case 'YUY2': // Uncompressed YUV 4:2:2 + $thisfile_video['lossless'] = true; + break; + + default: + $thisfile_video['lossless'] = false; + break; + } + + switch ($strhfccType) { + case 'vids': + $thisfile_riff_raw_strf_strhfccType_streamindex = self::ParseBITMAPINFOHEADER(substr($strfData, 0, 40), ($this->container == 'riff')); + $thisfile_video['bits_per_sample'] = $thisfile_riff_raw_strf_strhfccType_streamindex['biBitCount']; + + if ($thisfile_riff_video_current['codec'] == 'DV') { + $thisfile_riff_video_current['dv_type'] = 2; + } + break; + + case 'iavs': + $thisfile_riff_video_current['dv_type'] = 1; + break; + } + break; + + default: + $this->warning('Unhandled fccType for stream ('.$i.'): "'.$strhfccType.'"'); + break; + + } + } + } + + if (isset($thisfile_riff_raw_strf_strhfccType_streamindex) && isset($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc'])) { + + $thisfile_video['fourcc'] = $thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']; + if (self::fourccLookup($thisfile_video['fourcc'])) { + $thisfile_riff_video_current['codec'] = self::fourccLookup($thisfile_video['fourcc']); + $thisfile_video['codec'] = $thisfile_riff_video_current['codec']; + } + + switch ($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']) { + case 'HFYU': // Huffman Lossless Codec + case 'IRAW': // Intel YUV Uncompressed + case 'YUY2': // Uncompressed YUV 4:2:2 + $thisfile_video['lossless'] = true; + //$thisfile_video['bits_per_sample'] = 24; + break; + + default: + $thisfile_video['lossless'] = false; + //$thisfile_video['bits_per_sample'] = 24; + break; + } + + } + } + } + } + break; + + + case 'AMV ': + $info['fileformat'] = 'amv'; + $info['mime_type'] = 'video/amv'; + + $thisfile_video['bitrate_mode'] = 'vbr'; // it's MJPEG, presumably contant-quality encoding, thereby VBR + $thisfile_video['dataformat'] = 'mjpeg'; + $thisfile_video['codec'] = 'mjpeg'; + $thisfile_video['lossless'] = false; + $thisfile_video['bits_per_sample'] = 24; + + $thisfile_audio['dataformat'] = 'adpcm'; + $thisfile_audio['lossless'] = false; + break; + + + // http://en.wikipedia.org/wiki/CD-DA + case 'CDDA': + $info['fileformat'] = 'cda'; + unset($info['mime_type']); + + $thisfile_audio_dataformat = 'cda'; + + $info['avdataoffset'] = 44; + + if (isset($thisfile_riff['CDDA']['fmt '][0]['data'])) { + // shortcut + $thisfile_riff_CDDA_fmt_0 = &$thisfile_riff['CDDA']['fmt '][0]; + + $thisfile_riff_CDDA_fmt_0['unknown1'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 0, 2)); + $thisfile_riff_CDDA_fmt_0['track_num'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 2, 2)); + $thisfile_riff_CDDA_fmt_0['disc_id'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 4, 4)); + $thisfile_riff_CDDA_fmt_0['start_offset_frame'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 8, 4)); + $thisfile_riff_CDDA_fmt_0['playtime_frames'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 12, 4)); + $thisfile_riff_CDDA_fmt_0['unknown6'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 16, 4)); + $thisfile_riff_CDDA_fmt_0['unknown7'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 20, 4)); + + $thisfile_riff_CDDA_fmt_0['start_offset_seconds'] = (float) $thisfile_riff_CDDA_fmt_0['start_offset_frame'] / 75; + $thisfile_riff_CDDA_fmt_0['playtime_seconds'] = (float) $thisfile_riff_CDDA_fmt_0['playtime_frames'] / 75; + $info['comments']['track_number'] = $thisfile_riff_CDDA_fmt_0['track_num']; + $info['playtime_seconds'] = $thisfile_riff_CDDA_fmt_0['playtime_seconds']; + + // hardcoded data for CD-audio + $thisfile_audio['lossless'] = true; + $thisfile_audio['sample_rate'] = 44100; + $thisfile_audio['channels'] = 2; + $thisfile_audio['bits_per_sample'] = 16; + $thisfile_audio['bitrate'] = $thisfile_audio['sample_rate'] * $thisfile_audio['channels'] * $thisfile_audio['bits_per_sample']; + $thisfile_audio['bitrate_mode'] = 'cbr'; + } + break; + + // http://en.wikipedia.org/wiki/AIFF + case 'AIFF': + case 'AIFC': + $info['fileformat'] = 'aiff'; + $info['mime_type'] = 'audio/x-aiff'; + + $thisfile_audio['bitrate_mode'] = 'cbr'; + $thisfile_audio_dataformat = 'aiff'; + $thisfile_audio['lossless'] = true; + + if (isset($thisfile_riff[$RIFFsubtype]['SSND'][0]['offset'])) { + $info['avdataoffset'] = $thisfile_riff[$RIFFsubtype]['SSND'][0]['offset'] + 8; + $info['avdataend'] = $info['avdataoffset'] + $thisfile_riff[$RIFFsubtype]['SSND'][0]['size']; + if ($info['avdataend'] > $info['filesize']) { + if (($info['avdataend'] == ($info['filesize'] + 1)) && (($info['filesize'] % 2) == 1)) { + // structures rounded to 2-byte boundary, but dumb encoders + // forget to pad end of file to make this actually work + } else { + $this->warning('Probable truncated AIFF file: expecting '.$thisfile_riff[$RIFFsubtype]['SSND'][0]['size'].' bytes of audio data, only '.($info['filesize'] - $info['avdataoffset']).' bytes found'); + } + $info['avdataend'] = $info['filesize']; + } + } + + if (isset($thisfile_riff[$RIFFsubtype]['COMM'][0]['data'])) { + + // shortcut + $thisfile_riff_RIFFsubtype_COMM_0_data = &$thisfile_riff[$RIFFsubtype]['COMM'][0]['data']; + + $thisfile_riff_audio['channels'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_COMM_0_data, 0, 2), true); + $thisfile_riff_audio['total_samples'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_COMM_0_data, 2, 4), false); + $thisfile_riff_audio['bits_per_sample'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_COMM_0_data, 6, 2), true); + $thisfile_riff_audio['sample_rate'] = (int) getid3_lib::BigEndian2Float(substr($thisfile_riff_RIFFsubtype_COMM_0_data, 8, 10)); + + if ($thisfile_riff[$RIFFsubtype]['COMM'][0]['size'] > 18) { + $thisfile_riff_audio['codec_fourcc'] = substr($thisfile_riff_RIFFsubtype_COMM_0_data, 18, 4); + $CodecNameSize = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_COMM_0_data, 22, 1), false); + $thisfile_riff_audio['codec_name'] = substr($thisfile_riff_RIFFsubtype_COMM_0_data, 23, $CodecNameSize); + switch ($thisfile_riff_audio['codec_name']) { + case 'NONE': + $thisfile_audio['codec'] = 'Pulse Code Modulation (PCM)'; + $thisfile_audio['lossless'] = true; + break; + + case '': + switch ($thisfile_riff_audio['codec_fourcc']) { + // http://developer.apple.com/qa/snd/snd07.html + case 'sowt': + $thisfile_riff_audio['codec_name'] = 'Two\'s Compliment Little-Endian PCM'; + $thisfile_audio['lossless'] = true; + break; + + case 'twos': + $thisfile_riff_audio['codec_name'] = 'Two\'s Compliment Big-Endian PCM'; + $thisfile_audio['lossless'] = true; + break; + + default: + break; + } + break; + + default: + $thisfile_audio['codec'] = $thisfile_riff_audio['codec_name']; + $thisfile_audio['lossless'] = false; + break; + } + } + + $thisfile_audio['channels'] = $thisfile_riff_audio['channels']; + if ($thisfile_riff_audio['bits_per_sample'] > 0) { + $thisfile_audio['bits_per_sample'] = $thisfile_riff_audio['bits_per_sample']; + } + $thisfile_audio['sample_rate'] = $thisfile_riff_audio['sample_rate']; + if ($thisfile_audio['sample_rate'] == 0) { + $this->error('Corrupted AIFF file: sample_rate == zero'); + return false; + } + $info['playtime_seconds'] = $thisfile_riff_audio['total_samples'] / $thisfile_audio['sample_rate']; + } + + if (isset($thisfile_riff[$RIFFsubtype]['COMT'])) { + $offset = 0; + $CommentCount = getid3_lib::BigEndian2Int(substr($thisfile_riff[$RIFFsubtype]['COMT'][0]['data'], $offset, 2), false); + $offset += 2; + for ($i = 0; $i < $CommentCount; $i++) { + $info['comments_raw'][$i]['timestamp'] = getid3_lib::BigEndian2Int(substr($thisfile_riff[$RIFFsubtype]['COMT'][0]['data'], $offset, 4), false); + $offset += 4; + $info['comments_raw'][$i]['marker_id'] = getid3_lib::BigEndian2Int(substr($thisfile_riff[$RIFFsubtype]['COMT'][0]['data'], $offset, 2), true); + $offset += 2; + $CommentLength = getid3_lib::BigEndian2Int(substr($thisfile_riff[$RIFFsubtype]['COMT'][0]['data'], $offset, 2), false); + $offset += 2; + $info['comments_raw'][$i]['comment'] = substr($thisfile_riff[$RIFFsubtype]['COMT'][0]['data'], $offset, $CommentLength); + $offset += $CommentLength; + + $info['comments_raw'][$i]['timestamp_unix'] = getid3_lib::DateMac2Unix($info['comments_raw'][$i]['timestamp']); + $thisfile_riff['comments']['comment'][] = $info['comments_raw'][$i]['comment']; + } + } + + $CommentsChunkNames = array('NAME'=>'title', 'author'=>'artist', '(c) '=>'copyright', 'ANNO'=>'comment'); + foreach ($CommentsChunkNames as $key => $value) { + if (isset($thisfile_riff[$RIFFsubtype][$key][0]['data'])) { + $thisfile_riff['comments'][$value][] = $thisfile_riff[$RIFFsubtype][$key][0]['data']; + } + } +/* + if (isset($thisfile_riff[$RIFFsubtype]['ID3 '])) { + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true); + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_id3v2 = new getid3_id3v2($getid3_temp); + $getid3_id3v2->StartingOffset = $thisfile_riff[$RIFFsubtype]['ID3 '][0]['offset'] + 8; + if ($thisfile_riff[$RIFFsubtype]['ID3 '][0]['valid'] = $getid3_id3v2->Analyze()) { + $info['id3v2'] = $getid3_temp->info['id3v2']; + } + unset($getid3_temp, $getid3_id3v2); + } +*/ + break; + + // http://en.wikipedia.org/wiki/8SVX + case '8SVX': + $info['fileformat'] = '8svx'; + $info['mime_type'] = 'audio/8svx'; + + $thisfile_audio['bitrate_mode'] = 'cbr'; + $thisfile_audio_dataformat = '8svx'; + $thisfile_audio['bits_per_sample'] = 8; + $thisfile_audio['channels'] = 1; // overridden below, if need be + $ActualBitsPerSample = 0; + + if (isset($thisfile_riff[$RIFFsubtype]['BODY'][0]['offset'])) { + $info['avdataoffset'] = $thisfile_riff[$RIFFsubtype]['BODY'][0]['offset'] + 8; + $info['avdataend'] = $info['avdataoffset'] + $thisfile_riff[$RIFFsubtype]['BODY'][0]['size']; + if ($info['avdataend'] > $info['filesize']) { + $this->warning('Probable truncated AIFF file: expecting '.$thisfile_riff[$RIFFsubtype]['BODY'][0]['size'].' bytes of audio data, only '.($info['filesize'] - $info['avdataoffset']).' bytes found'); + } + } + + if (isset($thisfile_riff[$RIFFsubtype]['VHDR'][0]['offset'])) { + // shortcut + $thisfile_riff_RIFFsubtype_VHDR_0 = &$thisfile_riff[$RIFFsubtype]['VHDR'][0]; + + $thisfile_riff_RIFFsubtype_VHDR_0['oneShotHiSamples'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_VHDR_0['data'], 0, 4)); + $thisfile_riff_RIFFsubtype_VHDR_0['repeatHiSamples'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_VHDR_0['data'], 4, 4)); + $thisfile_riff_RIFFsubtype_VHDR_0['samplesPerHiCycle'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_VHDR_0['data'], 8, 4)); + $thisfile_riff_RIFFsubtype_VHDR_0['samplesPerSec'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_VHDR_0['data'], 12, 2)); + $thisfile_riff_RIFFsubtype_VHDR_0['ctOctave'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_VHDR_0['data'], 14, 1)); + $thisfile_riff_RIFFsubtype_VHDR_0['sCompression'] = getid3_lib::BigEndian2Int(substr($thisfile_riff_RIFFsubtype_VHDR_0['data'], 15, 1)); + $thisfile_riff_RIFFsubtype_VHDR_0['Volume'] = getid3_lib::FixedPoint16_16(substr($thisfile_riff_RIFFsubtype_VHDR_0['data'], 16, 4)); + + $thisfile_audio['sample_rate'] = $thisfile_riff_RIFFsubtype_VHDR_0['samplesPerSec']; + + switch ($thisfile_riff_RIFFsubtype_VHDR_0['sCompression']) { + case 0: + $thisfile_audio['codec'] = 'Pulse Code Modulation (PCM)'; + $thisfile_audio['lossless'] = true; + $ActualBitsPerSample = 8; + break; + + case 1: + $thisfile_audio['codec'] = 'Fibonacci-delta encoding'; + $thisfile_audio['lossless'] = false; + $ActualBitsPerSample = 4; + break; + + default: + $this->warning('Unexpected sCompression value in 8SVX.VHDR chunk - expecting 0 or 1, found "'.$thisfile_riff_RIFFsubtype_VHDR_0['sCompression'].'"'); + break; + } + } + + if (isset($thisfile_riff[$RIFFsubtype]['CHAN'][0]['data'])) { + $ChannelsIndex = getid3_lib::BigEndian2Int(substr($thisfile_riff[$RIFFsubtype]['CHAN'][0]['data'], 0, 4)); + switch ($ChannelsIndex) { + case 6: // Stereo + $thisfile_audio['channels'] = 2; + break; + + case 2: // Left channel only + case 4: // Right channel only + $thisfile_audio['channels'] = 1; + break; + + default: + $this->warning('Unexpected value in 8SVX.CHAN chunk - expecting 2 or 4 or 6, found "'.$ChannelsIndex.'"'); + break; + } + + } + + $CommentsChunkNames = array('NAME'=>'title', 'author'=>'artist', '(c) '=>'copyright', 'ANNO'=>'comment'); + foreach ($CommentsChunkNames as $key => $value) { + if (isset($thisfile_riff[$RIFFsubtype][$key][0]['data'])) { + $thisfile_riff['comments'][$value][] = $thisfile_riff[$RIFFsubtype][$key][0]['data']; + } + } + + $thisfile_audio['bitrate'] = $thisfile_audio['sample_rate'] * $ActualBitsPerSample * $thisfile_audio['channels']; + if (!empty($thisfile_audio['bitrate'])) { + $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) / ($thisfile_audio['bitrate'] / 8); + } + break; + + case 'CDXA': + $info['fileformat'] = 'vcd'; // Asume Video CD + $info['mime_type'] = 'video/mpeg'; + + if (!empty($thisfile_riff['CDXA']['data'][0]['size'])) { + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.mpeg.php', __FILE__, true); + + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_mpeg = new getid3_mpeg($getid3_temp); + $getid3_mpeg->Analyze(); + if (empty($getid3_temp->info['error'])) { + $info['audio'] = $getid3_temp->info['audio']; + $info['video'] = $getid3_temp->info['video']; + $info['mpeg'] = $getid3_temp->info['mpeg']; + $info['warning'] = $getid3_temp->info['warning']; + } + unset($getid3_temp, $getid3_mpeg); + } + break; + + case 'WEBP': + // https://developers.google.com/speed/webp/docs/riff_container + // https://tools.ietf.org/html/rfc6386 + // https://chromium.googlesource.com/webm/libwebp/+/master/doc/webp-lossless-bitstream-spec.txt + $info['fileformat'] = 'webp'; + $info['mime_type'] = 'image/webp'; + + if (!empty($thisfile_riff['WEBP']['VP8 '][0]['size'])) { + $old_offset = $this->ftell(); + $this->fseek($thisfile_riff['WEBP']['VP8 '][0]['offset'] + 8); // 4 bytes "VP8 " + 4 bytes chunk size + $WEBP_VP8_header = $this->fread(10); + $this->fseek($old_offset); + if (substr($WEBP_VP8_header, 3, 3) == "\x9D\x01\x2A") { + $thisfile_riff['WEBP']['VP8 '][0]['keyframe'] = !(getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 0, 3)) & 0x800000); + $thisfile_riff['WEBP']['VP8 '][0]['version'] = (getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 0, 3)) & 0x700000) >> 20; + $thisfile_riff['WEBP']['VP8 '][0]['show_frame'] = (getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 0, 3)) & 0x080000); + $thisfile_riff['WEBP']['VP8 '][0]['data_bytes'] = (getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 0, 3)) & 0x07FFFF) >> 0; + + $thisfile_riff['WEBP']['VP8 '][0]['scale_x'] = (getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 6, 2)) & 0xC000) >> 14; + $thisfile_riff['WEBP']['VP8 '][0]['width'] = (getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 6, 2)) & 0x3FFF); + $thisfile_riff['WEBP']['VP8 '][0]['scale_y'] = (getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 8, 2)) & 0xC000) >> 14; + $thisfile_riff['WEBP']['VP8 '][0]['height'] = (getid3_lib::LittleEndian2Int(substr($WEBP_VP8_header, 8, 2)) & 0x3FFF); + + $info['video']['resolution_x'] = $thisfile_riff['WEBP']['VP8 '][0]['width']; + $info['video']['resolution_y'] = $thisfile_riff['WEBP']['VP8 '][0]['height']; + } else { + $this->error('Expecting 9D 01 2A at offset '.($thisfile_riff['WEBP']['VP8 '][0]['offset'] + 8 + 3).', found "'.getid3_lib::PrintHexBytes(substr($WEBP_VP8_header, 3, 3)).'"'); + } + + } + if (!empty($thisfile_riff['WEBP']['VP8L'][0]['size'])) { + $old_offset = $this->ftell(); + $this->fseek($thisfile_riff['WEBP']['VP8L'][0]['offset'] + 8); // 4 bytes "VP8L" + 4 bytes chunk size + $WEBP_VP8L_header = $this->fread(10); + $this->fseek($old_offset); + if (substr($WEBP_VP8L_header, 0, 1) == "\x2F") { + $width_height_flags = getid3_lib::LittleEndian2Bin(substr($WEBP_VP8L_header, 1, 4)); + $thisfile_riff['WEBP']['VP8L'][0]['width'] = bindec(substr($width_height_flags, 18, 14)) + 1; + $thisfile_riff['WEBP']['VP8L'][0]['height'] = bindec(substr($width_height_flags, 4, 14)) + 1; + $thisfile_riff['WEBP']['VP8L'][0]['alpha_is_used'] = (bool) bindec(substr($width_height_flags, 3, 1)); + $thisfile_riff['WEBP']['VP8L'][0]['version'] = bindec(substr($width_height_flags, 0, 3)); + + $info['video']['resolution_x'] = $thisfile_riff['WEBP']['VP8L'][0]['width']; + $info['video']['resolution_y'] = $thisfile_riff['WEBP']['VP8L'][0]['height']; + } else { + $this->error('Expecting 2F at offset '.($thisfile_riff['WEBP']['VP8L'][0]['offset'] + 8).', found "'.getid3_lib::PrintHexBytes(substr($WEBP_VP8L_header, 0, 1)).'"'); + } + + } + break; + + default: + $this->error('Unknown RIFF type: expecting one of (WAVE|RMP3|AVI |CDDA|AIFF|AIFC|8SVX|CDXA|WEBP), found "'.$RIFFsubtype.'" instead'); + //unset($info['fileformat']); + } + + switch ($RIFFsubtype) { + case 'WAVE': + case 'AIFF': + case 'AIFC': + $ID3v2_key_good = 'id3 '; + $ID3v2_keys_bad = array('ID3 ', 'tag '); + foreach ($ID3v2_keys_bad as $ID3v2_key_bad) { + if (isset($thisfile_riff[$RIFFsubtype][$ID3v2_key_bad]) && !array_key_exists($ID3v2_key_good, $thisfile_riff[$RIFFsubtype])) { + $thisfile_riff[$RIFFsubtype][$ID3v2_key_good] = $thisfile_riff[$RIFFsubtype][$ID3v2_key_bad]; + $this->warning('mapping "'.$ID3v2_key_bad.'" chunk to "'.$ID3v2_key_good.'"'); + } + } + + if (isset($thisfile_riff[$RIFFsubtype]['id3 '])) { + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true); + + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_id3v2 = new getid3_id3v2($getid3_temp); + $getid3_id3v2->StartingOffset = $thisfile_riff[$RIFFsubtype]['id3 '][0]['offset'] + 8; + if ($thisfile_riff[$RIFFsubtype]['id3 '][0]['valid'] = $getid3_id3v2->Analyze()) { + $info['id3v2'] = $getid3_temp->info['id3v2']; + } + unset($getid3_temp, $getid3_id3v2); + } + break; + } + + if (isset($thisfile_riff_WAVE['DISP']) && is_array($thisfile_riff_WAVE['DISP'])) { + $thisfile_riff['comments']['title'][] = trim(substr($thisfile_riff_WAVE['DISP'][count($thisfile_riff_WAVE['DISP']) - 1]['data'], 4)); + } + if (isset($thisfile_riff_WAVE['INFO']) && is_array($thisfile_riff_WAVE['INFO'])) { + self::parseComments($thisfile_riff_WAVE['INFO'], $thisfile_riff['comments']); + } + if (isset($thisfile_riff['AVI ']['INFO']) && is_array($thisfile_riff['AVI ']['INFO'])) { + self::parseComments($thisfile_riff['AVI ']['INFO'], $thisfile_riff['comments']); + } + + if (empty($thisfile_audio['encoder']) && !empty($info['mpeg']['audio']['LAME']['short_version'])) { + $thisfile_audio['encoder'] = $info['mpeg']['audio']['LAME']['short_version']; + } + + if (!isset($info['playtime_seconds'])) { + $info['playtime_seconds'] = 0; + } + if (isset($thisfile_riff_raw['strh'][0]['dwLength']) && isset($thisfile_riff_raw['avih']['dwMicroSecPerFrame'])) { + // needed for >2GB AVIs where 'avih' chunk only lists number of frames in that chunk, not entire movie + $info['playtime_seconds'] = $thisfile_riff_raw['strh'][0]['dwLength'] * ($thisfile_riff_raw['avih']['dwMicroSecPerFrame'] / 1000000); + } elseif (isset($thisfile_riff_raw['avih']['dwTotalFrames']) && isset($thisfile_riff_raw['avih']['dwMicroSecPerFrame'])) { + $info['playtime_seconds'] = $thisfile_riff_raw['avih']['dwTotalFrames'] * ($thisfile_riff_raw['avih']['dwMicroSecPerFrame'] / 1000000); + } + + if ($info['playtime_seconds'] > 0) { + if (isset($thisfile_riff_audio) && isset($thisfile_riff_video)) { + + if (!isset($info['bitrate'])) { + $info['bitrate'] = ((($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8); + } + + } elseif (isset($thisfile_riff_audio) && !isset($thisfile_riff_video)) { + + if (!isset($thisfile_audio['bitrate'])) { + $thisfile_audio['bitrate'] = ((($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8); + } + + } elseif (!isset($thisfile_riff_audio) && isset($thisfile_riff_video)) { + + if (!isset($thisfile_video['bitrate'])) { + $thisfile_video['bitrate'] = ((($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8); + } + + } + } + + + if (isset($thisfile_riff_video) && isset($thisfile_audio['bitrate']) && ($thisfile_audio['bitrate'] > 0) && ($info['playtime_seconds'] > 0)) { + + $info['bitrate'] = ((($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8); + $thisfile_audio['bitrate'] = 0; + $thisfile_video['bitrate'] = $info['bitrate']; + foreach ($thisfile_riff_audio as $channelnumber => $audioinfoarray) { + $thisfile_video['bitrate'] -= $audioinfoarray['bitrate']; + $thisfile_audio['bitrate'] += $audioinfoarray['bitrate']; + } + if ($thisfile_video['bitrate'] <= 0) { + unset($thisfile_video['bitrate']); + } + if ($thisfile_audio['bitrate'] <= 0) { + unset($thisfile_audio['bitrate']); + } + } + + if (isset($info['mpeg']['audio'])) { + $thisfile_audio_dataformat = 'mp'.$info['mpeg']['audio']['layer']; + $thisfile_audio['sample_rate'] = $info['mpeg']['audio']['sample_rate']; + $thisfile_audio['channels'] = $info['mpeg']['audio']['channels']; + $thisfile_audio['bitrate'] = $info['mpeg']['audio']['bitrate']; + $thisfile_audio['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']); + if (!empty($info['mpeg']['audio']['codec'])) { + $thisfile_audio['codec'] = $info['mpeg']['audio']['codec'].' '.$thisfile_audio['codec']; + } + if (!empty($thisfile_audio['streams'])) { + foreach ($thisfile_audio['streams'] as $streamnumber => $streamdata) { + if ($streamdata['dataformat'] == $thisfile_audio_dataformat) { + $thisfile_audio['streams'][$streamnumber]['sample_rate'] = $thisfile_audio['sample_rate']; + $thisfile_audio['streams'][$streamnumber]['channels'] = $thisfile_audio['channels']; + $thisfile_audio['streams'][$streamnumber]['bitrate'] = $thisfile_audio['bitrate']; + $thisfile_audio['streams'][$streamnumber]['bitrate_mode'] = $thisfile_audio['bitrate_mode']; + $thisfile_audio['streams'][$streamnumber]['codec'] = $thisfile_audio['codec']; + } + } + } + $getid3_mp3 = new getid3_mp3($this->getid3); + $thisfile_audio['encoder_options'] = $getid3_mp3->GuessEncoderOptions(); + unset($getid3_mp3); + } + + + if (!empty($thisfile_riff_raw['fmt ']['wBitsPerSample']) && ($thisfile_riff_raw['fmt ']['wBitsPerSample'] > 0)) { + switch ($thisfile_audio_dataformat) { + case 'ac3': + // ignore bits_per_sample + break; + + default: + $thisfile_audio['bits_per_sample'] = $thisfile_riff_raw['fmt ']['wBitsPerSample']; + break; + } + } + + + if (empty($thisfile_riff_raw)) { + unset($thisfile_riff['raw']); + } + if (empty($thisfile_riff_audio)) { + unset($thisfile_riff['audio']); + } + if (empty($thisfile_riff_video)) { + unset($thisfile_riff['video']); + } + + return true; + } + + /** + * @param int $startoffset + * @param int $maxoffset + * + * @return array|false + * + * @throws Exception + * @throws getid3_exception + */ + public function ParseRIFFAMV($startoffset, $maxoffset) { + // AMV files are RIFF-AVI files with parts of the spec deliberately broken, such as chunk size fields hardcoded to zero (because players known in hardware that these fields are always a certain size + + // https://code.google.com/p/amv-codec-tools/wiki/AmvDocumentation + //typedef struct _amvmainheader { + //FOURCC fcc; // 'amvh' + //DWORD cb; + //DWORD dwMicroSecPerFrame; + //BYTE reserve[28]; + //DWORD dwWidth; + //DWORD dwHeight; + //DWORD dwSpeed; + //DWORD reserve0; + //DWORD reserve1; + //BYTE bTimeSec; + //BYTE bTimeMin; + //WORD wTimeHour; + //} AMVMAINHEADER; + + $info = &$this->getid3->info; + $RIFFchunk = false; + + try { + + $this->fseek($startoffset); + $maxoffset = min($maxoffset, $info['avdataend']); + $AMVheader = $this->fread(284); + if (substr($AMVheader, 0, 8) != 'hdrlamvh') { + throw new Exception('expecting "hdrlamv" at offset '.($startoffset + 0).', found "'.substr($AMVheader, 0, 8).'"'); + } + if (substr($AMVheader, 8, 4) != "\x38\x00\x00\x00") { // "amvh" chunk size, hardcoded to 0x38 = 56 bytes + throw new Exception('expecting "0x38000000" at offset '.($startoffset + 8).', found "'.getid3_lib::PrintHexBytes(substr($AMVheader, 8, 4)).'"'); + } + $RIFFchunk = array(); + $RIFFchunk['amvh']['us_per_frame'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 12, 4)); + $RIFFchunk['amvh']['reserved28'] = substr($AMVheader, 16, 28); // null? reserved? + $RIFFchunk['amvh']['resolution_x'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 44, 4)); + $RIFFchunk['amvh']['resolution_y'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 48, 4)); + $RIFFchunk['amvh']['frame_rate_int'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 52, 4)); + $RIFFchunk['amvh']['reserved0'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 56, 4)); // 1? reserved? + $RIFFchunk['amvh']['reserved1'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 60, 4)); // 0? reserved? + $RIFFchunk['amvh']['runtime_sec'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 64, 1)); + $RIFFchunk['amvh']['runtime_min'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 65, 1)); + $RIFFchunk['amvh']['runtime_hrs'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 66, 2)); + + $info['video']['frame_rate'] = 1000000 / $RIFFchunk['amvh']['us_per_frame']; + $info['video']['resolution_x'] = $RIFFchunk['amvh']['resolution_x']; + $info['video']['resolution_y'] = $RIFFchunk['amvh']['resolution_y']; + $info['playtime_seconds'] = ($RIFFchunk['amvh']['runtime_hrs'] * 3600) + ($RIFFchunk['amvh']['runtime_min'] * 60) + $RIFFchunk['amvh']['runtime_sec']; + + // the rest is all hardcoded(?) and does not appear to be useful until you get to audio info at offset 256, even then everything is probably hardcoded + + if (substr($AMVheader, 68, 20) != 'LIST'."\x00\x00\x00\x00".'strlstrh'."\x38\x00\x00\x00") { + throw new Exception('expecting "LIST<0x00000000>strlstrh<0x38000000>" at offset '.($startoffset + 68).', found "'.getid3_lib::PrintHexBytes(substr($AMVheader, 68, 20)).'"'); + } + // followed by 56 bytes of null: substr($AMVheader, 88, 56) -> 144 + if (substr($AMVheader, 144, 8) != 'strf'."\x24\x00\x00\x00") { + throw new Exception('expecting "strf<0x24000000>" at offset '.($startoffset + 144).', found "'.getid3_lib::PrintHexBytes(substr($AMVheader, 144, 8)).'"'); + } + // followed by 36 bytes of null: substr($AMVheader, 144, 36) -> 180 + + if (substr($AMVheader, 188, 20) != 'LIST'."\x00\x00\x00\x00".'strlstrh'."\x30\x00\x00\x00") { + throw new Exception('expecting "LIST<0x00000000>strlstrh<0x30000000>" at offset '.($startoffset + 188).', found "'.getid3_lib::PrintHexBytes(substr($AMVheader, 188, 20)).'"'); + } + // followed by 48 bytes of null: substr($AMVheader, 208, 48) -> 256 + if (substr($AMVheader, 256, 8) != 'strf'."\x14\x00\x00\x00") { + throw new Exception('expecting "strf<0x14000000>" at offset '.($startoffset + 256).', found "'.getid3_lib::PrintHexBytes(substr($AMVheader, 256, 8)).'"'); + } + // followed by 20 bytes of a modified WAVEFORMATEX: + // typedef struct { + // WORD wFormatTag; //(Fixme: this is equal to PCM's 0x01 format code) + // WORD nChannels; //(Fixme: this is always 1) + // DWORD nSamplesPerSec; //(Fixme: for all known sample files this is equal to 22050) + // DWORD nAvgBytesPerSec; //(Fixme: for all known sample files this is equal to 44100) + // WORD nBlockAlign; //(Fixme: this seems to be 2 in AMV files, is this correct ?) + // WORD wBitsPerSample; //(Fixme: this seems to be 16 in AMV files instead of the expected 4) + // WORD cbSize; //(Fixme: this seems to be 0 in AMV files) + // WORD reserved; + // } WAVEFORMATEX; + $RIFFchunk['strf']['wformattag'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 264, 2)); + $RIFFchunk['strf']['nchannels'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 266, 2)); + $RIFFchunk['strf']['nsamplespersec'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 268, 4)); + $RIFFchunk['strf']['navgbytespersec'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 272, 4)); + $RIFFchunk['strf']['nblockalign'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 276, 2)); + $RIFFchunk['strf']['wbitspersample'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 278, 2)); + $RIFFchunk['strf']['cbsize'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 280, 2)); + $RIFFchunk['strf']['reserved'] = getid3_lib::LittleEndian2Int(substr($AMVheader, 282, 2)); + + + $info['audio']['lossless'] = false; + $info['audio']['sample_rate'] = $RIFFchunk['strf']['nsamplespersec']; + $info['audio']['channels'] = $RIFFchunk['strf']['nchannels']; + $info['audio']['bits_per_sample'] = $RIFFchunk['strf']['wbitspersample']; + $info['audio']['bitrate'] = $info['audio']['sample_rate'] * $info['audio']['channels'] * $info['audio']['bits_per_sample']; + $info['audio']['bitrate_mode'] = 'cbr'; + + + } catch (getid3_exception $e) { + if ($e->getCode() == 10) { + $this->warning('RIFFAMV parser: '.$e->getMessage()); + } else { + throw $e; + } + } + + return $RIFFchunk; + } + + /** + * @param int $startoffset + * @param int $maxoffset + * + * @return array|false + * @throws getid3_exception + */ + public function ParseRIFF($startoffset, $maxoffset) { + $info = &$this->getid3->info; + + $RIFFchunk = false; + $FoundAllChunksWeNeed = false; + + try { + $this->fseek($startoffset); + $maxoffset = min($maxoffset, $info['avdataend']); + while ($this->ftell() < $maxoffset) { + $chunknamesize = $this->fread(8); + //$chunkname = substr($chunknamesize, 0, 4); + $chunkname = str_replace("\x00", '_', substr($chunknamesize, 0, 4)); // note: chunk names of 4 null bytes do appear to be legal (has been observed inside INFO and PRMI chunks, for example), but makes traversing array keys more difficult + $chunksize = $this->EitherEndian2Int(substr($chunknamesize, 4, 4)); + //if (strlen(trim($chunkname, "\x00")) < 4) { + if (strlen($chunkname) < 4) { + $this->error('Expecting chunk name at offset '.($this->ftell() - 8).' but found nothing. Aborting RIFF parsing.'); + break; + } + if (($chunksize == 0) && ($chunkname != 'JUNK')) { + $this->warning('Chunk ('.$chunkname.') size at offset '.($this->ftell() - 4).' is zero. Aborting RIFF parsing.'); + break; + } + if (($chunksize % 2) != 0) { + // all structures are packed on word boundaries + $chunksize++; + } + + switch ($chunkname) { + case 'LIST': + $listname = $this->fread(4); + if (preg_match('#^(movi|rec )$#i', $listname)) { + $RIFFchunk[$listname]['offset'] = $this->ftell() - 4; + $RIFFchunk[$listname]['size'] = $chunksize; + + if (!$FoundAllChunksWeNeed) { + $WhereWeWere = $this->ftell(); + $AudioChunkHeader = $this->fread(12); + $AudioChunkStreamNum = substr($AudioChunkHeader, 0, 2); + $AudioChunkStreamType = substr($AudioChunkHeader, 2, 2); + $AudioChunkSize = getid3_lib::LittleEndian2Int(substr($AudioChunkHeader, 4, 4)); + + if ($AudioChunkStreamType == 'wb') { + $FirstFourBytes = substr($AudioChunkHeader, 8, 4); + if (preg_match('/^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]/s', $FirstFourBytes)) { + // MP3 + if (getid3_mp3::MPEGaudioHeaderBytesValid($FirstFourBytes)) { + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_temp->info['avdataoffset'] = $this->ftell() - 4; + $getid3_temp->info['avdataend'] = $this->ftell() + $AudioChunkSize; + $getid3_mp3 = new getid3_mp3($getid3_temp, __CLASS__); + $getid3_mp3->getOnlyMPEGaudioInfo($getid3_temp->info['avdataoffset'], false); + if (isset($getid3_temp->info['mpeg']['audio'])) { + $info['mpeg']['audio'] = $getid3_temp->info['mpeg']['audio']; + $info['audio'] = $getid3_temp->info['audio']; + $info['audio']['dataformat'] = 'mp'.$info['mpeg']['audio']['layer']; + $info['audio']['sample_rate'] = $info['mpeg']['audio']['sample_rate']; + $info['audio']['channels'] = $info['mpeg']['audio']['channels']; + $info['audio']['bitrate'] = $info['mpeg']['audio']['bitrate']; + $info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']); + //$info['bitrate'] = $info['audio']['bitrate']; + } + unset($getid3_temp, $getid3_mp3); + } + + } elseif (strpos($FirstFourBytes, getid3_ac3::syncword) === 0) { + + // AC3 + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_temp->info['avdataoffset'] = $this->ftell() - 4; + $getid3_temp->info['avdataend'] = $this->ftell() + $AudioChunkSize; + $getid3_ac3 = new getid3_ac3($getid3_temp); + $getid3_ac3->Analyze(); + if (empty($getid3_temp->info['error'])) { + $info['audio'] = $getid3_temp->info['audio']; + $info['ac3'] = $getid3_temp->info['ac3']; + if (!empty($getid3_temp->info['warning'])) { + foreach ($getid3_temp->info['warning'] as $key => $value) { + $this->warning($value); + } + } + } + unset($getid3_temp, $getid3_ac3); + } + } + $FoundAllChunksWeNeed = true; + $this->fseek($WhereWeWere); + } + $this->fseek($chunksize - 4, SEEK_CUR); + + } else { + + if (!isset($RIFFchunk[$listname])) { + $RIFFchunk[$listname] = array(); + } + $LISTchunkParent = $listname; + $LISTchunkMaxOffset = $this->ftell() - 4 + $chunksize; + if ($parsedChunk = $this->ParseRIFF($this->ftell(), $LISTchunkMaxOffset)) { + $RIFFchunk[$listname] = array_merge_recursive($RIFFchunk[$listname], $parsedChunk); + } + + } + break; + + default: + if (preg_match('#^[0-9]{2}(wb|pc|dc|db)$#', $chunkname)) { + $this->fseek($chunksize, SEEK_CUR); + break; + } + $thisindex = 0; + if (isset($RIFFchunk[$chunkname]) && is_array($RIFFchunk[$chunkname])) { + $thisindex = count($RIFFchunk[$chunkname]); + } + $RIFFchunk[$chunkname][$thisindex]['offset'] = $this->ftell() - 8; + $RIFFchunk[$chunkname][$thisindex]['size'] = $chunksize; + switch ($chunkname) { + case 'data': + $info['avdataoffset'] = $this->ftell(); + $info['avdataend'] = $info['avdataoffset'] + $chunksize; + + $testData = $this->fread(36); + if ($testData === '') { + break; + } + if (preg_match('/^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]/s', substr($testData, 0, 4))) { + + // Probably is MP3 data + if (getid3_mp3::MPEGaudioHeaderBytesValid(substr($testData, 0, 4))) { + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_temp->info['avdataoffset'] = $info['avdataoffset']; + $getid3_temp->info['avdataend'] = $info['avdataend']; + $getid3_mp3 = new getid3_mp3($getid3_temp, __CLASS__); + $getid3_mp3->getOnlyMPEGaudioInfo($info['avdataoffset'], false); + if (empty($getid3_temp->info['error'])) { + $info['audio'] = $getid3_temp->info['audio']; + $info['mpeg'] = $getid3_temp->info['mpeg']; + } + unset($getid3_temp, $getid3_mp3); + } + + } elseif (($isRegularAC3 = (substr($testData, 0, 2) == getid3_ac3::syncword)) || substr($testData, 8, 2) == strrev(getid3_ac3::syncword)) { + + // This is probably AC-3 data + $getid3_temp = new getID3(); + if ($isRegularAC3) { + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_temp->info['avdataoffset'] = $info['avdataoffset']; + $getid3_temp->info['avdataend'] = $info['avdataend']; + } + $getid3_ac3 = new getid3_ac3($getid3_temp); + if ($isRegularAC3) { + $getid3_ac3->Analyze(); + } else { + // Dolby Digital WAV + // AC-3 content, but not encoded in same format as normal AC-3 file + // For one thing, byte order is swapped + $ac3_data = ''; + for ($i = 0; $i < 28; $i += 2) { + $ac3_data .= substr($testData, 8 + $i + 1, 1); + $ac3_data .= substr($testData, 8 + $i + 0, 1); + } + $getid3_ac3->AnalyzeString($ac3_data); + } + + if (empty($getid3_temp->info['error'])) { + $info['audio'] = $getid3_temp->info['audio']; + $info['ac3'] = $getid3_temp->info['ac3']; + if (!empty($getid3_temp->info['warning'])) { + foreach ($getid3_temp->info['warning'] as $newerror) { + $this->warning('getid3_ac3() says: ['.$newerror.']'); + } + } + } + unset($getid3_temp, $getid3_ac3); + + } elseif (preg_match('/^('.implode('|', array_map('preg_quote', getid3_dts::$syncwords)).')/', $testData)) { + + // This is probably DTS data + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp); + $getid3_temp->info['avdataoffset'] = $info['avdataoffset']; + $getid3_dts = new getid3_dts($getid3_temp); + $getid3_dts->Analyze(); + if (empty($getid3_temp->info['error'])) { + $info['audio'] = $getid3_temp->info['audio']; + $info['dts'] = $getid3_temp->info['dts']; + $info['playtime_seconds'] = $getid3_temp->info['playtime_seconds']; // may not match RIFF calculations since DTS-WAV often used 14/16 bit-word packing + if (!empty($getid3_temp->info['warning'])) { + foreach ($getid3_temp->info['warning'] as $newerror) { + $this->warning('getid3_dts() says: ['.$newerror.']'); + } + } + } + + unset($getid3_temp, $getid3_dts); + + } elseif (substr($testData, 0, 4) == 'wvpk') { + + // This is WavPack data + $info['wavpack']['offset'] = $info['avdataoffset']; + $info['wavpack']['size'] = getid3_lib::LittleEndian2Int(substr($testData, 4, 4)); + $this->parseWavPackHeader(substr($testData, 8, 28)); + + } else { + // This is some other kind of data (quite possibly just PCM) + // do nothing special, just skip it + } + $nextoffset = $info['avdataend']; + $this->fseek($nextoffset); + break; + + case 'iXML': + case 'bext': + case 'cart': + case 'fmt ': + case 'strh': + case 'strf': + case 'indx': + case 'MEXT': + case 'DISP': + // always read data in + case 'JUNK': + // should be: never read data in + // but some programs write their version strings in a JUNK chunk (e.g. VirtualDub, AVIdemux, etc) + if ($chunksize < 1048576) { + if ($chunksize > 0) { + $RIFFchunk[$chunkname][$thisindex]['data'] = $this->fread($chunksize); + if ($chunkname == 'JUNK') { + if (preg_match('#^([\\x20-\\x7F]+)#', $RIFFchunk[$chunkname][$thisindex]['data'], $matches)) { + // only keep text characters [chr(32)-chr(127)] + $info['riff']['comments']['junk'][] = trim($matches[1]); + } + // but if nothing there, ignore + // remove the key in either case + unset($RIFFchunk[$chunkname][$thisindex]['data']); + } + } + } else { + $this->warning('Chunk "'.$chunkname.'" at offset '.$this->ftell().' is unexpectedly larger than 1MB (claims to be '.number_format($chunksize).' bytes), skipping data'); + $this->fseek($chunksize, SEEK_CUR); + } + break; + + //case 'IDVX': + // $info['divxtag']['comments'] = self::ParseDIVXTAG($this->fread($chunksize)); + // break; + + case 'scot': + // https://cmsdk.com/node-js/adding-scot-chunk-to-wav-file.html + $RIFFchunk[$chunkname][$thisindex]['data'] = $this->fread($chunksize); + $RIFFchunk[$chunkname][$thisindex]['parsed']['alter'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 0, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['attrib'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 1, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['artnum'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 2, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['title'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 4, 43); // "name" in other documentation + $RIFFchunk[$chunkname][$thisindex]['parsed']['copy'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 47, 4); + $RIFFchunk[$chunkname][$thisindex]['parsed']['padd'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 51, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['asclen'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 52, 5); + $RIFFchunk[$chunkname][$thisindex]['parsed']['startseconds'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 57, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['starthundredths'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 59, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['endseconds'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 61, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['endhundreths'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 63, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['sdate'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 65, 6); + $RIFFchunk[$chunkname][$thisindex]['parsed']['kdate'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 71, 6); + $RIFFchunk[$chunkname][$thisindex]['parsed']['start_hr'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 77, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['kill_hr'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 78, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['digital'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 79, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 80, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['stereo'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 82, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['compress'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 83, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['eomstrt'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 84, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['eomlen'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 88, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['attrib2'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 90, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['future1'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 94, 12); + $RIFFchunk[$chunkname][$thisindex]['parsed']['catfontcolor'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 106, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['catcolor'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 110, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['segeompos'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 114, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['vt_startsecs'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 118, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['vt_starthunds'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 120, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['priorcat'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 122, 3); + $RIFFchunk[$chunkname][$thisindex]['parsed']['priorcopy'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 125, 4); + $RIFFchunk[$chunkname][$thisindex]['parsed']['priorpadd'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 129, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['postcat'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 130, 3); + $RIFFchunk[$chunkname][$thisindex]['parsed']['postcopy'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 133, 4); + $RIFFchunk[$chunkname][$thisindex]['parsed']['postpadd'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 137, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['hrcanplay'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 138, 21); + $RIFFchunk[$chunkname][$thisindex]['parsed']['future2'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 159, 108); + $RIFFchunk[$chunkname][$thisindex]['parsed']['artist'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 267, 34); + $RIFFchunk[$chunkname][$thisindex]['parsed']['comment'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 301, 34); // "trivia" in other documentation + $RIFFchunk[$chunkname][$thisindex]['parsed']['intro'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 335, 2); + $RIFFchunk[$chunkname][$thisindex]['parsed']['end'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 337, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['year'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 338, 4); + $RIFFchunk[$chunkname][$thisindex]['parsed']['obsolete2'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 342, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['rec_hr'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 343, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['rdate'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 344, 6); + $RIFFchunk[$chunkname][$thisindex]['parsed']['mpeg_bitrate'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 350, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['pitch'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 352, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['playlevel'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 354, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['lenvalid'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 356, 1); + $RIFFchunk[$chunkname][$thisindex]['parsed']['filelength'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 357, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['newplaylevel'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 361, 2)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['chopsize'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 363, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['vteomovr'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 367, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['desiredlen'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 371, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['triggers'] = getid3_lib::LittleEndian2Int(substr($RIFFchunk[$chunkname][$thisindex]['data'], 375, 4)); + $RIFFchunk[$chunkname][$thisindex]['parsed']['fillout'] = substr($RIFFchunk[$chunkname][$thisindex]['data'], 379, 33); + + foreach (array('title', 'artist', 'comment') as $key) { + if (trim($RIFFchunk[$chunkname][$thisindex]['parsed'][$key])) { + $info['riff']['comments'][$key] = array($RIFFchunk[$chunkname][$thisindex]['parsed'][$key]); + } + } + if ($RIFFchunk[$chunkname][$thisindex]['parsed']['filelength'] && !empty($info['filesize']) && ($RIFFchunk[$chunkname][$thisindex]['parsed']['filelength'] != $info['filesize'])) { + $this->warning('RIFF.WAVE.scot.filelength ('.$RIFFchunk[$chunkname][$thisindex]['parsed']['filelength'].') different from actual filesize ('.$info['filesize'].')'); + } + break; + + default: + if (!empty($LISTchunkParent) && isset($LISTchunkMaxOffset) && (($RIFFchunk[$chunkname][$thisindex]['offset'] + $RIFFchunk[$chunkname][$thisindex]['size']) <= $LISTchunkMaxOffset)) { + $RIFFchunk[$LISTchunkParent][$chunkname][$thisindex]['offset'] = $RIFFchunk[$chunkname][$thisindex]['offset']; + $RIFFchunk[$LISTchunkParent][$chunkname][$thisindex]['size'] = $RIFFchunk[$chunkname][$thisindex]['size']; + unset($RIFFchunk[$chunkname][$thisindex]['offset']); + unset($RIFFchunk[$chunkname][$thisindex]['size']); + if (isset($RIFFchunk[$chunkname][$thisindex]) && empty($RIFFchunk[$chunkname][$thisindex])) { + unset($RIFFchunk[$chunkname][$thisindex]); + } + if (isset($RIFFchunk[$chunkname]) && empty($RIFFchunk[$chunkname])) { + unset($RIFFchunk[$chunkname]); + } + $RIFFchunk[$LISTchunkParent][$chunkname][$thisindex]['data'] = $this->fread($chunksize); + } elseif ($chunksize < 2048) { + // only read data in if smaller than 2kB + $RIFFchunk[$chunkname][$thisindex]['data'] = $this->fread($chunksize); + } else { + $this->fseek($chunksize, SEEK_CUR); + } + break; + } + break; + } + } + + } catch (getid3_exception $e) { + if ($e->getCode() == 10) { + $this->warning('RIFF parser: '.$e->getMessage()); + } else { + throw $e; + } + } + + return $RIFFchunk; + } + + /** + * @param string $RIFFdata + * + * @return bool + */ + public function ParseRIFFdata(&$RIFFdata) { + $info = &$this->getid3->info; + if ($RIFFdata) { + $tempfile = tempnam(GETID3_TEMP_DIR, 'getID3'); + $fp_temp = fopen($tempfile, 'wb'); + $RIFFdataLength = strlen($RIFFdata); + $NewLengthString = getid3_lib::LittleEndian2String($RIFFdataLength, 4); + for ($i = 0; $i < 4; $i++) { + $RIFFdata[($i + 4)] = $NewLengthString[$i]; + } + fwrite($fp_temp, $RIFFdata); + fclose($fp_temp); + + $getid3_temp = new getID3(); + $getid3_temp->openfile($tempfile); + $getid3_temp->info['filesize'] = $RIFFdataLength; + $getid3_temp->info['filenamepath'] = $info['filenamepath']; + $getid3_temp->info['tags'] = $info['tags']; + $getid3_temp->info['warning'] = $info['warning']; + $getid3_temp->info['error'] = $info['error']; + $getid3_temp->info['comments'] = $info['comments']; + $getid3_temp->info['audio'] = (isset($info['audio']) ? $info['audio'] : array()); + $getid3_temp->info['video'] = (isset($info['video']) ? $info['video'] : array()); + $getid3_riff = new getid3_riff($getid3_temp); + $getid3_riff->Analyze(); + + $info['riff'] = $getid3_temp->info['riff']; + $info['warning'] = $getid3_temp->info['warning']; + $info['error'] = $getid3_temp->info['error']; + $info['tags'] = $getid3_temp->info['tags']; + $info['comments'] = $getid3_temp->info['comments']; + unset($getid3_riff, $getid3_temp); + unlink($tempfile); + } + return false; + } + + /** + * @param array $RIFFinfoArray + * @param array $CommentsTargetArray + * + * @return bool + */ + public static function parseComments(&$RIFFinfoArray, &$CommentsTargetArray) { + $RIFFinfoKeyLookup = array( + 'IARL'=>'archivallocation', + 'IART'=>'artist', + 'ICDS'=>'costumedesigner', + 'ICMS'=>'commissionedby', + 'ICMT'=>'comment', + 'ICNT'=>'country', + 'ICOP'=>'copyright', + 'ICRD'=>'creationdate', + 'IDIM'=>'dimensions', + 'IDIT'=>'digitizationdate', + 'IDPI'=>'resolution', + 'IDST'=>'distributor', + 'IEDT'=>'editor', + 'IENG'=>'engineers', + 'IFRM'=>'accountofparts', + 'IGNR'=>'genre', + 'IKEY'=>'keywords', + 'ILGT'=>'lightness', + 'ILNG'=>'language', + 'IMED'=>'orignalmedium', + 'IMUS'=>'composer', + 'INAM'=>'title', + 'IPDS'=>'productiondesigner', + 'IPLT'=>'palette', + 'IPRD'=>'product', + 'IPRO'=>'producer', + 'IPRT'=>'part', + 'IRTD'=>'rating', + 'ISBJ'=>'subject', + 'ISFT'=>'software', + 'ISGN'=>'secondarygenre', + 'ISHP'=>'sharpness', + 'ISRC'=>'sourcesupplier', + 'ISRF'=>'digitizationsource', + 'ISTD'=>'productionstudio', + 'ISTR'=>'starring', + 'ITCH'=>'encoded_by', + 'IWEB'=>'url', + 'IWRI'=>'writer', + '____'=>'comment', + ); + foreach ($RIFFinfoKeyLookup as $key => $value) { + if (isset($RIFFinfoArray[$key])) { + foreach ($RIFFinfoArray[$key] as $commentid => $commentdata) { + if (trim($commentdata['data']) != '') { + if (isset($CommentsTargetArray[$value])) { + $CommentsTargetArray[$value][] = trim($commentdata['data']); + } else { + $CommentsTargetArray[$value] = array(trim($commentdata['data'])); + } + } + } + } + } + return true; + } + + /** + * @param string $WaveFormatExData + * + * @return array + */ + public static function parseWAVEFORMATex($WaveFormatExData) { + // shortcut + $WaveFormatEx = array(); + $WaveFormatEx['raw'] = array(); + $WaveFormatEx_raw = &$WaveFormatEx['raw']; + + $WaveFormatEx_raw['wFormatTag'] = substr($WaveFormatExData, 0, 2); + $WaveFormatEx_raw['nChannels'] = substr($WaveFormatExData, 2, 2); + $WaveFormatEx_raw['nSamplesPerSec'] = substr($WaveFormatExData, 4, 4); + $WaveFormatEx_raw['nAvgBytesPerSec'] = substr($WaveFormatExData, 8, 4); + $WaveFormatEx_raw['nBlockAlign'] = substr($WaveFormatExData, 12, 2); + $WaveFormatEx_raw['wBitsPerSample'] = substr($WaveFormatExData, 14, 2); + if (strlen($WaveFormatExData) > 16) { + $WaveFormatEx_raw['cbSize'] = substr($WaveFormatExData, 16, 2); + } + $WaveFormatEx_raw = array_map('getid3_lib::LittleEndian2Int', $WaveFormatEx_raw); + + $WaveFormatEx['codec'] = self::wFormatTagLookup($WaveFormatEx_raw['wFormatTag']); + $WaveFormatEx['channels'] = $WaveFormatEx_raw['nChannels']; + $WaveFormatEx['sample_rate'] = $WaveFormatEx_raw['nSamplesPerSec']; + $WaveFormatEx['bitrate'] = $WaveFormatEx_raw['nAvgBytesPerSec'] * 8; + $WaveFormatEx['bits_per_sample'] = $WaveFormatEx_raw['wBitsPerSample']; + + return $WaveFormatEx; + } + + /** + * @param string $WavPackChunkData + * + * @return bool + */ + public function parseWavPackHeader($WavPackChunkData) { + // typedef struct { + // char ckID [4]; + // long ckSize; + // short version; + // short bits; // added for version 2.00 + // short flags, shift; // added for version 3.00 + // long total_samples, crc, crc2; + // char extension [4], extra_bc, extras [3]; + // } WavpackHeader; + + // shortcut + $info = &$this->getid3->info; + $info['wavpack'] = array(); + $thisfile_wavpack = &$info['wavpack']; + + $thisfile_wavpack['version'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 0, 2)); + if ($thisfile_wavpack['version'] >= 2) { + $thisfile_wavpack['bits'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 2, 2)); + } + if ($thisfile_wavpack['version'] >= 3) { + $thisfile_wavpack['flags_raw'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 4, 2)); + $thisfile_wavpack['shift'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 6, 2)); + $thisfile_wavpack['total_samples'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 8, 4)); + $thisfile_wavpack['crc1'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 12, 4)); + $thisfile_wavpack['crc2'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 16, 4)); + $thisfile_wavpack['extension'] = substr($WavPackChunkData, 20, 4); + $thisfile_wavpack['extra_bc'] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 24, 1)); + for ($i = 0; $i <= 2; $i++) { + $thisfile_wavpack['extras'][] = getid3_lib::LittleEndian2Int(substr($WavPackChunkData, 25 + $i, 1)); + } + + // shortcut + $thisfile_wavpack['flags'] = array(); + $thisfile_wavpack_flags = &$thisfile_wavpack['flags']; + + $thisfile_wavpack_flags['mono'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000001); + $thisfile_wavpack_flags['fast_mode'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000002); + $thisfile_wavpack_flags['raw_mode'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000004); + $thisfile_wavpack_flags['calc_noise'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000008); + $thisfile_wavpack_flags['high_quality'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000010); + $thisfile_wavpack_flags['3_byte_samples'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000020); + $thisfile_wavpack_flags['over_20_bits'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000040); + $thisfile_wavpack_flags['use_wvc'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000080); + $thisfile_wavpack_flags['noiseshaping'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000100); + $thisfile_wavpack_flags['very_fast_mode'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000200); + $thisfile_wavpack_flags['new_high_quality'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000400); + $thisfile_wavpack_flags['cancel_extreme'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x000800); + $thisfile_wavpack_flags['cross_decorrelation'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x001000); + $thisfile_wavpack_flags['new_decorrelation'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x002000); + $thisfile_wavpack_flags['joint_stereo'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x004000); + $thisfile_wavpack_flags['extra_decorrelation'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x008000); + $thisfile_wavpack_flags['override_noiseshape'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x010000); + $thisfile_wavpack_flags['override_jointstereo'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x020000); + $thisfile_wavpack_flags['copy_source_filetime'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x040000); + $thisfile_wavpack_flags['create_exe'] = (bool) ($thisfile_wavpack['flags_raw'] & 0x080000); + } + + return true; + } + + /** + * @param string $BITMAPINFOHEADER + * @param bool $littleEndian + * + * @return array + */ + public static function ParseBITMAPINFOHEADER($BITMAPINFOHEADER, $littleEndian=true) { + + $parsed['biSize'] = substr($BITMAPINFOHEADER, 0, 4); // number of bytes required by the BITMAPINFOHEADER structure + $parsed['biWidth'] = substr($BITMAPINFOHEADER, 4, 4); // width of the bitmap in pixels + $parsed['biHeight'] = substr($BITMAPINFOHEADER, 8, 4); // height of the bitmap in pixels. If biHeight is positive, the bitmap is a 'bottom-up' DIB and its origin is the lower left corner. If biHeight is negative, the bitmap is a 'top-down' DIB and its origin is the upper left corner + $parsed['biPlanes'] = substr($BITMAPINFOHEADER, 12, 2); // number of color planes on the target device. In most cases this value must be set to 1 + $parsed['biBitCount'] = substr($BITMAPINFOHEADER, 14, 2); // Specifies the number of bits per pixels + $parsed['biSizeImage'] = substr($BITMAPINFOHEADER, 20, 4); // size of the bitmap data section of the image (the actual pixel data, excluding BITMAPINFOHEADER and RGBQUAD structures) + $parsed['biXPelsPerMeter'] = substr($BITMAPINFOHEADER, 24, 4); // horizontal resolution, in pixels per metre, of the target device + $parsed['biYPelsPerMeter'] = substr($BITMAPINFOHEADER, 28, 4); // vertical resolution, in pixels per metre, of the target device + $parsed['biClrUsed'] = substr($BITMAPINFOHEADER, 32, 4); // actual number of color indices in the color table used by the bitmap. If this value is zero, the bitmap uses the maximum number of colors corresponding to the value of the biBitCount member for the compression mode specified by biCompression + $parsed['biClrImportant'] = substr($BITMAPINFOHEADER, 36, 4); // number of color indices that are considered important for displaying the bitmap. If this value is zero, all colors are important + $parsed = array_map('getid3_lib::'.($littleEndian ? 'Little' : 'Big').'Endian2Int', $parsed); + + $parsed['fourcc'] = substr($BITMAPINFOHEADER, 16, 4); // compression identifier + + return $parsed; + } + + /** + * @param string $DIVXTAG + * @param bool $raw + * + * @return array + */ + public static function ParseDIVXTAG($DIVXTAG, $raw=false) { + // structure from "IDivX" source, Form1.frm, by "Greg Frazier of Daemonic Software Group", email: gfrazier@icestorm.net, web: http://dsg.cjb.net/ + // source available at http://files.divx-digest.com/download/c663efe7ef8ad2e90bf4af4d3ea6188a/on0SWN2r/edit/IDivX.zip + // 'Byte Layout: '1111111111111111 + // '32 for Movie - 1 '1111111111111111 + // '28 for Author - 6 '6666666666666666 + // '4 for year - 2 '6666666666662222 + // '3 for genre - 3 '7777777777777777 + // '48 for Comments - 7 '7777777777777777 + // '1 for Rating - 4 '7777777777777777 + // '5 for Future Additions - 0 '333400000DIVXTAG + // '128 bytes total + + static $DIVXTAGgenre = array( + 0 => 'Action', + 1 => 'Action/Adventure', + 2 => 'Adventure', + 3 => 'Adult', + 4 => 'Anime', + 5 => 'Cartoon', + 6 => 'Claymation', + 7 => 'Comedy', + 8 => 'Commercial', + 9 => 'Documentary', + 10 => 'Drama', + 11 => 'Home Video', + 12 => 'Horror', + 13 => 'Infomercial', + 14 => 'Interactive', + 15 => 'Mystery', + 16 => 'Music Video', + 17 => 'Other', + 18 => 'Religion', + 19 => 'Sci Fi', + 20 => 'Thriller', + 21 => 'Western', + ), + $DIVXTAGrating = array( + 0 => 'Unrated', + 1 => 'G', + 2 => 'PG', + 3 => 'PG-13', + 4 => 'R', + 5 => 'NC-17', + ); + + $parsed = array(); + $parsed['title'] = trim(substr($DIVXTAG, 0, 32)); + $parsed['artist'] = trim(substr($DIVXTAG, 32, 28)); + $parsed['year'] = intval(trim(substr($DIVXTAG, 60, 4))); + $parsed['comment'] = trim(substr($DIVXTAG, 64, 48)); + $parsed['genre_id'] = intval(trim(substr($DIVXTAG, 112, 3))); + $parsed['rating_id'] = ord(substr($DIVXTAG, 115, 1)); + //$parsed['padding'] = substr($DIVXTAG, 116, 5); // 5-byte null + //$parsed['magic'] = substr($DIVXTAG, 121, 7); // "DIVXTAG" + + $parsed['genre'] = (isset($DIVXTAGgenre[$parsed['genre_id']]) ? $DIVXTAGgenre[$parsed['genre_id']] : $parsed['genre_id']); + $parsed['rating'] = (isset($DIVXTAGrating[$parsed['rating_id']]) ? $DIVXTAGrating[$parsed['rating_id']] : $parsed['rating_id']); + + if (!$raw) { + unset($parsed['genre_id'], $parsed['rating_id']); + foreach ($parsed as $key => $value) { + if (empty($value)) { + unset($parsed[$key]); + } + } + } + + foreach ($parsed as $tag => $value) { + $parsed[$tag] = array($value); + } + + return $parsed; + } + + /** + * @param string $tagshortname + * + * @return string + */ + public static function waveSNDMtagLookup($tagshortname) { + $begin = __LINE__; + + /** This is not a comment! + + ©kwd keywords + ©BPM bpm + ©trt tracktitle + ©des description + ©gen category + ©fin featuredinstrument + ©LID longid + ©bex bwdescription + ©pub publisher + ©cdt cdtitle + ©alb library + ©com composer + + */ + + return getid3_lib::EmbeddedLookup($tagshortname, $begin, __LINE__, __FILE__, 'riff-sndm'); + } + + /** + * @param int $wFormatTag + * + * @return string + */ + public static function wFormatTagLookup($wFormatTag) { + + $begin = __LINE__; + + /** This is not a comment! + + 0x0000 Microsoft Unknown Wave Format + 0x0001 Pulse Code Modulation (PCM) + 0x0002 Microsoft ADPCM + 0x0003 IEEE Float + 0x0004 Compaq Computer VSELP + 0x0005 IBM CVSD + 0x0006 Microsoft A-Law + 0x0007 Microsoft mu-Law + 0x0008 Microsoft DTS + 0x0010 OKI ADPCM + 0x0011 Intel DVI/IMA ADPCM + 0x0012 Videologic MediaSpace ADPCM + 0x0013 Sierra Semiconductor ADPCM + 0x0014 Antex Electronics G.723 ADPCM + 0x0015 DSP Solutions DigiSTD + 0x0016 DSP Solutions DigiFIX + 0x0017 Dialogic OKI ADPCM + 0x0018 MediaVision ADPCM + 0x0019 Hewlett-Packard CU + 0x0020 Yamaha ADPCM + 0x0021 Speech Compression Sonarc + 0x0022 DSP Group TrueSpeech + 0x0023 Echo Speech EchoSC1 + 0x0024 Audiofile AF36 + 0x0025 Audio Processing Technology APTX + 0x0026 AudioFile AF10 + 0x0027 Prosody 1612 + 0x0028 LRC + 0x0030 Dolby AC2 + 0x0031 Microsoft GSM 6.10 + 0x0032 MSNAudio + 0x0033 Antex Electronics ADPCME + 0x0034 Control Resources VQLPC + 0x0035 DSP Solutions DigiREAL + 0x0036 DSP Solutions DigiADPCM + 0x0037 Control Resources CR10 + 0x0038 Natural MicroSystems VBXADPCM + 0x0039 Crystal Semiconductor IMA ADPCM + 0x003A EchoSC3 + 0x003B Rockwell ADPCM + 0x003C Rockwell Digit LK + 0x003D Xebec + 0x0040 Antex Electronics G.721 ADPCM + 0x0041 G.728 CELP + 0x0042 MSG723 + 0x0050 MPEG Layer-2 or Layer-1 + 0x0052 RT24 + 0x0053 PAC + 0x0055 MPEG Layer-3 + 0x0059 Lucent G.723 + 0x0060 Cirrus + 0x0061 ESPCM + 0x0062 Voxware + 0x0063 Canopus Atrac + 0x0064 G.726 ADPCM + 0x0065 G.722 ADPCM + 0x0066 DSAT + 0x0067 DSAT Display + 0x0069 Voxware Byte Aligned + 0x0070 Voxware AC8 + 0x0071 Voxware AC10 + 0x0072 Voxware AC16 + 0x0073 Voxware AC20 + 0x0074 Voxware MetaVoice + 0x0075 Voxware MetaSound + 0x0076 Voxware RT29HW + 0x0077 Voxware VR12 + 0x0078 Voxware VR18 + 0x0079 Voxware TQ40 + 0x0080 Softsound + 0x0081 Voxware TQ60 + 0x0082 MSRT24 + 0x0083 G.729A + 0x0084 MVI MV12 + 0x0085 DF G.726 + 0x0086 DF GSM610 + 0x0088 ISIAudio + 0x0089 Onlive + 0x0091 SBC24 + 0x0092 Dolby AC3 SPDIF + 0x0093 MediaSonic G.723 + 0x0094 Aculab PLC Prosody 8kbps + 0x0097 ZyXEL ADPCM + 0x0098 Philips LPCBB + 0x0099 Packed + 0x00FF AAC + 0x0100 Rhetorex ADPCM + 0x0101 IBM mu-law + 0x0102 IBM A-law + 0x0103 IBM AVC Adaptive Differential Pulse Code Modulation (ADPCM) + 0x0111 Vivo G.723 + 0x0112 Vivo Siren + 0x0123 Digital G.723 + 0x0125 Sanyo LD ADPCM + 0x0130 Sipro Lab Telecom ACELP NET + 0x0131 Sipro Lab Telecom ACELP 4800 + 0x0132 Sipro Lab Telecom ACELP 8V3 + 0x0133 Sipro Lab Telecom G.729 + 0x0134 Sipro Lab Telecom G.729A + 0x0135 Sipro Lab Telecom Kelvin + 0x0140 Windows Media Video V8 + 0x0150 Qualcomm PureVoice + 0x0151 Qualcomm HalfRate + 0x0155 Ring Zero Systems TUB GSM + 0x0160 Microsoft Audio 1 + 0x0161 Windows Media Audio V7 / V8 / V9 + 0x0162 Windows Media Audio Professional V9 + 0x0163 Windows Media Audio Lossless V9 + 0x0200 Creative Labs ADPCM + 0x0202 Creative Labs Fastspeech8 + 0x0203 Creative Labs Fastspeech10 + 0x0210 UHER Informatic GmbH ADPCM + 0x0220 Quarterdeck + 0x0230 I-link Worldwide VC + 0x0240 Aureal RAW Sport + 0x0250 Interactive Products HSX + 0x0251 Interactive Products RPELP + 0x0260 Consistent Software CS2 + 0x0270 Sony SCX + 0x0300 Fujitsu FM Towns Snd + 0x0400 BTV Digital + 0x0401 Intel Music Coder + 0x0450 QDesign Music + 0x0680 VME VMPCM + 0x0681 AT&T Labs TPC + 0x08AE ClearJump LiteWave + 0x1000 Olivetti GSM + 0x1001 Olivetti ADPCM + 0x1002 Olivetti CELP + 0x1003 Olivetti SBC + 0x1004 Olivetti OPR + 0x1100 Lernout & Hauspie Codec (0x1100) + 0x1101 Lernout & Hauspie CELP Codec (0x1101) + 0x1102 Lernout & Hauspie SBC Codec (0x1102) + 0x1103 Lernout & Hauspie SBC Codec (0x1103) + 0x1104 Lernout & Hauspie SBC Codec (0x1104) + 0x1400 Norris + 0x1401 AT&T ISIAudio + 0x1500 Soundspace Music Compression + 0x181C VoxWare RT24 Speech + 0x1FC4 NCT Soft ALF2CD (www.nctsoft.com) + 0x2000 Dolby AC3 + 0x2001 Dolby DTS + 0x2002 WAVE_FORMAT_14_4 + 0x2003 WAVE_FORMAT_28_8 + 0x2004 WAVE_FORMAT_COOK + 0x2005 WAVE_FORMAT_DNET + 0x674F Ogg Vorbis 1 + 0x6750 Ogg Vorbis 2 + 0x6751 Ogg Vorbis 3 + 0x676F Ogg Vorbis 1+ + 0x6770 Ogg Vorbis 2+ + 0x6771 Ogg Vorbis 3+ + 0x7A21 GSM-AMR (CBR, no SID) + 0x7A22 GSM-AMR (VBR, including SID) + 0xFFFE WAVE_FORMAT_EXTENSIBLE + 0xFFFF WAVE_FORMAT_DEVELOPMENT + + */ + + return getid3_lib::EmbeddedLookup('0x'.str_pad(strtoupper(dechex($wFormatTag)), 4, '0', STR_PAD_LEFT), $begin, __LINE__, __FILE__, 'riff-wFormatTag'); + } + + /** + * @param string $fourcc + * + * @return string + */ + public static function fourccLookup($fourcc) { + + $begin = __LINE__; + + /** This is not a comment! + + swot http://developer.apple.com/qa/snd/snd07.html + ____ No Codec (____) + _BIT BI_BITFIELDS (Raw RGB) + _JPG JPEG compressed + _PNG PNG compressed W3C/ISO/IEC (RFC-2083) + _RAW Full Frames (Uncompressed) + _RGB Raw RGB Bitmap + _RL4 RLE 4bpp RGB + _RL8 RLE 8bpp RGB + 3IV1 3ivx MPEG-4 v1 + 3IV2 3ivx MPEG-4 v2 + 3IVX 3ivx MPEG-4 + AASC Autodesk Animator + ABYR Kensington ?ABYR? + AEMI Array Microsystems VideoONE MPEG1-I Capture + AFLC Autodesk Animator FLC + AFLI Autodesk Animator FLI + AMPG Array Microsystems VideoONE MPEG + ANIM Intel RDX (ANIM) + AP41 AngelPotion Definitive + ASV1 Asus Video v1 + ASV2 Asus Video v2 + ASVX Asus Video 2.0 (audio) + AUR2 AuraVision Aura 2 Codec - YUV 4:2:2 + AURA AuraVision Aura 1 Codec - YUV 4:1:1 + AVDJ Independent JPEG Group\'s codec (AVDJ) + AVRN Independent JPEG Group\'s codec (AVRN) + AYUV 4:4:4 YUV (AYUV) + AZPR Quicktime Apple Video (AZPR) + BGR Raw RGB32 + BLZ0 Blizzard DivX MPEG-4 + BTVC Conexant Composite Video + BINK RAD Game Tools Bink Video + BT20 Conexant Prosumer Video + BTCV Conexant Composite Video Codec + BW10 Data Translation Broadway MPEG Capture + CC12 Intel YUV12 + CDVC Canopus DV + CFCC Digital Processing Systems DPS Perception + CGDI Microsoft Office 97 Camcorder Video + CHAM Winnov Caviara Champagne + CJPG Creative WebCam JPEG + CLJR Cirrus Logic YUV 4:1:1 + CMYK Common Data Format in Printing (Colorgraph) + CPLA Weitek 4:2:0 YUV Planar + CRAM Microsoft Video 1 (CRAM) + cvid Radius Cinepak + CVID Radius Cinepak + CWLT Microsoft Color WLT DIB + CYUV Creative Labs YUV + CYUY ATI YUV + D261 H.261 + D263 H.263 + DIB Device Independent Bitmap + DIV1 FFmpeg OpenDivX + DIV2 Microsoft MPEG-4 v1/v2 + DIV3 DivX ;-) MPEG-4 v3.x Low-Motion + DIV4 DivX ;-) MPEG-4 v3.x Fast-Motion + DIV5 DivX MPEG-4 v5.x + DIV6 DivX ;-) (MS MPEG-4 v3.x) + DIVX DivX MPEG-4 v4 (OpenDivX / Project Mayo) + divx DivX MPEG-4 + DMB1 Matrox Rainbow Runner hardware MJPEG + DMB2 Paradigm MJPEG + DSVD ?DSVD? + DUCK Duck TrueMotion 1.0 + DPS0 DPS/Leitch Reality Motion JPEG + DPSC DPS/Leitch PAR Motion JPEG + DV25 Matrox DVCPRO codec + DV50 Matrox DVCPRO50 codec + DVC IEC 61834 and SMPTE 314M (DVC/DV Video) + DVCP IEC 61834 and SMPTE 314M (DVC/DV Video) + DVHD IEC Standard DV 1125 lines @ 30fps / 1250 lines @ 25fps + DVMA Darim Vision DVMPEG (dummy for MPEG compressor) (www.darvision.com) + DVSL IEC Standard DV compressed in SD (SDL) + DVAN ?DVAN? + DVE2 InSoft DVE-2 Videoconferencing + dvsd IEC 61834 and SMPTE 314M DVC/DV Video + DVSD IEC 61834 and SMPTE 314M DVC/DV Video + DVX1 Lucent DVX1000SP Video Decoder + DVX2 Lucent DVX2000S Video Decoder + DVX3 Lucent DVX3000S Video Decoder + DX50 DivX v5 + DXT1 Microsoft DirectX Compressed Texture (DXT1) + DXT2 Microsoft DirectX Compressed Texture (DXT2) + DXT3 Microsoft DirectX Compressed Texture (DXT3) + DXT4 Microsoft DirectX Compressed Texture (DXT4) + DXT5 Microsoft DirectX Compressed Texture (DXT5) + DXTC Microsoft DirectX Compressed Texture (DXTC) + DXTn Microsoft DirectX Compressed Texture (DXTn) + EM2V Etymonix MPEG-2 I-frame (www.etymonix.com) + EKQ0 Elsa ?EKQ0? + ELK0 Elsa ?ELK0? + ESCP Eidos Escape + ETV1 eTreppid Video ETV1 + ETV2 eTreppid Video ETV2 + ETVC eTreppid Video ETVC + FLIC Autodesk FLI/FLC Animation + FLV1 Sorenson Spark + FLV4 On2 TrueMotion VP6 + FRWT Darim Vision Forward Motion JPEG (www.darvision.com) + FRWU Darim Vision Forward Uncompressed (www.darvision.com) + FLJP D-Vision Field Encoded Motion JPEG + FPS1 FRAPS v1 + FRWA SoftLab-Nsk Forward Motion JPEG w/ alpha channel + FRWD SoftLab-Nsk Forward Motion JPEG + FVF1 Iterated Systems Fractal Video Frame + GLZW Motion LZW (gabest@freemail.hu) + GPEG Motion JPEG (gabest@freemail.hu) + GWLT Microsoft Greyscale WLT DIB + H260 Intel ITU H.260 Videoconferencing + H261 Intel ITU H.261 Videoconferencing + H262 Intel ITU H.262 Videoconferencing + H263 Intel ITU H.263 Videoconferencing + H264 Intel ITU H.264 Videoconferencing + H265 Intel ITU H.265 Videoconferencing + H266 Intel ITU H.266 Videoconferencing + H267 Intel ITU H.267 Videoconferencing + H268 Intel ITU H.268 Videoconferencing + H269 Intel ITU H.269 Videoconferencing + HFYU Huffman Lossless Codec + HMCR Rendition Motion Compensation Format (HMCR) + HMRR Rendition Motion Compensation Format (HMRR) + I263 FFmpeg I263 decoder + IF09 Indeo YVU9 ("YVU9 with additional delta-frame info after the U plane") + IUYV Interlaced version of UYVY (www.leadtools.com) + IY41 Interlaced version of Y41P (www.leadtools.com) + IYU1 12 bit format used in mode 2 of the IEEE 1394 Digital Camera 1.04 spec IEEE standard + IYU2 24 bit format used in mode 2 of the IEEE 1394 Digital Camera 1.04 spec IEEE standard + IYUV Planar YUV format (8-bpp Y plane, followed by 8-bpp 2×2 U and V planes) + i263 Intel ITU H.263 Videoconferencing (i263) + I420 Intel Indeo 4 + IAN Intel Indeo 4 (RDX) + ICLB InSoft CellB Videoconferencing + IGOR Power DVD + IJPG Intergraph JPEG + ILVC Intel Layered Video + ILVR ITU-T H.263+ + IPDV I-O Data Device Giga AVI DV Codec + IR21 Intel Indeo 2.1 + IRAW Intel YUV Uncompressed + IV30 Intel Indeo 3.0 + IV31 Intel Indeo 3.1 + IV32 Ligos Indeo 3.2 + IV33 Ligos Indeo 3.3 + IV34 Ligos Indeo 3.4 + IV35 Ligos Indeo 3.5 + IV36 Ligos Indeo 3.6 + IV37 Ligos Indeo 3.7 + IV38 Ligos Indeo 3.8 + IV39 Ligos Indeo 3.9 + IV40 Ligos Indeo Interactive 4.0 + IV41 Ligos Indeo Interactive 4.1 + IV42 Ligos Indeo Interactive 4.2 + IV43 Ligos Indeo Interactive 4.3 + IV44 Ligos Indeo Interactive 4.4 + IV45 Ligos Indeo Interactive 4.5 + IV46 Ligos Indeo Interactive 4.6 + IV47 Ligos Indeo Interactive 4.7 + IV48 Ligos Indeo Interactive 4.8 + IV49 Ligos Indeo Interactive 4.9 + IV50 Ligos Indeo Interactive 5.0 + JBYR Kensington ?JBYR? + JPEG Still Image JPEG DIB + JPGL Pegasus Lossless Motion JPEG + KMVC Team17 Software Karl Morton\'s Video Codec + LSVM Vianet Lighting Strike Vmail (Streaming) (www.vianet.com) + LEAD LEAD Video Codec + Ljpg LEAD MJPEG Codec + MDVD Alex MicroDVD Video (hacked MS MPEG-4) (www.tiasoft.de) + MJPA Morgan Motion JPEG (MJPA) (www.morgan-multimedia.com) + MJPB Morgan Motion JPEG (MJPB) (www.morgan-multimedia.com) + MMES Matrox MPEG-2 I-frame + MP2v Microsoft S-Mpeg 4 version 1 (MP2v) + MP42 Microsoft S-Mpeg 4 version 2 (MP42) + MP43 Microsoft S-Mpeg 4 version 3 (MP43) + MP4S Microsoft S-Mpeg 4 version 3 (MP4S) + MP4V FFmpeg MPEG-4 + MPG1 FFmpeg MPEG 1/2 + MPG2 FFmpeg MPEG 1/2 + MPG3 FFmpeg DivX ;-) (MS MPEG-4 v3) + MPG4 Microsoft MPEG-4 + MPGI Sigma Designs MPEG + MPNG PNG images decoder + MSS1 Microsoft Windows Screen Video + MSZH LCL (Lossless Codec Library) (www.geocities.co.jp/Playtown-Denei/2837/LRC.htm) + M261 Microsoft H.261 + M263 Microsoft H.263 + M4S2 Microsoft Fully Compliant MPEG-4 v2 simple profile (M4S2) + m4s2 Microsoft Fully Compliant MPEG-4 v2 simple profile (m4s2) + MC12 ATI Motion Compensation Format (MC12) + MCAM ATI Motion Compensation Format (MCAM) + MJ2C Morgan Multimedia Motion JPEG2000 + mJPG IBM Motion JPEG w/ Huffman Tables + MJPG Microsoft Motion JPEG DIB + MP42 Microsoft MPEG-4 (low-motion) + MP43 Microsoft MPEG-4 (fast-motion) + MP4S Microsoft MPEG-4 (MP4S) + mp4s Microsoft MPEG-4 (mp4s) + MPEG Chromatic Research MPEG-1 Video I-Frame + MPG4 Microsoft MPEG-4 Video High Speed Compressor + MPGI Sigma Designs MPEG + MRCA FAST Multimedia Martin Regen Codec + MRLE Microsoft Run Length Encoding + MSVC Microsoft Video 1 + MTX1 Matrox ?MTX1? + MTX2 Matrox ?MTX2? + MTX3 Matrox ?MTX3? + MTX4 Matrox ?MTX4? + MTX5 Matrox ?MTX5? + MTX6 Matrox ?MTX6? + MTX7 Matrox ?MTX7? + MTX8 Matrox ?MTX8? + MTX9 Matrox ?MTX9? + MV12 Motion Pixels Codec (old) + MWV1 Aware Motion Wavelets + nAVI SMR Codec (hack of Microsoft MPEG-4) (IRC #shadowrealm) + NT00 NewTek LightWave HDTV YUV w/ Alpha (www.newtek.com) + NUV1 NuppelVideo + NTN1 Nogatech Video Compression 1 + NVS0 nVidia GeForce Texture (NVS0) + NVS1 nVidia GeForce Texture (NVS1) + NVS2 nVidia GeForce Texture (NVS2) + NVS3 nVidia GeForce Texture (NVS3) + NVS4 nVidia GeForce Texture (NVS4) + NVS5 nVidia GeForce Texture (NVS5) + NVT0 nVidia GeForce Texture (NVT0) + NVT1 nVidia GeForce Texture (NVT1) + NVT2 nVidia GeForce Texture (NVT2) + NVT3 nVidia GeForce Texture (NVT3) + NVT4 nVidia GeForce Texture (NVT4) + NVT5 nVidia GeForce Texture (NVT5) + PIXL MiroXL, Pinnacle PCTV + PDVC I-O Data Device Digital Video Capture DV codec + PGVV Radius Video Vision + PHMO IBM Photomotion + PIM1 MPEG Realtime (Pinnacle Cards) + PIM2 Pegasus Imaging ?PIM2? + PIMJ Pegasus Imaging Lossless JPEG + PVEZ Horizons Technology PowerEZ + PVMM PacketVideo Corporation MPEG-4 + PVW2 Pegasus Imaging Wavelet Compression + Q1.0 Q-Team\'s QPEG 1.0 (www.q-team.de) + Q1.1 Q-Team\'s QPEG 1.1 (www.q-team.de) + QPEG Q-Team QPEG 1.0 + qpeq Q-Team QPEG 1.1 + RGB Raw BGR32 + RGBA Raw RGB w/ Alpha + RMP4 REALmagic MPEG-4 (unauthorized XVID copy) (www.sigmadesigns.com) + ROQV Id RoQ File Video Decoder + RPZA Quicktime Apple Video (RPZA) + RUD0 Rududu video codec (http://rududu.ifrance.com/rududu/) + RV10 RealVideo 1.0 (aka RealVideo 5.0) + RV13 RealVideo 1.0 (RV13) + RV20 RealVideo G2 + RV30 RealVideo 8 + RV40 RealVideo 9 + RGBT Raw RGB w/ Transparency + RLE Microsoft Run Length Encoder + RLE4 Run Length Encoded (4bpp, 16-color) + RLE8 Run Length Encoded (8bpp, 256-color) + RT21 Intel Indeo RealTime Video 2.1 + rv20 RealVideo G2 + rv30 RealVideo 8 + RVX Intel RDX (RVX ) + SMC Apple Graphics (SMC ) + SP54 Logitech Sunplus Sp54 Codec for Mustek GSmart Mini 2 + SPIG Radius Spigot + SVQ3 Sorenson Video 3 (Apple Quicktime 5) + s422 Tekram VideoCap C210 YUV 4:2:2 + SDCC Sun Communication Digital Camera Codec + SFMC CrystalNet Surface Fitting Method + SMSC Radius SMSC + SMSD Radius SMSD + smsv WorldConnect Wavelet Video + SPIG Radius Spigot + SPLC Splash Studios ACM Audio Codec (www.splashstudios.net) + SQZ2 Microsoft VXTreme Video Codec V2 + STVA ST Microelectronics CMOS Imager Data (Bayer) + STVB ST Microelectronics CMOS Imager Data (Nudged Bayer) + STVC ST Microelectronics CMOS Imager Data (Bunched) + STVX ST Microelectronics CMOS Imager Data (Extended CODEC Data Format) + STVY ST Microelectronics CMOS Imager Data (Extended CODEC Data Format with Correction Data) + SV10 Sorenson Video R1 + SVQ1 Sorenson Video + T420 Toshiba YUV 4:2:0 + TM2A Duck TrueMotion Archiver 2.0 (www.duck.com) + TVJP Pinnacle/Truevision Targa 2000 board (TVJP) + TVMJ Pinnacle/Truevision Targa 2000 board (TVMJ) + TY0N Tecomac Low-Bit Rate Codec (www.tecomac.com) + TY2C Trident Decompression Driver + TLMS TeraLogic Motion Intraframe Codec (TLMS) + TLST TeraLogic Motion Intraframe Codec (TLST) + TM20 Duck TrueMotion 2.0 + TM2X Duck TrueMotion 2X + TMIC TeraLogic Motion Intraframe Codec (TMIC) + TMOT Horizons Technology TrueMotion S + tmot Horizons TrueMotion Video Compression + TR20 Duck TrueMotion RealTime 2.0 + TSCC TechSmith Screen Capture Codec + TV10 Tecomac Low-Bit Rate Codec + TY2N Trident ?TY2N? + U263 UB Video H.263/H.263+/H.263++ Decoder + UMP4 UB Video MPEG 4 (www.ubvideo.com) + UYNV Nvidia UYVY packed 4:2:2 + UYVP Evans & Sutherland YCbCr 4:2:2 extended precision + UCOD eMajix.com ClearVideo + ULTI IBM Ultimotion + UYVY UYVY packed 4:2:2 + V261 Lucent VX2000S + VIFP VFAPI Reader Codec (www.yks.ne.jp/~hori/) + VIV1 FFmpeg H263+ decoder + VIV2 Vivo H.263 + VQC2 Vector-quantised codec 2 (research) http://eprints.ecs.soton.ac.uk/archive/00001310/01/VTC97-js.pdf) + VTLP Alaris VideoGramPiX + VYU9 ATI YUV (VYU9) + VYUY ATI YUV (VYUY) + V261 Lucent VX2000S + V422 Vitec Multimedia 24-bit YUV 4:2:2 Format + V655 Vitec Multimedia 16-bit YUV 4:2:2 Format + VCR1 ATI Video Codec 1 + VCR2 ATI Video Codec 2 + VCR3 ATI VCR 3.0 + VCR4 ATI VCR 4.0 + VCR5 ATI VCR 5.0 + VCR6 ATI VCR 6.0 + VCR7 ATI VCR 7.0 + VCR8 ATI VCR 8.0 + VCR9 ATI VCR 9.0 + VDCT Vitec Multimedia Video Maker Pro DIB + VDOM VDOnet VDOWave + VDOW VDOnet VDOLive (H.263) + VDTZ Darim Vison VideoTizer YUV + VGPX Alaris VideoGramPiX + VIDS Vitec Multimedia YUV 4:2:2 CCIR 601 for V422 + VIVO Vivo H.263 v2.00 + vivo Vivo H.263 + VIXL Miro/Pinnacle Video XL + VLV1 VideoLogic/PURE Digital Videologic Capture + VP30 On2 VP3.0 + VP31 On2 VP3.1 + VP6F On2 TrueMotion VP6 + VX1K Lucent VX1000S Video Codec + VX2K Lucent VX2000S Video Codec + VXSP Lucent VX1000SP Video Codec + WBVC Winbond W9960 + WHAM Microsoft Video 1 (WHAM) + WINX Winnov Software Compression + WJPG AverMedia Winbond JPEG + WMV1 Windows Media Video V7 + WMV2 Windows Media Video V8 + WMV3 Windows Media Video V9 + WNV1 Winnov Hardware Compression + XYZP Extended PAL format XYZ palette (www.riff.org) + x263 Xirlink H.263 + XLV0 NetXL Video Decoder + XMPG Xing MPEG (I-Frame only) + XVID XviD MPEG-4 (www.xvid.org) + XXAN ?XXAN? + YU92 Intel YUV (YU92) + YUNV Nvidia Uncompressed YUV 4:2:2 + YUVP Extended PAL format YUV palette (www.riff.org) + Y211 YUV 2:1:1 Packed + Y411 YUV 4:1:1 Packed + Y41B Weitek YUV 4:1:1 Planar + Y41P Brooktree PC1 YUV 4:1:1 Packed + Y41T Brooktree PC1 YUV 4:1:1 with transparency + Y42B Weitek YUV 4:2:2 Planar + Y42T Brooktree UYUV 4:2:2 with transparency + Y422 ADS Technologies Copy of UYVY used in Pyro WebCam firewire camera + Y800 Simple, single Y plane for monochrome images + Y8 Grayscale video + YC12 Intel YUV 12 codec + YUV8 Winnov Caviar YUV8 + YUV9 Intel YUV9 + YUY2 Uncompressed YUV 4:2:2 + YUYV Canopus YUV + YV12 YVU12 Planar + YVU9 Intel YVU9 Planar (8-bpp Y plane, followed by 8-bpp 4x4 U and V planes) + YVYU YVYU 4:2:2 Packed + ZLIB Lossless Codec Library zlib compression (www.geocities.co.jp/Playtown-Denei/2837/LRC.htm) + ZPEG Metheus Video Zipper + + */ + + return getid3_lib::EmbeddedLookup($fourcc, $begin, __LINE__, __FILE__, 'riff-fourcc'); + } + + /** + * @param string $byteword + * @param bool $signed + * + * @return int|float|false + */ + private function EitherEndian2Int($byteword, $signed=false) { + if ($this->container == 'riff') { + return getid3_lib::LittleEndian2Int($byteword, $signed); + } + return getid3_lib::BigEndian2Int($byteword, false, $signed); + } + +} diff --git a/projects/tests/tests/performance/ID3/module.audio.ac3.php b/projects/tests/tests/performance/ID3/module.audio.ac3.php new file mode 100644 index 00000000..636ff8f1 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio.ac3.php @@ -0,0 +1,823 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio.ac3.php // +// module for analyzing AC-3 (aka Dolby Digital) audio files // +// dependencies: NONE // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} + +class getid3_ac3 extends getid3_handler +{ + /** + * @var array + */ + private $AC3header = array(); + + /** + * @var int + */ + private $BSIoffset = 0; + + const syncword = 0x0B77; + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + ///AH + $info['ac3']['raw']['bsi'] = array(); + $thisfile_ac3 = &$info['ac3']; + $thisfile_ac3_raw = &$thisfile_ac3['raw']; + $thisfile_ac3_raw_bsi = &$thisfile_ac3_raw['bsi']; + + + // http://www.atsc.org/standards/a_52a.pdf + + $info['fileformat'] = 'ac3'; + + // An AC-3 serial coded audio bit stream is made up of a sequence of synchronization frames + // Each synchronization frame contains 6 coded audio blocks (AB), each of which represent 256 + // new audio samples per channel. A synchronization information (SI) header at the beginning + // of each frame contains information needed to acquire and maintain synchronization. A + // bit stream information (BSI) header follows SI, and contains parameters describing the coded + // audio service. The coded audio blocks may be followed by an auxiliary data (Aux) field. At the + // end of each frame is an error check field that includes a CRC word for error detection. An + // additional CRC word is located in the SI header, the use of which, by a decoder, is optional. + // + // syncinfo() | bsi() | AB0 | AB1 | AB2 | AB3 | AB4 | AB5 | Aux | CRC + + // syncinfo() { + // syncword 16 + // crc1 16 + // fscod 2 + // frmsizecod 6 + // } /* end of syncinfo */ + + $this->fseek($info['avdataoffset']); + $tempAC3header = $this->fread(100); // should be enough to cover all data, there are some variable-length fields...? + $this->AC3header['syncinfo'] = getid3_lib::BigEndian2Int(substr($tempAC3header, 0, 2)); + $this->AC3header['bsi'] = getid3_lib::BigEndian2Bin(substr($tempAC3header, 2)); + $thisfile_ac3_raw_bsi['bsid'] = (getid3_lib::LittleEndian2Int(substr($tempAC3header, 5, 1)) & 0xF8) >> 3; // AC3 and E-AC3 put the "bsid" version identifier in the same place, but unfortnately the 4 bytes between the syncword and the version identifier are interpreted differently, so grab it here so the following code structure can make sense + unset($tempAC3header); + + if ($this->AC3header['syncinfo'] !== self::syncword) { + if (!$this->isDependencyFor('matroska')) { + unset($info['fileformat'], $info['ac3']); + return $this->error('Expecting "'.dechex(self::syncword).'" at offset '.$info['avdataoffset'].', found "'.dechex($this->AC3header['syncinfo']).'"'); + } + } + + $info['audio']['dataformat'] = 'ac3'; + $info['audio']['bitrate_mode'] = 'cbr'; + $info['audio']['lossless'] = false; + + if ($thisfile_ac3_raw_bsi['bsid'] <= 8) { + + $thisfile_ac3_raw_bsi['crc1'] = getid3_lib::Bin2Dec($this->readHeaderBSI(16)); + $thisfile_ac3_raw_bsi['fscod'] = $this->readHeaderBSI(2); // 5.4.1.3 + $thisfile_ac3_raw_bsi['frmsizecod'] = $this->readHeaderBSI(6); // 5.4.1.4 + if ($thisfile_ac3_raw_bsi['frmsizecod'] > 37) { // binary: 100101 - see Table 5.18 Frame Size Code Table (1 word = 16 bits) + $this->warning('Unexpected ac3.bsi.frmsizecod value: '.$thisfile_ac3_raw_bsi['frmsizecod'].', bitrate not set correctly'); + } + + $thisfile_ac3_raw_bsi['bsid'] = $this->readHeaderBSI(5); // we already know this from pre-parsing the version identifier, but re-read it to let the bitstream flow as intended + $thisfile_ac3_raw_bsi['bsmod'] = $this->readHeaderBSI(3); + $thisfile_ac3_raw_bsi['acmod'] = $this->readHeaderBSI(3); + + if ($thisfile_ac3_raw_bsi['acmod'] & 0x01) { + // If the lsb of acmod is a 1, center channel is in use and cmixlev follows in the bit stream. + $thisfile_ac3_raw_bsi['cmixlev'] = $this->readHeaderBSI(2); + $thisfile_ac3['center_mix_level'] = self::centerMixLevelLookup($thisfile_ac3_raw_bsi['cmixlev']); + } + + if ($thisfile_ac3_raw_bsi['acmod'] & 0x04) { + // If the msb of acmod is a 1, surround channels are in use and surmixlev follows in the bit stream. + $thisfile_ac3_raw_bsi['surmixlev'] = $this->readHeaderBSI(2); + $thisfile_ac3['surround_mix_level'] = self::surroundMixLevelLookup($thisfile_ac3_raw_bsi['surmixlev']); + } + + if ($thisfile_ac3_raw_bsi['acmod'] == 0x02) { + // When operating in the two channel mode, this 2-bit code indicates whether or not the program has been encoded in Dolby Surround. + $thisfile_ac3_raw_bsi['dsurmod'] = $this->readHeaderBSI(2); + $thisfile_ac3['dolby_surround_mode'] = self::dolbySurroundModeLookup($thisfile_ac3_raw_bsi['dsurmod']); + } + + $thisfile_ac3_raw_bsi['flags']['lfeon'] = (bool) $this->readHeaderBSI(1); + + // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1-31. + // The value of 0 is reserved. The values of 1 to 31 are interpreted as -1 dB to -31 dB with respect to digital 100 percent. + $thisfile_ac3_raw_bsi['dialnorm'] = $this->readHeaderBSI(5); // 5.4.2.8 dialnorm: Dialogue Normalization, 5 Bits + + $thisfile_ac3_raw_bsi['flags']['compr'] = (bool) $this->readHeaderBSI(1); // 5.4.2.9 compre: Compression Gain Word Exists, 1 Bit + if ($thisfile_ac3_raw_bsi['flags']['compr']) { + $thisfile_ac3_raw_bsi['compr'] = $this->readHeaderBSI(8); // 5.4.2.10 compr: Compression Gain Word, 8 Bits + $thisfile_ac3['heavy_compression'] = self::heavyCompression($thisfile_ac3_raw_bsi['compr']); + } + + $thisfile_ac3_raw_bsi['flags']['langcod'] = (bool) $this->readHeaderBSI(1); // 5.4.2.11 langcode: Language Code Exists, 1 Bit + if ($thisfile_ac3_raw_bsi['flags']['langcod']) { + $thisfile_ac3_raw_bsi['langcod'] = $this->readHeaderBSI(8); // 5.4.2.12 langcod: Language Code, 8 Bits + } + + $thisfile_ac3_raw_bsi['flags']['audprodinfo'] = (bool) $this->readHeaderBSI(1); // 5.4.2.13 audprodie: Audio Production Information Exists, 1 Bit + if ($thisfile_ac3_raw_bsi['flags']['audprodinfo']) { + $thisfile_ac3_raw_bsi['mixlevel'] = $this->readHeaderBSI(5); // 5.4.2.14 mixlevel: Mixing Level, 5 Bits + $thisfile_ac3_raw_bsi['roomtyp'] = $this->readHeaderBSI(2); // 5.4.2.15 roomtyp: Room Type, 2 Bits + + $thisfile_ac3['mixing_level'] = (80 + $thisfile_ac3_raw_bsi['mixlevel']).'dB'; + $thisfile_ac3['room_type'] = self::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp']); + } + + + $thisfile_ac3_raw_bsi['dialnorm2'] = $this->readHeaderBSI(5); // 5.4.2.16 dialnorm2: Dialogue Normalization, ch2, 5 Bits + $thisfile_ac3['dialogue_normalization2'] = '-'.$thisfile_ac3_raw_bsi['dialnorm2'].'dB'; // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1-31. The value of 0 is reserved. The values of 1 to 31 are interpreted as -1 dB to -31 dB with respect to digital 100 percent. + + $thisfile_ac3_raw_bsi['flags']['compr2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.17 compr2e: Compression Gain Word Exists, ch2, 1 Bit + if ($thisfile_ac3_raw_bsi['flags']['compr2']) { + $thisfile_ac3_raw_bsi['compr2'] = $this->readHeaderBSI(8); // 5.4.2.18 compr2: Compression Gain Word, ch2, 8 Bits + $thisfile_ac3['heavy_compression2'] = self::heavyCompression($thisfile_ac3_raw_bsi['compr2']); + } + + $thisfile_ac3_raw_bsi['flags']['langcod2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.19 langcod2e: Language Code Exists, ch2, 1 Bit + if ($thisfile_ac3_raw_bsi['flags']['langcod2']) { + $thisfile_ac3_raw_bsi['langcod2'] = $this->readHeaderBSI(8); // 5.4.2.20 langcod2: Language Code, ch2, 8 Bits + } + + $thisfile_ac3_raw_bsi['flags']['audprodinfo2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.21 audprodi2e: Audio Production Information Exists, ch2, 1 Bit + if ($thisfile_ac3_raw_bsi['flags']['audprodinfo2']) { + $thisfile_ac3_raw_bsi['mixlevel2'] = $this->readHeaderBSI(5); // 5.4.2.22 mixlevel2: Mixing Level, ch2, 5 Bits + $thisfile_ac3_raw_bsi['roomtyp2'] = $this->readHeaderBSI(2); // 5.4.2.23 roomtyp2: Room Type, ch2, 2 Bits + + $thisfile_ac3['mixing_level2'] = (80 + $thisfile_ac3_raw_bsi['mixlevel2']).'dB'; + $thisfile_ac3['room_type2'] = self::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp2']); + } + + $thisfile_ac3_raw_bsi['copyright'] = (bool) $this->readHeaderBSI(1); // 5.4.2.24 copyrightb: Copyright Bit, 1 Bit + + $thisfile_ac3_raw_bsi['original'] = (bool) $this->readHeaderBSI(1); // 5.4.2.25 origbs: Original Bit Stream, 1 Bit + + $thisfile_ac3_raw_bsi['flags']['timecod1'] = $this->readHeaderBSI(2); // 5.4.2.26 timecod1e, timcode2e: Time Code (first and second) Halves Exist, 2 Bits + if ($thisfile_ac3_raw_bsi['flags']['timecod1'] & 0x01) { + $thisfile_ac3_raw_bsi['timecod1'] = $this->readHeaderBSI(14); // 5.4.2.27 timecod1: Time code first half, 14 bits + $thisfile_ac3['timecode1'] = 0; + $thisfile_ac3['timecode1'] += (($thisfile_ac3_raw_bsi['timecod1'] & 0x3E00) >> 9) * 3600; // The first 5 bits of this 14-bit field represent the time in hours, with valid values of 0�23 + $thisfile_ac3['timecode1'] += (($thisfile_ac3_raw_bsi['timecod1'] & 0x01F8) >> 3) * 60; // The next 6 bits represent the time in minutes, with valid values of 0�59 + $thisfile_ac3['timecode1'] += (($thisfile_ac3_raw_bsi['timecod1'] & 0x0003) >> 0) * 8; // The final 3 bits represents the time in 8 second increments, with valid values of 0�7 (representing 0, 8, 16, ... 56 seconds) + } + if ($thisfile_ac3_raw_bsi['flags']['timecod1'] & 0x02) { + $thisfile_ac3_raw_bsi['timecod2'] = $this->readHeaderBSI(14); // 5.4.2.28 timecod2: Time code second half, 14 bits + $thisfile_ac3['timecode2'] = 0; + $thisfile_ac3['timecode2'] += (($thisfile_ac3_raw_bsi['timecod2'] & 0x3800) >> 11) * 1; // The first 3 bits of this 14-bit field represent the time in seconds, with valid values from 0�7 (representing 0-7 seconds) + $thisfile_ac3['timecode2'] += (($thisfile_ac3_raw_bsi['timecod2'] & 0x07C0) >> 6) * (1 / 30); // The next 5 bits represents the time in frames, with valid values from 0�29 (one frame = 1/30th of a second) + $thisfile_ac3['timecode2'] += (($thisfile_ac3_raw_bsi['timecod2'] & 0x003F) >> 0) * ((1 / 30) / 60); // The final 6 bits represents fractions of 1/64 of a frame, with valid values from 0�63 + } + + $thisfile_ac3_raw_bsi['flags']['addbsi'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['addbsi']) { + $thisfile_ac3_raw_bsi['addbsi_length'] = $this->readHeaderBSI(6) + 1; // This 6-bit code, which exists only if addbside is a 1, indicates the length in bytes of additional bit stream information. The valid range of addbsil is 0�63, indicating 1�64 additional bytes, respectively. + + $this->AC3header['bsi'] .= getid3_lib::BigEndian2Bin($this->fread($thisfile_ac3_raw_bsi['addbsi_length'])); + + $thisfile_ac3_raw_bsi['addbsi_data'] = substr($this->AC3header['bsi'], $this->BSIoffset, $thisfile_ac3_raw_bsi['addbsi_length'] * 8); + $this->BSIoffset += $thisfile_ac3_raw_bsi['addbsi_length'] * 8; + } + + + } elseif ($thisfile_ac3_raw_bsi['bsid'] <= 16) { // E-AC3 + + + $this->error('E-AC3 parsing is incomplete and experimental in this version of getID3 ('.$this->getid3->version().'). Notably the bitrate calculations are wrong -- value might (or not) be correct, but it is not calculated correctly. Email info@getid3.org if you know how to calculate EAC3 bitrate correctly.'); + $info['audio']['dataformat'] = 'eac3'; + + $thisfile_ac3_raw_bsi['strmtyp'] = $this->readHeaderBSI(2); + $thisfile_ac3_raw_bsi['substreamid'] = $this->readHeaderBSI(3); + $thisfile_ac3_raw_bsi['frmsiz'] = $this->readHeaderBSI(11); + $thisfile_ac3_raw_bsi['fscod'] = $this->readHeaderBSI(2); + if ($thisfile_ac3_raw_bsi['fscod'] == 3) { + $thisfile_ac3_raw_bsi['fscod2'] = $this->readHeaderBSI(2); + $thisfile_ac3_raw_bsi['numblkscod'] = 3; // six blocks per syncframe + } else { + $thisfile_ac3_raw_bsi['numblkscod'] = $this->readHeaderBSI(2); + } + $thisfile_ac3['bsi']['blocks_per_sync_frame'] = self::blocksPerSyncFrame($thisfile_ac3_raw_bsi['numblkscod']); + $thisfile_ac3_raw_bsi['acmod'] = $this->readHeaderBSI(3); + $thisfile_ac3_raw_bsi['flags']['lfeon'] = (bool) $this->readHeaderBSI(1); + $thisfile_ac3_raw_bsi['bsid'] = $this->readHeaderBSI(5); // we already know this from pre-parsing the version identifier, but re-read it to let the bitstream flow as intended + $thisfile_ac3_raw_bsi['dialnorm'] = $this->readHeaderBSI(5); + $thisfile_ac3_raw_bsi['flags']['compr'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['compr']) { + $thisfile_ac3_raw_bsi['compr'] = $this->readHeaderBSI(8); + } + if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value) + $thisfile_ac3_raw_bsi['dialnorm2'] = $this->readHeaderBSI(5); + $thisfile_ac3_raw_bsi['flags']['compr2'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['compr2']) { + $thisfile_ac3_raw_bsi['compr2'] = $this->readHeaderBSI(8); + } + } + if ($thisfile_ac3_raw_bsi['strmtyp'] == 1) { // if dependent stream + $thisfile_ac3_raw_bsi['flags']['chanmap'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['chanmap']) { + $thisfile_ac3_raw_bsi['chanmap'] = $this->readHeaderBSI(8); + } + } + $thisfile_ac3_raw_bsi['flags']['mixmdat'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['mixmdat']) { // Mixing metadata + if ($thisfile_ac3_raw_bsi['acmod'] > 2) { // if more than 2 channels + $thisfile_ac3_raw_bsi['dmixmod'] = $this->readHeaderBSI(2); + } + if (($thisfile_ac3_raw_bsi['acmod'] & 0x01) && ($thisfile_ac3_raw_bsi['acmod'] > 2)) { // if three front channels exist + $thisfile_ac3_raw_bsi['ltrtcmixlev'] = $this->readHeaderBSI(3); + $thisfile_ac3_raw_bsi['lorocmixlev'] = $this->readHeaderBSI(3); + } + if ($thisfile_ac3_raw_bsi['acmod'] & 0x04) { // if a surround channel exists + $thisfile_ac3_raw_bsi['ltrtsurmixlev'] = $this->readHeaderBSI(3); + $thisfile_ac3_raw_bsi['lorosurmixlev'] = $this->readHeaderBSI(3); + } + if ($thisfile_ac3_raw_bsi['flags']['lfeon']) { // if the LFE channel exists + $thisfile_ac3_raw_bsi['flags']['lfemixlevcod'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['lfemixlevcod']) { + $thisfile_ac3_raw_bsi['lfemixlevcod'] = $this->readHeaderBSI(5); + } + } + if ($thisfile_ac3_raw_bsi['strmtyp'] == 0) { // if independent stream + $thisfile_ac3_raw_bsi['flags']['pgmscl'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['pgmscl']) { + $thisfile_ac3_raw_bsi['pgmscl'] = $this->readHeaderBSI(6); + } + if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value) + $thisfile_ac3_raw_bsi['flags']['pgmscl2'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['pgmscl2']) { + $thisfile_ac3_raw_bsi['pgmscl2'] = $this->readHeaderBSI(6); + } + } + $thisfile_ac3_raw_bsi['flags']['extpgmscl'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['extpgmscl']) { + $thisfile_ac3_raw_bsi['extpgmscl'] = $this->readHeaderBSI(6); + } + $thisfile_ac3_raw_bsi['mixdef'] = $this->readHeaderBSI(2); + if ($thisfile_ac3_raw_bsi['mixdef'] == 1) { // mixing option 2 + $thisfile_ac3_raw_bsi['premixcmpsel'] = (bool) $this->readHeaderBSI(1); + $thisfile_ac3_raw_bsi['drcsrc'] = (bool) $this->readHeaderBSI(1); + $thisfile_ac3_raw_bsi['premixcmpscl'] = $this->readHeaderBSI(3); + } elseif ($thisfile_ac3_raw_bsi['mixdef'] == 2) { // mixing option 3 + $thisfile_ac3_raw_bsi['mixdata'] = $this->readHeaderBSI(12); + } elseif ($thisfile_ac3_raw_bsi['mixdef'] == 3) { // mixing option 4 + $mixdefbitsread = 0; + $thisfile_ac3_raw_bsi['mixdeflen'] = $this->readHeaderBSI(5); $mixdefbitsread += 5; + $thisfile_ac3_raw_bsi['flags']['mixdata2'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['mixdata2']) { + $thisfile_ac3_raw_bsi['premixcmpsel'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + $thisfile_ac3_raw_bsi['drcsrc'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + $thisfile_ac3_raw_bsi['premixcmpscl'] = $this->readHeaderBSI(3); $mixdefbitsread += 3; + $thisfile_ac3_raw_bsi['flags']['extpgmlscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmlscl']) { + $thisfile_ac3_raw_bsi['extpgmlscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + $thisfile_ac3_raw_bsi['flags']['extpgmcscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmcscl']) { + $thisfile_ac3_raw_bsi['extpgmcscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + $thisfile_ac3_raw_bsi['flags']['extpgmrscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmrscl']) { + $thisfile_ac3_raw_bsi['extpgmrscl'] = $this->readHeaderBSI(4); + } + $thisfile_ac3_raw_bsi['flags']['extpgmlsscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmlsscl']) { + $thisfile_ac3_raw_bsi['extpgmlsscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + $thisfile_ac3_raw_bsi['flags']['extpgmrsscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmrsscl']) { + $thisfile_ac3_raw_bsi['extpgmrsscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + $thisfile_ac3_raw_bsi['flags']['extpgmlfescl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmlfescl']) { + $thisfile_ac3_raw_bsi['extpgmlfescl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + $thisfile_ac3_raw_bsi['flags']['dmixscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['dmixscl']) { + $thisfile_ac3_raw_bsi['dmixscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + $thisfile_ac3_raw_bsi['flags']['addch'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['addch']) { + $thisfile_ac3_raw_bsi['flags']['extpgmaux1scl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmaux1scl']) { + $thisfile_ac3_raw_bsi['extpgmaux1scl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + $thisfile_ac3_raw_bsi['flags']['extpgmaux2scl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['extpgmaux2scl']) { + $thisfile_ac3_raw_bsi['extpgmaux2scl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4; + } + } + } + $thisfile_ac3_raw_bsi['flags']['mixdata3'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['mixdata3']) { + $thisfile_ac3_raw_bsi['spchdat'] = $this->readHeaderBSI(5); $mixdefbitsread += 5; + $thisfile_ac3_raw_bsi['flags']['addspchdat'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['addspchdat']) { + $thisfile_ac3_raw_bsi['spchdat1'] = $this->readHeaderBSI(5); $mixdefbitsread += 5; + $thisfile_ac3_raw_bsi['spchan1att'] = $this->readHeaderBSI(2); $mixdefbitsread += 2; + $thisfile_ac3_raw_bsi['flags']['addspchdat1'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1; + if ($thisfile_ac3_raw_bsi['flags']['addspchdat1']) { + $thisfile_ac3_raw_bsi['spchdat2'] = $this->readHeaderBSI(5); $mixdefbitsread += 5; + $thisfile_ac3_raw_bsi['spchan2att'] = $this->readHeaderBSI(3); $mixdefbitsread += 3; + } + } + } + $mixdata_bits = (8 * ($thisfile_ac3_raw_bsi['mixdeflen'] + 2)) - $mixdefbitsread; + $mixdata_fill = (($mixdata_bits % 8) ? 8 - ($mixdata_bits % 8) : 0); + $thisfile_ac3_raw_bsi['mixdata'] = $this->readHeaderBSI($mixdata_bits); + $thisfile_ac3_raw_bsi['mixdatafill'] = $this->readHeaderBSI($mixdata_fill); + unset($mixdefbitsread, $mixdata_bits, $mixdata_fill); + } + if ($thisfile_ac3_raw_bsi['acmod'] < 2) { // if mono or dual mono source + $thisfile_ac3_raw_bsi['flags']['paninfo'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['paninfo']) { + $thisfile_ac3_raw_bsi['panmean'] = $this->readHeaderBSI(8); + $thisfile_ac3_raw_bsi['paninfo'] = $this->readHeaderBSI(6); + } + if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value) + $thisfile_ac3_raw_bsi['flags']['paninfo2'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['paninfo2']) { + $thisfile_ac3_raw_bsi['panmean2'] = $this->readHeaderBSI(8); + $thisfile_ac3_raw_bsi['paninfo2'] = $this->readHeaderBSI(6); + } + } + } + $thisfile_ac3_raw_bsi['flags']['frmmixcfginfo'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['frmmixcfginfo']) { // mixing configuration information + if ($thisfile_ac3_raw_bsi['numblkscod'] == 0) { + $thisfile_ac3_raw_bsi['blkmixcfginfo'][0] = $this->readHeaderBSI(5); + } else { + for ($blk = 0; $blk < $thisfile_ac3_raw_bsi['numblkscod']; $blk++) { + $thisfile_ac3_raw_bsi['flags']['blkmixcfginfo'.$blk] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['blkmixcfginfo'.$blk]) { // mixing configuration information + $thisfile_ac3_raw_bsi['blkmixcfginfo'][$blk] = $this->readHeaderBSI(5); + } + } + } + } + } + } + $thisfile_ac3_raw_bsi['flags']['infomdat'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['infomdat']) { // Informational metadata + $thisfile_ac3_raw_bsi['bsmod'] = $this->readHeaderBSI(3); + $thisfile_ac3_raw_bsi['flags']['copyrightb'] = (bool) $this->readHeaderBSI(1); + $thisfile_ac3_raw_bsi['flags']['origbs'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['acmod'] == 2) { // if in 2/0 mode + $thisfile_ac3_raw_bsi['dsurmod'] = $this->readHeaderBSI(2); + $thisfile_ac3_raw_bsi['dheadphonmod'] = $this->readHeaderBSI(2); + } + if ($thisfile_ac3_raw_bsi['acmod'] >= 6) { // if both surround channels exist + $thisfile_ac3_raw_bsi['dsurexmod'] = $this->readHeaderBSI(2); + } + $thisfile_ac3_raw_bsi['flags']['audprodi'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['audprodi']) { + $thisfile_ac3_raw_bsi['mixlevel'] = $this->readHeaderBSI(5); + $thisfile_ac3_raw_bsi['roomtyp'] = $this->readHeaderBSI(2); + $thisfile_ac3_raw_bsi['flags']['adconvtyp'] = (bool) $this->readHeaderBSI(1); + } + if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value) + $thisfile_ac3_raw_bsi['flags']['audprodi2'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['audprodi2']) { + $thisfile_ac3_raw_bsi['mixlevel2'] = $this->readHeaderBSI(5); + $thisfile_ac3_raw_bsi['roomtyp2'] = $this->readHeaderBSI(2); + $thisfile_ac3_raw_bsi['flags']['adconvtyp2'] = (bool) $this->readHeaderBSI(1); + } + } + if ($thisfile_ac3_raw_bsi['fscod'] < 3) { // if not half sample rate + $thisfile_ac3_raw_bsi['flags']['sourcefscod'] = (bool) $this->readHeaderBSI(1); + } + } + if (($thisfile_ac3_raw_bsi['strmtyp'] == 0) && ($thisfile_ac3_raw_bsi['numblkscod'] != 3)) { // if both surround channels exist + $thisfile_ac3_raw_bsi['flags']['convsync'] = (bool) $this->readHeaderBSI(1); + } + if ($thisfile_ac3_raw_bsi['strmtyp'] == 2) { // if bit stream converted from AC-3 + if ($thisfile_ac3_raw_bsi['numblkscod'] != 3) { // 6 blocks per syncframe + $thisfile_ac3_raw_bsi['flags']['blkid'] = 1; + } else { + $thisfile_ac3_raw_bsi['flags']['blkid'] = (bool) $this->readHeaderBSI(1); + } + if ($thisfile_ac3_raw_bsi['flags']['blkid']) { + $thisfile_ac3_raw_bsi['frmsizecod'] = $this->readHeaderBSI(6); + } + } + $thisfile_ac3_raw_bsi['flags']['addbsi'] = (bool) $this->readHeaderBSI(1); + if ($thisfile_ac3_raw_bsi['flags']['addbsi']) { + $thisfile_ac3_raw_bsi['addbsil'] = $this->readHeaderBSI(6); + $thisfile_ac3_raw_bsi['addbsi'] = $this->readHeaderBSI(($thisfile_ac3_raw_bsi['addbsil'] + 1) * 8); + } + + } else { + + $this->error('Bit stream identification is version '.$thisfile_ac3_raw_bsi['bsid'].', but getID3() only understands up to version 16. Please submit a support ticket with a sample file.'); + unset($info['ac3']); + return false; + + } + + if (isset($thisfile_ac3_raw_bsi['fscod2'])) { + $thisfile_ac3['sample_rate'] = self::sampleRateCodeLookup2($thisfile_ac3_raw_bsi['fscod2']); + } else { + $thisfile_ac3['sample_rate'] = self::sampleRateCodeLookup($thisfile_ac3_raw_bsi['fscod']); + } + if ($thisfile_ac3_raw_bsi['fscod'] <= 3) { + $info['audio']['sample_rate'] = $thisfile_ac3['sample_rate']; + } else { + $this->warning('Unexpected ac3.bsi.fscod value: '.$thisfile_ac3_raw_bsi['fscod']); + } + if (isset($thisfile_ac3_raw_bsi['frmsizecod'])) { + $thisfile_ac3['frame_length'] = self::frameSizeLookup($thisfile_ac3_raw_bsi['frmsizecod'], $thisfile_ac3_raw_bsi['fscod']); + $thisfile_ac3['bitrate'] = self::bitrateLookup($thisfile_ac3_raw_bsi['frmsizecod']); + } elseif (!empty($thisfile_ac3_raw_bsi['frmsiz'])) { + // this isn't right, but it's (usually) close, roughly 5% less than it should be. + // but WHERE is the actual bitrate value stored in EAC3?? email info@getid3.org if you know! + $thisfile_ac3['bitrate'] = ($thisfile_ac3_raw_bsi['frmsiz'] + 1) * 16 * 30; // The frmsiz field shall contain a value one less than the overall size of the coded syncframe in 16-bit words. That is, this field may assume a value ranging from 0 to 2047, and these values correspond to syncframe sizes ranging from 1 to 2048. + // kludge-fix to make it approximately the expected value, still not "right": + $thisfile_ac3['bitrate'] = round(($thisfile_ac3['bitrate'] * 1.05) / 16000) * 16000; + } + $info['audio']['bitrate'] = $thisfile_ac3['bitrate']; + + if (isset($thisfile_ac3_raw_bsi['bsmod']) && isset($thisfile_ac3_raw_bsi['acmod'])) { + $thisfile_ac3['service_type'] = self::serviceTypeLookup($thisfile_ac3_raw_bsi['bsmod'], $thisfile_ac3_raw_bsi['acmod']); + } + $ac3_coding_mode = self::audioCodingModeLookup($thisfile_ac3_raw_bsi['acmod']); + foreach($ac3_coding_mode as $key => $value) { + $thisfile_ac3[$key] = $value; + } + switch ($thisfile_ac3_raw_bsi['acmod']) { + case 0: + case 1: + $info['audio']['channelmode'] = 'mono'; + break; + case 3: + case 4: + $info['audio']['channelmode'] = 'stereo'; + break; + default: + $info['audio']['channelmode'] = 'surround'; + break; + } + $info['audio']['channels'] = $thisfile_ac3['num_channels']; + + $thisfile_ac3['lfe_enabled'] = $thisfile_ac3_raw_bsi['flags']['lfeon']; + if ($thisfile_ac3_raw_bsi['flags']['lfeon']) { + $info['audio']['channels'] .= '.1'; + } + + $thisfile_ac3['channels_enabled'] = self::channelsEnabledLookup($thisfile_ac3_raw_bsi['acmod'], $thisfile_ac3_raw_bsi['flags']['lfeon']); + $thisfile_ac3['dialogue_normalization'] = '-'.$thisfile_ac3_raw_bsi['dialnorm'].'dB'; + + return true; + } + + /** + * @param int $length + * + * @return int + */ + private function readHeaderBSI($length) { + $data = substr($this->AC3header['bsi'], $this->BSIoffset, $length); + $this->BSIoffset += $length; + + return bindec($data); + } + + /** + * @param int $fscod + * + * @return int|string|false + */ + public static function sampleRateCodeLookup($fscod) { + static $sampleRateCodeLookup = array( + 0 => 48000, + 1 => 44100, + 2 => 32000, + 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute. + ); + return (isset($sampleRateCodeLookup[$fscod]) ? $sampleRateCodeLookup[$fscod] : false); + } + + /** + * @param int $fscod2 + * + * @return int|string|false + */ + public static function sampleRateCodeLookup2($fscod2) { + static $sampleRateCodeLookup2 = array( + 0 => 24000, + 1 => 22050, + 2 => 16000, + 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute. + ); + return (isset($sampleRateCodeLookup2[$fscod2]) ? $sampleRateCodeLookup2[$fscod2] : false); + } + + /** + * @param int $bsmod + * @param int $acmod + * + * @return string|false + */ + public static function serviceTypeLookup($bsmod, $acmod) { + static $serviceTypeLookup = array(); + if (empty($serviceTypeLookup)) { + for ($i = 0; $i <= 7; $i++) { + $serviceTypeLookup[0][$i] = 'main audio service: complete main (CM)'; + $serviceTypeLookup[1][$i] = 'main audio service: music and effects (ME)'; + $serviceTypeLookup[2][$i] = 'associated service: visually impaired (VI)'; + $serviceTypeLookup[3][$i] = 'associated service: hearing impaired (HI)'; + $serviceTypeLookup[4][$i] = 'associated service: dialogue (D)'; + $serviceTypeLookup[5][$i] = 'associated service: commentary (C)'; + $serviceTypeLookup[6][$i] = 'associated service: emergency (E)'; + } + + $serviceTypeLookup[7][1] = 'associated service: voice over (VO)'; + for ($i = 2; $i <= 7; $i++) { + $serviceTypeLookup[7][$i] = 'main audio service: karaoke'; + } + } + return (isset($serviceTypeLookup[$bsmod][$acmod]) ? $serviceTypeLookup[$bsmod][$acmod] : false); + } + + /** + * @param int $acmod + * + * @return array|false + */ + public static function audioCodingModeLookup($acmod) { + // array(channel configuration, # channels (not incl LFE), channel order) + static $audioCodingModeLookup = array ( + 0 => array('channel_config'=>'1+1', 'num_channels'=>2, 'channel_order'=>'Ch1,Ch2'), + 1 => array('channel_config'=>'1/0', 'num_channels'=>1, 'channel_order'=>'C'), + 2 => array('channel_config'=>'2/0', 'num_channels'=>2, 'channel_order'=>'L,R'), + 3 => array('channel_config'=>'3/0', 'num_channels'=>3, 'channel_order'=>'L,C,R'), + 4 => array('channel_config'=>'2/1', 'num_channels'=>3, 'channel_order'=>'L,R,S'), + 5 => array('channel_config'=>'3/1', 'num_channels'=>4, 'channel_order'=>'L,C,R,S'), + 6 => array('channel_config'=>'2/2', 'num_channels'=>4, 'channel_order'=>'L,R,SL,SR'), + 7 => array('channel_config'=>'3/2', 'num_channels'=>5, 'channel_order'=>'L,C,R,SL,SR'), + ); + return (isset($audioCodingModeLookup[$acmod]) ? $audioCodingModeLookup[$acmod] : false); + } + + /** + * @param int $cmixlev + * + * @return int|float|string|false + */ + public static function centerMixLevelLookup($cmixlev) { + static $centerMixLevelLookup; + if (empty($centerMixLevelLookup)) { + $centerMixLevelLookup = array( + 0 => pow(2, -3.0 / 6), // 0.707 (-3.0 dB) + 1 => pow(2, -4.5 / 6), // 0.595 (-4.5 dB) + 2 => pow(2, -6.0 / 6), // 0.500 (-6.0 dB) + 3 => 'reserved' + ); + } + return (isset($centerMixLevelLookup[$cmixlev]) ? $centerMixLevelLookup[$cmixlev] : false); + } + + /** + * @param int $surmixlev + * + * @return int|float|string|false + */ + public static function surroundMixLevelLookup($surmixlev) { + static $surroundMixLevelLookup; + if (empty($surroundMixLevelLookup)) { + $surroundMixLevelLookup = array( + 0 => pow(2, -3.0 / 6), + 1 => pow(2, -6.0 / 6), + 2 => 0, + 3 => 'reserved' + ); + } + return (isset($surroundMixLevelLookup[$surmixlev]) ? $surroundMixLevelLookup[$surmixlev] : false); + } + + /** + * @param int $dsurmod + * + * @return string|false + */ + public static function dolbySurroundModeLookup($dsurmod) { + static $dolbySurroundModeLookup = array( + 0 => 'not indicated', + 1 => 'Not Dolby Surround encoded', + 2 => 'Dolby Surround encoded', + 3 => 'reserved' + ); + return (isset($dolbySurroundModeLookup[$dsurmod]) ? $dolbySurroundModeLookup[$dsurmod] : false); + } + + /** + * @param int $acmod + * @param bool $lfeon + * + * @return array + */ + public static function channelsEnabledLookup($acmod, $lfeon) { + $lookup = array( + 'ch1'=>($acmod == 0), + 'ch2'=>($acmod == 0), + 'left'=>($acmod > 1), + 'right'=>($acmod > 1), + 'center'=>(bool) ($acmod & 0x01), + 'surround_mono'=>false, + 'surround_left'=>false, + 'surround_right'=>false, + 'lfe'=>$lfeon); + switch ($acmod) { + case 4: + case 5: + $lookup['surround_mono'] = true; + break; + case 6: + case 7: + $lookup['surround_left'] = true; + $lookup['surround_right'] = true; + break; + } + return $lookup; + } + + /** + * @param int $compre + * + * @return float|int + */ + public static function heavyCompression($compre) { + // The first four bits indicate gain changes in 6.02dB increments which can be + // implemented with an arithmetic shift operation. The following four bits + // indicate linear gain changes, and require a 5-bit multiply. + // We will represent the two 4-bit fields of compr as follows: + // X0 X1 X2 X3 . Y4 Y5 Y6 Y7 + // The meaning of the X values is most simply described by considering X to represent a 4-bit + // signed integer with values from -8 to +7. The gain indicated by X is then (X + 1) * 6.02 dB. The + // following table shows this in detail. + + // Meaning of 4 msb of compr + // 7 +48.16 dB + // 6 +42.14 dB + // 5 +36.12 dB + // 4 +30.10 dB + // 3 +24.08 dB + // 2 +18.06 dB + // 1 +12.04 dB + // 0 +6.02 dB + // -1 0 dB + // -2 -6.02 dB + // -3 -12.04 dB + // -4 -18.06 dB + // -5 -24.08 dB + // -6 -30.10 dB + // -7 -36.12 dB + // -8 -42.14 dB + + $fourbit = str_pad(decbin(($compre & 0xF0) >> 4), 4, '0', STR_PAD_LEFT); + if ($fourbit[0] == '1') { + $log_gain = -8 + bindec(substr($fourbit, 1)); + } else { + $log_gain = bindec(substr($fourbit, 1)); + } + $log_gain = ($log_gain + 1) * getid3_lib::RGADamplitude2dB(2); + + // The value of Y is a linear representation of a gain change of up to -6 dB. Y is considered to + // be an unsigned fractional integer, with a leading value of 1, or: 0.1 Y4 Y5 Y6 Y7 (base 2). Y can + // represent values between 0.111112 (or 31/32) and 0.100002 (or 1/2). Thus, Y can represent gain + // changes from -0.28 dB to -6.02 dB. + + $lin_gain = (16 + ($compre & 0x0F)) / 32; + + // The combination of X and Y values allows compr to indicate gain changes from + // 48.16 - 0.28 = +47.89 dB, to + // -42.14 - 6.02 = -48.16 dB. + + return $log_gain - $lin_gain; + } + + /** + * @param int $roomtyp + * + * @return string|false + */ + public static function roomTypeLookup($roomtyp) { + static $roomTypeLookup = array( + 0 => 'not indicated', + 1 => 'large room, X curve monitor', + 2 => 'small room, flat monitor', + 3 => 'reserved' + ); + return (isset($roomTypeLookup[$roomtyp]) ? $roomTypeLookup[$roomtyp] : false); + } + + /** + * @param int $frmsizecod + * @param int $fscod + * + * @return int|false + */ + public static function frameSizeLookup($frmsizecod, $fscod) { + // LSB is whether padding is used or not + $padding = (bool) ($frmsizecod & 0x01); + $framesizeid = ($frmsizecod & 0x3E) >> 1; + + static $frameSizeLookup = array(); + if (empty($frameSizeLookup)) { + $frameSizeLookup = array ( + 0 => array( 128, 138, 192), // 32 kbps + 1 => array( 160, 174, 240), // 40 kbps + 2 => array( 192, 208, 288), // 48 kbps + 3 => array( 224, 242, 336), // 56 kbps + 4 => array( 256, 278, 384), // 64 kbps + 5 => array( 320, 348, 480), // 80 kbps + 6 => array( 384, 416, 576), // 96 kbps + 7 => array( 448, 486, 672), // 112 kbps + 8 => array( 512, 556, 768), // 128 kbps + 9 => array( 640, 696, 960), // 160 kbps + 10 => array( 768, 834, 1152), // 192 kbps + 11 => array( 896, 974, 1344), // 224 kbps + 12 => array(1024, 1114, 1536), // 256 kbps + 13 => array(1280, 1392, 1920), // 320 kbps + 14 => array(1536, 1670, 2304), // 384 kbps + 15 => array(1792, 1950, 2688), // 448 kbps + 16 => array(2048, 2228, 3072), // 512 kbps + 17 => array(2304, 2506, 3456), // 576 kbps + 18 => array(2560, 2786, 3840) // 640 kbps + ); + } + $paddingBytes = 0; + if (($fscod == 1) && $padding) { + // frame lengths are padded by 1 word (16 bits) at 44100 + // (fscode==1) means 44100Hz (see sampleRateCodeLookup) + $paddingBytes = 2; + } + return (isset($frameSizeLookup[$framesizeid][$fscod]) ? $frameSizeLookup[$framesizeid][$fscod] + $paddingBytes : false); + } + + /** + * @param int $frmsizecod + * + * @return int|false + */ + public static function bitrateLookup($frmsizecod) { + // LSB is whether padding is used or not + $padding = (bool) ($frmsizecod & 0x01); + $framesizeid = ($frmsizecod & 0x3E) >> 1; + + static $bitrateLookup = array( + 0 => 32000, + 1 => 40000, + 2 => 48000, + 3 => 56000, + 4 => 64000, + 5 => 80000, + 6 => 96000, + 7 => 112000, + 8 => 128000, + 9 => 160000, + 10 => 192000, + 11 => 224000, + 12 => 256000, + 13 => 320000, + 14 => 384000, + 15 => 448000, + 16 => 512000, + 17 => 576000, + 18 => 640000, + ); + return (isset($bitrateLookup[$framesizeid]) ? $bitrateLookup[$framesizeid] : false); + } + + /** + * @param int $numblkscod + * + * @return int|false + */ + public static function blocksPerSyncFrame($numblkscod) { + static $blocksPerSyncFrameLookup = array( + 0 => 1, + 1 => 2, + 2 => 3, + 3 => 6, + ); + return (isset($blocksPerSyncFrameLookup[$numblkscod]) ? $blocksPerSyncFrameLookup[$numblkscod] : false); + } + + +} diff --git a/projects/tests/tests/performance/ID3/module.audio.dts.php b/projects/tests/tests/performance/ID3/module.audio.dts.php new file mode 100644 index 00000000..ff1a88fc --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio.dts.php @@ -0,0 +1,327 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio.dts.php // +// module for analyzing DTS Audio files // +// dependencies: NONE // +// // +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} + +/** +* @tutorial http://wiki.multimedia.cx/index.php?title=DTS +*/ +class getid3_dts extends getid3_handler +{ + /** + * Default DTS syncword used in native .cpt or .dts formats. + */ + const syncword = "\x7F\xFE\x80\x01"; + + /** + * @var int + */ + private $readBinDataOffset = 0; + + /** + * Possible syncwords indicating bitstream encoding. + */ + public static $syncwords = array( + 0 => "\x7F\xFE\x80\x01", // raw big-endian + 1 => "\xFE\x7F\x01\x80", // raw little-endian + 2 => "\x1F\xFF\xE8\x00", // 14-bit big-endian + 3 => "\xFF\x1F\x00\xE8"); // 14-bit little-endian + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + $info['fileformat'] = 'dts'; + + $this->fseek($info['avdataoffset']); + $DTSheader = $this->fread(20); // we only need 2 words magic + 6 words frame header, but these words may be normal 16-bit words OR 14-bit words with 2 highest bits set to zero, so 8 words can be either 8*16/8 = 16 bytes OR 8*16*(16/14)/8 = 18.3 bytes + + // check syncword + $sync = substr($DTSheader, 0, 4); + if (($encoding = array_search($sync, self::$syncwords)) !== false) { + + $info['dts']['raw']['magic'] = $sync; + $this->readBinDataOffset = 32; + + } elseif ($this->isDependencyFor('matroska')) { + + // Matroska contains DTS without syncword encoded as raw big-endian format + $encoding = 0; + $this->readBinDataOffset = 0; + + } else { + + unset($info['fileformat']); + return $this->error('Expecting "'.implode('| ', array_map('getid3_lib::PrintHexBytes', self::$syncwords)).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($sync).'"'); + + } + + // decode header + $fhBS = ''; + for ($word_offset = 0; $word_offset <= strlen($DTSheader); $word_offset += 2) { + switch ($encoding) { + case 0: // raw big-endian + $fhBS .= getid3_lib::BigEndian2Bin( substr($DTSheader, $word_offset, 2) ); + break; + case 1: // raw little-endian + $fhBS .= getid3_lib::BigEndian2Bin(strrev(substr($DTSheader, $word_offset, 2))); + break; + case 2: // 14-bit big-endian + $fhBS .= substr(getid3_lib::BigEndian2Bin( substr($DTSheader, $word_offset, 2) ), 2, 14); + break; + case 3: // 14-bit little-endian + $fhBS .= substr(getid3_lib::BigEndian2Bin(strrev(substr($DTSheader, $word_offset, 2))), 2, 14); + break; + } + } + + $info['dts']['raw']['frame_type'] = $this->readBinData($fhBS, 1); + $info['dts']['raw']['deficit_samples'] = $this->readBinData($fhBS, 5); + $info['dts']['flags']['crc_present'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['raw']['pcm_sample_blocks'] = $this->readBinData($fhBS, 7); + $info['dts']['raw']['frame_byte_size'] = $this->readBinData($fhBS, 14); + $info['dts']['raw']['channel_arrangement'] = $this->readBinData($fhBS, 6); + $info['dts']['raw']['sample_frequency'] = $this->readBinData($fhBS, 4); + $info['dts']['raw']['bitrate'] = $this->readBinData($fhBS, 5); + $info['dts']['flags']['embedded_downmix'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['flags']['dynamicrange'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['flags']['timestamp'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['flags']['auxdata'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['flags']['hdcd'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['raw']['extension_audio'] = $this->readBinData($fhBS, 3); + $info['dts']['flags']['extended_coding'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['flags']['audio_sync_insertion'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['raw']['lfe_effects'] = $this->readBinData($fhBS, 2); + $info['dts']['flags']['predictor_history'] = (bool) $this->readBinData($fhBS, 1); + if ($info['dts']['flags']['crc_present']) { + $info['dts']['raw']['crc16'] = $this->readBinData($fhBS, 16); + } + $info['dts']['flags']['mri_perfect_reconst'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['raw']['encoder_soft_version'] = $this->readBinData($fhBS, 4); + $info['dts']['raw']['copy_history'] = $this->readBinData($fhBS, 2); + $info['dts']['raw']['bits_per_sample'] = $this->readBinData($fhBS, 2); + $info['dts']['flags']['surround_es'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['flags']['front_sum_diff'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['flags']['surround_sum_diff'] = (bool) $this->readBinData($fhBS, 1); + $info['dts']['raw']['dialog_normalization'] = $this->readBinData($fhBS, 4); + + + $info['dts']['bitrate'] = self::bitrateLookup($info['dts']['raw']['bitrate']); + $info['dts']['bits_per_sample'] = self::bitPerSampleLookup($info['dts']['raw']['bits_per_sample']); + $info['dts']['sample_rate'] = self::sampleRateLookup($info['dts']['raw']['sample_frequency']); + $info['dts']['dialog_normalization'] = self::dialogNormalization($info['dts']['raw']['dialog_normalization'], $info['dts']['raw']['encoder_soft_version']); + $info['dts']['flags']['lossless'] = (($info['dts']['raw']['bitrate'] == 31) ? true : false); + $info['dts']['bitrate_mode'] = (($info['dts']['raw']['bitrate'] == 30) ? 'vbr' : 'cbr'); + $info['dts']['channels'] = self::numChannelsLookup($info['dts']['raw']['channel_arrangement']); + $info['dts']['channel_arrangement'] = self::channelArrangementLookup($info['dts']['raw']['channel_arrangement']); + + $info['audio']['dataformat'] = 'dts'; + $info['audio']['lossless'] = $info['dts']['flags']['lossless']; + $info['audio']['bitrate_mode'] = $info['dts']['bitrate_mode']; + $info['audio']['bits_per_sample'] = $info['dts']['bits_per_sample']; + $info['audio']['sample_rate'] = $info['dts']['sample_rate']; + $info['audio']['channels'] = $info['dts']['channels']; + $info['audio']['bitrate'] = $info['dts']['bitrate']; + if (isset($info['avdataend']) && !empty($info['dts']['bitrate']) && is_numeric($info['dts']['bitrate'])) { + $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) / ($info['dts']['bitrate'] / 8); + if (($encoding == 2) || ($encoding == 3)) { + // 14-bit data packed into 16-bit words, so the playtime is wrong because only (14/16) of the bytes in the data portion of the file are used at the specified bitrate + $info['playtime_seconds'] *= (14 / 16); + } + } + return true; + } + + /** + * @param string $bin + * @param int $length + * + * @return int + */ + private function readBinData($bin, $length) { + $data = substr($bin, $this->readBinDataOffset, $length); + $this->readBinDataOffset += $length; + + return bindec($data); + } + + /** + * @param int $index + * + * @return int|string|false + */ + public static function bitrateLookup($index) { + static $lookup = array( + 0 => 32000, + 1 => 56000, + 2 => 64000, + 3 => 96000, + 4 => 112000, + 5 => 128000, + 6 => 192000, + 7 => 224000, + 8 => 256000, + 9 => 320000, + 10 => 384000, + 11 => 448000, + 12 => 512000, + 13 => 576000, + 14 => 640000, + 15 => 768000, + 16 => 960000, + 17 => 1024000, + 18 => 1152000, + 19 => 1280000, + 20 => 1344000, + 21 => 1408000, + 22 => 1411200, + 23 => 1472000, + 24 => 1536000, + 25 => 1920000, + 26 => 2048000, + 27 => 3072000, + 28 => 3840000, + 29 => 'open', + 30 => 'variable', + 31 => 'lossless', + ); + return (isset($lookup[$index]) ? $lookup[$index] : false); + } + + /** + * @param int $index + * + * @return int|string|false + */ + public static function sampleRateLookup($index) { + static $lookup = array( + 0 => 'invalid', + 1 => 8000, + 2 => 16000, + 3 => 32000, + 4 => 'invalid', + 5 => 'invalid', + 6 => 11025, + 7 => 22050, + 8 => 44100, + 9 => 'invalid', + 10 => 'invalid', + 11 => 12000, + 12 => 24000, + 13 => 48000, + 14 => 'invalid', + 15 => 'invalid', + ); + return (isset($lookup[$index]) ? $lookup[$index] : false); + } + + /** + * @param int $index + * + * @return int|false + */ + public static function bitPerSampleLookup($index) { + static $lookup = array( + 0 => 16, + 1 => 20, + 2 => 24, + 3 => 24, + ); + return (isset($lookup[$index]) ? $lookup[$index] : false); + } + + /** + * @param int $index + * + * @return int|false + */ + public static function numChannelsLookup($index) { + switch ($index) { + case 0: + return 1; + case 1: + case 2: + case 3: + case 4: + return 2; + case 5: + case 6: + return 3; + case 7: + case 8: + return 4; + case 9: + return 5; + case 10: + case 11: + case 12: + return 6; + case 13: + return 7; + case 14: + case 15: + return 8; + } + return false; + } + + /** + * @param int $index + * + * @return string + */ + public static function channelArrangementLookup($index) { + static $lookup = array( + 0 => 'A', + 1 => 'A + B (dual mono)', + 2 => 'L + R (stereo)', + 3 => '(L+R) + (L-R) (sum-difference)', + 4 => 'LT + RT (left and right total)', + 5 => 'C + L + R', + 6 => 'L + R + S', + 7 => 'C + L + R + S', + 8 => 'L + R + SL + SR', + 9 => 'C + L + R + SL + SR', + 10 => 'CL + CR + L + R + SL + SR', + 11 => 'C + L + R+ LR + RR + OV', + 12 => 'CF + CR + LF + RF + LR + RR', + 13 => 'CL + C + CR + L + R + SL + SR', + 14 => 'CL + CR + L + R + SL1 + SL2 + SR1 + SR2', + 15 => 'CL + C+ CR + L + R + SL + S + SR', + ); + return (isset($lookup[$index]) ? $lookup[$index] : 'user-defined'); + } + + /** + * @param int $index + * @param int $version + * + * @return int|false + */ + public static function dialogNormalization($index, $version) { + switch ($version) { + case 7: + return 0 - $index; + case 6: + return 0 - 16 - $index; + } + return false; + } + +} diff --git a/projects/tests/tests/performance/ID3/module.audio.flac.php b/projects/tests/tests/performance/ID3/module.audio.flac.php new file mode 100644 index 00000000..1cea4364 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio.flac.php @@ -0,0 +1,518 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio.flac.php // +// module for analyzing FLAC and OggFLAC audio files // +// dependencies: module.audio.ogg.php // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ogg.php', __FILE__, true); + +/** +* @tutorial http://flac.sourceforge.net/format.html +*/ +class getid3_flac extends getid3_handler +{ + const syncword = 'fLaC'; + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + $this->fseek($info['avdataoffset']); + $StreamMarker = $this->fread(4); + if ($StreamMarker != self::syncword) { + return $this->error('Expecting "'.getid3_lib::PrintHexBytes(self::syncword).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($StreamMarker).'"'); + } + $info['fileformat'] = 'flac'; + $info['audio']['dataformat'] = 'flac'; + $info['audio']['bitrate_mode'] = 'vbr'; + $info['audio']['lossless'] = true; + + // parse flac container + return $this->parseMETAdata(); + } + + /** + * @return bool + */ + public function parseMETAdata() { + $info = &$this->getid3->info; + do { + $BlockOffset = $this->ftell(); + $BlockHeader = $this->fread(4); + $LBFBT = getid3_lib::BigEndian2Int(substr($BlockHeader, 0, 1)); // LBFBT = LastBlockFlag + BlockType + $LastBlockFlag = (bool) ($LBFBT & 0x80); + $BlockType = ($LBFBT & 0x7F); + $BlockLength = getid3_lib::BigEndian2Int(substr($BlockHeader, 1, 3)); + $BlockTypeText = self::metaBlockTypeLookup($BlockType); + + if (($BlockOffset + 4 + $BlockLength) > $info['avdataend']) { + $this->warning('METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockTypeText.') at offset '.$BlockOffset.' extends beyond end of file'); + break; + } + if ($BlockLength < 1) { + if ($BlockTypeText != 'reserved') { + // probably supposed to be zero-length + $this->warning('METADATA_BLOCK_HEADER.BLOCK_LENGTH ('.$BlockTypeText.') at offset '.$BlockOffset.' is zero bytes'); + continue; + } + $this->error('METADATA_BLOCK_HEADER.BLOCK_LENGTH ('.$BlockLength.') at offset '.$BlockOffset.' is invalid'); + break; + } + + $info['flac'][$BlockTypeText]['raw'] = array(); + $BlockTypeText_raw = &$info['flac'][$BlockTypeText]['raw']; + + $BlockTypeText_raw['offset'] = $BlockOffset; + $BlockTypeText_raw['last_meta_block'] = $LastBlockFlag; + $BlockTypeText_raw['block_type'] = $BlockType; + $BlockTypeText_raw['block_type_text'] = $BlockTypeText; + $BlockTypeText_raw['block_length'] = $BlockLength; + if ($BlockTypeText_raw['block_type'] != 0x06) { // do not read attachment data automatically + $BlockTypeText_raw['block_data'] = $this->fread($BlockLength); + } + + switch ($BlockTypeText) { + case 'STREAMINFO': // 0x00 + if (!$this->parseSTREAMINFO($BlockTypeText_raw['block_data'])) { + return false; + } + break; + + case 'PADDING': // 0x01 + unset($info['flac']['PADDING']); // ignore + break; + + case 'APPLICATION': // 0x02 + if (!$this->parseAPPLICATION($BlockTypeText_raw['block_data'])) { + return false; + } + break; + + case 'SEEKTABLE': // 0x03 + if (!$this->parseSEEKTABLE($BlockTypeText_raw['block_data'])) { + return false; + } + break; + + case 'VORBIS_COMMENT': // 0x04 + if (!$this->parseVORBIS_COMMENT($BlockTypeText_raw['block_data'])) { + return false; + } + break; + + case 'CUESHEET': // 0x05 + if (!$this->parseCUESHEET($BlockTypeText_raw['block_data'])) { + return false; + } + break; + + case 'PICTURE': // 0x06 + if (!$this->parsePICTURE()) { + return false; + } + break; + + default: + $this->warning('Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockType.') at offset '.$BlockOffset); + } + + unset($info['flac'][$BlockTypeText]['raw']); + $info['avdataoffset'] = $this->ftell(); + } + while ($LastBlockFlag === false); + + // handle tags + if (!empty($info['flac']['VORBIS_COMMENT']['comments'])) { + $info['flac']['comments'] = $info['flac']['VORBIS_COMMENT']['comments']; + } + if (!empty($info['flac']['VORBIS_COMMENT']['vendor'])) { + $info['audio']['encoder'] = str_replace('reference ', '', $info['flac']['VORBIS_COMMENT']['vendor']); + } + + // copy attachments to 'comments' array if nesesary + if (isset($info['flac']['PICTURE']) && ($this->getid3->option_save_attachments !== getID3::ATTACHMENTS_NONE)) { + foreach ($info['flac']['PICTURE'] as $entry) { + if (!empty($entry['data'])) { + if (!isset($info['flac']['comments']['picture'])) { + $info['flac']['comments']['picture'] = array(); + } + $comments_picture_data = array(); + foreach (array('data', 'image_mime', 'image_width', 'image_height', 'imagetype', 'picturetype', 'description', 'datalength') as $picture_key) { + if (isset($entry[$picture_key])) { + $comments_picture_data[$picture_key] = $entry[$picture_key]; + } + } + $info['flac']['comments']['picture'][] = $comments_picture_data; + unset($comments_picture_data); + } + } + } + + if (isset($info['flac']['STREAMINFO'])) { + if (!$this->isDependencyFor('matroska')) { + $info['flac']['compressed_audio_bytes'] = $info['avdataend'] - $info['avdataoffset']; + } + $info['flac']['uncompressed_audio_bytes'] = $info['flac']['STREAMINFO']['samples_stream'] * $info['flac']['STREAMINFO']['channels'] * ($info['flac']['STREAMINFO']['bits_per_sample'] / 8); + if ($info['flac']['uncompressed_audio_bytes'] == 0) { + return $this->error('Corrupt FLAC file: uncompressed_audio_bytes == zero'); + } + if (!empty($info['flac']['compressed_audio_bytes'])) { + $info['flac']['compression_ratio'] = $info['flac']['compressed_audio_bytes'] / $info['flac']['uncompressed_audio_bytes']; + } + } + + // set md5_data_source - built into flac 0.5+ + if (isset($info['flac']['STREAMINFO']['audio_signature'])) { + + if ($info['flac']['STREAMINFO']['audio_signature'] === str_repeat("\x00", 16)) { + $this->warning('FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)'); + } + else { + $info['md5_data_source'] = ''; + $md5 = $info['flac']['STREAMINFO']['audio_signature']; + for ($i = 0; $i < strlen($md5); $i++) { + $info['md5_data_source'] .= str_pad(dechex(ord($md5[$i])), 2, '00', STR_PAD_LEFT); + } + if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) { + unset($info['md5_data_source']); + } + } + } + + if (isset($info['flac']['STREAMINFO']['bits_per_sample'])) { + $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample']; + if ($info['audio']['bits_per_sample'] == 8) { + // special case + // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value + // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed + $this->warning('FLAC calculates MD5 data strangely on 8-bit audio, so the stored md5_data_source value will not match the decoded WAV file'); + } + } + + return true; + } + + + /** + * @param string $BlockData + * + * @return array + */ + public static function parseSTREAMINFOdata($BlockData) { + $streaminfo = array(); + $streaminfo['min_block_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 0, 2)); + $streaminfo['max_block_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 2, 2)); + $streaminfo['min_frame_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 4, 3)); + $streaminfo['max_frame_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 7, 3)); + + $SRCSBSS = getid3_lib::BigEndian2Bin(substr($BlockData, 10, 8)); + $streaminfo['sample_rate'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 0, 20)); + $streaminfo['channels'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 20, 3)) + 1; + $streaminfo['bits_per_sample'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 23, 5)) + 1; + $streaminfo['samples_stream'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 28, 36)); + + $streaminfo['audio_signature'] = substr($BlockData, 18, 16); + + return $streaminfo; + } + + /** + * @param string $BlockData + * + * @return bool + */ + private function parseSTREAMINFO($BlockData) { + $info = &$this->getid3->info; + + $info['flac']['STREAMINFO'] = self::parseSTREAMINFOdata($BlockData); + + if (!empty($info['flac']['STREAMINFO']['sample_rate'])) { + + $info['audio']['bitrate_mode'] = 'vbr'; + $info['audio']['sample_rate'] = $info['flac']['STREAMINFO']['sample_rate']; + $info['audio']['channels'] = $info['flac']['STREAMINFO']['channels']; + $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample']; + $info['playtime_seconds'] = $info['flac']['STREAMINFO']['samples_stream'] / $info['flac']['STREAMINFO']['sample_rate']; + if ($info['playtime_seconds'] > 0) { + if (!$this->isDependencyFor('matroska')) { + $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; + } + else { + $this->warning('Cannot determine audio bitrate because total stream size is unknown'); + } + } + + } else { + return $this->error('Corrupt METAdata block: STREAMINFO'); + } + + return true; + } + + /** + * @param string $BlockData + * + * @return bool + */ + private function parseAPPLICATION($BlockData) { + $info = &$this->getid3->info; + + $ApplicationID = getid3_lib::BigEndian2Int(substr($BlockData, 0, 4)); + $info['flac']['APPLICATION'][$ApplicationID]['name'] = self::applicationIDLookup($ApplicationID); + $info['flac']['APPLICATION'][$ApplicationID]['data'] = substr($BlockData, 4); + + return true; + } + + /** + * @param string $BlockData + * + * @return bool + */ + private function parseSEEKTABLE($BlockData) { + $info = &$this->getid3->info; + + $offset = 0; + $BlockLength = strlen($BlockData); + $placeholderpattern = str_repeat("\xFF", 8); + while ($offset < $BlockLength) { + $SampleNumberString = substr($BlockData, $offset, 8); + $offset += 8; + if ($SampleNumberString == $placeholderpattern) { + + // placeholder point + getid3_lib::safe_inc($info['flac']['SEEKTABLE']['placeholders'], 1); + $offset += 10; + + } else { + + $SampleNumber = getid3_lib::BigEndian2Int($SampleNumberString); + $info['flac']['SEEKTABLE'][$SampleNumber]['offset'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); + $offset += 8; + $info['flac']['SEEKTABLE'][$SampleNumber]['samples'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 2)); + $offset += 2; + + } + } + + return true; + } + + /** + * @param string $BlockData + * + * @return bool + */ + private function parseVORBIS_COMMENT($BlockData) { + $info = &$this->getid3->info; + + $getid3_ogg = new getid3_ogg($this->getid3); + if ($this->isDependencyFor('matroska')) { + $getid3_ogg->setStringMode($this->data_string); + } + $getid3_ogg->ParseVorbisComments(); + if (isset($info['ogg'])) { + unset($info['ogg']['comments_raw']); + $info['flac']['VORBIS_COMMENT'] = $info['ogg']; + unset($info['ogg']); + } + + unset($getid3_ogg); + + return true; + } + + /** + * @param string $BlockData + * + * @return bool + */ + private function parseCUESHEET($BlockData) { + $info = &$this->getid3->info; + $offset = 0; + $info['flac']['CUESHEET']['media_catalog_number'] = trim(substr($BlockData, $offset, 128), "\0"); + $offset += 128; + $info['flac']['CUESHEET']['lead_in_samples'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); + $offset += 8; + $info['flac']['CUESHEET']['flags']['is_cd'] = (bool) (getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)) & 0x80); + $offset += 1; + + $offset += 258; // reserved + + $info['flac']['CUESHEET']['number_tracks'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); + $offset += 1; + + for ($track = 0; $track < $info['flac']['CUESHEET']['number_tracks']; $track++) { + $TrackSampleOffset = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); + $offset += 8; + $TrackNumber = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); + $offset += 1; + + $info['flac']['CUESHEET']['tracks'][$TrackNumber]['sample_offset'] = $TrackSampleOffset; + + $info['flac']['CUESHEET']['tracks'][$TrackNumber]['isrc'] = substr($BlockData, $offset, 12); + $offset += 12; + + $TrackFlagsRaw = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); + $offset += 1; + $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['is_audio'] = (bool) ($TrackFlagsRaw & 0x80); + $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['pre_emphasis'] = (bool) ($TrackFlagsRaw & 0x40); + + $offset += 13; // reserved + + $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); + $offset += 1; + + for ($index = 0; $index < $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points']; $index++) { + $IndexSampleOffset = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); + $offset += 8; + $IndexNumber = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); + $offset += 1; + + $offset += 3; // reserved + + $info['flac']['CUESHEET']['tracks'][$TrackNumber]['indexes'][$IndexNumber] = $IndexSampleOffset; + } + } + + return true; + } + + /** + * Parse METADATA_BLOCK_PICTURE flac structure and extract attachment + * External usage: audio.ogg + * + * @return bool + */ + public function parsePICTURE() { + $info = &$this->getid3->info; + + $picture['typeid'] = getid3_lib::BigEndian2Int($this->fread(4)); + $picture['picturetype'] = self::pictureTypeLookup($picture['typeid']); + $picture['image_mime'] = $this->fread(getid3_lib::BigEndian2Int($this->fread(4))); + $descr_length = getid3_lib::BigEndian2Int($this->fread(4)); + if ($descr_length) { + $picture['description'] = $this->fread($descr_length); + } + $picture['image_width'] = getid3_lib::BigEndian2Int($this->fread(4)); + $picture['image_height'] = getid3_lib::BigEndian2Int($this->fread(4)); + $picture['color_depth'] = getid3_lib::BigEndian2Int($this->fread(4)); + $picture['colors_indexed'] = getid3_lib::BigEndian2Int($this->fread(4)); + $picture['datalength'] = getid3_lib::BigEndian2Int($this->fread(4)); + + if ($picture['image_mime'] == '-->') { + $picture['data'] = $this->fread($picture['datalength']); + } else { + $picture['data'] = $this->saveAttachment( + str_replace('/', '_', $picture['picturetype']).'_'.$this->ftell(), + $this->ftell(), + $picture['datalength'], + $picture['image_mime']); + } + + $info['flac']['PICTURE'][] = $picture; + + return true; + } + + /** + * @param int $blocktype + * + * @return string + */ + public static function metaBlockTypeLookup($blocktype) { + static $lookup = array( + 0 => 'STREAMINFO', + 1 => 'PADDING', + 2 => 'APPLICATION', + 3 => 'SEEKTABLE', + 4 => 'VORBIS_COMMENT', + 5 => 'CUESHEET', + 6 => 'PICTURE', + ); + return (isset($lookup[$blocktype]) ? $lookup[$blocktype] : 'reserved'); + } + + /** + * @param int $applicationid + * + * @return string + */ + public static function applicationIDLookup($applicationid) { + // http://flac.sourceforge.net/id.html + static $lookup = array( + 0x41544348 => 'FlacFile', // "ATCH" + 0x42534F4C => 'beSolo', // "BSOL" + 0x42554753 => 'Bugs Player', // "BUGS" + 0x43756573 => 'GoldWave cue points (specification)', // "Cues" + 0x46696361 => 'CUE Splitter', // "Fica" + 0x46746F6C => 'flac-tools', // "Ftol" + 0x4D4F5442 => 'MOTB MetaCzar', // "MOTB" + 0x4D505345 => 'MP3 Stream Editor', // "MPSE" + 0x4D754D4C => 'MusicML: Music Metadata Language', // "MuML" + 0x52494646 => 'Sound Devices RIFF chunk storage', // "RIFF" + 0x5346464C => 'Sound Font FLAC', // "SFFL" + 0x534F4E59 => 'Sony Creative Software', // "SONY" + 0x5351455A => 'flacsqueeze', // "SQEZ" + 0x54745776 => 'TwistedWave', // "TtWv" + 0x55495453 => 'UITS Embedding tools', // "UITS" + 0x61696666 => 'FLAC AIFF chunk storage', // "aiff" + 0x696D6167 => 'flac-image application for storing arbitrary files in APPLICATION metadata blocks', // "imag" + 0x7065656D => 'Parseable Embedded Extensible Metadata (specification)', // "peem" + 0x71667374 => 'QFLAC Studio', // "qfst" + 0x72696666 => 'FLAC RIFF chunk storage', // "riff" + 0x74756E65 => 'TagTuner', // "tune" + 0x78626174 => 'XBAT', // "xbat" + 0x786D6364 => 'xmcd', // "xmcd" + ); + return (isset($lookup[$applicationid]) ? $lookup[$applicationid] : 'reserved'); + } + + /** + * @param int $type_id + * + * @return string + */ + public static function pictureTypeLookup($type_id) { + static $lookup = array ( + 0 => 'Other', + 1 => '32x32 pixels \'file icon\' (PNG only)', + 2 => 'Other file icon', + 3 => 'Cover (front)', + 4 => 'Cover (back)', + 5 => 'Leaflet page', + 6 => 'Media (e.g. label side of CD)', + 7 => 'Lead artist/lead performer/soloist', + 8 => 'Artist/performer', + 9 => 'Conductor', + 10 => 'Band/Orchestra', + 11 => 'Composer', + 12 => 'Lyricist/text writer', + 13 => 'Recording Location', + 14 => 'During recording', + 15 => 'During performance', + 16 => 'Movie/video screen capture', + 17 => 'A bright coloured fish', + 18 => 'Illustration', + 19 => 'Band/artist logotype', + 20 => 'Publisher/Studio logotype', + ); + return (isset($lookup[$type_id]) ? $lookup[$type_id] : 'reserved'); + } + +} diff --git a/projects/tests/tests/performance/ID3/module.audio.mp3.php b/projects/tests/tests/performance/ID3/module.audio.mp3.php new file mode 100644 index 00000000..26b28068 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio.mp3.php @@ -0,0 +1,2162 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio.mp3.php // +// module for analyzing MP3 files // +// dependencies: NONE // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} + +// number of frames to scan to determine if MPEG-audio sequence is valid +// Lower this number to 5-20 for faster scanning +// Increase this number to 50+ for most accurate detection of valid VBR/CBR +// mpeg-audio streams +define('GETID3_MP3_VALID_CHECK_FRAMES', 35); + + +class getid3_mp3 extends getid3_handler +{ + /** + * Forces getID3() to scan the file byte-by-byte and log all the valid audio frame headers - extremely slow, + * unrecommended, but may provide data from otherwise-unusable files. + * + * @var bool + */ + public $allow_bruteforce = false; + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + $initialOffset = $info['avdataoffset']; + + if (!$this->getOnlyMPEGaudioInfo($info['avdataoffset'])) { + if ($this->allow_bruteforce) { + $this->error('Rescanning file in BruteForce mode'); + $this->getOnlyMPEGaudioInfoBruteForce(); + } + } + + + if (isset($info['mpeg']['audio']['bitrate_mode'])) { + $info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']); + } + + if (((isset($info['id3v2']['headerlength']) && ($info['avdataoffset'] > $info['id3v2']['headerlength'])) || (!isset($info['id3v2']) && ($info['avdataoffset'] > 0) && ($info['avdataoffset'] != $initialOffset)))) { + + $synchoffsetwarning = 'Unknown data before synch '; + if (isset($info['id3v2']['headerlength'])) { + $synchoffsetwarning .= '(ID3v2 header ends at '.$info['id3v2']['headerlength'].', then '.($info['avdataoffset'] - $info['id3v2']['headerlength']).' bytes garbage, '; + } elseif ($initialOffset > 0) { + $synchoffsetwarning .= '(should be at '.$initialOffset.', '; + } else { + $synchoffsetwarning .= '(should be at beginning of file, '; + } + $synchoffsetwarning .= 'synch detected at '.$info['avdataoffset'].')'; + if (isset($info['audio']['bitrate_mode']) && ($info['audio']['bitrate_mode'] == 'cbr')) { + + if (!empty($info['id3v2']['headerlength']) && (($info['avdataoffset'] - $info['id3v2']['headerlength']) == $info['mpeg']['audio']['framelength'])) { + + $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90-3.92) DLL in CBR mode.'; + $info['audio']['codec'] = 'LAME'; + $CurrentDataLAMEversionString = 'LAME3.'; + + } elseif (empty($info['id3v2']['headerlength']) && ($info['avdataoffset'] == $info['mpeg']['audio']['framelength'])) { + + $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90 - 3.92) DLL in CBR mode.'; + $info['audio']['codec'] = 'LAME'; + $CurrentDataLAMEversionString = 'LAME3.'; + + } + + } + $this->warning($synchoffsetwarning); + + } + + if (isset($info['mpeg']['audio']['LAME'])) { + $info['audio']['codec'] = 'LAME'; + if (!empty($info['mpeg']['audio']['LAME']['long_version'])) { + $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['long_version'], "\x00"); + } elseif (!empty($info['mpeg']['audio']['LAME']['short_version'])) { + $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['short_version'], "\x00"); + } + } + + $CurrentDataLAMEversionString = (!empty($CurrentDataLAMEversionString) ? $CurrentDataLAMEversionString : (isset($info['audio']['encoder']) ? $info['audio']['encoder'] : '')); + if (!empty($CurrentDataLAMEversionString) && (substr($CurrentDataLAMEversionString, 0, 6) == 'LAME3.') && !preg_match('[0-9\)]', substr($CurrentDataLAMEversionString, -1))) { + // a version number of LAME that does not end with a number like "LAME3.92" + // or with a closing parenthesis like "LAME3.88 (alpha)" + // or a version of LAME with the LAMEtag-not-filled-in-DLL-mode bug (3.90-3.92) + + // not sure what the actual last frame length will be, but will be less than or equal to 1441 + $PossiblyLongerLAMEversion_FrameLength = 1441; + + // Not sure what version of LAME this is - look in padding of last frame for longer version string + $PossibleLAMEversionStringOffset = $info['avdataend'] - $PossiblyLongerLAMEversion_FrameLength; + $this->fseek($PossibleLAMEversionStringOffset); + $PossiblyLongerLAMEversion_Data = $this->fread($PossiblyLongerLAMEversion_FrameLength); + switch (substr($CurrentDataLAMEversionString, -1)) { + case 'a': + case 'b': + // "LAME3.94a" will have a longer version string of "LAME3.94 (alpha)" for example + // need to trim off "a" to match longer string + $CurrentDataLAMEversionString = substr($CurrentDataLAMEversionString, 0, -1); + break; + } + if (($PossiblyLongerLAMEversion_String = strstr($PossiblyLongerLAMEversion_Data, $CurrentDataLAMEversionString)) !== false) { + if (substr($PossiblyLongerLAMEversion_String, 0, strlen($CurrentDataLAMEversionString)) == $CurrentDataLAMEversionString) { + $PossiblyLongerLAMEversion_NewString = substr($PossiblyLongerLAMEversion_String, 0, strspn($PossiblyLongerLAMEversion_String, 'LAME0123456789., (abcdefghijklmnopqrstuvwxyzJFSOND)')); //"LAME3.90.3" "LAME3.87 (beta 1, Sep 27 2000)" "LAME3.88 (beta)" + if (empty($info['audio']['encoder']) || (strlen($PossiblyLongerLAMEversion_NewString) > strlen($info['audio']['encoder']))) { + $info['audio']['encoder'] = $PossiblyLongerLAMEversion_NewString; + } + } + } + } + if (!empty($info['audio']['encoder'])) { + $info['audio']['encoder'] = rtrim($info['audio']['encoder'], "\x00 "); + } + + switch (isset($info['mpeg']['audio']['layer']) ? $info['mpeg']['audio']['layer'] : '') { + case 1: + case 2: + $info['audio']['dataformat'] = 'mp'.$info['mpeg']['audio']['layer']; + break; + } + if (isset($info['fileformat']) && ($info['fileformat'] == 'mp3')) { + switch ($info['audio']['dataformat']) { + case 'mp1': + case 'mp2': + case 'mp3': + $info['fileformat'] = $info['audio']['dataformat']; + break; + + default: + $this->warning('Expecting [audio][dataformat] to be mp1/mp2/mp3 when fileformat == mp3, [audio][dataformat] actually "'.$info['audio']['dataformat'].'"'); + break; + } + } + + if (empty($info['fileformat'])) { + unset($info['fileformat']); + unset($info['audio']['bitrate_mode']); + unset($info['avdataoffset']); + unset($info['avdataend']); + return false; + } + + $info['mime_type'] = 'audio/mpeg'; + $info['audio']['lossless'] = false; + + // Calculate playtime + if (!isset($info['playtime_seconds']) && isset($info['audio']['bitrate']) && ($info['audio']['bitrate'] > 0)) { + // https://github.com/JamesHeinrich/getID3/issues/161 + // VBR header frame contains ~0.026s of silent audio data, but is not actually part of the original encoding and should be ignored + $xingVBRheaderFrameLength = ((isset($info['mpeg']['audio']['VBR_frames']) && isset($info['mpeg']['audio']['framelength'])) ? $info['mpeg']['audio']['framelength'] : 0); + + $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset'] - $xingVBRheaderFrameLength) * 8 / $info['audio']['bitrate']; + } + + $info['audio']['encoder_options'] = $this->GuessEncoderOptions(); + + return true; + } + + /** + * @return string + */ + public function GuessEncoderOptions() { + // shortcuts + $info = &$this->getid3->info; + $thisfile_mpeg_audio = array(); + $thisfile_mpeg_audio_lame = array(); + if (!empty($info['mpeg']['audio'])) { + $thisfile_mpeg_audio = &$info['mpeg']['audio']; + if (!empty($thisfile_mpeg_audio['LAME'])) { + $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME']; + } + } + + $encoder_options = ''; + static $NamedPresetBitrates = array(16, 24, 40, 56, 112, 128, 160, 192, 256); + + if (isset($thisfile_mpeg_audio['VBR_method']) && ($thisfile_mpeg_audio['VBR_method'] == 'Fraunhofer') && !empty($thisfile_mpeg_audio['VBR_quality'])) { + + $encoder_options = 'VBR q'.$thisfile_mpeg_audio['VBR_quality']; + + } elseif (!empty($thisfile_mpeg_audio_lame['preset_used']) && isset($thisfile_mpeg_audio_lame['preset_used_id']) && (!in_array($thisfile_mpeg_audio_lame['preset_used_id'], $NamedPresetBitrates))) { + + $encoder_options = $thisfile_mpeg_audio_lame['preset_used']; + + } elseif (!empty($thisfile_mpeg_audio_lame['vbr_quality'])) { + + static $KnownEncoderValues = array(); + if (empty($KnownEncoderValues)) { + + //$KnownEncoderValues[abrbitrate_minbitrate][vbr_quality][raw_vbr_method][raw_noise_shaping][raw_stereo_mode][ath_type][lowpass_frequency] = 'preset name'; + $KnownEncoderValues[0xFF][58][1][1][3][2][20500] = '--alt-preset insane'; // 3.90, 3.90.1, 3.92 + $KnownEncoderValues[0xFF][58][1][1][3][2][20600] = '--alt-preset insane'; // 3.90.2, 3.90.3, 3.91 + $KnownEncoderValues[0xFF][57][1][1][3][4][20500] = '--alt-preset insane'; // 3.94, 3.95 + $KnownEncoderValues['**'][78][3][2][3][2][19500] = '--alt-preset extreme'; // 3.90, 3.90.1, 3.92 + $KnownEncoderValues['**'][78][3][2][3][2][19600] = '--alt-preset extreme'; // 3.90.2, 3.91 + $KnownEncoderValues['**'][78][3][1][3][2][19600] = '--alt-preset extreme'; // 3.90.3 + $KnownEncoderValues['**'][78][4][2][3][2][19500] = '--alt-preset fast extreme'; // 3.90, 3.90.1, 3.92 + $KnownEncoderValues['**'][78][4][2][3][2][19600] = '--alt-preset fast extreme'; // 3.90.2, 3.90.3, 3.91 + $KnownEncoderValues['**'][78][3][2][3][4][19000] = '--alt-preset standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues['**'][78][3][1][3][4][19000] = '--alt-preset standard'; // 3.90.3 + $KnownEncoderValues['**'][78][4][2][3][4][19000] = '--alt-preset fast standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues['**'][78][4][1][3][4][19000] = '--alt-preset fast standard'; // 3.90.3 + $KnownEncoderValues['**'][88][4][1][3][3][19500] = '--r3mix'; // 3.90, 3.90.1, 3.92 + $KnownEncoderValues['**'][88][4][1][3][3][19600] = '--r3mix'; // 3.90.2, 3.90.3, 3.91 + $KnownEncoderValues['**'][67][4][1][3][4][18000] = '--r3mix'; // 3.94, 3.95 + $KnownEncoderValues['**'][68][3][2][3][4][18000] = '--alt-preset medium'; // 3.90.3 + $KnownEncoderValues['**'][68][4][2][3][4][18000] = '--alt-preset fast medium'; // 3.90.3 + + $KnownEncoderValues[0xFF][99][1][1][1][2][0] = '--preset studio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues[0xFF][58][2][1][3][2][20600] = '--preset studio'; // 3.90.3, 3.93.1 + $KnownEncoderValues[0xFF][58][2][1][3][2][20500] = '--preset studio'; // 3.93 + $KnownEncoderValues[0xFF][57][2][1][3][4][20500] = '--preset studio'; // 3.94, 3.95 + $KnownEncoderValues[0xC0][88][1][1][1][2][0] = '--preset cd'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues[0xC0][58][2][2][3][2][19600] = '--preset cd'; // 3.90.3, 3.93.1 + $KnownEncoderValues[0xC0][58][2][2][3][2][19500] = '--preset cd'; // 3.93 + $KnownEncoderValues[0xC0][57][2][1][3][4][19500] = '--preset cd'; // 3.94, 3.95 + $KnownEncoderValues[0xA0][78][1][1][3][2][18000] = '--preset hifi'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues[0xA0][58][2][2][3][2][18000] = '--preset hifi'; // 3.90.3, 3.93, 3.93.1 + $KnownEncoderValues[0xA0][57][2][1][3][4][18000] = '--preset hifi'; // 3.94, 3.95 + $KnownEncoderValues[0x80][67][1][1][3][2][18000] = '--preset tape'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues[0x80][67][1][1][3][2][15000] = '--preset radio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues[0x70][67][1][1][3][2][15000] = '--preset fm'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92 + $KnownEncoderValues[0x70][58][2][2][3][2][16000] = '--preset tape/radio/fm'; // 3.90.3, 3.93, 3.93.1 + $KnownEncoderValues[0x70][57][2][1][3][4][16000] = '--preset tape/radio/fm'; // 3.94, 3.95 + $KnownEncoderValues[0x38][58][2][2][0][2][10000] = '--preset voice'; // 3.90.3, 3.93, 3.93.1 + $KnownEncoderValues[0x38][57][2][1][0][4][15000] = '--preset voice'; // 3.94, 3.95 + $KnownEncoderValues[0x38][57][2][1][0][4][16000] = '--preset voice'; // 3.94a14 + $KnownEncoderValues[0x28][65][1][1][0][2][7500] = '--preset mw-us'; // 3.90, 3.90.1, 3.92 + $KnownEncoderValues[0x28][65][1][1][0][2][7600] = '--preset mw-us'; // 3.90.2, 3.91 + $KnownEncoderValues[0x28][58][2][2][0][2][7000] = '--preset mw-us'; // 3.90.3, 3.93, 3.93.1 + $KnownEncoderValues[0x28][57][2][1][0][4][10500] = '--preset mw-us'; // 3.94, 3.95 + $KnownEncoderValues[0x28][57][2][1][0][4][11200] = '--preset mw-us'; // 3.94a14 + $KnownEncoderValues[0x28][57][2][1][0][4][8800] = '--preset mw-us'; // 3.94a15 + $KnownEncoderValues[0x18][58][2][2][0][2][4000] = '--preset phon+/lw/mw-eu/sw'; // 3.90.3, 3.93.1 + $KnownEncoderValues[0x18][58][2][2][0][2][3900] = '--preset phon+/lw/mw-eu/sw'; // 3.93 + $KnownEncoderValues[0x18][57][2][1][0][4][5900] = '--preset phon+/lw/mw-eu/sw'; // 3.94, 3.95 + $KnownEncoderValues[0x18][57][2][1][0][4][6200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a14 + $KnownEncoderValues[0x18][57][2][1][0][4][3200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a15 + $KnownEncoderValues[0x10][58][2][2][0][2][3800] = '--preset phone'; // 3.90.3, 3.93.1 + $KnownEncoderValues[0x10][58][2][2][0][2][3700] = '--preset phone'; // 3.93 + $KnownEncoderValues[0x10][57][2][1][0][4][5600] = '--preset phone'; // 3.94, 3.95 + } + + if (isset($KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) { + + $encoder_options = $KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']]; + + } elseif (isset($KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) { + + $encoder_options = $KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']]; + + } elseif ($info['audio']['bitrate_mode'] == 'vbr') { + + // http://gabriel.mp3-tech.org/mp3infotag.html + // int Quality = (100 - 10 * gfp->VBR_q - gfp->quality)h + + + $LAME_V_value = 10 - ceil($thisfile_mpeg_audio_lame['vbr_quality'] / 10); + $LAME_q_value = 100 - $thisfile_mpeg_audio_lame['vbr_quality'] - ($LAME_V_value * 10); + $encoder_options = '-V'.$LAME_V_value.' -q'.$LAME_q_value; + + } elseif ($info['audio']['bitrate_mode'] == 'cbr') { + + $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000); + + } else { + + $encoder_options = strtoupper($info['audio']['bitrate_mode']); + + } + + } elseif (!empty($thisfile_mpeg_audio_lame['bitrate_abr'])) { + + $encoder_options = 'ABR'.$thisfile_mpeg_audio_lame['bitrate_abr']; + + } elseif (!empty($info['audio']['bitrate'])) { + + if ($info['audio']['bitrate_mode'] == 'cbr') { + $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000); + } else { + $encoder_options = strtoupper($info['audio']['bitrate_mode']); + } + + } + if (!empty($thisfile_mpeg_audio_lame['bitrate_min'])) { + $encoder_options .= ' -b'.$thisfile_mpeg_audio_lame['bitrate_min']; + } + + if (!empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev']) || !empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'])) { + $encoder_options .= ' --nogap'; + } + + if (!empty($thisfile_mpeg_audio_lame['lowpass_frequency'])) { + $ExplodedOptions = explode(' ', $encoder_options, 4); + if ($ExplodedOptions[0] == '--r3mix') { + $ExplodedOptions[1] = 'r3mix'; + } + switch ($ExplodedOptions[0]) { + case '--preset': + case '--alt-preset': + case '--r3mix': + if ($ExplodedOptions[1] == 'fast') { + $ExplodedOptions[1] .= ' '.$ExplodedOptions[2]; + } + switch ($ExplodedOptions[1]) { + case 'portable': + case 'medium': + case 'standard': + case 'extreme': + case 'insane': + case 'fast portable': + case 'fast medium': + case 'fast standard': + case 'fast extreme': + case 'fast insane': + case 'r3mix': + static $ExpectedLowpass = array( + 'insane|20500' => 20500, + 'insane|20600' => 20600, // 3.90.2, 3.90.3, 3.91 + 'medium|18000' => 18000, + 'fast medium|18000' => 18000, + 'extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95 + 'extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1 + 'fast extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95 + 'fast extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1 + 'standard|19000' => 19000, + 'fast standard|19000' => 19000, + 'r3mix|19500' => 19500, // 3.90, 3.90.1, 3.92 + 'r3mix|19600' => 19600, // 3.90.2, 3.90.3, 3.91 + 'r3mix|18000' => 18000, // 3.94, 3.95 + ); + if (!isset($ExpectedLowpass[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio_lame['lowpass_frequency']]) && ($thisfile_mpeg_audio_lame['lowpass_frequency'] < 22050) && (round($thisfile_mpeg_audio_lame['lowpass_frequency'] / 1000) < round($thisfile_mpeg_audio['sample_rate'] / 2000))) { + $encoder_options .= ' --lowpass '.$thisfile_mpeg_audio_lame['lowpass_frequency']; + } + break; + + default: + break; + } + break; + } + } + + if (isset($thisfile_mpeg_audio_lame['raw']['source_sample_freq'])) { + if (($thisfile_mpeg_audio['sample_rate'] == 44100) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 1)) { + $encoder_options .= ' --resample 44100'; + } elseif (($thisfile_mpeg_audio['sample_rate'] == 48000) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 2)) { + $encoder_options .= ' --resample 48000'; + } elseif ($thisfile_mpeg_audio['sample_rate'] < 44100) { + switch ($thisfile_mpeg_audio_lame['raw']['source_sample_freq']) { + case 0: // <= 32000 + // may or may not be same as source frequency - ignore + break; + case 1: // 44100 + case 2: // 48000 + case 3: // 48000+ + $ExplodedOptions = explode(' ', $encoder_options, 4); + switch ($ExplodedOptions[0]) { + case '--preset': + case '--alt-preset': + switch ($ExplodedOptions[1]) { + case 'fast': + case 'portable': + case 'medium': + case 'standard': + case 'extreme': + case 'insane': + $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate']; + break; + + default: + static $ExpectedResampledRate = array( + 'phon+/lw/mw-eu/sw|16000' => 16000, + 'mw-us|24000' => 24000, // 3.95 + 'mw-us|32000' => 32000, // 3.93 + 'mw-us|16000' => 16000, // 3.92 + 'phone|16000' => 16000, + 'phone|11025' => 11025, // 3.94a15 + 'radio|32000' => 32000, // 3.94a15 + 'fm/radio|32000' => 32000, // 3.92 + 'fm|32000' => 32000, // 3.90 + 'voice|32000' => 32000); + if (!isset($ExpectedResampledRate[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio['sample_rate']])) { + $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate']; + } + break; + } + break; + + case '--r3mix': + default: + $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate']; + break; + } + break; + } + } + } + if (empty($encoder_options) && !empty($info['audio']['bitrate']) && !empty($info['audio']['bitrate_mode'])) { + //$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000); + $encoder_options = strtoupper($info['audio']['bitrate_mode']); + } + + return $encoder_options; + } + + /** + * @param int $offset + * @param array $info + * @param bool $recursivesearch + * @param bool $ScanAsCBR + * @param bool $FastMPEGheaderScan + * + * @return bool + */ + public function decodeMPEGaudioHeader($offset, &$info, $recursivesearch=true, $ScanAsCBR=false, $FastMPEGheaderScan=false) { + static $MPEGaudioVersionLookup; + static $MPEGaudioLayerLookup; + static $MPEGaudioBitrateLookup; + static $MPEGaudioFrequencyLookup; + static $MPEGaudioChannelModeLookup; + static $MPEGaudioModeExtensionLookup; + static $MPEGaudioEmphasisLookup; + if (empty($MPEGaudioVersionLookup)) { + $MPEGaudioVersionLookup = self::MPEGaudioVersionArray(); + $MPEGaudioLayerLookup = self::MPEGaudioLayerArray(); + $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray(); + $MPEGaudioFrequencyLookup = self::MPEGaudioFrequencyArray(); + $MPEGaudioChannelModeLookup = self::MPEGaudioChannelModeArray(); + $MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray(); + $MPEGaudioEmphasisLookup = self::MPEGaudioEmphasisArray(); + } + + if ($this->fseek($offset) != 0) { + $this->error('decodeMPEGaudioHeader() failed to seek to next offset at '.$offset); + return false; + } + //$headerstring = $this->fread(1441); // worst-case max length = 32kHz @ 320kbps layer 3 = 1441 bytes/frame + $headerstring = $this->fread(226); // LAME header at offset 36 + 190 bytes of Xing/LAME data + + // MP3 audio frame structure: + // $aa $aa $aa $aa [$bb $bb] $cc... + // where $aa..$aa is the four-byte mpeg-audio header (below) + // $bb $bb is the optional 2-byte CRC + // and $cc... is the audio data + + $head4 = substr($headerstring, 0, 4); + $head4_key = getid3_lib::PrintHexBytes($head4, true, false, false); + static $MPEGaudioHeaderDecodeCache = array(); + if (isset($MPEGaudioHeaderDecodeCache[$head4_key])) { + $MPEGheaderRawArray = $MPEGaudioHeaderDecodeCache[$head4_key]; + } else { + $MPEGheaderRawArray = self::MPEGaudioHeaderDecode($head4); + $MPEGaudioHeaderDecodeCache[$head4_key] = $MPEGheaderRawArray; + } + + static $MPEGaudioHeaderValidCache = array(); + if (!isset($MPEGaudioHeaderValidCache[$head4_key])) { // Not in cache + //$MPEGaudioHeaderValidCache[$head4_key] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, true); // allow badly-formatted freeformat (from LAME 3.90 - 3.93.1) + $MPEGaudioHeaderValidCache[$head4_key] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, false); + } + + // shortcut + if (!isset($info['mpeg']['audio'])) { + $info['mpeg']['audio'] = array(); + } + $thisfile_mpeg_audio = &$info['mpeg']['audio']; + + if ($MPEGaudioHeaderValidCache[$head4_key]) { + $thisfile_mpeg_audio['raw'] = $MPEGheaderRawArray; + } else { + $this->error('Invalid MPEG audio header ('.getid3_lib::PrintHexBytes($head4).') at offset '.$offset); + return false; + } + + if (!$FastMPEGheaderScan) { + $thisfile_mpeg_audio['version'] = $MPEGaudioVersionLookup[$thisfile_mpeg_audio['raw']['version']]; + $thisfile_mpeg_audio['layer'] = $MPEGaudioLayerLookup[$thisfile_mpeg_audio['raw']['layer']]; + + $thisfile_mpeg_audio['channelmode'] = $MPEGaudioChannelModeLookup[$thisfile_mpeg_audio['raw']['channelmode']]; + $thisfile_mpeg_audio['channels'] = (($thisfile_mpeg_audio['channelmode'] == 'mono') ? 1 : 2); + $thisfile_mpeg_audio['sample_rate'] = $MPEGaudioFrequencyLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']]; + $thisfile_mpeg_audio['protection'] = !$thisfile_mpeg_audio['raw']['protection']; + $thisfile_mpeg_audio['private'] = (bool) $thisfile_mpeg_audio['raw']['private']; + $thisfile_mpeg_audio['modeextension'] = $MPEGaudioModeExtensionLookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']]; + $thisfile_mpeg_audio['copyright'] = (bool) $thisfile_mpeg_audio['raw']['copyright']; + $thisfile_mpeg_audio['original'] = (bool) $thisfile_mpeg_audio['raw']['original']; + $thisfile_mpeg_audio['emphasis'] = $MPEGaudioEmphasisLookup[$thisfile_mpeg_audio['raw']['emphasis']]; + + $info['audio']['channels'] = $thisfile_mpeg_audio['channels']; + $info['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate']; + + if ($thisfile_mpeg_audio['protection']) { + $thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($headerstring, 4, 2)); + } + } + + if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) { + // http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0 + $this->warning('Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1'); + $thisfile_mpeg_audio['raw']['bitrate'] = 0; + } + $thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding']; + $thisfile_mpeg_audio['bitrate'] = $MPEGaudioBitrateLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']]; + + if (($thisfile_mpeg_audio['bitrate'] == 'free') && ($offset == $info['avdataoffset'])) { + // only skip multiple frame check if free-format bitstream found at beginning of file + // otherwise is quite possibly simply corrupted data + $recursivesearch = false; + } + + // For Layer 2 there are some combinations of bitrate and mode which are not allowed. + if (!$FastMPEGheaderScan && ($thisfile_mpeg_audio['layer'] == '2')) { + + $info['audio']['dataformat'] = 'mp2'; + switch ($thisfile_mpeg_audio['channelmode']) { + + case 'mono': + if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] <= 192000)) { + // these are ok + } else { + $this->error($thisfile_mpeg_audio['bitrate'].'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.'); + return false; + } + break; + + case 'stereo': + case 'joint stereo': + case 'dual channel': + if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] == 64000) || ($thisfile_mpeg_audio['bitrate'] >= 96000)) { + // these are ok + } else { + $this->error(intval(round($thisfile_mpeg_audio['bitrate'] / 1000)).'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.'); + return false; + } + break; + + } + + } + + + if ($info['audio']['sample_rate'] > 0) { + $thisfile_mpeg_audio['framelength'] = self::MPEGaudioFrameLength($thisfile_mpeg_audio['bitrate'], $thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['layer'], (int) $thisfile_mpeg_audio['padding'], $info['audio']['sample_rate']); + } + + $nextframetestoffset = $offset + 1; + if ($thisfile_mpeg_audio['bitrate'] != 'free') { + + $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate']; + + if (isset($thisfile_mpeg_audio['framelength'])) { + $nextframetestoffset = $offset + $thisfile_mpeg_audio['framelength']; + } else { + $this->error('Frame at offset('.$offset.') is has an invalid frame length.'); + return false; + } + + } + + $ExpectedNumberOfAudioBytes = 0; + + //////////////////////////////////////////////////////////////////////////////////// + // Variable-bitrate headers + + if (substr($headerstring, 4 + 32, 4) == 'VBRI') { + // Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36) + // specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html + + $thisfile_mpeg_audio['bitrate_mode'] = 'vbr'; + $thisfile_mpeg_audio['VBR_method'] = 'Fraunhofer'; + $info['audio']['codec'] = 'Fraunhofer'; + + $SideInfoData = substr($headerstring, 4 + 2, 32); + + $FraunhoferVBROffset = 36; + + $thisfile_mpeg_audio['VBR_encoder_version'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 4, 2)); // VbriVersion + $thisfile_mpeg_audio['VBR_encoder_delay'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 6, 2)); // VbriDelay + $thisfile_mpeg_audio['VBR_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 8, 2)); // VbriQuality + $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 10, 4)); // VbriStreamBytes + $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 14, 4)); // VbriStreamFrames + $thisfile_mpeg_audio['VBR_seek_offsets'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 18, 2)); // VbriTableSize + $thisfile_mpeg_audio['VBR_seek_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 20, 2)); // VbriTableScale + $thisfile_mpeg_audio['VBR_entry_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 22, 2)); // VbriEntryBytes + $thisfile_mpeg_audio['VBR_entry_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 24, 2)); // VbriEntryFrames + + $ExpectedNumberOfAudioBytes = $thisfile_mpeg_audio['VBR_bytes']; + + $previousbyteoffset = $offset; + for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) { + $Fraunhofer_OffsetN = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset, $thisfile_mpeg_audio['VBR_entry_bytes'])); + $FraunhoferVBROffset += $thisfile_mpeg_audio['VBR_entry_bytes']; + $thisfile_mpeg_audio['VBR_offsets_relative'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']); + $thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']) + $previousbyteoffset; + $previousbyteoffset += $Fraunhofer_OffsetN; + } + + + } else { + + // Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36) + // depending on MPEG layer and number of channels + + $VBRidOffset = self::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']); + $SideInfoData = substr($headerstring, 4 + 2, $VBRidOffset - 4); + + if ((substr($headerstring, $VBRidOffset, strlen('Xing')) == 'Xing') || (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Info')) { + // 'Xing' is traditional Xing VBR frame + // 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.) + // 'Info' *can* legally be used to specify a VBR file as well, however. + + // http://www.multiweb.cz/twoinches/MP3inside.htm + //00..03 = "Xing" or "Info" + //04..07 = Flags: + // 0x01 Frames Flag set if value for number of frames in file is stored + // 0x02 Bytes Flag set if value for filesize in bytes is stored + // 0x04 TOC Flag set if values for TOC are stored + // 0x08 VBR Scale Flag set if values for VBR scale is stored + //08..11 Frames: Number of frames in file (including the first Xing/Info one) + //12..15 Bytes: File length in Bytes + //16..115 TOC (Table of Contents): + // Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file. + // Each Byte has a value according this formula: + // (TOC[i] / 256) * fileLenInBytes + // So if song lasts eg. 240 sec. and you want to jump to 60. sec. (and file is 5 000 000 Bytes length) you can use: + // TOC[(60/240)*100] = TOC[25] + // and corresponding Byte in file is then approximately at: + // (TOC[25]/256) * 5000000 + //116..119 VBR Scale + + + // should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME +// if (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Xing') { + $thisfile_mpeg_audio['bitrate_mode'] = 'vbr'; + $thisfile_mpeg_audio['VBR_method'] = 'Xing'; +// } else { +// $ScanAsCBR = true; +// $thisfile_mpeg_audio['bitrate_mode'] = 'cbr'; +// } + + $thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 4, 4)); + + $thisfile_mpeg_audio['xing_flags']['frames'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000001); + $thisfile_mpeg_audio['xing_flags']['bytes'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000002); + $thisfile_mpeg_audio['xing_flags']['toc'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000004); + $thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000008); + + if ($thisfile_mpeg_audio['xing_flags']['frames']) { + $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 8, 4)); + //$thisfile_mpeg_audio['VBR_frames']--; // don't count header Xing/Info frame + } + if ($thisfile_mpeg_audio['xing_flags']['bytes']) { + $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 12, 4)); + } + + //if (($thisfile_mpeg_audio['bitrate'] == 'free') && !empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) { + //if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) { + if (!empty($thisfile_mpeg_audio['VBR_frames'])) { + $used_filesize = 0; + if (!empty($thisfile_mpeg_audio['VBR_bytes'])) { + $used_filesize = $thisfile_mpeg_audio['VBR_bytes']; + } elseif (!empty($info['filesize'])) { + $used_filesize = $info['filesize']; + $used_filesize -= (isset($info['id3v2']['headerlength']) ? intval($info['id3v2']['headerlength']) : 0); + $used_filesize -= (isset($info['id3v1']) ? 128 : 0); + $used_filesize -= (isset($info['tag_offset_end']) ? $info['tag_offset_end'] - $info['tag_offset_start'] : 0); + $this->warning('MP3.Xing header missing VBR_bytes, assuming MPEG audio portion of file is '.number_format($used_filesize).' bytes'); + } + + $framelengthfloat = $used_filesize / $thisfile_mpeg_audio['VBR_frames']; + + if ($thisfile_mpeg_audio['layer'] == '1') { + // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12 + //$info['audio']['bitrate'] = ((($framelengthfloat / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12; + $info['audio']['bitrate'] = ($framelengthfloat / 4) * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 12; + } else { + // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144 + //$info['audio']['bitrate'] = (($framelengthfloat - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144; + $info['audio']['bitrate'] = $framelengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 144; + } + $thisfile_mpeg_audio['framelength'] = floor($framelengthfloat); + } + + if ($thisfile_mpeg_audio['xing_flags']['toc']) { + $LAMEtocData = substr($headerstring, $VBRidOffset + 16, 100); + for ($i = 0; $i < 100; $i++) { + $thisfile_mpeg_audio['toc'][$i] = ord($LAMEtocData[$i]); + } + } + if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) { + $thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 116, 4)); + } + + + // http://gabriel.mp3-tech.org/mp3infotag.html + if (substr($headerstring, $VBRidOffset + 120, 4) == 'LAME') { + + // shortcut + $thisfile_mpeg_audio['LAME'] = array(); + $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME']; + + + $thisfile_mpeg_audio_lame['long_version'] = substr($headerstring, $VBRidOffset + 120, 20); + $thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9); + $thisfile_mpeg_audio_lame['numeric_version'] = str_replace('LAME', '', $thisfile_mpeg_audio_lame['short_version']); + if (preg_match('#^LAME([0-9\\.a-z]+)#', $thisfile_mpeg_audio_lame['long_version'], $matches)) { + $thisfile_mpeg_audio_lame['short_version'] = $matches[0]; + $thisfile_mpeg_audio_lame['numeric_version'] = $matches[1]; + } + foreach (explode('.', $thisfile_mpeg_audio_lame['numeric_version']) as $key => $number) { + $thisfile_mpeg_audio_lame['integer_version'][$key] = intval($number); + } + + //if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') { + if ((($thisfile_mpeg_audio_lame['integer_version'][0] * 1000) + $thisfile_mpeg_audio_lame['integer_version'][1]) >= 3090) { // cannot use string version compare, may have "LAME3.90" or "LAME3.100" -- see https://github.com/JamesHeinrich/getID3/issues/207 + + // extra 11 chars are not part of version string when LAMEtag present + unset($thisfile_mpeg_audio_lame['long_version']); + + // It the LAME tag was only introduced in LAME v3.90 + // http://www.hydrogenaudio.org/?act=ST&f=15&t=9933 + + // Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html + // are assuming a 'Xing' identifier offset of 0x24, which is the case for + // MPEG-1 non-mono, but not for other combinations + $LAMEtagOffsetContant = $VBRidOffset - 0x24; + + // shortcuts + $thisfile_mpeg_audio_lame['RGAD'] = array('track'=>array(), 'album'=>array()); + $thisfile_mpeg_audio_lame_RGAD = &$thisfile_mpeg_audio_lame['RGAD']; + $thisfile_mpeg_audio_lame_RGAD_track = &$thisfile_mpeg_audio_lame_RGAD['track']; + $thisfile_mpeg_audio_lame_RGAD_album = &$thisfile_mpeg_audio_lame_RGAD['album']; + $thisfile_mpeg_audio_lame['raw'] = array(); + $thisfile_mpeg_audio_lame_raw = &$thisfile_mpeg_audio_lame['raw']; + + // byte $9B VBR Quality + // This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications. + // Actually overwrites original Xing bytes + unset($thisfile_mpeg_audio['VBR_scale']); + $thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0x9B, 1)); + + // bytes $9C-$A4 Encoder short VersionString + $thisfile_mpeg_audio_lame['short_version'] = substr($headerstring, $LAMEtagOffsetContant + 0x9C, 9); + + // byte $A5 Info Tag revision + VBR method + $LAMEtagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA5, 1)); + + $thisfile_mpeg_audio_lame['tag_revision'] = ($LAMEtagRevisionVBRmethod & 0xF0) >> 4; + $thisfile_mpeg_audio_lame_raw['vbr_method'] = $LAMEtagRevisionVBRmethod & 0x0F; + $thisfile_mpeg_audio_lame['vbr_method'] = self::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']); + $thisfile_mpeg_audio['bitrate_mode'] = substr($thisfile_mpeg_audio_lame['vbr_method'], 0, 3); // usually either 'cbr' or 'vbr', but truncates 'vbr-old / vbr-rh' to 'vbr' + + // byte $A6 Lowpass filter value + $thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA6, 1)) * 100; + + // bytes $A7-$AE Replay Gain + // http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html + // bytes $A7-$AA : 32 bit floating point "Peak signal amplitude" + if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') { + // LAME 3.94a16 and later - 9.23 fixed point + // ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375 + $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = (float) ((getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4))) / 8388608); + } else { + // LAME 3.94a15 and earlier - 32-bit floating point + // Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15 + $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4)); + } + if ($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] == 0) { + unset($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']); + } else { + $thisfile_mpeg_audio_lame_RGAD['peak_db'] = getid3_lib::RGADamplitude2dB($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']); + } + + $thisfile_mpeg_audio_lame_raw['RGAD_track'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAB, 2)); + $thisfile_mpeg_audio_lame_raw['RGAD_album'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAD, 2)); + + + if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) { + + $thisfile_mpeg_audio_lame_RGAD_track['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xE000) >> 13; + $thisfile_mpeg_audio_lame_RGAD_track['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1C00) >> 10; + $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x0200) >> 9; + $thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x01FF; + $thisfile_mpeg_audio_lame_RGAD_track['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['name']); + $thisfile_mpeg_audio_lame_RGAD_track['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['originator']); + $thisfile_mpeg_audio_lame_RGAD_track['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit']); + + if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) { + $info['replay_gain']['track']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude']; + } + $info['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_RGAD_track['originator']; + $info['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_track['gain_db']; + } else { + unset($thisfile_mpeg_audio_lame_RGAD['track']); + } + if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) { + + $thisfile_mpeg_audio_lame_RGAD_album['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xE000) >> 13; + $thisfile_mpeg_audio_lame_RGAD_album['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1C00) >> 10; + $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x0200) >> 9; + $thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x01FF; + $thisfile_mpeg_audio_lame_RGAD_album['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['name']); + $thisfile_mpeg_audio_lame_RGAD_album['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['originator']); + $thisfile_mpeg_audio_lame_RGAD_album['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit']); + + if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) { + $info['replay_gain']['album']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude']; + } + $info['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_RGAD_album['originator']; + $info['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_album['gain_db']; + } else { + unset($thisfile_mpeg_audio_lame_RGAD['album']); + } + if (empty($thisfile_mpeg_audio_lame_RGAD)) { + unset($thisfile_mpeg_audio_lame['RGAD']); + } + + + // byte $AF Encoding flags + ATH Type + $EncodingFlagsATHtype = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAF, 1)); + $thisfile_mpeg_audio_lame['encoding_flags']['nspsytune'] = (bool) ($EncodingFlagsATHtype & 0x10); + $thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($EncodingFlagsATHtype & 0x20); + $thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'] = (bool) ($EncodingFlagsATHtype & 0x40); + $thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] = (bool) ($EncodingFlagsATHtype & 0x80); + $thisfile_mpeg_audio_lame['ath_type'] = $EncodingFlagsATHtype & 0x0F; + + // byte $B0 if ABR {specified bitrate} else {minimal bitrate} + $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB0, 1)); + if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR) + $thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']; + } elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR) + // ignore + } elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate + $thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']; + } + + // bytes $B1-$B3 Encoder delays + $EncoderDelays = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB1, 3)); + $thisfile_mpeg_audio_lame['encoder_delay'] = ($EncoderDelays & 0xFFF000) >> 12; + $thisfile_mpeg_audio_lame['end_padding'] = $EncoderDelays & 0x000FFF; + + // byte $B4 Misc + $MiscByte = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB4, 1)); + $thisfile_mpeg_audio_lame_raw['noise_shaping'] = ($MiscByte & 0x03); + $thisfile_mpeg_audio_lame_raw['stereo_mode'] = ($MiscByte & 0x1C) >> 2; + $thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($MiscByte & 0x20) >> 5; + $thisfile_mpeg_audio_lame_raw['source_sample_freq'] = ($MiscByte & 0xC0) >> 6; + $thisfile_mpeg_audio_lame['noise_shaping'] = $thisfile_mpeg_audio_lame_raw['noise_shaping']; + $thisfile_mpeg_audio_lame['stereo_mode'] = self::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']); + $thisfile_mpeg_audio_lame['not_optimal_quality'] = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality']; + $thisfile_mpeg_audio_lame['source_sample_freq'] = self::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']); + + // byte $B5 MP3 Gain + $thisfile_mpeg_audio_lame_raw['mp3_gain'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB5, 1), false, true); + $thisfile_mpeg_audio_lame['mp3_gain_db'] = (getid3_lib::RGADamplitude2dB(2) / 4) * $thisfile_mpeg_audio_lame_raw['mp3_gain']; + $thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, ($thisfile_mpeg_audio_lame['mp3_gain_db'] / 6)); + + // bytes $B6-$B7 Preset and surround info + $PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB6, 2)); + // Reserved = ($PresetSurroundBytes & 0xC000); + $thisfile_mpeg_audio_lame_raw['surround_info'] = ($PresetSurroundBytes & 0x3800); + $thisfile_mpeg_audio_lame['surround_info'] = self::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']); + $thisfile_mpeg_audio_lame['preset_used_id'] = ($PresetSurroundBytes & 0x07FF); + $thisfile_mpeg_audio_lame['preset_used'] = self::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame); + if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) { + $this->warning('Unknown LAME preset used ('.$thisfile_mpeg_audio_lame['preset_used_id'].') - please report to info@getid3.org'); + } + if (($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.') && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) { + // this may change if 3.90.4 ever comes out + $thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3'; + } + + // bytes $B8-$BB MusicLength + $thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB8, 4)); + $ExpectedNumberOfAudioBytes = (($thisfile_mpeg_audio_lame['audio_bytes'] > 0) ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']); + + // bytes $BC-$BD MusicCRC + $thisfile_mpeg_audio_lame['music_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBC, 2)); + + // bytes $BE-$BF CRC-16 of Info Tag + $thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBE, 2)); + + + // LAME CBR + if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { + + $thisfile_mpeg_audio['bitrate_mode'] = 'cbr'; + $thisfile_mpeg_audio['bitrate'] = self::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']); + $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate']; + //if (empty($thisfile_mpeg_audio['bitrate']) || (!empty($thisfile_mpeg_audio_lame['bitrate_min']) && ($thisfile_mpeg_audio_lame['bitrate_min'] != 255))) { + // $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio_lame['bitrate_min']; + //} + + } + + } + } + + } else { + + // not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header) + $thisfile_mpeg_audio['bitrate_mode'] = 'cbr'; + if ($recursivesearch) { + $thisfile_mpeg_audio['bitrate_mode'] = 'vbr'; + if ($this->RecursiveFrameScanning($offset, $nextframetestoffset, true)) { + $recursivesearch = false; + $thisfile_mpeg_audio['bitrate_mode'] = 'cbr'; + } + if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') { + $this->warning('VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.'); + } + } + + } + + } + + if (($ExpectedNumberOfAudioBytes > 0) && ($ExpectedNumberOfAudioBytes != ($info['avdataend'] - $info['avdataoffset']))) { + if ($ExpectedNumberOfAudioBytes > ($info['avdataend'] - $info['avdataoffset'])) { + if ($this->isDependencyFor('matroska') || $this->isDependencyFor('riff')) { + // ignore, audio data is broken into chunks so will always be data "missing" + } + elseif (($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])) == 1) { + $this->warning('Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)'); + } + else { + $this->warning('Probable truncated file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' (short by '.($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])).' bytes)'); + } + } else { + if ((($info['avdataend'] - $info['avdataoffset']) - $ExpectedNumberOfAudioBytes) == 1) { + // $prenullbytefileoffset = $this->ftell(); + // $this->fseek($info['avdataend']); + // $PossibleNullByte = $this->fread(1); + // $this->fseek($prenullbytefileoffset); + // if ($PossibleNullByte === "\x00") { + $info['avdataend']--; + // $this->warning('Extra null byte at end of MP3 data assumed to be RIFF padding and therefore ignored'); + // } else { + // $this->warning('Too much data in file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, found '.($info['avdataend'] - $info['avdataoffset']).' ('.(($info['avdataend'] - $info['avdataoffset']) - $ExpectedNumberOfAudioBytes).' bytes too many)'); + // } + } else { + $this->warning('Too much data in file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, found '.($info['avdataend'] - $info['avdataoffset']).' ('.(($info['avdataend'] - $info['avdataoffset']) - $ExpectedNumberOfAudioBytes).' bytes too many)'); + } + } + } + + if (($thisfile_mpeg_audio['bitrate'] == 'free') && empty($info['audio']['bitrate'])) { + if (($offset == $info['avdataoffset']) && empty($thisfile_mpeg_audio['VBR_frames'])) { + $framebytelength = $this->FreeFormatFrameLength($offset, true); + if ($framebytelength > 0) { + $thisfile_mpeg_audio['framelength'] = $framebytelength; + if ($thisfile_mpeg_audio['layer'] == '1') { + // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12 + $info['audio']['bitrate'] = ((($framebytelength / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12; + } else { + // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144 + $info['audio']['bitrate'] = (($framebytelength - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144; + } + } else { + $this->error('Error calculating frame length of free-format MP3 without Xing/LAME header'); + } + } + } + + if (isset($thisfile_mpeg_audio['VBR_frames']) ? $thisfile_mpeg_audio['VBR_frames'] : '') { + switch ($thisfile_mpeg_audio['bitrate_mode']) { + case 'vbr': + case 'abr': + $bytes_per_frame = 1152; + if (($thisfile_mpeg_audio['version'] == '1') && ($thisfile_mpeg_audio['layer'] == 1)) { + $bytes_per_frame = 384; + } elseif ((($thisfile_mpeg_audio['version'] == '2') || ($thisfile_mpeg_audio['version'] == '2.5')) && ($thisfile_mpeg_audio['layer'] == 3)) { + $bytes_per_frame = 576; + } + $thisfile_mpeg_audio['VBR_bitrate'] = (isset($thisfile_mpeg_audio['VBR_bytes']) ? (($thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($info['audio']['sample_rate'] / $bytes_per_frame) : 0); + if ($thisfile_mpeg_audio['VBR_bitrate'] > 0) { + $info['audio']['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate']; + $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate']; // to avoid confusion + } + break; + } + } + + // End variable-bitrate headers + //////////////////////////////////////////////////////////////////////////////////// + + if ($recursivesearch) { + + if (!$this->RecursiveFrameScanning($offset, $nextframetestoffset, $ScanAsCBR)) { + return false; + } + + } + + + //if (false) { + // // experimental side info parsing section - not returning anything useful yet + // + // $SideInfoBitstream = getid3_lib::BigEndian2Bin($SideInfoData); + // $SideInfoOffset = 0; + // + // if ($thisfile_mpeg_audio['version'] == '1') { + // if ($thisfile_mpeg_audio['channelmode'] == 'mono') { + // // MPEG-1 (mono) + // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 9); + // $SideInfoOffset += 9; + // $SideInfoOffset += 5; + // } else { + // // MPEG-1 (stereo, joint-stereo, dual-channel) + // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 9); + // $SideInfoOffset += 9; + // $SideInfoOffset += 3; + // } + // } else { // 2 or 2.5 + // if ($thisfile_mpeg_audio['channelmode'] == 'mono') { + // // MPEG-2, MPEG-2.5 (mono) + // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 8); + // $SideInfoOffset += 8; + // $SideInfoOffset += 1; + // } else { + // // MPEG-2, MPEG-2.5 (stereo, joint-stereo, dual-channel) + // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 8); + // $SideInfoOffset += 8; + // $SideInfoOffset += 2; + // } + // } + // + // if ($thisfile_mpeg_audio['version'] == '1') { + // for ($channel = 0; $channel < $info['audio']['channels']; $channel++) { + // for ($scfsi_band = 0; $scfsi_band < 4; $scfsi_band++) { + // $thisfile_mpeg_audio['scfsi'][$channel][$scfsi_band] = substr($SideInfoBitstream, $SideInfoOffset, 1); + // $SideInfoOffset += 2; + // } + // } + // } + // for ($granule = 0; $granule < (($thisfile_mpeg_audio['version'] == '1') ? 2 : 1); $granule++) { + // for ($channel = 0; $channel < $info['audio']['channels']; $channel++) { + // $thisfile_mpeg_audio['part2_3_length'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 12); + // $SideInfoOffset += 12; + // $thisfile_mpeg_audio['big_values'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 9); + // $SideInfoOffset += 9; + // $thisfile_mpeg_audio['global_gain'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 8); + // $SideInfoOffset += 8; + // if ($thisfile_mpeg_audio['version'] == '1') { + // $thisfile_mpeg_audio['scalefac_compress'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 4); + // $SideInfoOffset += 4; + // } else { + // $thisfile_mpeg_audio['scalefac_compress'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 9); + // $SideInfoOffset += 9; + // } + // $thisfile_mpeg_audio['window_switching_flag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1); + // $SideInfoOffset += 1; + // + // if ($thisfile_mpeg_audio['window_switching_flag'][$granule][$channel] == '1') { + // + // $thisfile_mpeg_audio['block_type'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 2); + // $SideInfoOffset += 2; + // $thisfile_mpeg_audio['mixed_block_flag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1); + // $SideInfoOffset += 1; + // + // for ($region = 0; $region < 2; $region++) { + // $thisfile_mpeg_audio['table_select'][$granule][$channel][$region] = substr($SideInfoBitstream, $SideInfoOffset, 5); + // $SideInfoOffset += 5; + // } + // $thisfile_mpeg_audio['table_select'][$granule][$channel][2] = 0; + // + // for ($window = 0; $window < 3; $window++) { + // $thisfile_mpeg_audio['subblock_gain'][$granule][$channel][$window] = substr($SideInfoBitstream, $SideInfoOffset, 3); + // $SideInfoOffset += 3; + // } + // + // } else { + // + // for ($region = 0; $region < 3; $region++) { + // $thisfile_mpeg_audio['table_select'][$granule][$channel][$region] = substr($SideInfoBitstream, $SideInfoOffset, 5); + // $SideInfoOffset += 5; + // } + // + // $thisfile_mpeg_audio['region0_count'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 4); + // $SideInfoOffset += 4; + // $thisfile_mpeg_audio['region1_count'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 3); + // $SideInfoOffset += 3; + // $thisfile_mpeg_audio['block_type'][$granule][$channel] = 0; + // } + // + // if ($thisfile_mpeg_audio['version'] == '1') { + // $thisfile_mpeg_audio['preflag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1); + // $SideInfoOffset += 1; + // } + // $thisfile_mpeg_audio['scalefac_scale'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1); + // $SideInfoOffset += 1; + // $thisfile_mpeg_audio['count1table_select'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1); + // $SideInfoOffset += 1; + // } + // } + //} + + return true; + } + + /** + * @param int $offset + * @param int $nextframetestoffset + * @param bool $ScanAsCBR + * + * @return bool + */ + public function RecursiveFrameScanning(&$offset, &$nextframetestoffset, $ScanAsCBR) { + $info = &$this->getid3->info; + $firstframetestarray = array('error' => array(), 'warning'=> array(), 'avdataend' => $info['avdataend'], 'avdataoffset' => $info['avdataoffset']); + $this->decodeMPEGaudioHeader($offset, $firstframetestarray, false); + + for ($i = 0; $i < GETID3_MP3_VALID_CHECK_FRAMES; $i++) { + // check next GETID3_MP3_VALID_CHECK_FRAMES frames for validity, to make sure we haven't run across a false synch + if (($nextframetestoffset + 4) >= $info['avdataend']) { + // end of file + return true; + } + + $nextframetestarray = array('error' => array(), 'warning' => array(), 'avdataend' => $info['avdataend'], 'avdataoffset'=>$info['avdataoffset']); + if ($this->decodeMPEGaudioHeader($nextframetestoffset, $nextframetestarray, false)) { + if ($ScanAsCBR) { + // force CBR mode, used for trying to pick out invalid audio streams with valid(?) VBR headers, or VBR streams with no VBR header + if (!isset($nextframetestarray['mpeg']['audio']['bitrate']) || !isset($firstframetestarray['mpeg']['audio']['bitrate']) || ($nextframetestarray['mpeg']['audio']['bitrate'] != $firstframetestarray['mpeg']['audio']['bitrate'])) { + return false; + } + } + + + // next frame is OK, get ready to check the one after that + if (isset($nextframetestarray['mpeg']['audio']['framelength']) && ($nextframetestarray['mpeg']['audio']['framelength'] > 0)) { + $nextframetestoffset += $nextframetestarray['mpeg']['audio']['framelength']; + } else { + $this->error('Frame at offset ('.$offset.') is has an invalid frame length.'); + return false; + } + + } elseif (!empty($firstframetestarray['mpeg']['audio']['framelength']) && (($nextframetestoffset + $firstframetestarray['mpeg']['audio']['framelength']) > $info['avdataend'])) { + + // it's not the end of the file, but there's not enough data left for another frame, so assume it's garbage/padding and return OK + return true; + + } else { + + // next frame is not valid, note the error and fail, so scanning can contiue for a valid frame sequence + $this->warning('Frame at offset ('.$offset.') is valid, but the next one at ('.$nextframetestoffset.') is not.'); + + return false; + } + } + return true; + } + + /** + * @param int $offset + * @param bool $deepscan + * + * @return int|false + */ + public function FreeFormatFrameLength($offset, $deepscan=false) { + $info = &$this->getid3->info; + + $this->fseek($offset); + $MPEGaudioData = $this->fread(32768); + + $SyncPattern1 = substr($MPEGaudioData, 0, 4); + // may be different pattern due to padding + $SyncPattern2 = $SyncPattern1[0].$SyncPattern1[1].chr(ord($SyncPattern1[2]) | 0x02).$SyncPattern1[3]; + if ($SyncPattern2 === $SyncPattern1) { + $SyncPattern2 = $SyncPattern1[0].$SyncPattern1[1].chr(ord($SyncPattern1[2]) & 0xFD).$SyncPattern1[3]; + } + + $framelength = false; + $framelength1 = strpos($MPEGaudioData, $SyncPattern1, 4); + $framelength2 = strpos($MPEGaudioData, $SyncPattern2, 4); + if ($framelength1 > 4) { + $framelength = $framelength1; + } + if (($framelength2 > 4) && ($framelength2 < $framelength1)) { + $framelength = $framelength2; + } + if (!$framelength) { + + // LAME 3.88 has a different value for modeextension on the first frame vs the rest + $framelength1 = strpos($MPEGaudioData, substr($SyncPattern1, 0, 3), 4); + $framelength2 = strpos($MPEGaudioData, substr($SyncPattern2, 0, 3), 4); + + if ($framelength1 > 4) { + $framelength = $framelength1; + } + if (($framelength2 > 4) && ($framelength2 < $framelength1)) { + $framelength = $framelength2; + } + if (!$framelength) { + $this->error('Cannot find next free-format synch pattern ('.getid3_lib::PrintHexBytes($SyncPattern1).' or '.getid3_lib::PrintHexBytes($SyncPattern2).') after offset '.$offset); + return false; + } else { + $this->warning('ModeExtension varies between first frame and other frames (known free-format issue in LAME 3.88)'); + $info['audio']['codec'] = 'LAME'; + $info['audio']['encoder'] = 'LAME3.88'; + $SyncPattern1 = substr($SyncPattern1, 0, 3); + $SyncPattern2 = substr($SyncPattern2, 0, 3); + } + } + + if ($deepscan) { + + $ActualFrameLengthValues = array(); + $nextoffset = $offset + $framelength; + while ($nextoffset < ($info['avdataend'] - 6)) { + $this->fseek($nextoffset - 1); + $NextSyncPattern = $this->fread(6); + if ((substr($NextSyncPattern, 1, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 1, strlen($SyncPattern2)) == $SyncPattern2)) { + // good - found where expected + $ActualFrameLengthValues[] = $framelength; + } elseif ((substr($NextSyncPattern, 0, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 0, strlen($SyncPattern2)) == $SyncPattern2)) { + // ok - found one byte earlier than expected (last frame wasn't padded, first frame was) + $ActualFrameLengthValues[] = ($framelength - 1); + $nextoffset--; + } elseif ((substr($NextSyncPattern, 2, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 2, strlen($SyncPattern2)) == $SyncPattern2)) { + // ok - found one byte later than expected (last frame was padded, first frame wasn't) + $ActualFrameLengthValues[] = ($framelength + 1); + $nextoffset++; + } else { + $this->error('Did not find expected free-format sync pattern at offset '.$nextoffset); + return false; + } + $nextoffset += $framelength; + } + if (count($ActualFrameLengthValues) > 0) { + $framelength = intval(round(array_sum($ActualFrameLengthValues) / count($ActualFrameLengthValues))); + } + } + return $framelength; + } + + /** + * @return bool + */ + public function getOnlyMPEGaudioInfoBruteForce() { + $MPEGaudioHeaderDecodeCache = array(); + $MPEGaudioHeaderValidCache = array(); + $MPEGaudioHeaderLengthCache = array(); + $MPEGaudioVersionLookup = self::MPEGaudioVersionArray(); + $MPEGaudioLayerLookup = self::MPEGaudioLayerArray(); + $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray(); + $MPEGaudioFrequencyLookup = self::MPEGaudioFrequencyArray(); + $MPEGaudioChannelModeLookup = self::MPEGaudioChannelModeArray(); + $MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray(); + $MPEGaudioEmphasisLookup = self::MPEGaudioEmphasisArray(); + $LongMPEGversionLookup = array(); + $LongMPEGlayerLookup = array(); + $LongMPEGbitrateLookup = array(); + $LongMPEGpaddingLookup = array(); + $LongMPEGfrequencyLookup = array(); + $Distribution['bitrate'] = array(); + $Distribution['frequency'] = array(); + $Distribution['layer'] = array(); + $Distribution['version'] = array(); + $Distribution['padding'] = array(); + + $info = &$this->getid3->info; + $this->fseek($info['avdataoffset']); + + $max_frames_scan = 5000; + $frames_scanned = 0; + + $previousvalidframe = $info['avdataoffset']; + while ($this->ftell() < $info['avdataend']) { + set_time_limit(30); + $head4 = $this->fread(4); + if (strlen($head4) < 4) { + break; + } + if ($head4[0] != "\xFF") { + for ($i = 1; $i < 4; $i++) { + if ($head4[$i] == "\xFF") { + $this->fseek($i - 4, SEEK_CUR); + continue 2; + } + } + continue; + } + if (!isset($MPEGaudioHeaderDecodeCache[$head4])) { + $MPEGaudioHeaderDecodeCache[$head4] = self::MPEGaudioHeaderDecode($head4); + } + if (!isset($MPEGaudioHeaderValidCache[$head4])) { + $MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$head4], false, false); + } + if ($MPEGaudioHeaderValidCache[$head4]) { + + if (!isset($MPEGaudioHeaderLengthCache[$head4])) { + $LongMPEGversionLookup[$head4] = $MPEGaudioVersionLookup[$MPEGaudioHeaderDecodeCache[$head4]['version']]; + $LongMPEGlayerLookup[$head4] = $MPEGaudioLayerLookup[$MPEGaudioHeaderDecodeCache[$head4]['layer']]; + $LongMPEGbitrateLookup[$head4] = $MPEGaudioBitrateLookup[$LongMPEGversionLookup[$head4]][$LongMPEGlayerLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['bitrate']]; + $LongMPEGpaddingLookup[$head4] = (bool) $MPEGaudioHeaderDecodeCache[$head4]['padding']; + $LongMPEGfrequencyLookup[$head4] = $MPEGaudioFrequencyLookup[$LongMPEGversionLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['sample_rate']]; + $MPEGaudioHeaderLengthCache[$head4] = self::MPEGaudioFrameLength( + $LongMPEGbitrateLookup[$head4], + $LongMPEGversionLookup[$head4], + $LongMPEGlayerLookup[$head4], + $LongMPEGpaddingLookup[$head4], + $LongMPEGfrequencyLookup[$head4]); + } + if ($MPEGaudioHeaderLengthCache[$head4] > 4) { + $WhereWeWere = $this->ftell(); + $this->fseek($MPEGaudioHeaderLengthCache[$head4] - 4, SEEK_CUR); + $next4 = $this->fread(4); + if ($next4[0] == "\xFF") { + if (!isset($MPEGaudioHeaderDecodeCache[$next4])) { + $MPEGaudioHeaderDecodeCache[$next4] = self::MPEGaudioHeaderDecode($next4); + } + if (!isset($MPEGaudioHeaderValidCache[$next4])) { + $MPEGaudioHeaderValidCache[$next4] = self::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$next4], false, false); + } + if ($MPEGaudioHeaderValidCache[$next4]) { + $this->fseek(-4, SEEK_CUR); + + $Distribution['bitrate'][$LongMPEGbitrateLookup[$head4]] = isset($Distribution['bitrate'][$LongMPEGbitrateLookup[$head4]]) ? ++$Distribution['bitrate'][$LongMPEGbitrateLookup[$head4]] : 1; + $Distribution['layer'][$LongMPEGlayerLookup[$head4]] = isset($Distribution['layer'][$LongMPEGlayerLookup[$head4]]) ? ++$Distribution['layer'][$LongMPEGlayerLookup[$head4]] : 1; + $Distribution['version'][$LongMPEGversionLookup[$head4]] = isset($Distribution['version'][$LongMPEGversionLookup[$head4]]) ? ++$Distribution['version'][$LongMPEGversionLookup[$head4]] : 1; + $Distribution['padding'][intval($LongMPEGpaddingLookup[$head4])] = isset($Distribution['padding'][intval($LongMPEGpaddingLookup[$head4])]) ? ++$Distribution['padding'][intval($LongMPEGpaddingLookup[$head4])] : 1; + $Distribution['frequency'][$LongMPEGfrequencyLookup[$head4]] = isset($Distribution['frequency'][$LongMPEGfrequencyLookup[$head4]]) ? ++$Distribution['frequency'][$LongMPEGfrequencyLookup[$head4]] : 1; + if (++$frames_scanned >= $max_frames_scan) { + $pct_data_scanned = ($this->ftell() - $info['avdataoffset']) / ($info['avdataend'] - $info['avdataoffset']); + $this->warning('too many MPEG audio frames to scan, only scanned first '.$max_frames_scan.' frames ('.number_format($pct_data_scanned * 100, 1).'% of file) and extrapolated distribution, playtime and bitrate may be incorrect.'); + foreach ($Distribution as $key1 => $value1) { + foreach ($value1 as $key2 => $value2) { + $Distribution[$key1][$key2] = round($value2 / $pct_data_scanned); + } + } + break; + } + continue; + } + } + unset($next4); + $this->fseek($WhereWeWere - 3); + } + + } + } + foreach ($Distribution as $key => $value) { + ksort($Distribution[$key], SORT_NUMERIC); + } + ksort($Distribution['version'], SORT_STRING); + $info['mpeg']['audio']['bitrate_distribution'] = $Distribution['bitrate']; + $info['mpeg']['audio']['frequency_distribution'] = $Distribution['frequency']; + $info['mpeg']['audio']['layer_distribution'] = $Distribution['layer']; + $info['mpeg']['audio']['version_distribution'] = $Distribution['version']; + $info['mpeg']['audio']['padding_distribution'] = $Distribution['padding']; + if (count($Distribution['version']) > 1) { + $this->error('Corrupt file - more than one MPEG version detected'); + } + if (count($Distribution['layer']) > 1) { + $this->error('Corrupt file - more than one MPEG layer detected'); + } + if (count($Distribution['frequency']) > 1) { + $this->error('Corrupt file - more than one MPEG sample rate detected'); + } + + + $bittotal = 0; + foreach ($Distribution['bitrate'] as $bitratevalue => $bitratecount) { + if ($bitratevalue != 'free') { + $bittotal += ($bitratevalue * $bitratecount); + } + } + $info['mpeg']['audio']['frame_count'] = array_sum($Distribution['bitrate']); + if ($info['mpeg']['audio']['frame_count'] == 0) { + $this->error('no MPEG audio frames found'); + return false; + } + $info['mpeg']['audio']['bitrate'] = ($bittotal / $info['mpeg']['audio']['frame_count']); + $info['mpeg']['audio']['bitrate_mode'] = ((count($Distribution['bitrate']) > 0) ? 'vbr' : 'cbr'); + $info['mpeg']['audio']['sample_rate'] = getid3_lib::array_max($Distribution['frequency'], true); + + $info['audio']['bitrate'] = $info['mpeg']['audio']['bitrate']; + $info['audio']['bitrate_mode'] = $info['mpeg']['audio']['bitrate_mode']; + $info['audio']['sample_rate'] = $info['mpeg']['audio']['sample_rate']; + $info['audio']['dataformat'] = 'mp'.getid3_lib::array_max($Distribution['layer'], true); + $info['fileformat'] = $info['audio']['dataformat']; + + return true; + } + + /** + * @param int $avdataoffset + * @param bool $BitrateHistogram + * + * @return bool + */ + public function getOnlyMPEGaudioInfo($avdataoffset, $BitrateHistogram=false) { + // looks for synch, decodes MPEG audio header + + $info = &$this->getid3->info; + + static $MPEGaudioVersionLookup; + static $MPEGaudioLayerLookup; + static $MPEGaudioBitrateLookup; + if (empty($MPEGaudioVersionLookup)) { + $MPEGaudioVersionLookup = self::MPEGaudioVersionArray(); + $MPEGaudioLayerLookup = self::MPEGaudioLayerArray(); + $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray(); + } + + $this->fseek($avdataoffset); + $sync_seek_buffer_size = min(128 * 1024, $info['avdataend'] - $avdataoffset); + if ($sync_seek_buffer_size <= 0) { + $this->error('Invalid $sync_seek_buffer_size at offset '.$avdataoffset); + return false; + } + $header = $this->fread($sync_seek_buffer_size); + $sync_seek_buffer_size = strlen($header); + $SynchSeekOffset = 0; + while ($SynchSeekOffset < $sync_seek_buffer_size) { + if ((($avdataoffset + $SynchSeekOffset) < $info['avdataend']) && !feof($this->getid3->fp)) { + + if ($SynchSeekOffset > $sync_seek_buffer_size) { + // if a synch's not found within the first 128k bytes, then give up + $this->error('Could not find valid MPEG audio synch within the first '.round($sync_seek_buffer_size / 1024).'kB'); + if (isset($info['audio']['bitrate'])) { + unset($info['audio']['bitrate']); + } + if (isset($info['mpeg']['audio'])) { + unset($info['mpeg']['audio']); + } + if (empty($info['mpeg'])) { + unset($info['mpeg']); + } + return false; + + } elseif (feof($this->getid3->fp)) { + + $this->error('Could not find valid MPEG audio synch before end of file'); + if (isset($info['audio']['bitrate'])) { + unset($info['audio']['bitrate']); + } + if (isset($info['mpeg']['audio'])) { + unset($info['mpeg']['audio']); + } + if (isset($info['mpeg']) && (!is_array($info['mpeg']) || (count($info['mpeg']) == 0))) { + unset($info['mpeg']); + } + return false; + } + } + + if (($SynchSeekOffset + 1) >= strlen($header)) { + $this->error('Could not find valid MPEG synch before end of file'); + return false; + } + + if (($header[$SynchSeekOffset] == "\xFF") && ($header[($SynchSeekOffset + 1)] > "\xE0")) { // synch detected + $FirstFrameAVDataOffset = null; + if (!isset($FirstFrameThisfileInfo) && !isset($info['mpeg']['audio'])) { + $FirstFrameThisfileInfo = $info; + $FirstFrameAVDataOffset = $avdataoffset + $SynchSeekOffset; + if (!$this->decodeMPEGaudioHeader($FirstFrameAVDataOffset, $FirstFrameThisfileInfo, false)) { + // if this is the first valid MPEG-audio frame, save it in case it's a VBR header frame and there's + // garbage between this frame and a valid sequence of MPEG-audio frames, to be restored below + unset($FirstFrameThisfileInfo); + } + } + + $dummy = $info; // only overwrite real data if valid header found + if ($this->decodeMPEGaudioHeader($avdataoffset + $SynchSeekOffset, $dummy, true)) { + $info = $dummy; + $info['avdataoffset'] = $avdataoffset + $SynchSeekOffset; + switch (isset($info['fileformat']) ? $info['fileformat'] : '') { + case '': + case 'id3': + case 'ape': + case 'mp3': + $info['fileformat'] = 'mp3'; + $info['audio']['dataformat'] = 'mp3'; + break; + } + if (isset($FirstFrameThisfileInfo) && isset($FirstFrameThisfileInfo['mpeg']['audio']['bitrate_mode']) && ($FirstFrameThisfileInfo['mpeg']['audio']['bitrate_mode'] == 'vbr')) { + if (!(abs($info['audio']['bitrate'] - $FirstFrameThisfileInfo['audio']['bitrate']) <= 1)) { + // If there is garbage data between a valid VBR header frame and a sequence + // of valid MPEG-audio frames the VBR data is no longer discarded. + $info = $FirstFrameThisfileInfo; + $info['avdataoffset'] = $FirstFrameAVDataOffset; + $info['fileformat'] = 'mp3'; + $info['audio']['dataformat'] = 'mp3'; + $dummy = $info; + unset($dummy['mpeg']['audio']); + $GarbageOffsetStart = $FirstFrameAVDataOffset + $FirstFrameThisfileInfo['mpeg']['audio']['framelength']; + $GarbageOffsetEnd = $avdataoffset + $SynchSeekOffset; + if ($this->decodeMPEGaudioHeader($GarbageOffsetEnd, $dummy, true, true)) { + $info = $dummy; + $info['avdataoffset'] = $GarbageOffsetEnd; + $this->warning('apparently-valid VBR header not used because could not find '.GETID3_MP3_VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.'), but did find valid CBR stream starting at '.$GarbageOffsetEnd); + } else { + $this->warning('using data from VBR header even though could not find '.GETID3_MP3_VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.')'); + } + } + } + if (isset($info['mpeg']['audio']['bitrate_mode']) && ($info['mpeg']['audio']['bitrate_mode'] == 'vbr') && !isset($info['mpeg']['audio']['VBR_method'])) { + // VBR file with no VBR header + $BitrateHistogram = true; + } + + if ($BitrateHistogram) { + + $info['mpeg']['audio']['stereo_distribution'] = array('stereo'=>0, 'joint stereo'=>0, 'dual channel'=>0, 'mono'=>0); + $info['mpeg']['audio']['version_distribution'] = array('1'=>0, '2'=>0, '2.5'=>0); + + if ($info['mpeg']['audio']['version'] == '1') { + if ($info['mpeg']['audio']['layer'] == 3) { + $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 40000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 320000=>0); + } elseif ($info['mpeg']['audio']['layer'] == 2) { + $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 320000=>0, 384000=>0); + } elseif ($info['mpeg']['audio']['layer'] == 1) { + $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 64000=>0, 96000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 288000=>0, 320000=>0, 352000=>0, 384000=>0, 416000=>0, 448000=>0); + } + } elseif ($info['mpeg']['audio']['layer'] == 1) { + $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 144000=>0, 160000=>0, 176000=>0, 192000=>0, 224000=>0, 256000=>0); + } else { + $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 8000=>0, 16000=>0, 24000=>0, 32000=>0, 40000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 144000=>0, 160000=>0); + } + + $dummy = array('error'=>$info['error'], 'warning'=>$info['warning'], 'avdataend'=>$info['avdataend'], 'avdataoffset'=>$info['avdataoffset']); + $synchstartoffset = $info['avdataoffset']; + $this->fseek($info['avdataoffset']); + + // you can play with these numbers: + $max_frames_scan = 50000; + $max_scan_segments = 10; + + // don't play with these numbers: + $FastMode = false; + $SynchErrorsFound = 0; + $frames_scanned = 0; + $this_scan_segment = 0; + $frames_scan_per_segment = ceil($max_frames_scan / $max_scan_segments); + $pct_data_scanned = 0; + for ($current_segment = 0; $current_segment < $max_scan_segments; $current_segment++) { + $frames_scanned_this_segment = 0; + if ($this->ftell() >= $info['avdataend']) { + break; + } + $scan_start_offset[$current_segment] = max($this->ftell(), $info['avdataoffset'] + round($current_segment * (($info['avdataend'] - $info['avdataoffset']) / $max_scan_segments))); + if ($current_segment > 0) { + $this->fseek($scan_start_offset[$current_segment]); + $buffer_4k = $this->fread(4096); + for ($j = 0; $j < (strlen($buffer_4k) - 4); $j++) { + if (($buffer_4k[$j] == "\xFF") && ($buffer_4k[($j + 1)] > "\xE0")) { // synch detected + if ($this->decodeMPEGaudioHeader($scan_start_offset[$current_segment] + $j, $dummy, false, false, $FastMode)) { + $calculated_next_offset = $scan_start_offset[$current_segment] + $j + $dummy['mpeg']['audio']['framelength']; + if ($this->decodeMPEGaudioHeader($calculated_next_offset, $dummy, false, false, $FastMode)) { + $scan_start_offset[$current_segment] += $j; + break; + } + } + } + } + } + $synchstartoffset = $scan_start_offset[$current_segment]; + while (($synchstartoffset < $info['avdataend']) && $this->decodeMPEGaudioHeader($synchstartoffset, $dummy, false, false, $FastMode)) { + $FastMode = true; + $thisframebitrate = $MPEGaudioBitrateLookup[$MPEGaudioVersionLookup[$dummy['mpeg']['audio']['raw']['version']]][$MPEGaudioLayerLookup[$dummy['mpeg']['audio']['raw']['layer']]][$dummy['mpeg']['audio']['raw']['bitrate']]; + + if (empty($dummy['mpeg']['audio']['framelength'])) { + $SynchErrorsFound++; + $synchstartoffset++; + } else { + getid3_lib::safe_inc($info['mpeg']['audio']['bitrate_distribution'][$thisframebitrate]); + getid3_lib::safe_inc($info['mpeg']['audio']['stereo_distribution'][$dummy['mpeg']['audio']['channelmode']]); + getid3_lib::safe_inc($info['mpeg']['audio']['version_distribution'][$dummy['mpeg']['audio']['version']]); + $synchstartoffset += $dummy['mpeg']['audio']['framelength']; + } + $frames_scanned++; + if ($frames_scan_per_segment && (++$frames_scanned_this_segment >= $frames_scan_per_segment)) { + $this_pct_scanned = ($this->ftell() - $scan_start_offset[$current_segment]) / ($info['avdataend'] - $info['avdataoffset']); + if (($current_segment == 0) && (($this_pct_scanned * $max_scan_segments) >= 1)) { + // file likely contains < $max_frames_scan, just scan as one segment + $max_scan_segments = 1; + $frames_scan_per_segment = $max_frames_scan; + } else { + $pct_data_scanned += $this_pct_scanned; + break; + } + } + } + } + if ($pct_data_scanned > 0) { + $this->warning('too many MPEG audio frames to scan, only scanned '.$frames_scanned.' frames in '.$max_scan_segments.' segments ('.number_format($pct_data_scanned * 100, 1).'% of file) and extrapolated distribution, playtime and bitrate may be incorrect.'); + foreach ($info['mpeg']['audio'] as $key1 => $value1) { + if (!preg_match('#_distribution$#i', $key1)) { + continue; + } + foreach ($value1 as $key2 => $value2) { + $info['mpeg']['audio'][$key1][$key2] = round($value2 / $pct_data_scanned); + } + } + } + + if ($SynchErrorsFound > 0) { + $this->warning('Found '.$SynchErrorsFound.' synch errors in histogram analysis'); + //return false; + } + + $bittotal = 0; + $framecounter = 0; + foreach ($info['mpeg']['audio']['bitrate_distribution'] as $bitratevalue => $bitratecount) { + $framecounter += $bitratecount; + if ($bitratevalue != 'free') { + $bittotal += ($bitratevalue * $bitratecount); + } + } + if ($framecounter == 0) { + $this->error('Corrupt MP3 file: framecounter == zero'); + return false; + } + $info['mpeg']['audio']['frame_count'] = getid3_lib::CastAsInt($framecounter); + $info['mpeg']['audio']['bitrate'] = ($bittotal / $framecounter); + + $info['audio']['bitrate'] = $info['mpeg']['audio']['bitrate']; + + + // Definitively set VBR vs CBR, even if the Xing/LAME/VBRI header says differently + $distinct_bitrates = 0; + foreach ($info['mpeg']['audio']['bitrate_distribution'] as $bitrate_value => $bitrate_count) { + if ($bitrate_count > 0) { + $distinct_bitrates++; + } + } + if ($distinct_bitrates > 1) { + $info['mpeg']['audio']['bitrate_mode'] = 'vbr'; + } else { + $info['mpeg']['audio']['bitrate_mode'] = 'cbr'; + } + $info['audio']['bitrate_mode'] = $info['mpeg']['audio']['bitrate_mode']; + + } + + break; // exit while() + } + } + + $SynchSeekOffset++; + if (($avdataoffset + $SynchSeekOffset) >= $info['avdataend']) { + // end of file/data + + if (empty($info['mpeg']['audio'])) { + + $this->error('could not find valid MPEG synch before end of file'); + if (isset($info['audio']['bitrate'])) { + unset($info['audio']['bitrate']); + } + if (isset($info['mpeg']['audio'])) { + unset($info['mpeg']['audio']); + } + if (isset($info['mpeg']) && (!is_array($info['mpeg']) || empty($info['mpeg']))) { + unset($info['mpeg']); + } + return false; + + } + break; + } + + } + $info['audio']['channels'] = $info['mpeg']['audio']['channels']; + $info['audio']['channelmode'] = $info['mpeg']['audio']['channelmode']; + $info['audio']['sample_rate'] = $info['mpeg']['audio']['sample_rate']; + return true; + } + + /** + * @return array + */ + public static function MPEGaudioVersionArray() { + static $MPEGaudioVersion = array('2.5', false, '2', '1'); + return $MPEGaudioVersion; + } + + /** + * @return array + */ + public static function MPEGaudioLayerArray() { + static $MPEGaudioLayer = array(false, 3, 2, 1); + return $MPEGaudioLayer; + } + + /** + * @return array + */ + public static function MPEGaudioBitrateArray() { + static $MPEGaudioBitrate; + if (empty($MPEGaudioBitrate)) { + $MPEGaudioBitrate = array ( + '1' => array (1 => array('free', 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000), + 2 => array('free', 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000), + 3 => array('free', 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000) + ), + + '2' => array (1 => array('free', 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000), + 2 => array('free', 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000), + ) + ); + $MPEGaudioBitrate['2'][3] = $MPEGaudioBitrate['2'][2]; + $MPEGaudioBitrate['2.5'] = $MPEGaudioBitrate['2']; + } + return $MPEGaudioBitrate; + } + + /** + * @return array + */ + public static function MPEGaudioFrequencyArray() { + static $MPEGaudioFrequency; + if (empty($MPEGaudioFrequency)) { + $MPEGaudioFrequency = array ( + '1' => array(44100, 48000, 32000), + '2' => array(22050, 24000, 16000), + '2.5' => array(11025, 12000, 8000) + ); + } + return $MPEGaudioFrequency; + } + + /** + * @return array + */ + public static function MPEGaudioChannelModeArray() { + static $MPEGaudioChannelMode = array('stereo', 'joint stereo', 'dual channel', 'mono'); + return $MPEGaudioChannelMode; + } + + /** + * @return array + */ + public static function MPEGaudioModeExtensionArray() { + static $MPEGaudioModeExtension; + if (empty($MPEGaudioModeExtension)) { + $MPEGaudioModeExtension = array ( + 1 => array('4-31', '8-31', '12-31', '16-31'), + 2 => array('4-31', '8-31', '12-31', '16-31'), + 3 => array('', 'IS', 'MS', 'IS+MS') + ); + } + return $MPEGaudioModeExtension; + } + + /** + * @return array + */ + public static function MPEGaudioEmphasisArray() { + static $MPEGaudioEmphasis = array('none', '50/15ms', false, 'CCIT J.17'); + return $MPEGaudioEmphasis; + } + + /** + * @param string $head4 + * @param bool $allowBitrate15 + * + * @return bool + */ + public static function MPEGaudioHeaderBytesValid($head4, $allowBitrate15=false) { + return self::MPEGaudioHeaderValid(self::MPEGaudioHeaderDecode($head4), false, $allowBitrate15); + } + + /** + * @param array $rawarray + * @param bool $echoerrors + * @param bool $allowBitrate15 + * + * @return bool + */ + public static function MPEGaudioHeaderValid($rawarray, $echoerrors=false, $allowBitrate15=false) { + if (!isset($rawarray['synch']) || ($rawarray['synch'] & 0x0FFE) != 0x0FFE) { + return false; + } + + static $MPEGaudioVersionLookup; + static $MPEGaudioLayerLookup; + static $MPEGaudioBitrateLookup; + static $MPEGaudioFrequencyLookup; + static $MPEGaudioChannelModeLookup; + static $MPEGaudioModeExtensionLookup; + static $MPEGaudioEmphasisLookup; + if (empty($MPEGaudioVersionLookup)) { + $MPEGaudioVersionLookup = self::MPEGaudioVersionArray(); + $MPEGaudioLayerLookup = self::MPEGaudioLayerArray(); + $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray(); + $MPEGaudioFrequencyLookup = self::MPEGaudioFrequencyArray(); + $MPEGaudioChannelModeLookup = self::MPEGaudioChannelModeArray(); + $MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray(); + $MPEGaudioEmphasisLookup = self::MPEGaudioEmphasisArray(); + } + + if (isset($MPEGaudioVersionLookup[$rawarray['version']])) { + $decodedVersion = $MPEGaudioVersionLookup[$rawarray['version']]; + } else { + echo ($echoerrors ? "\n".'invalid Version ('.$rawarray['version'].')' : ''); + return false; + } + if (isset($MPEGaudioLayerLookup[$rawarray['layer']])) { + $decodedLayer = $MPEGaudioLayerLookup[$rawarray['layer']]; + } else { + echo ($echoerrors ? "\n".'invalid Layer ('.$rawarray['layer'].')' : ''); + return false; + } + if (!isset($MPEGaudioBitrateLookup[$decodedVersion][$decodedLayer][$rawarray['bitrate']])) { + echo ($echoerrors ? "\n".'invalid Bitrate ('.$rawarray['bitrate'].')' : ''); + if ($rawarray['bitrate'] == 15) { + // known issue in LAME 3.90 - 3.93.1 where free-format has bitrate ID of 15 instead of 0 + // let it go through here otherwise file will not be identified + if (!$allowBitrate15) { + return false; + } + } else { + return false; + } + } + if (!isset($MPEGaudioFrequencyLookup[$decodedVersion][$rawarray['sample_rate']])) { + echo ($echoerrors ? "\n".'invalid Frequency ('.$rawarray['sample_rate'].')' : ''); + return false; + } + if (!isset($MPEGaudioChannelModeLookup[$rawarray['channelmode']])) { + echo ($echoerrors ? "\n".'invalid ChannelMode ('.$rawarray['channelmode'].')' : ''); + return false; + } + if (!isset($MPEGaudioModeExtensionLookup[$decodedLayer][$rawarray['modeextension']])) { + echo ($echoerrors ? "\n".'invalid Mode Extension ('.$rawarray['modeextension'].')' : ''); + return false; + } + if (!isset($MPEGaudioEmphasisLookup[$rawarray['emphasis']])) { + echo ($echoerrors ? "\n".'invalid Emphasis ('.$rawarray['emphasis'].')' : ''); + return false; + } + // These are just either set or not set, you can't mess that up :) + // $rawarray['protection']; + // $rawarray['padding']; + // $rawarray['private']; + // $rawarray['copyright']; + // $rawarray['original']; + + return true; + } + + /** + * @param string $Header4Bytes + * + * @return array|false + */ + public static function MPEGaudioHeaderDecode($Header4Bytes) { + // AAAA AAAA AAAB BCCD EEEE FFGH IIJJ KLMM + // A - Frame sync (all bits set) + // B - MPEG Audio version ID + // C - Layer description + // D - Protection bit + // E - Bitrate index + // F - Sampling rate frequency index + // G - Padding bit + // H - Private bit + // I - Channel Mode + // J - Mode extension (Only if Joint stereo) + // K - Copyright + // L - Original + // M - Emphasis + + if (strlen($Header4Bytes) != 4) { + return false; + } + + $MPEGrawHeader['synch'] = (getid3_lib::BigEndian2Int(substr($Header4Bytes, 0, 2)) & 0xFFE0) >> 4; + $MPEGrawHeader['version'] = (ord($Header4Bytes[1]) & 0x18) >> 3; // BB + $MPEGrawHeader['layer'] = (ord($Header4Bytes[1]) & 0x06) >> 1; // CC + $MPEGrawHeader['protection'] = (ord($Header4Bytes[1]) & 0x01); // D + $MPEGrawHeader['bitrate'] = (ord($Header4Bytes[2]) & 0xF0) >> 4; // EEEE + $MPEGrawHeader['sample_rate'] = (ord($Header4Bytes[2]) & 0x0C) >> 2; // FF + $MPEGrawHeader['padding'] = (ord($Header4Bytes[2]) & 0x02) >> 1; // G + $MPEGrawHeader['private'] = (ord($Header4Bytes[2]) & 0x01); // H + $MPEGrawHeader['channelmode'] = (ord($Header4Bytes[3]) & 0xC0) >> 6; // II + $MPEGrawHeader['modeextension'] = (ord($Header4Bytes[3]) & 0x30) >> 4; // JJ + $MPEGrawHeader['copyright'] = (ord($Header4Bytes[3]) & 0x08) >> 3; // K + $MPEGrawHeader['original'] = (ord($Header4Bytes[3]) & 0x04) >> 2; // L + $MPEGrawHeader['emphasis'] = (ord($Header4Bytes[3]) & 0x03); // MM + + return $MPEGrawHeader; + } + + /** + * @param int|string $bitrate + * @param string $version + * @param string $layer + * @param bool $padding + * @param int $samplerate + * + * @return int|false + */ + public static function MPEGaudioFrameLength(&$bitrate, &$version, &$layer, $padding, &$samplerate) { + static $AudioFrameLengthCache = array(); + + if (!isset($AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate])) { + $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate] = false; + if ($bitrate != 'free') { + + if ($version == '1') { + + if ($layer == '1') { + + // For Layer I slot is 32 bits long + $FrameLengthCoefficient = 48; + $SlotLength = 4; + + } else { // Layer 2 / 3 + + // for Layer 2 and Layer 3 slot is 8 bits long. + $FrameLengthCoefficient = 144; + $SlotLength = 1; + + } + + } else { // MPEG-2 / MPEG-2.5 + + if ($layer == '1') { + + // For Layer I slot is 32 bits long + $FrameLengthCoefficient = 24; + $SlotLength = 4; + + } elseif ($layer == '2') { + + // for Layer 2 and Layer 3 slot is 8 bits long. + $FrameLengthCoefficient = 144; + $SlotLength = 1; + + } else { // layer 3 + + // for Layer 2 and Layer 3 slot is 8 bits long. + $FrameLengthCoefficient = 72; + $SlotLength = 1; + + } + + } + + // FrameLengthInBytes = ((Coefficient * BitRate) / SampleRate) + Padding + if ($samplerate > 0) { + $NewFramelength = ($FrameLengthCoefficient * $bitrate) / $samplerate; + $NewFramelength = floor($NewFramelength / $SlotLength) * $SlotLength; // round to next-lower multiple of SlotLength (1 byte for Layer 2/3, 4 bytes for Layer I) + if ($padding) { + $NewFramelength += $SlotLength; + } + $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate] = (int) $NewFramelength; + } + } + } + return $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate]; + } + + /** + * @param float|int $bit_rate + * + * @return int|float|string + */ + public static function ClosestStandardMP3Bitrate($bit_rate) { + static $standard_bit_rates = array (320000, 256000, 224000, 192000, 160000, 128000, 112000, 96000, 80000, 64000, 56000, 48000, 40000, 32000, 24000, 16000, 8000); + static $bit_rate_table = array (0=>'-'); + $round_bit_rate = intval(round($bit_rate, -3)); + if (!isset($bit_rate_table[$round_bit_rate])) { + if ($round_bit_rate > max($standard_bit_rates)) { + $bit_rate_table[$round_bit_rate] = round($bit_rate, 2 - strlen($bit_rate)); + } else { + $bit_rate_table[$round_bit_rate] = max($standard_bit_rates); + foreach ($standard_bit_rates as $standard_bit_rate) { + if ($round_bit_rate >= $standard_bit_rate + (($bit_rate_table[$round_bit_rate] - $standard_bit_rate) / 2)) { + break; + } + $bit_rate_table[$round_bit_rate] = $standard_bit_rate; + } + } + } + return $bit_rate_table[$round_bit_rate]; + } + + /** + * @param string $version + * @param string $channelmode + * + * @return int + */ + public static function XingVBRidOffset($version, $channelmode) { + static $XingVBRidOffsetCache = array(); + if (empty($XingVBRidOffsetCache)) { + $XingVBRidOffsetCache = array ( + '1' => array ('mono' => 0x15, // 4 + 17 = 21 + 'stereo' => 0x24, // 4 + 32 = 36 + 'joint stereo' => 0x24, + 'dual channel' => 0x24 + ), + + '2' => array ('mono' => 0x0D, // 4 + 9 = 13 + 'stereo' => 0x15, // 4 + 17 = 21 + 'joint stereo' => 0x15, + 'dual channel' => 0x15 + ), + + '2.5' => array ('mono' => 0x15, + 'stereo' => 0x15, + 'joint stereo' => 0x15, + 'dual channel' => 0x15 + ) + ); + } + return $XingVBRidOffsetCache[$version][$channelmode]; + } + + /** + * @param int $VBRmethodID + * + * @return string + */ + public static function LAMEvbrMethodLookup($VBRmethodID) { + static $LAMEvbrMethodLookup = array( + 0x00 => 'unknown', + 0x01 => 'cbr', + 0x02 => 'abr', + 0x03 => 'vbr-old / vbr-rh', + 0x04 => 'vbr-new / vbr-mtrh', + 0x05 => 'vbr-mt', + 0x06 => 'vbr (full vbr method 4)', + 0x08 => 'cbr (constant bitrate 2 pass)', + 0x09 => 'abr (2 pass)', + 0x0F => 'reserved' + ); + return (isset($LAMEvbrMethodLookup[$VBRmethodID]) ? $LAMEvbrMethodLookup[$VBRmethodID] : ''); + } + + /** + * @param int $StereoModeID + * + * @return string + */ + public static function LAMEmiscStereoModeLookup($StereoModeID) { + static $LAMEmiscStereoModeLookup = array( + 0 => 'mono', + 1 => 'stereo', + 2 => 'dual mono', + 3 => 'joint stereo', + 4 => 'forced stereo', + 5 => 'auto', + 6 => 'intensity stereo', + 7 => 'other' + ); + return (isset($LAMEmiscStereoModeLookup[$StereoModeID]) ? $LAMEmiscStereoModeLookup[$StereoModeID] : ''); + } + + /** + * @param int $SourceSampleFrequencyID + * + * @return string + */ + public static function LAMEmiscSourceSampleFrequencyLookup($SourceSampleFrequencyID) { + static $LAMEmiscSourceSampleFrequencyLookup = array( + 0 => '<= 32 kHz', + 1 => '44.1 kHz', + 2 => '48 kHz', + 3 => '> 48kHz' + ); + return (isset($LAMEmiscSourceSampleFrequencyLookup[$SourceSampleFrequencyID]) ? $LAMEmiscSourceSampleFrequencyLookup[$SourceSampleFrequencyID] : ''); + } + + /** + * @param int $SurroundInfoID + * + * @return string + */ + public static function LAMEsurroundInfoLookup($SurroundInfoID) { + static $LAMEsurroundInfoLookup = array( + 0 => 'no surround info', + 1 => 'DPL encoding', + 2 => 'DPL2 encoding', + 3 => 'Ambisonic encoding' + ); + return (isset($LAMEsurroundInfoLookup[$SurroundInfoID]) ? $LAMEsurroundInfoLookup[$SurroundInfoID] : 'reserved'); + } + + /** + * @param array $LAMEtag + * + * @return string + */ + public static function LAMEpresetUsedLookup($LAMEtag) { + + if ($LAMEtag['preset_used_id'] == 0) { + // no preset used (LAME >=3.93) + // no preset recorded (LAME <3.93) + return ''; + } + $LAMEpresetUsedLookup = array(); + + ///// THIS PART CANNOT BE STATIC . + for ($i = 8; $i <= 320; $i++) { + switch ($LAMEtag['vbr_method']) { + case 'cbr': + $LAMEpresetUsedLookup[$i] = '--alt-preset '.$LAMEtag['vbr_method'].' '.$i; + break; + case 'abr': + default: // other VBR modes shouldn't be here(?) + $LAMEpresetUsedLookup[$i] = '--alt-preset '.$i; + break; + } + } + + // named old-style presets (studio, phone, voice, etc) are handled in GuessEncoderOptions() + + // named alt-presets + $LAMEpresetUsedLookup[1000] = '--r3mix'; + $LAMEpresetUsedLookup[1001] = '--alt-preset standard'; + $LAMEpresetUsedLookup[1002] = '--alt-preset extreme'; + $LAMEpresetUsedLookup[1003] = '--alt-preset insane'; + $LAMEpresetUsedLookup[1004] = '--alt-preset fast standard'; + $LAMEpresetUsedLookup[1005] = '--alt-preset fast extreme'; + $LAMEpresetUsedLookup[1006] = '--alt-preset medium'; + $LAMEpresetUsedLookup[1007] = '--alt-preset fast medium'; + + // LAME 3.94 additions/changes + $LAMEpresetUsedLookup[1010] = '--preset portable'; // 3.94a15 Oct 21 2003 + $LAMEpresetUsedLookup[1015] = '--preset radio'; // 3.94a15 Oct 21 2003 + + $LAMEpresetUsedLookup[320] = '--preset insane'; // 3.94a15 Nov 12 2003 + $LAMEpresetUsedLookup[410] = '-V9'; + $LAMEpresetUsedLookup[420] = '-V8'; + $LAMEpresetUsedLookup[440] = '-V6'; + $LAMEpresetUsedLookup[430] = '--preset radio'; // 3.94a15 Nov 12 2003 + $LAMEpresetUsedLookup[450] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'portable'; // 3.94a15 Nov 12 2003 + $LAMEpresetUsedLookup[460] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'medium'; // 3.94a15 Nov 12 2003 + $LAMEpresetUsedLookup[470] = '--r3mix'; // 3.94b1 Dec 18 2003 + $LAMEpresetUsedLookup[480] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'standard'; // 3.94a15 Nov 12 2003 + $LAMEpresetUsedLookup[490] = '-V1'; + $LAMEpresetUsedLookup[500] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'extreme'; // 3.94a15 Nov 12 2003 + + return (isset($LAMEpresetUsedLookup[$LAMEtag['preset_used_id']]) ? $LAMEpresetUsedLookup[$LAMEtag['preset_used_id']] : 'new/unknown preset: '.$LAMEtag['preset_used_id'].' - report to info@getid3.org'); + } + +} diff --git a/projects/tests/tests/performance/ID3/module.audio.ogg.php b/projects/tests/tests/performance/ID3/module.audio.ogg.php new file mode 100644 index 00000000..fe092d9d --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.audio.ogg.php @@ -0,0 +1,920 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.audio.ogg.php // +// module for analyzing Ogg Vorbis, OggFLAC and Speex files // +// dependencies: module.audio.flac.php // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.flac.php', __FILE__, true); + +class getid3_ogg extends getid3_handler +{ + /** + * @link http://xiph.org/vorbis/doc/Vorbis_I_spec.html + * + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + $info['fileformat'] = 'ogg'; + + // Warn about illegal tags - only vorbiscomments are allowed + if (isset($info['id3v2'])) { + $this->warning('Illegal ID3v2 tag present.'); + } + if (isset($info['id3v1'])) { + $this->warning('Illegal ID3v1 tag present.'); + } + if (isset($info['ape'])) { + $this->warning('Illegal APE tag present.'); + } + + + // Page 1 - Stream Header + + $this->fseek($info['avdataoffset']); + + $oggpageinfo = $this->ParseOggPageHeader(); + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo; + + if ($this->ftell() >= $this->getid3->fread_buffer_size()) { + $this->error('Could not find start of Ogg page in the first '.$this->getid3->fread_buffer_size().' bytes (this might not be an Ogg-Vorbis file?)'); + unset($info['fileformat']); + unset($info['ogg']); + return false; + } + + $filedata = $this->fread($oggpageinfo['page_length']); + $filedataoffset = 0; + + if (substr($filedata, 0, 4) == 'fLaC') { + + $info['audio']['dataformat'] = 'flac'; + $info['audio']['bitrate_mode'] = 'vbr'; + $info['audio']['lossless'] = true; + + } elseif (substr($filedata, 1, 6) == 'vorbis') { + + $this->ParseVorbisPageHeader($filedata, $filedataoffset, $oggpageinfo); + + } elseif (substr($filedata, 0, 8) == 'OpusHead') { + + if ($this->ParseOpusPageHeader($filedata, $filedataoffset, $oggpageinfo) === false) { + return false; + } + + } elseif (substr($filedata, 0, 8) == 'Speex ') { + + // http://www.speex.org/manual/node10.html + + $info['audio']['dataformat'] = 'speex'; + $info['mime_type'] = 'audio/speex'; + $info['audio']['bitrate_mode'] = 'abr'; + $info['audio']['lossless'] = false; + + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_string'] = substr($filedata, $filedataoffset, 8); // hard-coded to 'Speex ' + $filedataoffset += 8; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version'] = substr($filedata, $filedataoffset, 20); + $filedataoffset += 20; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version_id'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['header_size'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['rate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode_bitstream_version'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['nb_channels'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['bitrate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['framesize'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['vbr'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['frames_per_packet'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['extra_headers'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['reserved1'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['reserved2'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + + $info['speex']['speex_version'] = trim($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version']); + $info['speex']['sample_rate'] = $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['rate']; + $info['speex']['channels'] = $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['nb_channels']; + $info['speex']['vbr'] = (bool) $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['vbr']; + $info['speex']['band_type'] = $this->SpeexBandModeLookup($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode']); + + $info['audio']['sample_rate'] = $info['speex']['sample_rate']; + $info['audio']['channels'] = $info['speex']['channels']; + if ($info['speex']['vbr']) { + $info['audio']['bitrate_mode'] = 'vbr'; + } + + } elseif (substr($filedata, 0, 7) == "\x80".'theora') { + + // http://www.theora.org/doc/Theora.pdf (section 6.2) + + $info['ogg']['pageheader']['theora']['theora_magic'] = substr($filedata, $filedataoffset, 7); // hard-coded to "\x80.'theora' + $filedataoffset += 7; + $info['ogg']['pageheader']['theora']['version_major'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['pageheader']['theora']['version_minor'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['pageheader']['theora']['version_revision'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['pageheader']['theora']['frame_width_macroblocks'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 2)); + $filedataoffset += 2; + $info['ogg']['pageheader']['theora']['frame_height_macroblocks'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 2)); + $filedataoffset += 2; + $info['ogg']['pageheader']['theora']['resolution_x'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3)); + $filedataoffset += 3; + $info['ogg']['pageheader']['theora']['resolution_y'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3)); + $filedataoffset += 3; + $info['ogg']['pageheader']['theora']['picture_offset_x'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['pageheader']['theora']['picture_offset_y'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['pageheader']['theora']['frame_rate_numerator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader']['theora']['frame_rate_denominator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['pageheader']['theora']['pixel_aspect_numerator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3)); + $filedataoffset += 3; + $info['ogg']['pageheader']['theora']['pixel_aspect_denominator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3)); + $filedataoffset += 3; + $info['ogg']['pageheader']['theora']['color_space_id'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['pageheader']['theora']['nominal_bitrate'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3)); + $filedataoffset += 3; + $info['ogg']['pageheader']['theora']['flags'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 2)); + $filedataoffset += 2; + + $info['ogg']['pageheader']['theora']['quality'] = ($info['ogg']['pageheader']['theora']['flags'] & 0xFC00) >> 10; + $info['ogg']['pageheader']['theora']['kfg_shift'] = ($info['ogg']['pageheader']['theora']['flags'] & 0x03E0) >> 5; + $info['ogg']['pageheader']['theora']['pixel_format_id'] = ($info['ogg']['pageheader']['theora']['flags'] & 0x0018) >> 3; + $info['ogg']['pageheader']['theora']['reserved'] = ($info['ogg']['pageheader']['theora']['flags'] & 0x0007) >> 0; // should be 0 + $info['ogg']['pageheader']['theora']['color_space'] = self::TheoraColorSpace($info['ogg']['pageheader']['theora']['color_space_id']); + $info['ogg']['pageheader']['theora']['pixel_format'] = self::TheoraPixelFormat($info['ogg']['pageheader']['theora']['pixel_format_id']); + + $info['video']['dataformat'] = 'theora'; + $info['mime_type'] = 'video/ogg'; + //$info['audio']['bitrate_mode'] = 'abr'; + //$info['audio']['lossless'] = false; + $info['video']['resolution_x'] = $info['ogg']['pageheader']['theora']['resolution_x']; + $info['video']['resolution_y'] = $info['ogg']['pageheader']['theora']['resolution_y']; + if ($info['ogg']['pageheader']['theora']['frame_rate_denominator'] > 0) { + $info['video']['frame_rate'] = (float) $info['ogg']['pageheader']['theora']['frame_rate_numerator'] / $info['ogg']['pageheader']['theora']['frame_rate_denominator']; + } + if ($info['ogg']['pageheader']['theora']['pixel_aspect_denominator'] > 0) { + $info['video']['pixel_aspect_ratio'] = (float) $info['ogg']['pageheader']['theora']['pixel_aspect_numerator'] / $info['ogg']['pageheader']['theora']['pixel_aspect_denominator']; + } + $this->warning('Ogg Theora (v3) not fully supported in this version of getID3 ['.$this->getid3->version().'] -- bitrate, playtime and all audio data are currently unavailable'); + + + } elseif (substr($filedata, 0, 8) == "fishead\x00") { + + // Ogg Skeleton version 3.0 Format Specification + // http://xiph.org/ogg/doc/skeleton.html + $filedataoffset += 8; + $info['ogg']['skeleton']['fishead']['raw']['version_major'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2)); + $filedataoffset += 2; + $info['ogg']['skeleton']['fishead']['raw']['version_minor'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2)); + $filedataoffset += 2; + $info['ogg']['skeleton']['fishead']['raw']['presentationtime_numerator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $info['ogg']['skeleton']['fishead']['raw']['presentationtime_denominator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $info['ogg']['skeleton']['fishead']['raw']['basetime_numerator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $info['ogg']['skeleton']['fishead']['raw']['basetime_denominator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $info['ogg']['skeleton']['fishead']['raw']['utc'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 20)); + $filedataoffset += 20; + + $info['ogg']['skeleton']['fishead']['version'] = $info['ogg']['skeleton']['fishead']['raw']['version_major'].'.'.$info['ogg']['skeleton']['fishead']['raw']['version_minor']; + $info['ogg']['skeleton']['fishead']['presentationtime'] = $info['ogg']['skeleton']['fishead']['raw']['presentationtime_numerator'] / $info['ogg']['skeleton']['fishead']['raw']['presentationtime_denominator']; + $info['ogg']['skeleton']['fishead']['basetime'] = $info['ogg']['skeleton']['fishead']['raw']['basetime_numerator'] / $info['ogg']['skeleton']['fishead']['raw']['basetime_denominator']; + $info['ogg']['skeleton']['fishead']['utc'] = $info['ogg']['skeleton']['fishead']['raw']['utc']; + + + $counter = 0; + do { + $oggpageinfo = $this->ParseOggPageHeader(); + $info['ogg']['pageheader'][$oggpageinfo['page_seqno'].'.'.$counter++] = $oggpageinfo; + $filedata = $this->fread($oggpageinfo['page_length']); + $this->fseek($oggpageinfo['page_end_offset']); + + if (substr($filedata, 0, 8) == "fisbone\x00") { + + $filedataoffset = 8; + $info['ogg']['skeleton']['fisbone']['raw']['message_header_offset'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['skeleton']['fisbone']['raw']['serial_number'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['skeleton']['fisbone']['raw']['number_header_packets'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['skeleton']['fisbone']['raw']['granulerate_numerator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $info['ogg']['skeleton']['fisbone']['raw']['granulerate_denominator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $info['ogg']['skeleton']['fisbone']['raw']['basegranule'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $info['ogg']['skeleton']['fisbone']['raw']['preroll'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['skeleton']['fisbone']['raw']['granuleshift'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['skeleton']['fisbone']['raw']['padding'] = substr($filedata, $filedataoffset, 3); + $filedataoffset += 3; + + } elseif (substr($filedata, 1, 6) == 'theora') { + + $info['video']['dataformat'] = 'theora1'; + $this->error('Ogg Theora (v1) not correctly handled in this version of getID3 ['.$this->getid3->version().']'); + //break; + + } elseif (substr($filedata, 1, 6) == 'vorbis') { + + $this->ParseVorbisPageHeader($filedata, $filedataoffset, $oggpageinfo); + + } else { + $this->error('unexpected'); + //break; + } + //} while ($oggpageinfo['page_seqno'] == 0); + } while (($oggpageinfo['page_seqno'] == 0) && (substr($filedata, 0, 8) != "fisbone\x00")); + + $this->fseek($oggpageinfo['page_start_offset']); + + $this->error('Ogg Skeleton not correctly handled in this version of getID3 ['.$this->getid3->version().']'); + //return false; + + } elseif (substr($filedata, 0, 5) == "\x7F".'FLAC') { + // https://xiph.org/flac/ogg_mapping.html + + $info['audio']['dataformat'] = 'flac'; + $info['audio']['bitrate_mode'] = 'vbr'; + $info['audio']['lossless'] = true; + + $info['ogg']['flac']['header']['version_major'] = ord(substr($filedata, 5, 1)); + $info['ogg']['flac']['header']['version_minor'] = ord(substr($filedata, 6, 1)); + $info['ogg']['flac']['header']['header_packets'] = getid3_lib::BigEndian2Int(substr($filedata, 7, 2)) + 1; // "A two-byte, big-endian binary number signifying the number of header (non-audio) packets, not including this one. This number may be zero (0x0000) to signify 'unknown' but be aware that some decoders may not be able to handle such streams." + $info['ogg']['flac']['header']['magic'] = substr($filedata, 9, 4); + if ($info['ogg']['flac']['header']['magic'] != 'fLaC') { + $this->error('Ogg-FLAC expecting "fLaC", found "'.$info['ogg']['flac']['header']['magic'].'" ('.trim(getid3_lib::PrintHexBytes($info['ogg']['flac']['header']['magic'])).')'); + return false; + } + $info['ogg']['flac']['header']['STREAMINFO_bytes'] = getid3_lib::BigEndian2Int(substr($filedata, 13, 4)); + $info['flac']['STREAMINFO'] = getid3_flac::parseSTREAMINFOdata(substr($filedata, 17, 34)); + if (!empty($info['flac']['STREAMINFO']['sample_rate'])) { + $info['audio']['bitrate_mode'] = 'vbr'; + $info['audio']['sample_rate'] = $info['flac']['STREAMINFO']['sample_rate']; + $info['audio']['channels'] = $info['flac']['STREAMINFO']['channels']; + $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample']; + $info['playtime_seconds'] = $info['flac']['STREAMINFO']['samples_stream'] / $info['flac']['STREAMINFO']['sample_rate']; + } + + } else { + + $this->error('Expecting one of "vorbis", "Speex", "OpusHead", "vorbis", "fishhead", "theora", "fLaC" identifier strings, found "'.substr($filedata, 0, 8).'"'); + unset($info['ogg']); + unset($info['mime_type']); + return false; + + } + + // Page 2 - Comment Header + $oggpageinfo = $this->ParseOggPageHeader(); + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo; + + switch ($info['audio']['dataformat']) { + case 'vorbis': + $filedata = $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']); + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['packet_type'] = getid3_lib::LittleEndian2Int(substr($filedata, 0, 1)); + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, 1, 6); // hard-coded to 'vorbis' + + $this->ParseVorbisComments(); + break; + + case 'flac': + $flac = new getid3_flac($this->getid3); + if (!$flac->parseMETAdata()) { + $this->error('Failed to parse FLAC headers'); + return false; + } + unset($flac); + break; + + case 'speex': + $this->fseek($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length'], SEEK_CUR); + $this->ParseVorbisComments(); + break; + + case 'opus': + $filedata = $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']); + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, 0, 8); // hard-coded to 'OpusTags' + if(substr($filedata, 0, 8) != 'OpusTags') { + $this->error('Expected "OpusTags" as header but got "'.substr($filedata, 0, 8).'"'); + return false; + } + + $this->ParseVorbisComments(); + break; + + } + + // Last Page - Number of Samples + if (!getid3_lib::intValueSupported($info['avdataend'])) { + + $this->warning('Unable to parse Ogg end chunk file (PHP does not support file operations beyond '.round(PHP_INT_MAX / 1073741824).'GB)'); + + } else { + + $this->fseek(max($info['avdataend'] - $this->getid3->fread_buffer_size(), 0)); + $LastChunkOfOgg = strrev($this->fread($this->getid3->fread_buffer_size())); + if ($LastOggSpostion = strpos($LastChunkOfOgg, 'SggO')) { + $this->fseek($info['avdataend'] - ($LastOggSpostion + strlen('SggO'))); + $info['avdataend'] = $this->ftell(); + $info['ogg']['pageheader']['eos'] = $this->ParseOggPageHeader(); + $info['ogg']['samples'] = $info['ogg']['pageheader']['eos']['pcm_abs_position']; + if ($info['ogg']['samples'] == 0) { + $this->error('Corrupt Ogg file: eos.number of samples == zero'); + return false; + } + if (!empty($info['audio']['sample_rate'])) { + $info['ogg']['bitrate_average'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / ($info['ogg']['samples'] / $info['audio']['sample_rate']); + } + } + + } + + if (!empty($info['ogg']['bitrate_average'])) { + $info['audio']['bitrate'] = $info['ogg']['bitrate_average']; + } elseif (!empty($info['ogg']['bitrate_nominal'])) { + $info['audio']['bitrate'] = $info['ogg']['bitrate_nominal']; + } elseif (!empty($info['ogg']['bitrate_min']) && !empty($info['ogg']['bitrate_max'])) { + $info['audio']['bitrate'] = ($info['ogg']['bitrate_min'] + $info['ogg']['bitrate_max']) / 2; + } + if (isset($info['audio']['bitrate']) && !isset($info['playtime_seconds'])) { + if ($info['audio']['bitrate'] == 0) { + $this->error('Corrupt Ogg file: bitrate_audio == zero'); + return false; + } + $info['playtime_seconds'] = (float) ((($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate']); + } + + if (isset($info['ogg']['vendor'])) { + $info['audio']['encoder'] = preg_replace('/^Encoded with /', '', $info['ogg']['vendor']); + + // Vorbis only + if ($info['audio']['dataformat'] == 'vorbis') { + + // Vorbis 1.0 starts with Xiph.Org + if (preg_match('/^Xiph.Org/', $info['audio']['encoder'])) { + + if ($info['audio']['bitrate_mode'] == 'abr') { + + // Set -b 128 on abr files + $info['audio']['encoder_options'] = '-b '.round($info['ogg']['bitrate_nominal'] / 1000); + + } elseif (($info['audio']['bitrate_mode'] == 'vbr') && ($info['audio']['channels'] == 2) && ($info['audio']['sample_rate'] >= 44100) && ($info['audio']['sample_rate'] <= 48000)) { + // Set -q N on vbr files + $info['audio']['encoder_options'] = '-q '.$this->get_quality_from_nominal_bitrate($info['ogg']['bitrate_nominal']); + + } + } + + if (empty($info['audio']['encoder_options']) && !empty($info['ogg']['bitrate_nominal'])) { + $info['audio']['encoder_options'] = 'Nominal bitrate: '.intval(round($info['ogg']['bitrate_nominal'] / 1000)).'kbps'; + } + } + } + + return true; + } + + /** + * @param string $filedata + * @param int $filedataoffset + * @param array $oggpageinfo + * + * @return bool + */ + public function ParseVorbisPageHeader(&$filedata, &$filedataoffset, &$oggpageinfo) { + $info = &$this->getid3->info; + $info['audio']['dataformat'] = 'vorbis'; + $info['audio']['lossless'] = false; + + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['packet_type'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, $filedataoffset, 6); // hard-coded to 'vorbis' + $filedataoffset += 6; + $info['ogg']['bitstreamversion'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['numberofchannels'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $info['audio']['channels'] = $info['ogg']['numberofchannels']; + $info['ogg']['samplerate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + if ($info['ogg']['samplerate'] == 0) { + $this->error('Corrupt Ogg file: sample rate == zero'); + return false; + } + $info['audio']['sample_rate'] = $info['ogg']['samplerate']; + $info['ogg']['samples'] = 0; // filled in later + $info['ogg']['bitrate_average'] = 0; // filled in later + $info['ogg']['bitrate_max'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['bitrate_nominal'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['bitrate_min'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $info['ogg']['blocksize_small'] = pow(2, getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)) & 0x0F); + $info['ogg']['blocksize_large'] = pow(2, (getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)) & 0xF0) >> 4); + $info['ogg']['stop_bit'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); // must be 1, marks end of packet + + $info['audio']['bitrate_mode'] = 'vbr'; // overridden if actually abr + if ($info['ogg']['bitrate_max'] == 0xFFFFFFFF) { + unset($info['ogg']['bitrate_max']); + $info['audio']['bitrate_mode'] = 'abr'; + } + if ($info['ogg']['bitrate_nominal'] == 0xFFFFFFFF) { + unset($info['ogg']['bitrate_nominal']); + } + if ($info['ogg']['bitrate_min'] == 0xFFFFFFFF) { + unset($info['ogg']['bitrate_min']); + $info['audio']['bitrate_mode'] = 'abr'; + } + return true; + } + + /** + * @link http://tools.ietf.org/html/draft-ietf-codec-oggopus-03 + * + * @param string $filedata + * @param int $filedataoffset + * @param array $oggpageinfo + * + * @return bool + */ + public function ParseOpusPageHeader(&$filedata, &$filedataoffset, &$oggpageinfo) { + $info = &$this->getid3->info; + $info['audio']['dataformat'] = 'opus'; + $info['mime_type'] = 'audio/ogg; codecs=opus'; + + /** @todo find a usable way to detect abr (vbr that is padded to be abr) */ + $info['audio']['bitrate_mode'] = 'vbr'; + + $info['audio']['lossless'] = false; + + $info['ogg']['pageheader']['opus']['opus_magic'] = substr($filedata, $filedataoffset, 8); // hard-coded to 'OpusHead' + $filedataoffset += 8; + $info['ogg']['pageheader']['opus']['version'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + + if ($info['ogg']['pageheader']['opus']['version'] < 1 || $info['ogg']['pageheader']['opus']['version'] > 15) { + $this->error('Unknown opus version number (only accepting 1-15)'); + return false; + } + + $info['ogg']['pageheader']['opus']['out_channel_count'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + + if ($info['ogg']['pageheader']['opus']['out_channel_count'] == 0) { + $this->error('Invalid channel count in opus header (must not be zero)'); + return false; + } + + $info['ogg']['pageheader']['opus']['pre_skip'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2)); + $filedataoffset += 2; + + $info['ogg']['pageheader']['opus']['input_sample_rate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + + //$info['ogg']['pageheader']['opus']['output_gain'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2)); + //$filedataoffset += 2; + + //$info['ogg']['pageheader']['opus']['channel_mapping_family'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + //$filedataoffset += 1; + + $info['opus']['opus_version'] = $info['ogg']['pageheader']['opus']['version']; + $info['opus']['sample_rate_input'] = $info['ogg']['pageheader']['opus']['input_sample_rate']; + $info['opus']['out_channel_count'] = $info['ogg']['pageheader']['opus']['out_channel_count']; + + $info['audio']['channels'] = $info['opus']['out_channel_count']; + $info['audio']['sample_rate_input'] = $info['opus']['sample_rate_input']; + $info['audio']['sample_rate'] = 48000; // "All Opus audio is coded at 48 kHz, and should also be decoded at 48 kHz for playback (unless the target hardware does not support this sampling rate). However, this field may be used to resample the audio back to the original sampling rate, for example, when saving the output to a file." -- https://mf4.xiph.org/jenkins/view/opus/job/opusfile-unix/ws/doc/html/structOpusHead.html + return true; + } + + /** + * @return array|false + */ + public function ParseOggPageHeader() { + // http://xiph.org/ogg/vorbis/doc/framing.html + $oggheader['page_start_offset'] = $this->ftell(); // where we started from in the file + + $filedata = $this->fread($this->getid3->fread_buffer_size()); + $filedataoffset = 0; + while ((substr($filedata, $filedataoffset++, 4) != 'OggS')) { + if (($this->ftell() - $oggheader['page_start_offset']) >= $this->getid3->fread_buffer_size()) { + // should be found before here + return false; + } + if ((($filedataoffset + 28) > strlen($filedata)) || (strlen($filedata) < 28)) { + if ($this->feof() || (($filedata .= $this->fread($this->getid3->fread_buffer_size())) === '')) { + // get some more data, unless eof, in which case fail + return false; + } + } + } + $filedataoffset += strlen('OggS') - 1; // page, delimited by 'OggS' + + $oggheader['stream_structver'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $oggheader['flags_raw'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $oggheader['flags']['fresh'] = (bool) ($oggheader['flags_raw'] & 0x01); // fresh packet + $oggheader['flags']['bos'] = (bool) ($oggheader['flags_raw'] & 0x02); // first page of logical bitstream (bos) + $oggheader['flags']['eos'] = (bool) ($oggheader['flags_raw'] & 0x04); // last page of logical bitstream (eos) + + $oggheader['pcm_abs_position'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8)); + $filedataoffset += 8; + $oggheader['stream_serialno'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $oggheader['page_seqno'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $oggheader['page_checksum'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4)); + $filedataoffset += 4; + $oggheader['page_segments'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $oggheader['page_length'] = 0; + for ($i = 0; $i < $oggheader['page_segments']; $i++) { + $oggheader['segment_table'][$i] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); + $filedataoffset += 1; + $oggheader['page_length'] += $oggheader['segment_table'][$i]; + } + $oggheader['header_end_offset'] = $oggheader['page_start_offset'] + $filedataoffset; + $oggheader['page_end_offset'] = $oggheader['header_end_offset'] + $oggheader['page_length']; + $this->fseek($oggheader['header_end_offset']); + + return $oggheader; + } + + /** + * @link http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-810005 + * + * @return bool + */ + public function ParseVorbisComments() { + $info = &$this->getid3->info; + + $OriginalOffset = $this->ftell(); + $commentdata = null; + $commentdataoffset = 0; + $VorbisCommentPage = 1; + $CommentStartOffset = 0; + + switch ($info['audio']['dataformat']) { + case 'vorbis': + case 'speex': + case 'opus': + $CommentStartOffset = $info['ogg']['pageheader'][$VorbisCommentPage]['page_start_offset']; // Second Ogg page, after header block + $this->fseek($CommentStartOffset); + $commentdataoffset = 27 + $info['ogg']['pageheader'][$VorbisCommentPage]['page_segments']; + $commentdata = $this->fread(self::OggPageSegmentLength($info['ogg']['pageheader'][$VorbisCommentPage], 1) + $commentdataoffset); + + if ($info['audio']['dataformat'] == 'vorbis') { + $commentdataoffset += (strlen('vorbis') + 1); + } + else if ($info['audio']['dataformat'] == 'opus') { + $commentdataoffset += strlen('OpusTags'); + } + + break; + + case 'flac': + $CommentStartOffset = $info['flac']['VORBIS_COMMENT']['raw']['offset'] + 4; + $this->fseek($CommentStartOffset); + $commentdata = $this->fread($info['flac']['VORBIS_COMMENT']['raw']['block_length']); + break; + + default: + return false; + } + + $VendorSize = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4)); + $commentdataoffset += 4; + + $info['ogg']['vendor'] = substr($commentdata, $commentdataoffset, $VendorSize); + $commentdataoffset += $VendorSize; + + $CommentsCount = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4)); + $commentdataoffset += 4; + $info['avdataoffset'] = $CommentStartOffset + $commentdataoffset; + + $basicfields = array('TITLE', 'ARTIST', 'ALBUM', 'TRACKNUMBER', 'GENRE', 'DATE', 'DESCRIPTION', 'COMMENT'); + $ThisFileInfo_ogg_comments_raw = &$info['ogg']['comments_raw']; + for ($i = 0; $i < $CommentsCount; $i++) { + + if ($i >= 10000) { + // https://github.com/owncloud/music/issues/212#issuecomment-43082336 + $this->warning('Unexpectedly large number ('.$CommentsCount.') of Ogg comments - breaking after reading '.$i.' comments'); + break; + } + + $ThisFileInfo_ogg_comments_raw[$i]['dataoffset'] = $CommentStartOffset + $commentdataoffset; + + if ($this->ftell() < ($ThisFileInfo_ogg_comments_raw[$i]['dataoffset'] + 4)) { + if ($oggpageinfo = $this->ParseOggPageHeader()) { + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo; + + $VorbisCommentPage++; + + // First, save what we haven't read yet + $AsYetUnusedData = substr($commentdata, $commentdataoffset); + + // Then take that data off the end + $commentdata = substr($commentdata, 0, $commentdataoffset); + + // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct + $commentdata .= str_repeat("\x00", 27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']); + $commentdataoffset += (27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']); + + // Finally, stick the unused data back on the end + $commentdata .= $AsYetUnusedData; + + //$commentdata .= $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']); + $commentdata .= $this->fread($this->OggPageSegmentLength($info['ogg']['pageheader'][$VorbisCommentPage], 1)); + } + + } + $ThisFileInfo_ogg_comments_raw[$i]['size'] = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4)); + + // replace avdataoffset with position just after the last vorbiscomment + $info['avdataoffset'] = $ThisFileInfo_ogg_comments_raw[$i]['dataoffset'] + $ThisFileInfo_ogg_comments_raw[$i]['size'] + 4; + + $commentdataoffset += 4; + while ((strlen($commentdata) - $commentdataoffset) < $ThisFileInfo_ogg_comments_raw[$i]['size']) { + if (($ThisFileInfo_ogg_comments_raw[$i]['size'] > $info['avdataend']) || ($ThisFileInfo_ogg_comments_raw[$i]['size'] < 0)) { + $this->warning('Invalid Ogg comment size (comment #'.$i.', claims to be '.number_format($ThisFileInfo_ogg_comments_raw[$i]['size']).' bytes) - aborting reading comments'); + break 2; + } + + $VorbisCommentPage++; + + $oggpageinfo = $this->ParseOggPageHeader(); + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo; + + // First, save what we haven't read yet + $AsYetUnusedData = substr($commentdata, $commentdataoffset); + + // Then take that data off the end + $commentdata = substr($commentdata, 0, $commentdataoffset); + + // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct + $commentdata .= str_repeat("\x00", 27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']); + $commentdataoffset += (27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']); + + // Finally, stick the unused data back on the end + $commentdata .= $AsYetUnusedData; + + //$commentdata .= $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']); + if (!isset($info['ogg']['pageheader'][$VorbisCommentPage])) { + $this->warning('undefined Vorbis Comment page "'.$VorbisCommentPage.'" at offset '.$this->ftell()); + break; + } + $readlength = self::OggPageSegmentLength($info['ogg']['pageheader'][$VorbisCommentPage], 1); + if ($readlength <= 0) { + $this->warning('invalid length Vorbis Comment page "'.$VorbisCommentPage.'" at offset '.$this->ftell()); + break; + } + $commentdata .= $this->fread($readlength); + + //$filebaseoffset += $oggpageinfo['header_end_offset'] - $oggpageinfo['page_start_offset']; + } + $ThisFileInfo_ogg_comments_raw[$i]['offset'] = $commentdataoffset; + $commentstring = substr($commentdata, $commentdataoffset, $ThisFileInfo_ogg_comments_raw[$i]['size']); + $commentdataoffset += $ThisFileInfo_ogg_comments_raw[$i]['size']; + + if (!$commentstring) { + + // no comment? + $this->warning('Blank Ogg comment ['.$i.']'); + + } elseif (strstr($commentstring, '=')) { + + $commentexploded = explode('=', $commentstring, 2); + $ThisFileInfo_ogg_comments_raw[$i]['key'] = strtoupper($commentexploded[0]); + $ThisFileInfo_ogg_comments_raw[$i]['value'] = (isset($commentexploded[1]) ? $commentexploded[1] : ''); + + if ($ThisFileInfo_ogg_comments_raw[$i]['key'] == 'METADATA_BLOCK_PICTURE') { + + // http://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE + // The unencoded format is that of the FLAC picture block. The fields are stored in big endian order as in FLAC, picture data is stored according to the relevant standard. + // http://flac.sourceforge.net/format.html#metadata_block_picture + $flac = new getid3_flac($this->getid3); + $flac->setStringMode(base64_decode($ThisFileInfo_ogg_comments_raw[$i]['value'])); + $flac->parsePICTURE(); + $info['ogg']['comments']['picture'][] = $flac->getid3->info['flac']['PICTURE'][0]; + unset($flac); + + } elseif ($ThisFileInfo_ogg_comments_raw[$i]['key'] == 'COVERART') { + + $data = base64_decode($ThisFileInfo_ogg_comments_raw[$i]['value']); + $this->notice('Found deprecated COVERART tag, it should be replaced in honor of METADATA_BLOCK_PICTURE structure'); + /** @todo use 'coverartmime' where available */ + $imageinfo = getid3_lib::GetDataImageSize($data); + if ($imageinfo === false || !isset($imageinfo['mime'])) { + $this->warning('COVERART vorbiscomment tag contains invalid image'); + continue; + } + + $ogg = new self($this->getid3); + $ogg->setStringMode($data); + $info['ogg']['comments']['picture'][] = array( + 'image_mime' => $imageinfo['mime'], + 'datalength' => strlen($data), + 'picturetype' => 'cover art', + 'image_height' => $imageinfo['height'], + 'image_width' => $imageinfo['width'], + 'data' => $ogg->saveAttachment('coverart', 0, strlen($data), $imageinfo['mime']), + ); + unset($ogg); + + } else { + + $info['ogg']['comments'][strtolower($ThisFileInfo_ogg_comments_raw[$i]['key'])][] = $ThisFileInfo_ogg_comments_raw[$i]['value']; + + } + + } else { + + $this->warning('[known problem with CDex >= v1.40, < v1.50b7] Invalid Ogg comment name/value pair ['.$i.']: '.$commentstring); + + } + unset($ThisFileInfo_ogg_comments_raw[$i]); + } + unset($ThisFileInfo_ogg_comments_raw); + + + // Replay Gain Adjustment + // http://privatewww.essex.ac.uk/~djmrob/replaygain/ + if (isset($info['ogg']['comments']) && is_array($info['ogg']['comments'])) { + foreach ($info['ogg']['comments'] as $index => $commentvalue) { + switch ($index) { + case 'rg_audiophile': + case 'replaygain_album_gain': + $info['replay_gain']['album']['adjustment'] = (double) $commentvalue[0]; + unset($info['ogg']['comments'][$index]); + break; + + case 'rg_radio': + case 'replaygain_track_gain': + $info['replay_gain']['track']['adjustment'] = (double) $commentvalue[0]; + unset($info['ogg']['comments'][$index]); + break; + + case 'replaygain_album_peak': + $info['replay_gain']['album']['peak'] = (double) $commentvalue[0]; + unset($info['ogg']['comments'][$index]); + break; + + case 'rg_peak': + case 'replaygain_track_peak': + $info['replay_gain']['track']['peak'] = (double) $commentvalue[0]; + unset($info['ogg']['comments'][$index]); + break; + + case 'replaygain_reference_loudness': + $info['replay_gain']['reference_volume'] = (double) $commentvalue[0]; + unset($info['ogg']['comments'][$index]); + break; + + default: + // do nothing + break; + } + } + } + + $this->fseek($OriginalOffset); + + return true; + } + + /** + * @param int $mode + * + * @return string|null + */ + public static function SpeexBandModeLookup($mode) { + static $SpeexBandModeLookup = array(); + if (empty($SpeexBandModeLookup)) { + $SpeexBandModeLookup[0] = 'narrow'; + $SpeexBandModeLookup[1] = 'wide'; + $SpeexBandModeLookup[2] = 'ultra-wide'; + } + return (isset($SpeexBandModeLookup[$mode]) ? $SpeexBandModeLookup[$mode] : null); + } + + /** + * @param array $OggInfoArray + * @param int $SegmentNumber + * + * @return int + */ + public static function OggPageSegmentLength($OggInfoArray, $SegmentNumber=1) { + $segmentlength = 0; + for ($i = 0; $i < $SegmentNumber; $i++) { + $segmentlength = 0; + foreach ($OggInfoArray['segment_table'] as $key => $value) { + $segmentlength += $value; + if ($value < 255) { + break; + } + } + } + return $segmentlength; + } + + /** + * @param int $nominal_bitrate + * + * @return float + */ + public static function get_quality_from_nominal_bitrate($nominal_bitrate) { + + // decrease precision + $nominal_bitrate = $nominal_bitrate / 1000; + + if ($nominal_bitrate < 128) { + // q-1 to q4 + $qval = ($nominal_bitrate - 64) / 16; + } elseif ($nominal_bitrate < 256) { + // q4 to q8 + $qval = $nominal_bitrate / 32; + } elseif ($nominal_bitrate < 320) { + // q8 to q9 + $qval = ($nominal_bitrate + 256) / 64; + } else { + // q9 to q10 + $qval = ($nominal_bitrate + 1300) / 180; + } + //return $qval; // 5.031324 + //return intval($qval); // 5 + return round($qval, 1); // 5 or 4.9 + } + + /** + * @param int $colorspace_id + * + * @return string|null + */ + public static function TheoraColorSpace($colorspace_id) { + // http://www.theora.org/doc/Theora.pdf (table 6.3) + static $TheoraColorSpaceLookup = array(); + if (empty($TheoraColorSpaceLookup)) { + $TheoraColorSpaceLookup[0] = 'Undefined'; + $TheoraColorSpaceLookup[1] = 'Rec. 470M'; + $TheoraColorSpaceLookup[2] = 'Rec. 470BG'; + $TheoraColorSpaceLookup[3] = 'Reserved'; + } + return (isset($TheoraColorSpaceLookup[$colorspace_id]) ? $TheoraColorSpaceLookup[$colorspace_id] : null); + } + + /** + * @param int $pixelformat_id + * + * @return string|null + */ + public static function TheoraPixelFormat($pixelformat_id) { + // http://www.theora.org/doc/Theora.pdf (table 6.4) + static $TheoraPixelFormatLookup = array(); + if (empty($TheoraPixelFormatLookup)) { + $TheoraPixelFormatLookup[0] = '4:2:0'; + $TheoraPixelFormatLookup[1] = 'Reserved'; + $TheoraPixelFormatLookup[2] = '4:2:2'; + $TheoraPixelFormatLookup[3] = '4:4:4'; + } + return (isset($TheoraPixelFormatLookup[$pixelformat_id]) ? $TheoraPixelFormatLookup[$pixelformat_id] : null); + } + +} diff --git a/projects/tests/tests/performance/ID3/module.tag.apetag.php b/projects/tests/tests/performance/ID3/module.tag.apetag.php new file mode 100644 index 00000000..26be982c --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.tag.apetag.php @@ -0,0 +1,452 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.tag.apetag.php // +// module for analyzing APE tags // +// dependencies: NONE // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} + +class getid3_apetag extends getid3_handler +{ + /** + * true: return full data for all attachments; + * false: return no data for all attachments; + * integer: return data for attachments <= than this; + * string: save as file to this directory. + * + * @var int|bool|string + */ + public $inline_attachments = true; + + public $overrideendoffset = 0; + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + if (!getid3_lib::intValueSupported($info['filesize'])) { + $this->warning('Unable to check for APEtags because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); + return false; + } + + $id3v1tagsize = 128; + $apetagheadersize = 32; + $lyrics3tagsize = 10; + + if ($this->overrideendoffset == 0) { + + $this->fseek(0 - $id3v1tagsize - $apetagheadersize - $lyrics3tagsize, SEEK_END); + $APEfooterID3v1 = $this->fread($id3v1tagsize + $apetagheadersize + $lyrics3tagsize); + + //if (preg_match('/APETAGEX.{24}TAG.{125}$/i', $APEfooterID3v1)) { + if (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $id3v1tagsize - $apetagheadersize, 8) == 'APETAGEX') { + + // APE tag found before ID3v1 + $info['ape']['tag_offset_end'] = $info['filesize'] - $id3v1tagsize; + + //} elseif (preg_match('/APETAGEX.{24}$/i', $APEfooterID3v1)) { + } elseif (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $apetagheadersize, 8) == 'APETAGEX') { + + // APE tag found, no ID3v1 + $info['ape']['tag_offset_end'] = $info['filesize']; + + } + + } else { + + $this->fseek($this->overrideendoffset - $apetagheadersize); + if ($this->fread(8) == 'APETAGEX') { + $info['ape']['tag_offset_end'] = $this->overrideendoffset; + } + + } + if (!isset($info['ape']['tag_offset_end'])) { + + // APE tag not found + unset($info['ape']); + return false; + + } + + // shortcut + $thisfile_ape = &$info['ape']; + + $this->fseek($thisfile_ape['tag_offset_end'] - $apetagheadersize); + $APEfooterData = $this->fread(32); + if (!($thisfile_ape['footer'] = $this->parseAPEheaderFooter($APEfooterData))) { + $this->error('Error parsing APE footer at offset '.$thisfile_ape['tag_offset_end']); + return false; + } + + if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) { + $this->fseek($thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize'] - $apetagheadersize); + $thisfile_ape['tag_offset_start'] = $this->ftell(); + $APEtagData = $this->fread($thisfile_ape['footer']['raw']['tagsize'] + $apetagheadersize); + } else { + $thisfile_ape['tag_offset_start'] = $thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize']; + $this->fseek($thisfile_ape['tag_offset_start']); + $APEtagData = $this->fread($thisfile_ape['footer']['raw']['tagsize']); + } + $info['avdataend'] = $thisfile_ape['tag_offset_start']; + + if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] < $thisfile_ape['tag_offset_end'])) { + $this->warning('ID3v1 tag information ignored since it appears to be a false synch in APEtag data'); + unset($info['id3v1']); + foreach ($info['warning'] as $key => $value) { + if ($value == 'Some ID3v1 fields do not use NULL characters for padding') { + unset($info['warning'][$key]); + sort($info['warning']); + break; + } + } + } + + $offset = 0; + if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) { + if ($thisfile_ape['header'] = $this->parseAPEheaderFooter(substr($APEtagData, 0, $apetagheadersize))) { + $offset += $apetagheadersize; + } else { + $this->error('Error parsing APE header at offset '.$thisfile_ape['tag_offset_start']); + return false; + } + } + + // shortcut + $info['replay_gain'] = array(); + $thisfile_replaygain = &$info['replay_gain']; + + for ($i = 0; $i < $thisfile_ape['footer']['raw']['tag_items']; $i++) { + $value_size = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4)); + $offset += 4; + $item_flags = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4)); + $offset += 4; + if (strstr(substr($APEtagData, $offset), "\x00") === false) { + $this->error('Cannot find null-byte (0x00) separator between ItemKey #'.$i.' and value. ItemKey starts '.$offset.' bytes into the APE tag, at file offset '.($thisfile_ape['tag_offset_start'] + $offset)); + return false; + } + $ItemKeyLength = strpos($APEtagData, "\x00", $offset) - $offset; + $item_key = strtolower(substr($APEtagData, $offset, $ItemKeyLength)); + + // shortcut + $thisfile_ape['items'][$item_key] = array(); + $thisfile_ape_items_current = &$thisfile_ape['items'][$item_key]; + + $thisfile_ape_items_current['offset'] = $thisfile_ape['tag_offset_start'] + $offset; + + $offset += ($ItemKeyLength + 1); // skip 0x00 terminator + $thisfile_ape_items_current['data'] = substr($APEtagData, $offset, $value_size); + $offset += $value_size; + + $thisfile_ape_items_current['flags'] = $this->parseAPEtagFlags($item_flags); + switch ($thisfile_ape_items_current['flags']['item_contents_raw']) { + case 0: // UTF-8 + case 2: // Locator (URL, filename, etc), UTF-8 encoded + $thisfile_ape_items_current['data'] = explode("\x00", $thisfile_ape_items_current['data']); + break; + + case 1: // binary data + default: + break; + } + + switch (strtolower($item_key)) { + // http://wiki.hydrogenaud.io/index.php?title=ReplayGain#MP3Gain + case 'replaygain_track_gain': + if (preg_match('#^([\\-\\+][0-9\\.,]{8})( dB)?$#', $thisfile_ape_items_current['data'][0], $matches)) { + $thisfile_replaygain['track']['adjustment'] = (float) str_replace(',', '.', $matches[1]); // float casting will see "0,95" as zero! + $thisfile_replaygain['track']['originator'] = 'unspecified'; + } else { + $this->warning('MP3gainTrackGain value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"'); + } + break; + + case 'replaygain_track_peak': + if (preg_match('#^([0-9\\.,]{8})$#', $thisfile_ape_items_current['data'][0], $matches)) { + $thisfile_replaygain['track']['peak'] = (float) str_replace(',', '.', $matches[1]); // float casting will see "0,95" as zero! + $thisfile_replaygain['track']['originator'] = 'unspecified'; + if ($thisfile_replaygain['track']['peak'] <= 0) { + $this->warning('ReplayGain Track peak from APEtag appears invalid: '.$thisfile_replaygain['track']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")'); + } + } else { + $this->warning('MP3gainTrackPeak value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"'); + } + break; + + case 'replaygain_album_gain': + if (preg_match('#^([\\-\\+][0-9\\.,]{8})( dB)?$#', $thisfile_ape_items_current['data'][0], $matches)) { + $thisfile_replaygain['album']['adjustment'] = (float) str_replace(',', '.', $matches[1]); // float casting will see "0,95" as zero! + $thisfile_replaygain['album']['originator'] = 'unspecified'; + } else { + $this->warning('MP3gainAlbumGain value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"'); + } + break; + + case 'replaygain_album_peak': + if (preg_match('#^([0-9\\.,]{8})$#', $thisfile_ape_items_current['data'][0], $matches)) { + $thisfile_replaygain['album']['peak'] = (float) str_replace(',', '.', $matches[1]); // float casting will see "0,95" as zero! + $thisfile_replaygain['album']['originator'] = 'unspecified'; + if ($thisfile_replaygain['album']['peak'] <= 0) { + $this->warning('ReplayGain Album peak from APEtag appears invalid: '.$thisfile_replaygain['album']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")'); + } + } else { + $this->warning('MP3gainAlbumPeak value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"'); + } + break; + + case 'mp3gain_undo': + if (preg_match('#^[\\-\\+][0-9]{3},[\\-\\+][0-9]{3},[NW]$#', $thisfile_ape_items_current['data'][0])) { + list($mp3gain_undo_left, $mp3gain_undo_right, $mp3gain_undo_wrap) = explode(',', $thisfile_ape_items_current['data'][0]); + $thisfile_replaygain['mp3gain']['undo_left'] = intval($mp3gain_undo_left); + $thisfile_replaygain['mp3gain']['undo_right'] = intval($mp3gain_undo_right); + $thisfile_replaygain['mp3gain']['undo_wrap'] = (($mp3gain_undo_wrap == 'Y') ? true : false); + } else { + $this->warning('MP3gainUndo value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"'); + } + break; + + case 'mp3gain_minmax': + if (preg_match('#^[0-9]{3},[0-9]{3}$#', $thisfile_ape_items_current['data'][0])) { + list($mp3gain_globalgain_min, $mp3gain_globalgain_max) = explode(',', $thisfile_ape_items_current['data'][0]); + $thisfile_replaygain['mp3gain']['globalgain_track_min'] = intval($mp3gain_globalgain_min); + $thisfile_replaygain['mp3gain']['globalgain_track_max'] = intval($mp3gain_globalgain_max); + } else { + $this->warning('MP3gainMinMax value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"'); + } + break; + + case 'mp3gain_album_minmax': + if (preg_match('#^[0-9]{3},[0-9]{3}$#', $thisfile_ape_items_current['data'][0])) { + list($mp3gain_globalgain_album_min, $mp3gain_globalgain_album_max) = explode(',', $thisfile_ape_items_current['data'][0]); + $thisfile_replaygain['mp3gain']['globalgain_album_min'] = intval($mp3gain_globalgain_album_min); + $thisfile_replaygain['mp3gain']['globalgain_album_max'] = intval($mp3gain_globalgain_album_max); + } else { + $this->warning('MP3gainAlbumMinMax value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"'); + } + break; + + case 'tracknumber': + if (is_array($thisfile_ape_items_current['data'])) { + foreach ($thisfile_ape_items_current['data'] as $comment) { + $thisfile_ape['comments']['track_number'][] = $comment; + } + } + break; + + case 'cover art (artist)': + case 'cover art (back)': + case 'cover art (band logo)': + case 'cover art (band)': + case 'cover art (colored fish)': + case 'cover art (composer)': + case 'cover art (conductor)': + case 'cover art (front)': + case 'cover art (icon)': + case 'cover art (illustration)': + case 'cover art (lead)': + case 'cover art (leaflet)': + case 'cover art (lyricist)': + case 'cover art (media)': + case 'cover art (movie scene)': + case 'cover art (other icon)': + case 'cover art (other)': + case 'cover art (performance)': + case 'cover art (publisher logo)': + case 'cover art (recording)': + case 'cover art (studio)': + // list of possible cover arts from http://taglib-sharp.sourcearchive.com/documentation/2.0.3.0-2/Ape_2Tag_8cs-source.html + if (is_array($thisfile_ape_items_current['data'])) { + $this->warning('APEtag "'.$item_key.'" should be flagged as Binary data, but was incorrectly flagged as UTF-8'); + $thisfile_ape_items_current['data'] = implode("\x00", $thisfile_ape_items_current['data']); + } + list($thisfile_ape_items_current['filename'], $thisfile_ape_items_current['data']) = explode("\x00", $thisfile_ape_items_current['data'], 2); + $thisfile_ape_items_current['data_offset'] = $thisfile_ape_items_current['offset'] + strlen($thisfile_ape_items_current['filename']."\x00"); + $thisfile_ape_items_current['data_length'] = strlen($thisfile_ape_items_current['data']); + + do { + $thisfile_ape_items_current['image_mime'] = ''; + $imageinfo = array(); + $imagechunkcheck = getid3_lib::GetDataImageSize($thisfile_ape_items_current['data'], $imageinfo); + if (($imagechunkcheck === false) || !isset($imagechunkcheck[2])) { + $this->warning('APEtag "'.$item_key.'" contains invalid image data'); + break; + } + $thisfile_ape_items_current['image_mime'] = image_type_to_mime_type($imagechunkcheck[2]); + + if ($this->inline_attachments === false) { + // skip entirely + unset($thisfile_ape_items_current['data']); + break; + } + if ($this->inline_attachments === true) { + // great + } elseif (is_int($this->inline_attachments)) { + if ($this->inline_attachments < $thisfile_ape_items_current['data_length']) { + // too big, skip + $this->warning('attachment at '.$thisfile_ape_items_current['offset'].' is too large to process inline ('.number_format($thisfile_ape_items_current['data_length']).' bytes)'); + unset($thisfile_ape_items_current['data']); + break; + } + } elseif (is_string($this->inline_attachments)) { + $this->inline_attachments = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->inline_attachments), DIRECTORY_SEPARATOR); + if (!is_dir($this->inline_attachments) || !getID3::is_writable($this->inline_attachments)) { + // cannot write, skip + $this->warning('attachment at '.$thisfile_ape_items_current['offset'].' cannot be saved to "'.$this->inline_attachments.'" (not writable)'); + unset($thisfile_ape_items_current['data']); + break; + } + } + // if we get this far, must be OK + if (is_string($this->inline_attachments)) { + $destination_filename = $this->inline_attachments.DIRECTORY_SEPARATOR.md5($info['filenamepath']).'_'.$thisfile_ape_items_current['data_offset']; + if (!file_exists($destination_filename) || getID3::is_writable($destination_filename)) { + file_put_contents($destination_filename, $thisfile_ape_items_current['data']); + } else { + $this->warning('attachment at '.$thisfile_ape_items_current['offset'].' cannot be saved to "'.$destination_filename.'" (not writable)'); + } + $thisfile_ape_items_current['data_filename'] = $destination_filename; + unset($thisfile_ape_items_current['data']); + } else { + if (!isset($info['ape']['comments']['picture'])) { + $info['ape']['comments']['picture'] = array(); + } + $comments_picture_data = array(); + foreach (array('data', 'image_mime', 'image_width', 'image_height', 'imagetype', 'picturetype', 'description', 'datalength') as $picture_key) { + if (isset($thisfile_ape_items_current[$picture_key])) { + $comments_picture_data[$picture_key] = $thisfile_ape_items_current[$picture_key]; + } + } + $info['ape']['comments']['picture'][] = $comments_picture_data; + unset($comments_picture_data); + } + } while (false); + break; + + default: + if (is_array($thisfile_ape_items_current['data'])) { + foreach ($thisfile_ape_items_current['data'] as $comment) { + $thisfile_ape['comments'][strtolower($item_key)][] = $comment; + } + } + break; + } + + } + if (empty($thisfile_replaygain)) { + unset($info['replay_gain']); + } + return true; + } + + /** + * @param string $APEheaderFooterData + * + * @return array|false + */ + public function parseAPEheaderFooter($APEheaderFooterData) { + // http://www.uni-jena.de/~pfk/mpp/sv8/apeheader.html + + // shortcut + $headerfooterinfo['raw'] = array(); + $headerfooterinfo_raw = &$headerfooterinfo['raw']; + + $headerfooterinfo_raw['footer_tag'] = substr($APEheaderFooterData, 0, 8); + if ($headerfooterinfo_raw['footer_tag'] != 'APETAGEX') { + return false; + } + $headerfooterinfo_raw['version'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 8, 4)); + $headerfooterinfo_raw['tagsize'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 12, 4)); + $headerfooterinfo_raw['tag_items'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 16, 4)); + $headerfooterinfo_raw['global_flags'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 20, 4)); + $headerfooterinfo_raw['reserved'] = substr($APEheaderFooterData, 24, 8); + + $headerfooterinfo['tag_version'] = $headerfooterinfo_raw['version'] / 1000; + if ($headerfooterinfo['tag_version'] >= 2) { + $headerfooterinfo['flags'] = $this->parseAPEtagFlags($headerfooterinfo_raw['global_flags']); + } + return $headerfooterinfo; + } + + /** + * @param int $rawflagint + * + * @return array + */ + public function parseAPEtagFlags($rawflagint) { + // "Note: APE Tags 1.0 do not use any of the APE Tag flags. + // All are set to zero on creation and ignored on reading." + // http://wiki.hydrogenaud.io/index.php?title=Ape_Tags_Flags + $flags['header'] = (bool) ($rawflagint & 0x80000000); + $flags['footer'] = (bool) ($rawflagint & 0x40000000); + $flags['this_is_header'] = (bool) ($rawflagint & 0x20000000); + $flags['item_contents_raw'] = ($rawflagint & 0x00000006) >> 1; + $flags['read_only'] = (bool) ($rawflagint & 0x00000001); + + $flags['item_contents'] = $this->APEcontentTypeFlagLookup($flags['item_contents_raw']); + + return $flags; + } + + /** + * @param int $contenttypeid + * + * @return string + */ + public function APEcontentTypeFlagLookup($contenttypeid) { + static $APEcontentTypeFlagLookup = array( + 0 => 'utf-8', + 1 => 'binary', + 2 => 'external', + 3 => 'reserved' + ); + return (isset($APEcontentTypeFlagLookup[$contenttypeid]) ? $APEcontentTypeFlagLookup[$contenttypeid] : 'invalid'); + } + + /** + * @param string $itemkey + * + * @return bool + */ + public function APEtagItemIsUTF8Lookup($itemkey) { + static $APEtagItemIsUTF8Lookup = array( + 'title', + 'subtitle', + 'artist', + 'album', + 'debut album', + 'publisher', + 'conductor', + 'track', + 'composer', + 'comment', + 'copyright', + 'publicationright', + 'file', + 'year', + 'record date', + 'record location', + 'genre', + 'media', + 'related', + 'isrc', + 'abstract', + 'language', + 'bibliography' + ); + return in_array(strtolower($itemkey), $APEtagItemIsUTF8Lookup); + } + +} diff --git a/projects/tests/tests/performance/ID3/module.tag.id3v1.php b/projects/tests/tests/performance/ID3/module.tag.id3v1.php new file mode 100644 index 00000000..16dcf253 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.tag.id3v1.php @@ -0,0 +1,428 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +// // +// module.tag.id3v1.php // +// module for analyzing ID3v1 tags // +// dependencies: NONE // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} + +class getid3_id3v1 extends getid3_handler +{ + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + if (!getid3_lib::intValueSupported($info['filesize'])) { + $this->warning('Unable to check for ID3v1 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); + return false; + } + + $this->fseek(-256, SEEK_END); + $preid3v1 = $this->fread(128); + $id3v1tag = $this->fread(128); + + if (substr($id3v1tag, 0, 3) == 'TAG') { + + $info['avdataend'] = $info['filesize'] - 128; + + $ParsedID3v1['title'] = $this->cutfield(substr($id3v1tag, 3, 30)); + $ParsedID3v1['artist'] = $this->cutfield(substr($id3v1tag, 33, 30)); + $ParsedID3v1['album'] = $this->cutfield(substr($id3v1tag, 63, 30)); + $ParsedID3v1['year'] = $this->cutfield(substr($id3v1tag, 93, 4)); + $ParsedID3v1['comment'] = substr($id3v1tag, 97, 30); // can't remove nulls yet, track detection depends on them + $ParsedID3v1['genreid'] = ord(substr($id3v1tag, 127, 1)); + + // If second-last byte of comment field is null and last byte of comment field is non-null + // then this is ID3v1.1 and the comment field is 28 bytes long and the 30th byte is the track number + if (($id3v1tag[125] === "\x00") && ($id3v1tag[126] !== "\x00")) { + $ParsedID3v1['track_number'] = ord(substr($ParsedID3v1['comment'], 29, 1)); + $ParsedID3v1['comment'] = substr($ParsedID3v1['comment'], 0, 28); + } + $ParsedID3v1['comment'] = $this->cutfield($ParsedID3v1['comment']); + + $ParsedID3v1['genre'] = $this->LookupGenreName($ParsedID3v1['genreid']); + if (!empty($ParsedID3v1['genre'])) { + unset($ParsedID3v1['genreid']); + } + if (isset($ParsedID3v1['genre']) && (empty($ParsedID3v1['genre']) || ($ParsedID3v1['genre'] == 'Unknown'))) { + unset($ParsedID3v1['genre']); + } + + foreach ($ParsedID3v1 as $key => $value) { + $ParsedID3v1['comments'][$key][0] = $value; + } + $ID3v1encoding = $this->getid3->encoding_id3v1; + if ($this->getid3->encoding_id3v1_autodetect) { + // ID3v1 encoding detection hack START + // ID3v1 is defined as always using ISO-8859-1 encoding, but it is not uncommon to find files tagged with ID3v1 using Windows-1251 or other character sets + // Since ID3v1 has no concept of character sets there is no certain way to know we have the correct non-ISO-8859-1 character set, but we can guess + foreach ($ParsedID3v1['comments'] as $tag_key => $valuearray) { + foreach ($valuearray as $key => $value) { + if (preg_match('#^[\\x00-\\x40\\x80-\\xFF]+$#', $value) && !ctype_digit((string) $value)) { // check for strings with only characters above chr(128) and punctuation/numbers, but not just numeric strings (e.g. track numbers or years) + foreach (array('Windows-1251', 'KOI8-R') as $id3v1_bad_encoding) { + if (function_exists('mb_convert_encoding') && @mb_convert_encoding($value, $id3v1_bad_encoding, $id3v1_bad_encoding) === $value) { + $ID3v1encoding = $id3v1_bad_encoding; + $this->warning('ID3v1 detected as '.$id3v1_bad_encoding.' text encoding in '.$tag_key); + break 3; + } elseif (function_exists('iconv') && @iconv($id3v1_bad_encoding, $id3v1_bad_encoding, $value) === $value) { + $ID3v1encoding = $id3v1_bad_encoding; + $this->warning('ID3v1 detected as '.$id3v1_bad_encoding.' text encoding in '.$tag_key); + break 3; + } + } + } + } + } + // ID3v1 encoding detection hack END + } + + // ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces + $GoodFormatID3v1tag = $this->GenerateID3v1Tag( + $ParsedID3v1['title'], + $ParsedID3v1['artist'], + $ParsedID3v1['album'], + $ParsedID3v1['year'], + (isset($ParsedID3v1['genre']) ? $this->LookupGenreID($ParsedID3v1['genre']) : false), + $ParsedID3v1['comment'], + (!empty($ParsedID3v1['track_number']) ? $ParsedID3v1['track_number'] : '')); + $ParsedID3v1['padding_valid'] = true; + if ($id3v1tag !== $GoodFormatID3v1tag) { + $ParsedID3v1['padding_valid'] = false; + $this->warning('Some ID3v1 fields do not use NULL characters for padding'); + } + + $ParsedID3v1['tag_offset_end'] = $info['filesize']; + $ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128; + + $info['id3v1'] = $ParsedID3v1; + $info['id3v1']['encoding'] = $ID3v1encoding; + } + + if (substr($preid3v1, 0, 3) == 'TAG') { + // The way iTunes handles tags is, well, brain-damaged. + // It completely ignores v1 if ID3v2 is present. + // This goes as far as adding a new v1 tag *even if there already is one* + + // A suspected double-ID3v1 tag has been detected, but it could be that + // the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag + if (substr($preid3v1, 96, 8) == 'APETAGEX') { + // an APE tag footer was found before the last ID3v1, assume false "TAG" synch + } elseif (substr($preid3v1, 119, 6) == 'LYRICS') { + // a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch + } else { + // APE and Lyrics3 footers not found - assume double ID3v1 + $this->warning('Duplicate ID3v1 tag detected - this has been known to happen with iTunes'); + $info['avdataend'] -= 128; + } + } + + return true; + } + + /** + * @param string $str + * + * @return string + */ + public static function cutfield($str) { + return trim(substr($str, 0, strcspn($str, "\x00"))); + } + + /** + * @param bool $allowSCMPXextended + * + * @return string[] + */ + public static function ArrayOfGenres($allowSCMPXextended=false) { + static $GenreLookup = array( + 0 => 'Blues', + 1 => 'Classic Rock', + 2 => 'Country', + 3 => 'Dance', + 4 => 'Disco', + 5 => 'Funk', + 6 => 'Grunge', + 7 => 'Hip-Hop', + 8 => 'Jazz', + 9 => 'Metal', + 10 => 'New Age', + 11 => 'Oldies', + 12 => 'Other', + 13 => 'Pop', + 14 => 'R&B', + 15 => 'Rap', + 16 => 'Reggae', + 17 => 'Rock', + 18 => 'Techno', + 19 => 'Industrial', + 20 => 'Alternative', + 21 => 'Ska', + 22 => 'Death Metal', + 23 => 'Pranks', + 24 => 'Soundtrack', + 25 => 'Euro-Techno', + 26 => 'Ambient', + 27 => 'Trip-Hop', + 28 => 'Vocal', + 29 => 'Jazz+Funk', + 30 => 'Fusion', + 31 => 'Trance', + 32 => 'Classical', + 33 => 'Instrumental', + 34 => 'Acid', + 35 => 'House', + 36 => 'Game', + 37 => 'Sound Clip', + 38 => 'Gospel', + 39 => 'Noise', + 40 => 'Alt. Rock', + 41 => 'Bass', + 42 => 'Soul', + 43 => 'Punk', + 44 => 'Space', + 45 => 'Meditative', + 46 => 'Instrumental Pop', + 47 => 'Instrumental Rock', + 48 => 'Ethnic', + 49 => 'Gothic', + 50 => 'Darkwave', + 51 => 'Techno-Industrial', + 52 => 'Electronic', + 53 => 'Pop-Folk', + 54 => 'Eurodance', + 55 => 'Dream', + 56 => 'Southern Rock', + 57 => 'Comedy', + 58 => 'Cult', + 59 => 'Gangsta Rap', + 60 => 'Top 40', + 61 => 'Christian Rap', + 62 => 'Pop/Funk', + 63 => 'Jungle', + 64 => 'Native American', + 65 => 'Cabaret', + 66 => 'New Wave', + 67 => 'Psychedelic', + 68 => 'Rave', + 69 => 'Showtunes', + 70 => 'Trailer', + 71 => 'Lo-Fi', + 72 => 'Tribal', + 73 => 'Acid Punk', + 74 => 'Acid Jazz', + 75 => 'Polka', + 76 => 'Retro', + 77 => 'Musical', + 78 => 'Rock & Roll', + 79 => 'Hard Rock', + 80 => 'Folk', + 81 => 'Folk/Rock', + 82 => 'National Folk', + 83 => 'Swing', + 84 => 'Fast-Fusion', + 85 => 'Bebob', + 86 => 'Latin', + 87 => 'Revival', + 88 => 'Celtic', + 89 => 'Bluegrass', + 90 => 'Avantgarde', + 91 => 'Gothic Rock', + 92 => 'Progressive Rock', + 93 => 'Psychedelic Rock', + 94 => 'Symphonic Rock', + 95 => 'Slow Rock', + 96 => 'Big Band', + 97 => 'Chorus', + 98 => 'Easy Listening', + 99 => 'Acoustic', + 100 => 'Humour', + 101 => 'Speech', + 102 => 'Chanson', + 103 => 'Opera', + 104 => 'Chamber Music', + 105 => 'Sonata', + 106 => 'Symphony', + 107 => 'Booty Bass', + 108 => 'Primus', + 109 => 'Porn Groove', + 110 => 'Satire', + 111 => 'Slow Jam', + 112 => 'Club', + 113 => 'Tango', + 114 => 'Samba', + 115 => 'Folklore', + 116 => 'Ballad', + 117 => 'Power Ballad', + 118 => 'Rhythmic Soul', + 119 => 'Freestyle', + 120 => 'Duet', + 121 => 'Punk Rock', + 122 => 'Drum Solo', + 123 => 'A Cappella', + 124 => 'Euro-House', + 125 => 'Dance Hall', + 126 => 'Goa', + 127 => 'Drum & Bass', + 128 => 'Club-House', + 129 => 'Hardcore', + 130 => 'Terror', + 131 => 'Indie', + 132 => 'BritPop', + 133 => 'Negerpunk', + 134 => 'Polsk Punk', + 135 => 'Beat', + 136 => 'Christian Gangsta Rap', + 137 => 'Heavy Metal', + 138 => 'Black Metal', + 139 => 'Crossover', + 140 => 'Contemporary Christian', + 141 => 'Christian Rock', + 142 => 'Merengue', + 143 => 'Salsa', + 144 => 'Thrash Metal', + 145 => 'Anime', + 146 => 'JPop', + 147 => 'Synthpop', + + 255 => 'Unknown', + + 'CR' => 'Cover', + 'RX' => 'Remix' + ); + + static $GenreLookupSCMPX = array(); + if ($allowSCMPXextended && empty($GenreLookupSCMPX)) { + $GenreLookupSCMPX = $GenreLookup; + // http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended + // Extended ID3v1 genres invented by SCMPX + // Note that 255 "Japanese Anime" conflicts with standard "Unknown" + $GenreLookupSCMPX[240] = 'Sacred'; + $GenreLookupSCMPX[241] = 'Northern Europe'; + $GenreLookupSCMPX[242] = 'Irish & Scottish'; + $GenreLookupSCMPX[243] = 'Scotland'; + $GenreLookupSCMPX[244] = 'Ethnic Europe'; + $GenreLookupSCMPX[245] = 'Enka'; + $GenreLookupSCMPX[246] = 'Children\'s Song'; + $GenreLookupSCMPX[247] = 'Japanese Sky'; + $GenreLookupSCMPX[248] = 'Japanese Heavy Rock'; + $GenreLookupSCMPX[249] = 'Japanese Doom Rock'; + $GenreLookupSCMPX[250] = 'Japanese J-POP'; + $GenreLookupSCMPX[251] = 'Japanese Seiyu'; + $GenreLookupSCMPX[252] = 'Japanese Ambient Techno'; + $GenreLookupSCMPX[253] = 'Japanese Moemoe'; + $GenreLookupSCMPX[254] = 'Japanese Tokusatsu'; + //$GenreLookupSCMPX[255] = 'Japanese Anime'; + } + + return ($allowSCMPXextended ? $GenreLookupSCMPX : $GenreLookup); + } + + /** + * @param string $genreid + * @param bool $allowSCMPXextended + * + * @return string|false + */ + public static function LookupGenreName($genreid, $allowSCMPXextended=true) { + switch ($genreid) { + case 'RX': + case 'CR': + break; + default: + if (!is_numeric($genreid)) { + return false; + } + $genreid = intval($genreid); // to handle 3 or '3' or '03' + break; + } + $GenreLookup = self::ArrayOfGenres($allowSCMPXextended); + return (isset($GenreLookup[$genreid]) ? $GenreLookup[$genreid] : false); + } + + /** + * @param string $genre + * @param bool $allowSCMPXextended + * + * @return string|false + */ + public static function LookupGenreID($genre, $allowSCMPXextended=false) { + $GenreLookup = self::ArrayOfGenres($allowSCMPXextended); + $LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre)); + foreach ($GenreLookup as $key => $value) { + if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) { + return $key; + } + } + return false; + } + + /** + * @param string $OriginalGenre + * + * @return string|false + */ + public static function StandardiseID3v1GenreName($OriginalGenre) { + if (($GenreID = self::LookupGenreID($OriginalGenre)) !== false) { + return self::LookupGenreName($GenreID); + } + return $OriginalGenre; + } + + /** + * @param string $title + * @param string $artist + * @param string $album + * @param string $year + * @param int $genreid + * @param string $comment + * @param int|string $track + * + * @return string + */ + public static function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') { + $ID3v1Tag = 'TAG'; + $ID3v1Tag .= str_pad(trim(substr($title, 0, 30)), 30, "\x00", STR_PAD_RIGHT); + $ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT); + $ID3v1Tag .= str_pad(trim(substr($album, 0, 30)), 30, "\x00", STR_PAD_RIGHT); + $ID3v1Tag .= str_pad(trim(substr($year, 0, 4)), 4, "\x00", STR_PAD_LEFT); + if (!empty($track) && ($track > 0) && ($track <= 255)) { + $ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT); + $ID3v1Tag .= "\x00"; + if (gettype($track) == 'string') { + $track = (int) $track; + } + $ID3v1Tag .= chr($track); + } else { + $ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT); + } + if (($genreid < 0) || ($genreid > 147)) { + $genreid = 255; // 'unknown' genre + } + switch (gettype($genreid)) { + case 'string': + case 'integer': + $ID3v1Tag .= chr(intval($genreid)); + break; + default: + $ID3v1Tag .= chr(255); // 'unknown' genre + break; + } + + return $ID3v1Tag; + } + +} diff --git a/projects/tests/tests/performance/ID3/module.tag.id3v2.php b/projects/tests/tests/performance/ID3/module.tag.id3v2.php new file mode 100644 index 00000000..85dd7a14 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.tag.id3v2.php @@ -0,0 +1,3899 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +/// // +// module.tag.id3v2.php // +// module for analyzing ID3v2 tags // +// dependencies: module.tag.id3v1.php // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} +getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v1.php', __FILE__, true); + +class getid3_id3v2 extends getid3_handler +{ + public $StartingOffset = 0; + + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + // Overall tag structure: + // +-----------------------------+ + // | Header (10 bytes) | + // +-----------------------------+ + // | Extended Header | + // | (variable length, OPTIONAL) | + // +-----------------------------+ + // | Frames (variable length) | + // +-----------------------------+ + // | Padding | + // | (variable length, OPTIONAL) | + // +-----------------------------+ + // | Footer (10 bytes, OPTIONAL) | + // +-----------------------------+ + + // Header + // ID3v2/file identifier "ID3" + // ID3v2 version $04 00 + // ID3v2 flags (%ab000000 in v2.2, %abc00000 in v2.3, %abcd0000 in v2.4.x) + // ID3v2 size 4 * %0xxxxxxx + + + // shortcuts + $info['id3v2']['header'] = true; + $thisfile_id3v2 = &$info['id3v2']; + $thisfile_id3v2['flags'] = array(); + $thisfile_id3v2_flags = &$thisfile_id3v2['flags']; + + + $this->fseek($this->StartingOffset); + $header = $this->fread(10); + if (substr($header, 0, 3) == 'ID3' && strlen($header) == 10) { + + $thisfile_id3v2['majorversion'] = ord($header[3]); + $thisfile_id3v2['minorversion'] = ord($header[4]); + + // shortcut + $id3v2_majorversion = &$thisfile_id3v2['majorversion']; + + } else { + + unset($info['id3v2']); + return false; + + } + + if ($id3v2_majorversion > 4) { // this script probably won't correctly parse ID3v2.5.x and above (if it ever exists) + + $this->error('this script only parses up to ID3v2.4.x - this tag is ID3v2.'.$id3v2_majorversion.'.'.$thisfile_id3v2['minorversion']); + return false; + + } + + $id3_flags = ord($header[5]); + switch ($id3v2_majorversion) { + case 2: + // %ab000000 in v2.2 + $thisfile_id3v2_flags['unsynch'] = (bool) ($id3_flags & 0x80); // a - Unsynchronisation + $thisfile_id3v2_flags['compression'] = (bool) ($id3_flags & 0x40); // b - Compression + break; + + case 3: + // %abc00000 in v2.3 + $thisfile_id3v2_flags['unsynch'] = (bool) ($id3_flags & 0x80); // a - Unsynchronisation + $thisfile_id3v2_flags['exthead'] = (bool) ($id3_flags & 0x40); // b - Extended header + $thisfile_id3v2_flags['experim'] = (bool) ($id3_flags & 0x20); // c - Experimental indicator + break; + + case 4: + // %abcd0000 in v2.4 + $thisfile_id3v2_flags['unsynch'] = (bool) ($id3_flags & 0x80); // a - Unsynchronisation + $thisfile_id3v2_flags['exthead'] = (bool) ($id3_flags & 0x40); // b - Extended header + $thisfile_id3v2_flags['experim'] = (bool) ($id3_flags & 0x20); // c - Experimental indicator + $thisfile_id3v2_flags['isfooter'] = (bool) ($id3_flags & 0x10); // d - Footer present + break; + } + + $thisfile_id3v2['headerlength'] = getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length + + $thisfile_id3v2['tag_offset_start'] = $this->StartingOffset; + $thisfile_id3v2['tag_offset_end'] = $thisfile_id3v2['tag_offset_start'] + $thisfile_id3v2['headerlength']; + + + + // create 'encoding' key - used by getid3::HandleAllTags() + // in ID3v2 every field can have it's own encoding type + // so force everything to UTF-8 so it can be handled consistantly + $thisfile_id3v2['encoding'] = 'UTF-8'; + + + // Frames + + // All ID3v2 frames consists of one frame header followed by one or more + // fields containing the actual information. The header is always 10 + // bytes and laid out as follows: + // + // Frame ID $xx xx xx xx (four characters) + // Size 4 * %0xxxxxxx + // Flags $xx xx + + $sizeofframes = $thisfile_id3v2['headerlength'] - 10; // not including 10-byte initial header + if (!empty($thisfile_id3v2['exthead']['length'])) { + $sizeofframes -= ($thisfile_id3v2['exthead']['length'] + 4); + } + if (!empty($thisfile_id3v2_flags['isfooter'])) { + $sizeofframes -= 10; // footer takes last 10 bytes of ID3v2 header, after frame data, before audio + } + if ($sizeofframes > 0) { + + $framedata = $this->fread($sizeofframes); // read all frames from file into $framedata variable + + // if entire frame data is unsynched, de-unsynch it now (ID3v2.3.x) + if (!empty($thisfile_id3v2_flags['unsynch']) && ($id3v2_majorversion <= 3)) { + $framedata = $this->DeUnsynchronise($framedata); + } + // [in ID3v2.4.0] Unsynchronisation [S:6.1] is done on frame level, instead + // of on tag level, making it easier to skip frames, increasing the streamability + // of the tag. The unsynchronisation flag in the header [S:3.1] indicates that + // there exists an unsynchronised frame, while the new unsynchronisation flag in + // the frame header [S:4.1.2] indicates unsynchronisation. + + + //$framedataoffset = 10 + ($thisfile_id3v2['exthead']['length'] ? $thisfile_id3v2['exthead']['length'] + 4 : 0); // how many bytes into the stream - start from after the 10-byte header (and extended header length+4, if present) + $framedataoffset = 10; // how many bytes into the stream - start from after the 10-byte header + + + // Extended Header + if (!empty($thisfile_id3v2_flags['exthead'])) { + $extended_header_offset = 0; + + if ($id3v2_majorversion == 3) { + + // v2.3 definition: + //Extended header size $xx xx xx xx // 32-bit integer + //Extended Flags $xx xx + // %x0000000 %00000000 // v2.3 + // x - CRC data present + //Size of padding $xx xx xx xx + + $thisfile_id3v2['exthead']['length'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 4), 0); + $extended_header_offset += 4; + + $thisfile_id3v2['exthead']['flag_bytes'] = 2; + $thisfile_id3v2['exthead']['flag_raw'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, $thisfile_id3v2['exthead']['flag_bytes'])); + $extended_header_offset += $thisfile_id3v2['exthead']['flag_bytes']; + + $thisfile_id3v2['exthead']['flags']['crc'] = (bool) ($thisfile_id3v2['exthead']['flag_raw'] & 0x8000); + + $thisfile_id3v2['exthead']['padding_size'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 4)); + $extended_header_offset += 4; + + if ($thisfile_id3v2['exthead']['flags']['crc']) { + $thisfile_id3v2['exthead']['flag_data']['crc'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 4)); + $extended_header_offset += 4; + } + $extended_header_offset += $thisfile_id3v2['exthead']['padding_size']; + + } elseif ($id3v2_majorversion == 4) { + + // v2.4 definition: + //Extended header size 4 * %0xxxxxxx // 28-bit synchsafe integer + //Number of flag bytes $01 + //Extended Flags $xx + // %0bcd0000 // v2.4 + // b - Tag is an update + // Flag data length $00 + // c - CRC data present + // Flag data length $05 + // Total frame CRC 5 * %0xxxxxxx + // d - Tag restrictions + // Flag data length $01 + + $thisfile_id3v2['exthead']['length'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 4), true); + $extended_header_offset += 4; + + $thisfile_id3v2['exthead']['flag_bytes'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 1)); // should always be 1 + $extended_header_offset += 1; + + $thisfile_id3v2['exthead']['flag_raw'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, $thisfile_id3v2['exthead']['flag_bytes'])); + $extended_header_offset += $thisfile_id3v2['exthead']['flag_bytes']; + + $thisfile_id3v2['exthead']['flags']['update'] = (bool) ($thisfile_id3v2['exthead']['flag_raw'] & 0x40); + $thisfile_id3v2['exthead']['flags']['crc'] = (bool) ($thisfile_id3v2['exthead']['flag_raw'] & 0x20); + $thisfile_id3v2['exthead']['flags']['restrictions'] = (bool) ($thisfile_id3v2['exthead']['flag_raw'] & 0x10); + + if ($thisfile_id3v2['exthead']['flags']['update']) { + $ext_header_chunk_length = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 1)); // should be 0 + $extended_header_offset += 1; + } + + if ($thisfile_id3v2['exthead']['flags']['crc']) { + $ext_header_chunk_length = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 1)); // should be 5 + $extended_header_offset += 1; + $thisfile_id3v2['exthead']['flag_data']['crc'] = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, $ext_header_chunk_length), true, false); + $extended_header_offset += $ext_header_chunk_length; + } + + if ($thisfile_id3v2['exthead']['flags']['restrictions']) { + $ext_header_chunk_length = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 1)); // should be 1 + $extended_header_offset += 1; + + // %ppqrrstt + $restrictions_raw = getid3_lib::BigEndian2Int(substr($framedata, $extended_header_offset, 1)); + $extended_header_offset += 1; + $thisfile_id3v2['exthead']['flags']['restrictions']['tagsize'] = ($restrictions_raw & 0xC0) >> 6; // p - Tag size restrictions + $thisfile_id3v2['exthead']['flags']['restrictions']['textenc'] = ($restrictions_raw & 0x20) >> 5; // q - Text encoding restrictions + $thisfile_id3v2['exthead']['flags']['restrictions']['textsize'] = ($restrictions_raw & 0x18) >> 3; // r - Text fields size restrictions + $thisfile_id3v2['exthead']['flags']['restrictions']['imgenc'] = ($restrictions_raw & 0x04) >> 2; // s - Image encoding restrictions + $thisfile_id3v2['exthead']['flags']['restrictions']['imgsize'] = ($restrictions_raw & 0x03) >> 0; // t - Image size restrictions + + $thisfile_id3v2['exthead']['flags']['restrictions_text']['tagsize'] = $this->LookupExtendedHeaderRestrictionsTagSizeLimits($thisfile_id3v2['exthead']['flags']['restrictions']['tagsize']); + $thisfile_id3v2['exthead']['flags']['restrictions_text']['textenc'] = $this->LookupExtendedHeaderRestrictionsTextEncodings($thisfile_id3v2['exthead']['flags']['restrictions']['textenc']); + $thisfile_id3v2['exthead']['flags']['restrictions_text']['textsize'] = $this->LookupExtendedHeaderRestrictionsTextFieldSize($thisfile_id3v2['exthead']['flags']['restrictions']['textsize']); + $thisfile_id3v2['exthead']['flags']['restrictions_text']['imgenc'] = $this->LookupExtendedHeaderRestrictionsImageEncoding($thisfile_id3v2['exthead']['flags']['restrictions']['imgenc']); + $thisfile_id3v2['exthead']['flags']['restrictions_text']['imgsize'] = $this->LookupExtendedHeaderRestrictionsImageSizeSize($thisfile_id3v2['exthead']['flags']['restrictions']['imgsize']); + } + + if ($thisfile_id3v2['exthead']['length'] != $extended_header_offset) { + $this->warning('ID3v2.4 extended header length mismatch (expecting '.intval($thisfile_id3v2['exthead']['length']).', found '.intval($extended_header_offset).')'); + } + } + + $framedataoffset += $extended_header_offset; + $framedata = substr($framedata, $extended_header_offset); + } // end extended header + + + while (isset($framedata) && (strlen($framedata) > 0)) { // cycle through until no more frame data is left to parse + if (strlen($framedata) <= $this->ID3v2HeaderLength($id3v2_majorversion)) { + // insufficient room left in ID3v2 header for actual data - must be padding + $thisfile_id3v2['padding']['start'] = $framedataoffset; + $thisfile_id3v2['padding']['length'] = strlen($framedata); + $thisfile_id3v2['padding']['valid'] = true; + for ($i = 0; $i < $thisfile_id3v2['padding']['length']; $i++) { + if ($framedata[$i] != "\x00") { + $thisfile_id3v2['padding']['valid'] = false; + $thisfile_id3v2['padding']['errorpos'] = $thisfile_id3v2['padding']['start'] + $i; + $this->warning('Invalid ID3v2 padding found at offset '.$thisfile_id3v2['padding']['errorpos'].' (the remaining '.($thisfile_id3v2['padding']['length'] - $i).' bytes are considered invalid)'); + break; + } + } + break; // skip rest of ID3v2 header + } + $frame_header = null; + $frame_name = null; + $frame_size = null; + $frame_flags = null; + if ($id3v2_majorversion == 2) { + // Frame ID $xx xx xx (three characters) + // Size $xx xx xx (24-bit integer) + // Flags $xx xx + + $frame_header = substr($framedata, 0, 6); // take next 6 bytes for header + $framedata = substr($framedata, 6); // and leave the rest in $framedata + $frame_name = substr($frame_header, 0, 3); + $frame_size = getid3_lib::BigEndian2Int(substr($frame_header, 3, 3), 0); + $frame_flags = 0; // not used for anything in ID3v2.2, just set to avoid E_NOTICEs + + } elseif ($id3v2_majorversion > 2) { + + // Frame ID $xx xx xx xx (four characters) + // Size $xx xx xx xx (32-bit integer in v2.3, 28-bit synchsafe in v2.4+) + // Flags $xx xx + + $frame_header = substr($framedata, 0, 10); // take next 10 bytes for header + $framedata = substr($framedata, 10); // and leave the rest in $framedata + + $frame_name = substr($frame_header, 0, 4); + if ($id3v2_majorversion == 3) { + $frame_size = getid3_lib::BigEndian2Int(substr($frame_header, 4, 4), 0); // 32-bit integer + } else { // ID3v2.4+ + $frame_size = getid3_lib::BigEndian2Int(substr($frame_header, 4, 4), 1); // 32-bit synchsafe integer (28-bit value) + } + + if ($frame_size < (strlen($framedata) + 4)) { + $nextFrameID = substr($framedata, $frame_size, 4); + if ($this->IsValidID3v2FrameName($nextFrameID, $id3v2_majorversion)) { + // next frame is OK + } elseif (($frame_name == "\x00".'MP3') || ($frame_name == "\x00\x00".'MP') || ($frame_name == ' MP3') || ($frame_name == 'MP3e')) { + // MP3ext known broken frames - "ok" for the purposes of this test + } elseif (($id3v2_majorversion == 4) && ($this->IsValidID3v2FrameName(substr($framedata, getid3_lib::BigEndian2Int(substr($frame_header, 4, 4), 0), 4), 3))) { + $this->warning('ID3v2 tag written as ID3v2.4, but with non-synchsafe integers (ID3v2.3 style). Older versions of (Helium2; iTunes) are known culprits of this. Tag has been parsed as ID3v2.3'); + $id3v2_majorversion = 3; + $frame_size = getid3_lib::BigEndian2Int(substr($frame_header, 4, 4), 0); // 32-bit integer + } + } + + + $frame_flags = getid3_lib::BigEndian2Int(substr($frame_header, 8, 2)); + } + + if ((($id3v2_majorversion == 2) && ($frame_name == "\x00\x00\x00")) || ($frame_name == "\x00\x00\x00\x00")) { + // padding encountered + + $thisfile_id3v2['padding']['start'] = $framedataoffset; + $thisfile_id3v2['padding']['length'] = strlen($frame_header) + strlen($framedata); + $thisfile_id3v2['padding']['valid'] = true; + + $len = strlen($framedata); + for ($i = 0; $i < $len; $i++) { + if ($framedata[$i] != "\x00") { + $thisfile_id3v2['padding']['valid'] = false; + $thisfile_id3v2['padding']['errorpos'] = $thisfile_id3v2['padding']['start'] + $i; + $this->warning('Invalid ID3v2 padding found at offset '.$thisfile_id3v2['padding']['errorpos'].' (the remaining '.($thisfile_id3v2['padding']['length'] - $i).' bytes are considered invalid)'); + break; + } + } + break; // skip rest of ID3v2 header + } + + if ($iTunesBrokenFrameNameFixed = self::ID3v22iTunesBrokenFrameName($frame_name)) { + $this->warning('error parsing "'.$frame_name.'" ('.$framedataoffset.' bytes into the ID3v2.'.$id3v2_majorversion.' tag). (ERROR: IsValidID3v2FrameName("'.str_replace("\x00", ' ', $frame_name).'", '.$id3v2_majorversion.'))). [Note: this particular error has been known to happen with tags edited by iTunes (versions "X v2.0.3", "v3.0.1", "v7.0.0.70" are known-guilty, probably others too)]. Translated frame name from "'.str_replace("\x00", ' ', $frame_name).'" to "'.$iTunesBrokenFrameNameFixed.'" for parsing.'); + $frame_name = $iTunesBrokenFrameNameFixed; + } + if (($frame_size <= strlen($framedata)) && ($this->IsValidID3v2FrameName($frame_name, $id3v2_majorversion))) { + + unset($parsedFrame); + $parsedFrame['frame_name'] = $frame_name; + $parsedFrame['frame_flags_raw'] = $frame_flags; + $parsedFrame['data'] = substr($framedata, 0, $frame_size); + $parsedFrame['datalength'] = getid3_lib::CastAsInt($frame_size); + $parsedFrame['dataoffset'] = $framedataoffset; + + $this->ParseID3v2Frame($parsedFrame); + $thisfile_id3v2[$frame_name][] = $parsedFrame; + + $framedata = substr($framedata, $frame_size); + + } else { // invalid frame length or FrameID + + if ($frame_size <= strlen($framedata)) { + + if ($this->IsValidID3v2FrameName(substr($framedata, $frame_size, 4), $id3v2_majorversion)) { + + // next frame is valid, just skip the current frame + $framedata = substr($framedata, $frame_size); + $this->warning('Next ID3v2 frame is valid, skipping current frame.'); + + } else { + + // next frame is invalid too, abort processing + //unset($framedata); + $framedata = null; + $this->error('Next ID3v2 frame is also invalid, aborting processing.'); + + } + + } elseif ($frame_size == strlen($framedata)) { + + // this is the last frame, just skip + $this->warning('This was the last ID3v2 frame.'); + + } else { + + // next frame is invalid too, abort processing + //unset($framedata); + $framedata = null; + $this->warning('Invalid ID3v2 frame size, aborting.'); + + } + if (!$this->IsValidID3v2FrameName($frame_name, $id3v2_majorversion)) { + + switch ($frame_name) { + case "\x00\x00".'MP': + case "\x00".'MP3': + case ' MP3': + case 'MP3e': + case "\x00".'MP': + case ' MP': + case 'MP3': + $this->warning('error parsing "'.$frame_name.'" ('.$framedataoffset.' bytes into the ID3v2.'.$id3v2_majorversion.' tag). (ERROR: !IsValidID3v2FrameName("'.str_replace("\x00", ' ', $frame_name).'", '.$id3v2_majorversion.'))). [Note: this particular error has been known to happen with tags edited by "MP3ext (www.mutschler.de/mp3ext/)"]'); + break; + + default: + $this->warning('error parsing "'.$frame_name.'" ('.$framedataoffset.' bytes into the ID3v2.'.$id3v2_majorversion.' tag). (ERROR: !IsValidID3v2FrameName("'.str_replace("\x00", ' ', $frame_name).'", '.$id3v2_majorversion.'))).'); + break; + } + + } elseif (!isset($framedata) || ($frame_size > strlen($framedata))) { + + $this->error('error parsing "'.$frame_name.'" ('.$framedataoffset.' bytes into the ID3v2.'.$id3v2_majorversion.' tag). (ERROR: $frame_size ('.$frame_size.') > strlen($framedata) ('.(isset($framedata) ? strlen($framedata) : 'null').')).'); + + } else { + + $this->error('error parsing "'.$frame_name.'" ('.$framedataoffset.' bytes into the ID3v2.'.$id3v2_majorversion.' tag).'); + + } + + } + $framedataoffset += ($frame_size + $this->ID3v2HeaderLength($id3v2_majorversion)); + + } + + } + + + // Footer + + // The footer is a copy of the header, but with a different identifier. + // ID3v2 identifier "3DI" + // ID3v2 version $04 00 + // ID3v2 flags %abcd0000 + // ID3v2 size 4 * %0xxxxxxx + + if (isset($thisfile_id3v2_flags['isfooter']) && $thisfile_id3v2_flags['isfooter']) { + $footer = $this->fread(10); + if (substr($footer, 0, 3) == '3DI') { + $thisfile_id3v2['footer'] = true; + $thisfile_id3v2['majorversion_footer'] = ord($footer[3]); + $thisfile_id3v2['minorversion_footer'] = ord($footer[4]); + } + if ($thisfile_id3v2['majorversion_footer'] <= 4) { + $id3_flags = ord($footer[5]); + $thisfile_id3v2_flags['unsynch_footer'] = (bool) ($id3_flags & 0x80); + $thisfile_id3v2_flags['extfoot_footer'] = (bool) ($id3_flags & 0x40); + $thisfile_id3v2_flags['experim_footer'] = (bool) ($id3_flags & 0x20); + $thisfile_id3v2_flags['isfooter_footer'] = (bool) ($id3_flags & 0x10); + + $thisfile_id3v2['footerlength'] = getid3_lib::BigEndian2Int(substr($footer, 6, 4), 1); + } + } // end footer + + if (isset($thisfile_id3v2['comments']['genre'])) { + $genres = array(); + foreach ($thisfile_id3v2['comments']['genre'] as $key => $value) { + foreach ($this->ParseID3v2GenreString($value) as $genre) { + $genres[] = $genre; + } + } + $thisfile_id3v2['comments']['genre'] = array_unique($genres); + unset($key, $value, $genres, $genre); + } + + if (isset($thisfile_id3v2['comments']['track_number'])) { + foreach ($thisfile_id3v2['comments']['track_number'] as $key => $value) { + if (strstr($value, '/')) { + list($thisfile_id3v2['comments']['track_number'][$key], $thisfile_id3v2['comments']['totaltracks'][$key]) = explode('/', $thisfile_id3v2['comments']['track_number'][$key]); + } + } + } + + if (!isset($thisfile_id3v2['comments']['year']) && !empty($thisfile_id3v2['comments']['recording_time'][0]) && preg_match('#^([0-9]{4})#', trim($thisfile_id3v2['comments']['recording_time'][0]), $matches)) { + $thisfile_id3v2['comments']['year'] = array($matches[1]); + } + + + if (!empty($thisfile_id3v2['TXXX'])) { + // MediaMonkey does this, maybe others: write a blank RGAD frame, but put replay-gain adjustment values in TXXX frames + foreach ($thisfile_id3v2['TXXX'] as $txxx_array) { + switch ($txxx_array['description']) { + case 'replaygain_track_gain': + if (empty($info['replay_gain']['track']['adjustment']) && !empty($txxx_array['data'])) { + $info['replay_gain']['track']['adjustment'] = floatval(trim(str_replace('dB', '', $txxx_array['data']))); + } + break; + case 'replaygain_track_peak': + if (empty($info['replay_gain']['track']['peak']) && !empty($txxx_array['data'])) { + $info['replay_gain']['track']['peak'] = floatval($txxx_array['data']); + } + break; + case 'replaygain_album_gain': + if (empty($info['replay_gain']['album']['adjustment']) && !empty($txxx_array['data'])) { + $info['replay_gain']['album']['adjustment'] = floatval(trim(str_replace('dB', '', $txxx_array['data']))); + } + break; + } + } + } + + + // Set avdataoffset + $info['avdataoffset'] = $thisfile_id3v2['headerlength']; + if (isset($thisfile_id3v2['footer'])) { + $info['avdataoffset'] += 10; + } + + return true; + } + + /** + * @param string $genrestring + * + * @return array + */ + public function ParseID3v2GenreString($genrestring) { + // Parse genres into arrays of genreName and genreID + // ID3v2.2.x, ID3v2.3.x: '(21)' or '(4)Eurodisco' or '(51)(39)' or '(55)((I think...)' + // ID3v2.4.x: '21' $00 'Eurodisco' $00 + $clean_genres = array(); + + // hack-fixes for some badly-written ID3v2.3 taggers, while trying not to break correctly-written tags + if (($this->getid3->info['id3v2']['majorversion'] == 3) && !preg_match('#[\x00]#', $genrestring)) { + // note: MusicBrainz Picard incorrectly stores plaintext genres separated by "/" when writing in ID3v2.3 mode, hack-fix here: + // replace / with NULL, then replace back the two ID3v1 genres that legitimately have "/" as part of the single genre name + if (strpos($genrestring, '/') !== false) { + $LegitimateSlashedGenreList = array( // https://github.com/JamesHeinrich/getID3/issues/223 + 'Pop/Funk', // ID3v1 genre #62 - https://en.wikipedia.org/wiki/ID3#standard + 'Cut-up/DJ', // Discogs - https://www.discogs.com/style/cut-up/dj + 'RnB/Swing', // Discogs - https://www.discogs.com/style/rnb/swing + 'Funk / Soul', // Discogs (note spaces) - https://www.discogs.com/genre/funk+%2F+soul + ); + $genrestring = str_replace('/', "\x00", $genrestring); + foreach ($LegitimateSlashedGenreList as $SlashedGenre) { + $genrestring = str_ireplace(str_replace('/', "\x00", $SlashedGenre), $SlashedGenre, $genrestring); + } + } + + // some other taggers separate multiple genres with semicolon, e.g. "Heavy Metal;Thrash Metal;Metal" + if (strpos($genrestring, ';') !== false) { + $genrestring = str_replace(';', "\x00", $genrestring); + } + } + + + if (strpos($genrestring, "\x00") === false) { + $genrestring = preg_replace('#\(([0-9]{1,3})\)#', '$1'."\x00", $genrestring); + } + + $genre_elements = explode("\x00", $genrestring); + foreach ($genre_elements as $element) { + $element = trim($element); + if ($element) { + if (preg_match('#^[0-9]{1,3}$#', $element)) { + $clean_genres[] = getid3_id3v1::LookupGenreName($element); + } else { + $clean_genres[] = str_replace('((', '(', $element); + } + } + } + return $clean_genres; + } + + /** + * @param array $parsedFrame + * + * @return bool + */ + public function ParseID3v2Frame(&$parsedFrame) { + + // shortcuts + $info = &$this->getid3->info; + $id3v2_majorversion = $info['id3v2']['majorversion']; + + $parsedFrame['framenamelong'] = $this->FrameNameLongLookup($parsedFrame['frame_name']); + if (empty($parsedFrame['framenamelong'])) { + unset($parsedFrame['framenamelong']); + } + $parsedFrame['framenameshort'] = $this->FrameNameShortLookup($parsedFrame['frame_name']); + if (empty($parsedFrame['framenameshort'])) { + unset($parsedFrame['framenameshort']); + } + + if ($id3v2_majorversion >= 3) { // frame flags are not part of the ID3v2.2 standard + if ($id3v2_majorversion == 3) { + // Frame Header Flags + // %abc00000 %ijk00000 + $parsedFrame['flags']['TagAlterPreservation'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x8000); // a - Tag alter preservation + $parsedFrame['flags']['FileAlterPreservation'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x4000); // b - File alter preservation + $parsedFrame['flags']['ReadOnly'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x2000); // c - Read only + $parsedFrame['flags']['compression'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0080); // i - Compression + $parsedFrame['flags']['Encryption'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0040); // j - Encryption + $parsedFrame['flags']['GroupingIdentity'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0020); // k - Grouping identity + + } elseif ($id3v2_majorversion == 4) { + // Frame Header Flags + // %0abc0000 %0h00kmnp + $parsedFrame['flags']['TagAlterPreservation'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x4000); // a - Tag alter preservation + $parsedFrame['flags']['FileAlterPreservation'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x2000); // b - File alter preservation + $parsedFrame['flags']['ReadOnly'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x1000); // c - Read only + $parsedFrame['flags']['GroupingIdentity'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0040); // h - Grouping identity + $parsedFrame['flags']['compression'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0008); // k - Compression + $parsedFrame['flags']['Encryption'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0004); // m - Encryption + $parsedFrame['flags']['Unsynchronisation'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0002); // n - Unsynchronisation + $parsedFrame['flags']['DataLengthIndicator'] = (bool) ($parsedFrame['frame_flags_raw'] & 0x0001); // p - Data length indicator + + // Frame-level de-unsynchronisation - ID3v2.4 + if ($parsedFrame['flags']['Unsynchronisation']) { + $parsedFrame['data'] = $this->DeUnsynchronise($parsedFrame['data']); + } + + if ($parsedFrame['flags']['DataLengthIndicator']) { + $parsedFrame['data_length_indicator'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], 0, 4), 1); + $parsedFrame['data'] = substr($parsedFrame['data'], 4); + } + } + + // Frame-level de-compression + if ($parsedFrame['flags']['compression']) { + $parsedFrame['decompressed_size'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], 0, 4)); + if (!function_exists('gzuncompress')) { + $this->warning('gzuncompress() support required to decompress ID3v2 frame "'.$parsedFrame['frame_name'].'"'); + } else { + if ($decompresseddata = @gzuncompress(substr($parsedFrame['data'], 4))) { + //if ($decompresseddata = @gzuncompress($parsedFrame['data'])) { + $parsedFrame['data'] = $decompresseddata; + unset($decompresseddata); + } else { + $this->warning('gzuncompress() failed on compressed contents of ID3v2 frame "'.$parsedFrame['frame_name'].'"'); + } + } + } + } + + if (!empty($parsedFrame['flags']['DataLengthIndicator'])) { + if ($parsedFrame['data_length_indicator'] != strlen($parsedFrame['data'])) { + $this->warning('ID3v2 frame "'.$parsedFrame['frame_name'].'" should be '.$parsedFrame['data_length_indicator'].' bytes long according to DataLengthIndicator, but found '.strlen($parsedFrame['data']).' bytes of data'); + } + } + + if (isset($parsedFrame['datalength']) && ($parsedFrame['datalength'] == 0)) { + + $warning = 'Frame "'.$parsedFrame['frame_name'].'" at offset '.$parsedFrame['dataoffset'].' has no data portion'; + switch ($parsedFrame['frame_name']) { + case 'WCOM': + $warning .= ' (this is known to happen with files tagged by RioPort)'; + break; + + default: + break; + } + $this->warning($warning); + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'UFID')) || // 4.1 UFID Unique file identifier + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'UFI'))) { // 4.1 UFI Unique file identifier + // There may be more than one 'UFID' frame in a tag, + // but only one with the same 'Owner identifier'. + //
    + // Owner identifier $00 + // Identifier + $exploded = explode("\x00", $parsedFrame['data'], 2); + $parsedFrame['ownerid'] = (isset($exploded[0]) ? $exploded[0] : ''); + $parsedFrame['data'] = (isset($exploded[1]) ? $exploded[1] : ''); + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'TXXX')) || // 4.2.2 TXXX User defined text information frame + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'TXX'))) { // 4.2.2 TXX User defined text information frame + // There may be more than one 'TXXX' frame in each tag, + // but only one with the same description. + //
    + // Text encoding $xx + // Description $00 (00) + // Value + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $parsedFrame['description'] = trim(getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $parsedFrame['description'])); + $parsedFrame['data'] = substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator)); + $parsedFrame['data'] = $this->RemoveStringTerminator($parsedFrame['data'], $frame_textencoding_terminator); + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + $commentkey = ($parsedFrame['description'] ? $parsedFrame['description'] : (isset($info['id3v2']['comments'][$parsedFrame['framenameshort']]) ? count($info['id3v2']['comments'][$parsedFrame['framenameshort']]) : 0)); + if (!isset($info['id3v2']['comments'][$parsedFrame['framenameshort']]) || !array_key_exists($commentkey, $info['id3v2']['comments'][$parsedFrame['framenameshort']])) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][$commentkey] = trim(getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $parsedFrame['data'])); + } else { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = trim(getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $parsedFrame['data'])); + } + } + //unset($parsedFrame['data']); do not unset, may be needed elsewhere, e.g. for replaygain + + + } elseif ($parsedFrame['frame_name'][0] == 'T') { // 4.2. T??[?] Text information frame + // There may only be one text information frame of its kind in an tag. + //
    + // Text encoding $xx + // Information + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + } + + $parsedFrame['data'] = (string) substr($parsedFrame['data'], $frame_offset); + $parsedFrame['data'] = $this->RemoveStringTerminator($parsedFrame['data'], $this->TextEncodingTerminatorLookup($frame_textencoding)); + + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + // ID3v2.3 specs say that TPE1 (and others) can contain multiple artist values separated with / + // This of course breaks when an artist name contains slash character, e.g. "AC/DC" + // MP3tag (maybe others) implement alternative system where multiple artists are null-separated, which makes more sense + // getID3 will split null-separated artists into multiple artists and leave slash-separated ones to the user + switch ($parsedFrame['encoding']) { + case 'UTF-16': + case 'UTF-16BE': + case 'UTF-16LE': + $wordsize = 2; + break; + case 'ISO-8859-1': + case 'UTF-8': + default: + $wordsize = 1; + break; + } + $Txxx_elements = array(); + $Txxx_elements_start_offset = 0; + for ($i = 0; $i < strlen($parsedFrame['data']); $i += $wordsize) { + if (substr($parsedFrame['data'], $i, $wordsize) == str_repeat("\x00", $wordsize)) { + $Txxx_elements[] = substr($parsedFrame['data'], $Txxx_elements_start_offset, $i - $Txxx_elements_start_offset); + $Txxx_elements_start_offset = $i + $wordsize; + } + } + $Txxx_elements[] = substr($parsedFrame['data'], $Txxx_elements_start_offset, $i - $Txxx_elements_start_offset); + foreach ($Txxx_elements as $Txxx_element) { + $string = getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $Txxx_element); + if (!empty($string)) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = $string; + } + } + unset($string, $wordsize, $i, $Txxx_elements, $Txxx_element, $Txxx_elements_start_offset); + } + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'WXXX')) || // 4.3.2 WXXX User defined URL link frame + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'WXX'))) { // 4.3.2 WXX User defined URL link frame + // There may be more than one 'WXXX' frame in each tag, + // but only one with the same description + //
    + // Text encoding $xx + // Description $00 (00) + // URL + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); // according to the frame text encoding + $parsedFrame['url'] = substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator)); // always ISO-8859-1 + $parsedFrame['description'] = $this->RemoveStringTerminator($parsedFrame['description'], $frame_textencoding_terminator); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + + if (!empty($parsedFrame['framenameshort']) && $parsedFrame['url']) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = getid3_lib::iconv_fallback('ISO-8859-1', $info['id3v2']['encoding'], $parsedFrame['url']); + } + unset($parsedFrame['data']); + + + } elseif ($parsedFrame['frame_name'][0] == 'W') { // 4.3. W??? URL link frames + // There may only be one URL link frame of its kind in a tag, + // except when stated otherwise in the frame description + //
    + // URL + + $parsedFrame['url'] = trim($parsedFrame['data']); // always ISO-8859-1 + if (!empty($parsedFrame['framenameshort']) && $parsedFrame['url']) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = getid3_lib::iconv_fallback('ISO-8859-1', $info['id3v2']['encoding'], $parsedFrame['url']); + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion == 3) && ($parsedFrame['frame_name'] == 'IPLS')) || // 4.4 IPLS Involved people list (ID3v2.3 only) + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'IPL'))) { // 4.4 IPL Involved people list (ID3v2.2 only) + // http://id3.org/id3v2.3.0#sec4.4 + // There may only be one 'IPL' frame in each tag + //
    + // Text encoding $xx + // People list strings + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + } + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($parsedFrame['encodingid']); + $parsedFrame['data_raw'] = (string) substr($parsedFrame['data'], $frame_offset); + + // https://www.getid3.org/phpBB3/viewtopic.php?t=1369 + // "this tag typically contains null terminated strings, which are associated in pairs" + // "there are users that use the tag incorrectly" + $IPLS_parts = array(); + if (strpos($parsedFrame['data_raw'], "\x00") !== false) { + $IPLS_parts_unsorted = array(); + if (((strlen($parsedFrame['data_raw']) % 2) == 0) && ((substr($parsedFrame['data_raw'], 0, 2) == "\xFF\xFE") || (substr($parsedFrame['data_raw'], 0, 2) == "\xFE\xFF"))) { + // UTF-16, be careful looking for null bytes since most 2-byte characters may contain one; you need to find twin null bytes, and on even padding + $thisILPS = ''; + for ($i = 0; $i < strlen($parsedFrame['data_raw']); $i += 2) { + $twobytes = substr($parsedFrame['data_raw'], $i, 2); + if ($twobytes === "\x00\x00") { + $IPLS_parts_unsorted[] = getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $thisILPS); + $thisILPS = ''; + } else { + $thisILPS .= $twobytes; + } + } + if (strlen($thisILPS) > 2) { // 2-byte BOM + $IPLS_parts_unsorted[] = getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $thisILPS); + } + } else { + // ISO-8859-1 or UTF-8 or other single-byte-null character set + $IPLS_parts_unsorted = explode("\x00", $parsedFrame['data_raw']); + } + if (count($IPLS_parts_unsorted) == 1) { + // just a list of names, e.g. "Dino Baptiste, Jimmy Copley, John Gordon, Bernie Marsden, Sharon Watson" + foreach ($IPLS_parts_unsorted as $key => $value) { + $IPLS_parts_sorted = preg_split('#[;,\\r\\n\\t]#', $value); + $position = ''; + foreach ($IPLS_parts_sorted as $person) { + $IPLS_parts[] = array('position'=>$position, 'person'=>$person); + } + } + } elseif ((count($IPLS_parts_unsorted) % 2) == 0) { + $position = ''; + $person = ''; + foreach ($IPLS_parts_unsorted as $key => $value) { + if (($key % 2) == 0) { + $position = $value; + } else { + $person = $value; + $IPLS_parts[] = array('position'=>$position, 'person'=>$person); + $position = ''; + $person = ''; + } + } + } else { + foreach ($IPLS_parts_unsorted as $key => $value) { + $IPLS_parts[] = array($value); + } + } + + } else { + $IPLS_parts = preg_split('#[;,\\r\\n\\t]#', $parsedFrame['data_raw']); + } + $parsedFrame['data'] = $IPLS_parts; + + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = $parsedFrame['data']; + } + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'MCDI')) || // 4.4 MCDI Music CD identifier + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'MCI'))) { // 4.5 MCI Music CD identifier + // There may only be one 'MCDI' frame in each tag + //
    + // CD TOC + + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = $parsedFrame['data']; + } + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'ETCO')) || // 4.5 ETCO Event timing codes + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'ETC'))) { // 4.6 ETC Event timing codes + // There may only be one 'ETCO' frame in each tag + //
    + // Time stamp format $xx + // Where time stamp format is: + // $01 (32-bit value) MPEG frames from beginning of file + // $02 (32-bit value) milliseconds from beginning of file + // Followed by a list of key events in the following format: + // Type of event $xx + // Time stamp $xx (xx ...) + // The 'Time stamp' is set to zero if directly at the beginning of the sound + // or after the previous event. All events MUST be sorted in chronological order. + + $frame_offset = 0; + $parsedFrame['timestampformat'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + + while ($frame_offset < strlen($parsedFrame['data'])) { + $parsedFrame['typeid'] = substr($parsedFrame['data'], $frame_offset++, 1); + $parsedFrame['type'] = $this->ETCOEventLookup($parsedFrame['typeid']); + $parsedFrame['timestamp'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'MLLT')) || // 4.6 MLLT MPEG location lookup table + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'MLL'))) { // 4.7 MLL MPEG location lookup table + // There may only be one 'MLLT' frame in each tag + //
    + // MPEG frames between reference $xx xx + // Bytes between reference $xx xx xx + // Milliseconds between reference $xx xx xx + // Bits for bytes deviation $xx + // Bits for milliseconds dev. $xx + // Then for every reference the following data is included; + // Deviation in bytes %xxx.... + // Deviation in milliseconds %xxx.... + + $frame_offset = 0; + $parsedFrame['framesbetweenreferences'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], 0, 2)); + $parsedFrame['bytesbetweenreferences'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], 2, 3)); + $parsedFrame['msbetweenreferences'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], 5, 3)); + $parsedFrame['bitsforbytesdeviation'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], 8, 1)); + $parsedFrame['bitsformsdeviation'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], 9, 1)); + $parsedFrame['data'] = substr($parsedFrame['data'], 10); + $deviationbitstream = ''; + while ($frame_offset < strlen($parsedFrame['data'])) { + $deviationbitstream .= getid3_lib::BigEndian2Bin(substr($parsedFrame['data'], $frame_offset++, 1)); + } + $reference_counter = 0; + while (strlen($deviationbitstream) > 0) { + $parsedFrame[$reference_counter]['bytedeviation'] = bindec(substr($deviationbitstream, 0, $parsedFrame['bitsforbytesdeviation'])); + $parsedFrame[$reference_counter]['msdeviation'] = bindec(substr($deviationbitstream, $parsedFrame['bitsforbytesdeviation'], $parsedFrame['bitsformsdeviation'])); + $deviationbitstream = substr($deviationbitstream, $parsedFrame['bitsforbytesdeviation'] + $parsedFrame['bitsformsdeviation']); + $reference_counter++; + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'SYTC')) || // 4.7 SYTC Synchronised tempo codes + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'STC'))) { // 4.8 STC Synchronised tempo codes + // There may only be one 'SYTC' frame in each tag + //
    + // Time stamp format $xx + // Tempo data + // Where time stamp format is: + // $01 (32-bit value) MPEG frames from beginning of file + // $02 (32-bit value) milliseconds from beginning of file + + $frame_offset = 0; + $parsedFrame['timestampformat'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $timestamp_counter = 0; + while ($frame_offset < strlen($parsedFrame['data'])) { + $parsedFrame[$timestamp_counter]['tempo'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + if ($parsedFrame[$timestamp_counter]['tempo'] == 255) { + $parsedFrame[$timestamp_counter]['tempo'] += ord(substr($parsedFrame['data'], $frame_offset++, 1)); + } + $parsedFrame[$timestamp_counter]['timestamp'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + $timestamp_counter++; + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'USLT')) || // 4.8 USLT Unsynchronised lyric/text transcription + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'ULT'))) { // 4.9 ULT Unsynchronised lyric/text transcription + // There may be more than one 'Unsynchronised lyrics/text transcription' frame + // in each tag, but only one with the same language and content descriptor. + //
    + // Text encoding $xx + // Language $xx xx xx + // Content descriptor $00 (00) + // Lyrics/text + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + $frame_language = substr($parsedFrame['data'], $frame_offset, 3); + $frame_offset += 3; + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + $parsedFrame['data'] = substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator)); + $parsedFrame['data'] = $this->RemoveStringTerminator($parsedFrame['data'], $frame_textencoding_terminator); + + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $parsedFrame['language'] = $frame_language; + $parsedFrame['languagename'] = $this->LanguageLookup($frame_language, false); + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $parsedFrame['data']); + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'SYLT')) || // 4.9 SYLT Synchronised lyric/text + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'SLT'))) { // 4.10 SLT Synchronised lyric/text + // There may be more than one 'SYLT' frame in each tag, + // but only one with the same language and content descriptor. + //
    + // Text encoding $xx + // Language $xx xx xx + // Time stamp format $xx + // $01 (32-bit value) MPEG frames from beginning of file + // $02 (32-bit value) milliseconds from beginning of file + // Content type $xx + // Content descriptor $00 (00) + // Terminated text to be synced (typically a syllable) + // Sync identifier (terminator to above string) $00 (00) + // Time stamp $xx (xx ...) + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + $frame_language = substr($parsedFrame['data'], $frame_offset, 3); + $frame_offset += 3; + $parsedFrame['timestampformat'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['contenttypeid'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['contenttype'] = $this->SYTLContentTypeLookup($parsedFrame['contenttypeid']); + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $parsedFrame['language'] = $frame_language; + $parsedFrame['languagename'] = $this->LanguageLookup($frame_language, false); + + $timestampindex = 0; + $frame_remainingdata = substr($parsedFrame['data'], $frame_offset); + while (strlen($frame_remainingdata)) { + $frame_offset = 0; + $frame_terminatorpos = strpos($frame_remainingdata, $frame_textencoding_terminator); + if ($frame_terminatorpos === false) { + $frame_remainingdata = ''; + } else { + if (ord(substr($frame_remainingdata, $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['lyrics'][$timestampindex]['data'] = substr($frame_remainingdata, $frame_offset, $frame_terminatorpos - $frame_offset); + + $frame_remainingdata = substr($frame_remainingdata, $frame_terminatorpos + strlen($frame_textencoding_terminator)); + if (($timestampindex == 0) && (ord($frame_remainingdata[0]) != 0)) { + // timestamp probably omitted for first data item + } else { + $parsedFrame['lyrics'][$timestampindex]['timestamp'] = getid3_lib::BigEndian2Int(substr($frame_remainingdata, 0, 4)); + $frame_remainingdata = substr($frame_remainingdata, 4); + } + $timestampindex++; + } + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'COMM')) || // 4.10 COMM Comments + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'COM'))) { // 4.11 COM Comments + // There may be more than one comment frame in each tag, + // but only one with the same language and content descriptor. + //
    + // Text encoding $xx + // Language $xx xx xx + // Short content descrip. $00 (00) + // The actual text + + if (strlen($parsedFrame['data']) < 5) { + + $this->warning('Invalid data (too short) for "'.$parsedFrame['frame_name'].'" frame at offset '.$parsedFrame['dataoffset']); + + } else { + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + $frame_language = substr($parsedFrame['data'], $frame_offset, 3); + $frame_offset += 3; + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + $frame_text = (string) substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator)); + $frame_text = $this->RemoveStringTerminator($frame_text, $frame_textencoding_terminator); + + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $parsedFrame['language'] = $frame_language; + $parsedFrame['languagename'] = $this->LanguageLookup($frame_language, false); + $parsedFrame['data'] = $frame_text; + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + $commentkey = ($parsedFrame['description'] ? $parsedFrame['description'] : (!empty($info['id3v2']['comments'][$parsedFrame['framenameshort']]) ? count($info['id3v2']['comments'][$parsedFrame['framenameshort']]) : 0)); + if (!isset($info['id3v2']['comments'][$parsedFrame['framenameshort']]) || !array_key_exists($commentkey, $info['id3v2']['comments'][$parsedFrame['framenameshort']])) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][$commentkey] = getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $parsedFrame['data']); + } else { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $parsedFrame['data']); + } + } + + } + + } elseif (($id3v2_majorversion >= 4) && ($parsedFrame['frame_name'] == 'RVA2')) { // 4.11 RVA2 Relative volume adjustment (2) (ID3v2.4+ only) + // There may be more than one 'RVA2' frame in each tag, + // but only one with the same identification string + //
    + // Identification $00 + // The 'identification' string is used to identify the situation and/or + // device where this adjustment should apply. The following is then + // repeated for every channel: + // Type of channel $xx + // Volume adjustment $xx xx + // Bits representing peak $xx + // Peak volume $xx (xx ...) + + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00"); + $frame_idstring = substr($parsedFrame['data'], 0, $frame_terminatorpos); + if (ord($frame_idstring) === 0) { + $frame_idstring = ''; + } + $frame_remainingdata = substr($parsedFrame['data'], $frame_terminatorpos + strlen("\x00")); + $parsedFrame['description'] = $frame_idstring; + $RVA2channelcounter = 0; + while (strlen($frame_remainingdata) >= 5) { + $frame_offset = 0; + $frame_channeltypeid = ord(substr($frame_remainingdata, $frame_offset++, 1)); + $parsedFrame[$RVA2channelcounter]['channeltypeid'] = $frame_channeltypeid; + $parsedFrame[$RVA2channelcounter]['channeltype'] = $this->RVA2ChannelTypeLookup($frame_channeltypeid); + $parsedFrame[$RVA2channelcounter]['volumeadjust'] = getid3_lib::BigEndian2Int(substr($frame_remainingdata, $frame_offset, 2), false, true); // 16-bit signed + $frame_offset += 2; + $parsedFrame[$RVA2channelcounter]['bitspeakvolume'] = ord(substr($frame_remainingdata, $frame_offset++, 1)); + if (($parsedFrame[$RVA2channelcounter]['bitspeakvolume'] < 1) || ($parsedFrame[$RVA2channelcounter]['bitspeakvolume'] > 4)) { + $this->warning('ID3v2::RVA2 frame['.$RVA2channelcounter.'] contains invalid '.$parsedFrame[$RVA2channelcounter]['bitspeakvolume'].'-byte bits-representing-peak value'); + break; + } + $frame_bytespeakvolume = ceil($parsedFrame[$RVA2channelcounter]['bitspeakvolume'] / 8); + $parsedFrame[$RVA2channelcounter]['peakvolume'] = getid3_lib::BigEndian2Int(substr($frame_remainingdata, $frame_offset, $frame_bytespeakvolume)); + $frame_remainingdata = substr($frame_remainingdata, $frame_offset + $frame_bytespeakvolume); + $RVA2channelcounter++; + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion == 3) && ($parsedFrame['frame_name'] == 'RVAD')) || // 4.12 RVAD Relative volume adjustment (ID3v2.3 only) + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'RVA'))) { // 4.12 RVA Relative volume adjustment (ID3v2.2 only) + // There may only be one 'RVA' frame in each tag + //
    + // ID3v2.2 => Increment/decrement %000000ba + // ID3v2.3 => Increment/decrement %00fedcba + // Bits used for volume descr. $xx + // Relative volume change, right $xx xx (xx ...) // a + // Relative volume change, left $xx xx (xx ...) // b + // Peak volume right $xx xx (xx ...) + // Peak volume left $xx xx (xx ...) + // ID3v2.3 only, optional (not present in ID3v2.2): + // Relative volume change, right back $xx xx (xx ...) // c + // Relative volume change, left back $xx xx (xx ...) // d + // Peak volume right back $xx xx (xx ...) + // Peak volume left back $xx xx (xx ...) + // ID3v2.3 only, optional (not present in ID3v2.2): + // Relative volume change, center $xx xx (xx ...) // e + // Peak volume center $xx xx (xx ...) + // ID3v2.3 only, optional (not present in ID3v2.2): + // Relative volume change, bass $xx xx (xx ...) // f + // Peak volume bass $xx xx (xx ...) + + $frame_offset = 0; + $frame_incrdecrflags = getid3_lib::BigEndian2Bin(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['incdec']['right'] = (bool) substr($frame_incrdecrflags, 6, 1); + $parsedFrame['incdec']['left'] = (bool) substr($frame_incrdecrflags, 7, 1); + $parsedFrame['bitsvolume'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_bytesvolume = ceil($parsedFrame['bitsvolume'] / 8); + $parsedFrame['volumechange']['right'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + if ($parsedFrame['incdec']['right'] === false) { + $parsedFrame['volumechange']['right'] *= -1; + } + $frame_offset += $frame_bytesvolume; + $parsedFrame['volumechange']['left'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + if ($parsedFrame['incdec']['left'] === false) { + $parsedFrame['volumechange']['left'] *= -1; + } + $frame_offset += $frame_bytesvolume; + $parsedFrame['peakvolume']['right'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + $frame_offset += $frame_bytesvolume; + $parsedFrame['peakvolume']['left'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + $frame_offset += $frame_bytesvolume; + if ($id3v2_majorversion == 3) { + $parsedFrame['data'] = substr($parsedFrame['data'], $frame_offset); + if (strlen($parsedFrame['data']) > 0) { + $parsedFrame['incdec']['rightrear'] = (bool) substr($frame_incrdecrflags, 4, 1); + $parsedFrame['incdec']['leftrear'] = (bool) substr($frame_incrdecrflags, 5, 1); + $parsedFrame['volumechange']['rightrear'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + if ($parsedFrame['incdec']['rightrear'] === false) { + $parsedFrame['volumechange']['rightrear'] *= -1; + } + $frame_offset += $frame_bytesvolume; + $parsedFrame['volumechange']['leftrear'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + if ($parsedFrame['incdec']['leftrear'] === false) { + $parsedFrame['volumechange']['leftrear'] *= -1; + } + $frame_offset += $frame_bytesvolume; + $parsedFrame['peakvolume']['rightrear'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + $frame_offset += $frame_bytesvolume; + $parsedFrame['peakvolume']['leftrear'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + $frame_offset += $frame_bytesvolume; + } + $parsedFrame['data'] = substr($parsedFrame['data'], $frame_offset); + if (strlen($parsedFrame['data']) > 0) { + $parsedFrame['incdec']['center'] = (bool) substr($frame_incrdecrflags, 3, 1); + $parsedFrame['volumechange']['center'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + if ($parsedFrame['incdec']['center'] === false) { + $parsedFrame['volumechange']['center'] *= -1; + } + $frame_offset += $frame_bytesvolume; + $parsedFrame['peakvolume']['center'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + $frame_offset += $frame_bytesvolume; + } + $parsedFrame['data'] = substr($parsedFrame['data'], $frame_offset); + if (strlen($parsedFrame['data']) > 0) { + $parsedFrame['incdec']['bass'] = (bool) substr($frame_incrdecrflags, 2, 1); + $parsedFrame['volumechange']['bass'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + if ($parsedFrame['incdec']['bass'] === false) { + $parsedFrame['volumechange']['bass'] *= -1; + } + $frame_offset += $frame_bytesvolume; + $parsedFrame['peakvolume']['bass'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesvolume)); + $frame_offset += $frame_bytesvolume; + } + } + unset($parsedFrame['data']); + + + } elseif (($id3v2_majorversion >= 4) && ($parsedFrame['frame_name'] == 'EQU2')) { // 4.12 EQU2 Equalisation (2) (ID3v2.4+ only) + // There may be more than one 'EQU2' frame in each tag, + // but only one with the same identification string + //
    + // Interpolation method $xx + // $00 Band + // $01 Linear + // Identification $00 + // The following is then repeated for every adjustment point + // Frequency $xx xx + // Volume adjustment $xx xx + + $frame_offset = 0; + $frame_interpolationmethod = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_idstring = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_idstring) === 0) { + $frame_idstring = ''; + } + $parsedFrame['description'] = $frame_idstring; + $frame_remainingdata = substr($parsedFrame['data'], $frame_terminatorpos + strlen("\x00")); + while (strlen($frame_remainingdata)) { + $frame_frequency = getid3_lib::BigEndian2Int(substr($frame_remainingdata, 0, 2)) / 2; + $parsedFrame['data'][$frame_frequency] = getid3_lib::BigEndian2Int(substr($frame_remainingdata, 2, 2), false, true); + $frame_remainingdata = substr($frame_remainingdata, 4); + } + $parsedFrame['interpolationmethod'] = $frame_interpolationmethod; + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion == 3) && ($parsedFrame['frame_name'] == 'EQUA')) || // 4.12 EQUA Equalisation (ID3v2.3 only) + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'EQU'))) { // 4.13 EQU Equalisation (ID3v2.2 only) + // There may only be one 'EQUA' frame in each tag + //
    + // Adjustment bits $xx + // This is followed by 2 bytes + ('adjustment bits' rounded up to the + // nearest byte) for every equalisation band in the following format, + // giving a frequency range of 0 - 32767Hz: + // Increment/decrement %x (MSB of the Frequency) + // Frequency (lower 15 bits) + // Adjustment $xx (xx ...) + + $frame_offset = 0; + $parsedFrame['adjustmentbits'] = substr($parsedFrame['data'], $frame_offset++, 1); + $frame_adjustmentbytes = ceil($parsedFrame['adjustmentbits'] / 8); + + $frame_remainingdata = (string) substr($parsedFrame['data'], $frame_offset); + while (strlen($frame_remainingdata) > 0) { + $frame_frequencystr = getid3_lib::BigEndian2Bin(substr($frame_remainingdata, 0, 2)); + $frame_incdec = (bool) substr($frame_frequencystr, 0, 1); + $frame_frequency = bindec(substr($frame_frequencystr, 1, 15)); + $parsedFrame[$frame_frequency]['incdec'] = $frame_incdec; + $parsedFrame[$frame_frequency]['adjustment'] = getid3_lib::BigEndian2Int(substr($frame_remainingdata, 2, $frame_adjustmentbytes)); + if ($parsedFrame[$frame_frequency]['incdec'] === false) { + $parsedFrame[$frame_frequency]['adjustment'] *= -1; + } + $frame_remainingdata = substr($frame_remainingdata, 2 + $frame_adjustmentbytes); + } + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'RVRB')) || // 4.13 RVRB Reverb + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'REV'))) { // 4.14 REV Reverb + // There may only be one 'RVRB' frame in each tag. + //
    + // Reverb left (ms) $xx xx + // Reverb right (ms) $xx xx + // Reverb bounces, left $xx + // Reverb bounces, right $xx + // Reverb feedback, left to left $xx + // Reverb feedback, left to right $xx + // Reverb feedback, right to right $xx + // Reverb feedback, right to left $xx + // Premix left to right $xx + // Premix right to left $xx + + $frame_offset = 0; + $parsedFrame['left'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + $parsedFrame['right'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + $parsedFrame['bouncesL'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['bouncesR'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['feedbackLL'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['feedbackLR'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['feedbackRR'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['feedbackRL'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['premixLR'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['premixRL'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'APIC')) || // 4.14 APIC Attached picture + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'PIC'))) { // 4.15 PIC Attached picture + // There may be several pictures attached to one file, + // each in their individual 'APIC' frame, but only one + // with the same content descriptor + //
    + // Text encoding $xx + // ID3v2.3+ => MIME type $00 + // ID3v2.2 => Image format $xx xx xx + // Picture type $xx + // Description $00 (00) + // Picture data + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + + if ($id3v2_majorversion == 2 && strlen($parsedFrame['data']) > $frame_offset) { + $frame_imagetype = substr($parsedFrame['data'], $frame_offset, 3); + if (strtolower($frame_imagetype) == 'ima') { + // complete hack for mp3Rage (www.chaoticsoftware.com) that puts ID3v2.3-formatted + // MIME type instead of 3-char ID3v2.2-format image type (thanks xbhoffØpacbell*net) + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_mimetype = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_mimetype) === 0) { + $frame_mimetype = ''; + } + $frame_imagetype = strtoupper(str_replace('image/', '', strtolower($frame_mimetype))); + if ($frame_imagetype == 'JPEG') { + $frame_imagetype = 'JPG'; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + } else { + $frame_offset += 3; + } + } + if ($id3v2_majorversion > 2 && strlen($parsedFrame['data']) > $frame_offset) { + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_mimetype = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_mimetype) === 0) { + $frame_mimetype = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + } + + $frame_picturetype = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + + if ($frame_offset >= $parsedFrame['datalength']) { + $this->warning('data portion of APIC frame is missing at offset '.($parsedFrame['dataoffset'] + 8 + $frame_offset)); + } else { + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + if ($id3v2_majorversion == 2) { + $parsedFrame['imagetype'] = isset($frame_imagetype) ? $frame_imagetype : null; + } else { + $parsedFrame['mime'] = isset($frame_mimetype) ? $frame_mimetype : null; + } + $parsedFrame['picturetypeid'] = $frame_picturetype; + $parsedFrame['picturetype'] = $this->APICPictureTypeLookup($frame_picturetype); + $parsedFrame['data'] = substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator)); + $parsedFrame['datalength'] = strlen($parsedFrame['data']); + + $parsedFrame['image_mime'] = ''; + $imageinfo = array(); + if ($imagechunkcheck = getid3_lib::GetDataImageSize($parsedFrame['data'], $imageinfo)) { + if (($imagechunkcheck[2] >= 1) && ($imagechunkcheck[2] <= 3)) { + $parsedFrame['image_mime'] = image_type_to_mime_type($imagechunkcheck[2]); + if ($imagechunkcheck[0]) { + $parsedFrame['image_width'] = $imagechunkcheck[0]; + } + if ($imagechunkcheck[1]) { + $parsedFrame['image_height'] = $imagechunkcheck[1]; + } + } + } + + do { + if ($this->getid3->option_save_attachments === false) { + // skip entirely + unset($parsedFrame['data']); + break; + } + $dir = ''; + if ($this->getid3->option_save_attachments === true) { + // great +/* + } elseif (is_int($this->getid3->option_save_attachments)) { + if ($this->getid3->option_save_attachments < $parsedFrame['data_length']) { + // too big, skip + $this->warning('attachment at '.$frame_offset.' is too large to process inline ('.number_format($parsedFrame['data_length']).' bytes)'); + unset($parsedFrame['data']); + break; + } +*/ + } elseif (is_string($this->getid3->option_save_attachments)) { + $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->getid3->option_save_attachments), DIRECTORY_SEPARATOR); + if (!is_dir($dir) || !getID3::is_writable($dir)) { + // cannot write, skip + $this->warning('attachment at '.$frame_offset.' cannot be saved to "'.$dir.'" (not writable)'); + unset($parsedFrame['data']); + break; + } + } + // if we get this far, must be OK + if (is_string($this->getid3->option_save_attachments)) { + $destination_filename = $dir.DIRECTORY_SEPARATOR.md5($info['filenamepath']).'_'.$frame_offset; + if (!file_exists($destination_filename) || getID3::is_writable($destination_filename)) { + file_put_contents($destination_filename, $parsedFrame['data']); + } else { + $this->warning('attachment at '.$frame_offset.' cannot be saved to "'.$destination_filename.'" (not writable)'); + } + $parsedFrame['data_filename'] = $destination_filename; + unset($parsedFrame['data']); + } else { + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + if (!isset($info['id3v2']['comments']['picture'])) { + $info['id3v2']['comments']['picture'] = array(); + } + $comments_picture_data = array(); + foreach (array('data', 'image_mime', 'image_width', 'image_height', 'imagetype', 'picturetype', 'description', 'datalength') as $picture_key) { + if (isset($parsedFrame[$picture_key])) { + $comments_picture_data[$picture_key] = $parsedFrame[$picture_key]; + } + } + $info['id3v2']['comments']['picture'][] = $comments_picture_data; + unset($comments_picture_data); + } + } + } while (false); + } + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'GEOB')) || // 4.15 GEOB General encapsulated object + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'GEO'))) { // 4.16 GEO General encapsulated object + // There may be more than one 'GEOB' frame in each tag, + // but only one with the same content descriptor + //
    + // Text encoding $xx + // MIME type $00 + // Filename $00 (00) + // Content description $00 (00) + // Encapsulated object + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_mimetype = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_mimetype) === 0) { + $frame_mimetype = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $frame_filename = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_filename) === 0) { + $frame_filename = ''; + } + $frame_offset = $frame_terminatorpos + strlen($frame_textencoding_terminator); + + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + $frame_offset = $frame_terminatorpos + strlen($frame_textencoding_terminator); + + $parsedFrame['objectdata'] = (string) substr($parsedFrame['data'], $frame_offset); + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $parsedFrame['mime'] = $frame_mimetype; + $parsedFrame['filename'] = $frame_filename; + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'PCNT')) || // 4.16 PCNT Play counter + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'CNT'))) { // 4.17 CNT Play counter + // There may only be one 'PCNT' frame in each tag. + // When the counter reaches all one's, one byte is inserted in + // front of the counter thus making the counter eight bits bigger + //
    + // Counter $xx xx xx xx (xx ...) + + $parsedFrame['data'] = getid3_lib::BigEndian2Int($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'POPM')) || // 4.17 POPM Popularimeter + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'POP'))) { // 4.18 POP Popularimeter + // There may be more than one 'POPM' frame in each tag, + // but only one with the same email address + //
    + // Email to user $00 + // Rating $xx + // Counter $xx xx xx xx (xx ...) + + $frame_offset = 0; + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_emailaddress = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_emailaddress) === 0) { + $frame_emailaddress = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + $frame_rating = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['counter'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset)); + $parsedFrame['email'] = $frame_emailaddress; + $parsedFrame['rating'] = $frame_rating; + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'RBUF')) || // 4.18 RBUF Recommended buffer size + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'BUF'))) { // 4.19 BUF Recommended buffer size + // There may only be one 'RBUF' frame in each tag + //
    + // Buffer size $xx xx xx + // Embedded info flag %0000000x + // Offset to next tag $xx xx xx xx + + $frame_offset = 0; + $parsedFrame['buffersize'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 3)); + $frame_offset += 3; + + $frame_embeddedinfoflags = getid3_lib::BigEndian2Bin(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['flags']['embededinfo'] = (bool) substr($frame_embeddedinfoflags, 7, 1); + $parsedFrame['nexttagoffset'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + unset($parsedFrame['data']); + + + } elseif (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'CRM')) { // 4.20 Encrypted meta frame (ID3v2.2 only) + // There may be more than one 'CRM' frame in a tag, + // but only one with the same 'owner identifier' + //
    + // Owner identifier $00 (00) + // Content/explanation $00 (00) + // Encrypted datablock + + $frame_offset = 0; + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_ownerid = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $parsedFrame['ownerid'] = $frame_ownerid; + $parsedFrame['data'] = (string) substr($parsedFrame['data'], $frame_offset); + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'AENC')) || // 4.19 AENC Audio encryption + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'CRA'))) { // 4.21 CRA Audio encryption + // There may be more than one 'AENC' frames in a tag, + // but only one with the same 'Owner identifier' + //
    + // Owner identifier $00 + // Preview start $xx xx + // Preview length $xx xx + // Encryption info + + $frame_offset = 0; + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_ownerid = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_ownerid) === 0) { + $frame_ownerid = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + $parsedFrame['ownerid'] = $frame_ownerid; + $parsedFrame['previewstart'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + $parsedFrame['previewlength'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + $parsedFrame['encryptioninfo'] = (string) substr($parsedFrame['data'], $frame_offset); + unset($parsedFrame['data']); + + + } elseif ((($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'LINK')) || // 4.20 LINK Linked information + (($id3v2_majorversion == 2) && ($parsedFrame['frame_name'] == 'LNK'))) { // 4.22 LNK Linked information + // There may be more than one 'LINK' frame in a tag, + // but only one with the same contents + //
    + // ID3v2.3+ => Frame identifier $xx xx xx xx + // ID3v2.2 => Frame identifier $xx xx xx + // URL $00 + // ID and additional data + + $frame_offset = 0; + if ($id3v2_majorversion == 2) { + $parsedFrame['frameid'] = substr($parsedFrame['data'], $frame_offset, 3); + $frame_offset += 3; + } else { + $parsedFrame['frameid'] = substr($parsedFrame['data'], $frame_offset, 4); + $frame_offset += 4; + } + + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_url = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_url) === 0) { + $frame_url = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + $parsedFrame['url'] = $frame_url; + + $parsedFrame['additionaldata'] = (string) substr($parsedFrame['data'], $frame_offset); + if (!empty($parsedFrame['framenameshort']) && $parsedFrame['url']) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = getid3_lib::iconv_fallback_iso88591_utf8($parsedFrame['url']); + } + unset($parsedFrame['data']); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'POSS')) { // 4.21 POSS Position synchronisation frame (ID3v2.3+ only) + // There may only be one 'POSS' frame in each tag + // + // Time stamp format $xx + // Position $xx (xx ...) + + $frame_offset = 0; + $parsedFrame['timestampformat'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['position'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset)); + unset($parsedFrame['data']); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'USER')) { // 4.22 USER Terms of use (ID3v2.3+ only) + // There may be more than one 'Terms of use' frame in a tag, + // but only one with the same 'Language' + //
    + // Text encoding $xx + // Language $xx xx xx + // The actual text + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + } + $frame_language = substr($parsedFrame['data'], $frame_offset, 3); + $frame_offset += 3; + $parsedFrame['language'] = $frame_language; + $parsedFrame['languagename'] = $this->LanguageLookup($frame_language, false); + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $parsedFrame['data'] = (string) substr($parsedFrame['data'], $frame_offset); + $parsedFrame['data'] = $this->RemoveStringTerminator($parsedFrame['data'], $this->TextEncodingTerminatorLookup($frame_textencoding)); + if (!empty($parsedFrame['framenameshort']) && !empty($parsedFrame['data'])) { + $info['id3v2']['comments'][$parsedFrame['framenameshort']][] = getid3_lib::iconv_fallback($parsedFrame['encoding'], $info['id3v2']['encoding'], $parsedFrame['data']); + } + unset($parsedFrame['data']); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'OWNE')) { // 4.23 OWNE Ownership frame (ID3v2.3+ only) + // There may only be one 'OWNE' frame in a tag + //
    + // Text encoding $xx + // Price paid $00 + // Date of purch. + // Seller + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + } + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_pricepaid = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $parsedFrame['pricepaid']['currencyid'] = substr($frame_pricepaid, 0, 3); + $parsedFrame['pricepaid']['currency'] = $this->LookupCurrencyUnits($parsedFrame['pricepaid']['currencyid']); + $parsedFrame['pricepaid']['value'] = substr($frame_pricepaid, 3); + + $parsedFrame['purchasedate'] = substr($parsedFrame['data'], $frame_offset, 8); + if ($this->IsValidDateStampString($parsedFrame['purchasedate'])) { + $parsedFrame['purchasedateunix'] = mktime (0, 0, 0, substr($parsedFrame['purchasedate'], 4, 2), substr($parsedFrame['purchasedate'], 6, 2), substr($parsedFrame['purchasedate'], 0, 4)); + } + $frame_offset += 8; + + $parsedFrame['seller'] = (string) substr($parsedFrame['data'], $frame_offset); + $parsedFrame['seller'] = $this->RemoveStringTerminator($parsedFrame['seller'], $this->TextEncodingTerminatorLookup($frame_textencoding)); + unset($parsedFrame['data']); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'COMR')) { // 4.24 COMR Commercial frame (ID3v2.3+ only) + // There may be more than one 'commercial frame' in a tag, + // but no two may be identical + //
    + // Text encoding $xx + // Price string $00 + // Valid until + // Contact URL $00 + // Received as $xx + // Name of seller $00 (00) + // Description $00 (00) + // Picture MIME type $00 + // Seller logo + + $frame_offset = 0; + $frame_textencoding = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_textencoding_terminator = $this->TextEncodingTerminatorLookup($frame_textencoding); + if ((($id3v2_majorversion <= 3) && ($frame_textencoding > 1)) || (($id3v2_majorversion == 4) && ($frame_textencoding > 3))) { + $this->warning('Invalid text encoding byte ('.$frame_textencoding.') in frame "'.$parsedFrame['frame_name'].'" - defaulting to ISO-8859-1 encoding'); + $frame_textencoding_terminator = "\x00"; + } + + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_pricestring = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $frame_offset = $frame_terminatorpos + strlen("\x00"); + $frame_rawpricearray = explode('/', $frame_pricestring); + foreach ($frame_rawpricearray as $key => $val) { + $frame_currencyid = substr($val, 0, 3); + $parsedFrame['price'][$frame_currencyid]['currency'] = $this->LookupCurrencyUnits($frame_currencyid); + $parsedFrame['price'][$frame_currencyid]['value'] = substr($val, 3); + } + + $frame_datestring = substr($parsedFrame['data'], $frame_offset, 8); + $frame_offset += 8; + + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_contacturl = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $frame_receivedasid = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $frame_sellername = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_sellername) === 0) { + $frame_sellername = ''; + } + $frame_offset = $frame_terminatorpos + strlen($frame_textencoding_terminator); + + $frame_terminatorpos = strpos($parsedFrame['data'], $frame_textencoding_terminator, $frame_offset); + if (ord(substr($parsedFrame['data'], $frame_terminatorpos + strlen($frame_textencoding_terminator), 1)) === 0) { + $frame_terminatorpos++; // strpos() fooled because 2nd byte of Unicode chars are often 0x00 + } + $parsedFrame['description'] = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $parsedFrame['description'] = $this->MakeUTF16emptyStringEmpty($parsedFrame['description']); + $frame_offset = $frame_terminatorpos + strlen($frame_textencoding_terminator); + + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_mimetype = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $frame_sellerlogo = substr($parsedFrame['data'], $frame_offset); + + $parsedFrame['encodingid'] = $frame_textencoding; + $parsedFrame['encoding'] = $this->TextEncodingNameLookup($frame_textencoding); + + $parsedFrame['pricevaliduntil'] = $frame_datestring; + $parsedFrame['contacturl'] = $frame_contacturl; + $parsedFrame['receivedasid'] = $frame_receivedasid; + $parsedFrame['receivedas'] = $this->COMRReceivedAsLookup($frame_receivedasid); + $parsedFrame['sellername'] = $frame_sellername; + $parsedFrame['mime'] = $frame_mimetype; + $parsedFrame['logo'] = $frame_sellerlogo; + unset($parsedFrame['data']); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'ENCR')) { // 4.25 ENCR Encryption method registration (ID3v2.3+ only) + // There may be several 'ENCR' frames in a tag, + // but only one containing the same symbol + // and only one containing the same owner identifier + //
    + // Owner identifier $00 + // Method symbol $xx + // Encryption data + + $frame_offset = 0; + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_ownerid = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_ownerid) === 0) { + $frame_ownerid = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $parsedFrame['ownerid'] = $frame_ownerid; + $parsedFrame['methodsymbol'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['data'] = (string) substr($parsedFrame['data'], $frame_offset); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'GRID')) { // 4.26 GRID Group identification registration (ID3v2.3+ only) + + // There may be several 'GRID' frames in a tag, + // but only one containing the same symbol + // and only one containing the same owner identifier + //
    + // Owner identifier $00 + // Group symbol $xx + // Group dependent data + + $frame_offset = 0; + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_ownerid = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_ownerid) === 0) { + $frame_ownerid = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $parsedFrame['ownerid'] = $frame_ownerid; + $parsedFrame['groupsymbol'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['data'] = (string) substr($parsedFrame['data'], $frame_offset); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'PRIV')) { // 4.27 PRIV Private frame (ID3v2.3+ only) + // The tag may contain more than one 'PRIV' frame + // but only with different contents + //
    + // Owner identifier $00 + // The private data + + $frame_offset = 0; + $frame_terminatorpos = strpos($parsedFrame['data'], "\x00", $frame_offset); + $frame_ownerid = substr($parsedFrame['data'], $frame_offset, $frame_terminatorpos - $frame_offset); + if (ord($frame_ownerid) === 0) { + $frame_ownerid = ''; + } + $frame_offset = $frame_terminatorpos + strlen("\x00"); + + $parsedFrame['ownerid'] = $frame_ownerid; + $parsedFrame['data'] = (string) substr($parsedFrame['data'], $frame_offset); + + + } elseif (($id3v2_majorversion >= 4) && ($parsedFrame['frame_name'] == 'SIGN')) { // 4.28 SIGN Signature frame (ID3v2.4+ only) + // There may be more than one 'signature frame' in a tag, + // but no two may be identical + //
    + // Group symbol $xx + // Signature + + $frame_offset = 0; + $parsedFrame['groupsymbol'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $parsedFrame['data'] = (string) substr($parsedFrame['data'], $frame_offset); + + + } elseif (($id3v2_majorversion >= 4) && ($parsedFrame['frame_name'] == 'SEEK')) { // 4.29 SEEK Seek frame (ID3v2.4+ only) + // There may only be one 'seek frame' in a tag + //
    + // Minimum offset to next tag $xx xx xx xx + + $frame_offset = 0; + $parsedFrame['data'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + + + } elseif (($id3v2_majorversion >= 4) && ($parsedFrame['frame_name'] == 'ASPI')) { // 4.30 ASPI Audio seek point index (ID3v2.4+ only) + // There may only be one 'audio seek point index' frame in a tag + //
    + // Indexed data start (S) $xx xx xx xx + // Indexed data length (L) $xx xx xx xx + // Number of index points (N) $xx xx + // Bits per index point (b) $xx + // Then for every index point the following data is included: + // Fraction at index (Fi) $xx (xx) + + $frame_offset = 0; + $parsedFrame['datastart'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + $parsedFrame['indexeddatalength'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + $parsedFrame['indexpoints'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + $parsedFrame['bitsperpoint'] = ord(substr($parsedFrame['data'], $frame_offset++, 1)); + $frame_bytesperpoint = ceil($parsedFrame['bitsperpoint'] / 8); + for ($i = 0; $i < $parsedFrame['indexpoints']; $i++) { + $parsedFrame['indexes'][$i] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, $frame_bytesperpoint)); + $frame_offset += $frame_bytesperpoint; + } + unset($parsedFrame['data']); + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'RGAD')) { // Replay Gain Adjustment + // http://privatewww.essex.ac.uk/~djmrob/replaygain/file_format_id3v2.html + // There may only be one 'RGAD' frame in a tag + //
    + // Peak Amplitude $xx $xx $xx $xx + // Radio Replay Gain Adjustment %aaabbbcd %dddddddd + // Audiophile Replay Gain Adjustment %aaabbbcd %dddddddd + // a - name code + // b - originator code + // c - sign bit + // d - replay gain adjustment + + $frame_offset = 0; + $parsedFrame['peakamplitude'] = getid3_lib::BigEndian2Float(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + $rg_track_adjustment = getid3_lib::Dec2Bin(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + $rg_album_adjustment = getid3_lib::Dec2Bin(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + $parsedFrame['raw']['track']['name'] = getid3_lib::Bin2Dec(substr($rg_track_adjustment, 0, 3)); + $parsedFrame['raw']['track']['originator'] = getid3_lib::Bin2Dec(substr($rg_track_adjustment, 3, 3)); + $parsedFrame['raw']['track']['signbit'] = getid3_lib::Bin2Dec(substr($rg_track_adjustment, 6, 1)); + $parsedFrame['raw']['track']['adjustment'] = getid3_lib::Bin2Dec(substr($rg_track_adjustment, 7, 9)); + $parsedFrame['raw']['album']['name'] = getid3_lib::Bin2Dec(substr($rg_album_adjustment, 0, 3)); + $parsedFrame['raw']['album']['originator'] = getid3_lib::Bin2Dec(substr($rg_album_adjustment, 3, 3)); + $parsedFrame['raw']['album']['signbit'] = getid3_lib::Bin2Dec(substr($rg_album_adjustment, 6, 1)); + $parsedFrame['raw']['album']['adjustment'] = getid3_lib::Bin2Dec(substr($rg_album_adjustment, 7, 9)); + $parsedFrame['track']['name'] = getid3_lib::RGADnameLookup($parsedFrame['raw']['track']['name']); + $parsedFrame['track']['originator'] = getid3_lib::RGADoriginatorLookup($parsedFrame['raw']['track']['originator']); + $parsedFrame['track']['adjustment'] = getid3_lib::RGADadjustmentLookup($parsedFrame['raw']['track']['adjustment'], $parsedFrame['raw']['track']['signbit']); + $parsedFrame['album']['name'] = getid3_lib::RGADnameLookup($parsedFrame['raw']['album']['name']); + $parsedFrame['album']['originator'] = getid3_lib::RGADoriginatorLookup($parsedFrame['raw']['album']['originator']); + $parsedFrame['album']['adjustment'] = getid3_lib::RGADadjustmentLookup($parsedFrame['raw']['album']['adjustment'], $parsedFrame['raw']['album']['signbit']); + + $info['replay_gain']['track']['peak'] = $parsedFrame['peakamplitude']; + $info['replay_gain']['track']['originator'] = $parsedFrame['track']['originator']; + $info['replay_gain']['track']['adjustment'] = $parsedFrame['track']['adjustment']; + $info['replay_gain']['album']['originator'] = $parsedFrame['album']['originator']; + $info['replay_gain']['album']['adjustment'] = $parsedFrame['album']['adjustment']; + + unset($parsedFrame['data']); + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'CHAP')) { // CHAP Chapters frame (ID3v2.3+ only) + // http://id3.org/id3v2-chapters-1.0 + // (10 bytes) + // Element ID $00 + // Start time $xx xx xx xx + // End time $xx xx xx xx + // Start offset $xx xx xx xx + // End offset $xx xx xx xx + // + + $frame_offset = 0; + @list($parsedFrame['element_id']) = explode("\x00", $parsedFrame['data'], 2); + $frame_offset += strlen($parsedFrame['element_id']."\x00"); + $parsedFrame['time_begin'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + $parsedFrame['time_end'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + if (substr($parsedFrame['data'], $frame_offset, 4) != "\xFF\xFF\xFF\xFF") { + // "If these bytes are all set to 0xFF then the value should be ignored and the start time value should be utilized." + $parsedFrame['offset_begin'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + } + $frame_offset += 4; + if (substr($parsedFrame['data'], $frame_offset, 4) != "\xFF\xFF\xFF\xFF") { + // "If these bytes are all set to 0xFF then the value should be ignored and the start time value should be utilized." + $parsedFrame['offset_end'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + } + $frame_offset += 4; + + if ($frame_offset < strlen($parsedFrame['data'])) { + $parsedFrame['subframes'] = array(); + while ($frame_offset < strlen($parsedFrame['data'])) { + // + $subframe = array(); + $subframe['name'] = substr($parsedFrame['data'], $frame_offset, 4); + $frame_offset += 4; + $subframe['size'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + $subframe['flags_raw'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + if ($subframe['size'] > (strlen($parsedFrame['data']) - $frame_offset)) { + $this->warning('CHAP subframe "'.$subframe['name'].'" at frame offset '.$frame_offset.' claims to be "'.$subframe['size'].'" bytes, which is more than the available data ('.(strlen($parsedFrame['data']) - $frame_offset).' bytes)'); + break; + } + $subframe_rawdata = substr($parsedFrame['data'], $frame_offset, $subframe['size']); + $frame_offset += $subframe['size']; + + $subframe['encodingid'] = ord(substr($subframe_rawdata, 0, 1)); + $subframe['text'] = substr($subframe_rawdata, 1); + $subframe['encoding'] = $this->TextEncodingNameLookup($subframe['encodingid']); + $encoding_converted_text = trim(getid3_lib::iconv_fallback($subframe['encoding'], $info['encoding'], $subframe['text'])); + switch (substr($encoding_converted_text, 0, 2)) { + case "\xFF\xFE": + case "\xFE\xFF": + switch (strtoupper($info['id3v2']['encoding'])) { + case 'ISO-8859-1': + case 'UTF-8': + $encoding_converted_text = substr($encoding_converted_text, 2); + // remove unwanted byte-order-marks + break; + default: + // ignore + break; + } + break; + default: + // do not remove BOM + break; + } + + switch ($subframe['name']) { + case 'TIT2': + $parsedFrame['chapter_name'] = $encoding_converted_text; + $parsedFrame['subframes'][] = $subframe; + break; + case 'TIT3': + $parsedFrame['chapter_description'] = $encoding_converted_text; + $parsedFrame['subframes'][] = $subframe; + break; + case 'WXXX': + list($subframe['chapter_url_description'], $subframe['chapter_url']) = explode("\x00", $encoding_converted_text, 2); + $parsedFrame['chapter_url'][$subframe['chapter_url_description']] = $subframe['chapter_url']; + $parsedFrame['subframes'][] = $subframe; + break; + case 'APIC': + if (preg_match('#^([^\\x00]+)*\\x00(.)([^\\x00]+)*\\x00(.+)$#s', $subframe['text'], $matches)) { + list($dummy, $subframe_apic_mime, $subframe_apic_picturetype, $subframe_apic_description, $subframe_apic_picturedata) = $matches; + $subframe['image_mime'] = trim(getid3_lib::iconv_fallback($subframe['encoding'], $info['encoding'], $subframe_apic_mime)); + $subframe['picture_type'] = $this->APICPictureTypeLookup($subframe_apic_picturetype); + $subframe['description'] = trim(getid3_lib::iconv_fallback($subframe['encoding'], $info['encoding'], $subframe_apic_description)); + if (strlen($this->TextEncodingTerminatorLookup($subframe['encoding'])) == 2) { + // the null terminator between "description" and "picture data" could be either 1 byte (ISO-8859-1, UTF-8) or two bytes (UTF-16) + // the above regex assumes one byte, if it's actually two then strip the second one here + $subframe_apic_picturedata = substr($subframe_apic_picturedata, 1); + } + $subframe['data'] = $subframe_apic_picturedata; + unset($dummy, $subframe_apic_mime, $subframe_apic_picturetype, $subframe_apic_description, $subframe_apic_picturedata); + unset($subframe['text'], $parsedFrame['text']); + $parsedFrame['subframes'][] = $subframe; + $parsedFrame['picture_present'] = true; + } else { + $this->warning('ID3v2.CHAP subframe #'.(count($parsedFrame['subframes']) + 1).' "'.$subframe['name'].'" not in expected format'); + } + break; + default: + $this->warning('ID3v2.CHAP subframe "'.$subframe['name'].'" not handled (supported: TIT2, TIT3, WXXX, APIC)'); + break; + } + } + unset($subframe_rawdata, $subframe, $encoding_converted_text); + unset($parsedFrame['data']); // debatable whether this this be here, without it the returned structure may contain a large amount of duplicate data if chapters contain APIC + } + + $id3v2_chapter_entry = array(); + foreach (array('id', 'time_begin', 'time_end', 'offset_begin', 'offset_end', 'chapter_name', 'chapter_description', 'chapter_url', 'picture_present') as $id3v2_chapter_key) { + if (isset($parsedFrame[$id3v2_chapter_key])) { + $id3v2_chapter_entry[$id3v2_chapter_key] = $parsedFrame[$id3v2_chapter_key]; + } + } + if (!isset($info['id3v2']['chapters'])) { + $info['id3v2']['chapters'] = array(); + } + $info['id3v2']['chapters'][] = $id3v2_chapter_entry; + unset($id3v2_chapter_entry, $id3v2_chapter_key); + + + } elseif (($id3v2_majorversion >= 3) && ($parsedFrame['frame_name'] == 'CTOC')) { // CTOC Chapters Table Of Contents frame (ID3v2.3+ only) + // http://id3.org/id3v2-chapters-1.0 + // (10 bytes) + // Element ID $00 + // CTOC flags %xx + // Entry count $xx + // Child Element ID $00 /* zero or more child CHAP or CTOC entries */ + // + + $frame_offset = 0; + @list($parsedFrame['element_id']) = explode("\x00", $parsedFrame['data'], 2); + $frame_offset += strlen($parsedFrame['element_id']."\x00"); + $ctoc_flags_raw = ord(substr($parsedFrame['data'], $frame_offset, 1)); + $frame_offset += 1; + $parsedFrame['entry_count'] = ord(substr($parsedFrame['data'], $frame_offset, 1)); + $frame_offset += 1; + + $terminator_position = null; + for ($i = 0; $i < $parsedFrame['entry_count']; $i++) { + $terminator_position = strpos($parsedFrame['data'], "\x00", $frame_offset); + $parsedFrame['child_element_ids'][$i] = substr($parsedFrame['data'], $frame_offset, $terminator_position - $frame_offset); + $frame_offset = $terminator_position + 1; + } + + $parsedFrame['ctoc_flags']['ordered'] = (bool) ($ctoc_flags_raw & 0x01); + $parsedFrame['ctoc_flags']['top_level'] = (bool) ($ctoc_flags_raw & 0x03); + + unset($ctoc_flags_raw, $terminator_position); + + if ($frame_offset < strlen($parsedFrame['data'])) { + $parsedFrame['subframes'] = array(); + while ($frame_offset < strlen($parsedFrame['data'])) { + // + $subframe = array(); + $subframe['name'] = substr($parsedFrame['data'], $frame_offset, 4); + $frame_offset += 4; + $subframe['size'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 4)); + $frame_offset += 4; + $subframe['flags_raw'] = getid3_lib::BigEndian2Int(substr($parsedFrame['data'], $frame_offset, 2)); + $frame_offset += 2; + if ($subframe['size'] > (strlen($parsedFrame['data']) - $frame_offset)) { + $this->warning('CTOS subframe "'.$subframe['name'].'" at frame offset '.$frame_offset.' claims to be "'.$subframe['size'].'" bytes, which is more than the available data ('.(strlen($parsedFrame['data']) - $frame_offset).' bytes)'); + break; + } + $subframe_rawdata = substr($parsedFrame['data'], $frame_offset, $subframe['size']); + $frame_offset += $subframe['size']; + + $subframe['encodingid'] = ord(substr($subframe_rawdata, 0, 1)); + $subframe['text'] = substr($subframe_rawdata, 1); + $subframe['encoding'] = $this->TextEncodingNameLookup($subframe['encodingid']); + $encoding_converted_text = trim(getid3_lib::iconv_fallback($subframe['encoding'], $info['encoding'], $subframe['text']));; + switch (substr($encoding_converted_text, 0, 2)) { + case "\xFF\xFE": + case "\xFE\xFF": + switch (strtoupper($info['id3v2']['encoding'])) { + case 'ISO-8859-1': + case 'UTF-8': + $encoding_converted_text = substr($encoding_converted_text, 2); + // remove unwanted byte-order-marks + break; + default: + // ignore + break; + } + break; + default: + // do not remove BOM + break; + } + + if (($subframe['name'] == 'TIT2') || ($subframe['name'] == 'TIT3')) { + if ($subframe['name'] == 'TIT2') { + $parsedFrame['toc_name'] = $encoding_converted_text; + } elseif ($subframe['name'] == 'TIT3') { + $parsedFrame['toc_description'] = $encoding_converted_text; + } + $parsedFrame['subframes'][] = $subframe; + } else { + $this->warning('ID3v2.CTOC subframe "'.$subframe['name'].'" not handled (only TIT2 and TIT3)'); + } + } + unset($subframe_rawdata, $subframe, $encoding_converted_text); + } + + } + + return true; + } + + /** + * @param string $data + * + * @return string + */ + public function DeUnsynchronise($data) { + return str_replace("\xFF\x00", "\xFF", $data); + } + + /** + * @param int $index + * + * @return string + */ + public function LookupExtendedHeaderRestrictionsTagSizeLimits($index) { + static $LookupExtendedHeaderRestrictionsTagSizeLimits = array( + 0x00 => 'No more than 128 frames and 1 MB total tag size', + 0x01 => 'No more than 64 frames and 128 KB total tag size', + 0x02 => 'No more than 32 frames and 40 KB total tag size', + 0x03 => 'No more than 32 frames and 4 KB total tag size', + ); + return (isset($LookupExtendedHeaderRestrictionsTagSizeLimits[$index]) ? $LookupExtendedHeaderRestrictionsTagSizeLimits[$index] : ''); + } + + /** + * @param int $index + * + * @return string + */ + public function LookupExtendedHeaderRestrictionsTextEncodings($index) { + static $LookupExtendedHeaderRestrictionsTextEncodings = array( + 0x00 => 'No restrictions', + 0x01 => 'Strings are only encoded with ISO-8859-1 or UTF-8', + ); + return (isset($LookupExtendedHeaderRestrictionsTextEncodings[$index]) ? $LookupExtendedHeaderRestrictionsTextEncodings[$index] : ''); + } + + /** + * @param int $index + * + * @return string + */ + public function LookupExtendedHeaderRestrictionsTextFieldSize($index) { + static $LookupExtendedHeaderRestrictionsTextFieldSize = array( + 0x00 => 'No restrictions', + 0x01 => 'No string is longer than 1024 characters', + 0x02 => 'No string is longer than 128 characters', + 0x03 => 'No string is longer than 30 characters', + ); + return (isset($LookupExtendedHeaderRestrictionsTextFieldSize[$index]) ? $LookupExtendedHeaderRestrictionsTextFieldSize[$index] : ''); + } + + /** + * @param int $index + * + * @return string + */ + public function LookupExtendedHeaderRestrictionsImageEncoding($index) { + static $LookupExtendedHeaderRestrictionsImageEncoding = array( + 0x00 => 'No restrictions', + 0x01 => 'Images are encoded only with PNG or JPEG', + ); + return (isset($LookupExtendedHeaderRestrictionsImageEncoding[$index]) ? $LookupExtendedHeaderRestrictionsImageEncoding[$index] : ''); + } + + /** + * @param int $index + * + * @return string + */ + public function LookupExtendedHeaderRestrictionsImageSizeSize($index) { + static $LookupExtendedHeaderRestrictionsImageSizeSize = array( + 0x00 => 'No restrictions', + 0x01 => 'All images are 256x256 pixels or smaller', + 0x02 => 'All images are 64x64 pixels or smaller', + 0x03 => 'All images are exactly 64x64 pixels, unless required otherwise', + ); + return (isset($LookupExtendedHeaderRestrictionsImageSizeSize[$index]) ? $LookupExtendedHeaderRestrictionsImageSizeSize[$index] : ''); + } + + /** + * @param string $currencyid + * + * @return string + */ + public function LookupCurrencyUnits($currencyid) { + + $begin = __LINE__; + + /** This is not a comment! + + + AED Dirhams + AFA Afghanis + ALL Leke + AMD Drams + ANG Guilders + AOA Kwanza + ARS Pesos + ATS Schillings + AUD Dollars + AWG Guilders + AZM Manats + BAM Convertible Marka + BBD Dollars + BDT Taka + BEF Francs + BGL Leva + BHD Dinars + BIF Francs + BMD Dollars + BND Dollars + BOB Bolivianos + BRL Brazil Real + BSD Dollars + BTN Ngultrum + BWP Pulas + BYR Rubles + BZD Dollars + CAD Dollars + CDF Congolese Francs + CHF Francs + CLP Pesos + CNY Yuan Renminbi + COP Pesos + CRC Colones + CUP Pesos + CVE Escudos + CYP Pounds + CZK Koruny + DEM Deutsche Marks + DJF Francs + DKK Kroner + DOP Pesos + DZD Algeria Dinars + EEK Krooni + EGP Pounds + ERN Nakfa + ESP Pesetas + ETB Birr + EUR Euro + FIM Markkaa + FJD Dollars + FKP Pounds + FRF Francs + GBP Pounds + GEL Lari + GGP Pounds + GHC Cedis + GIP Pounds + GMD Dalasi + GNF Francs + GRD Drachmae + GTQ Quetzales + GYD Dollars + HKD Dollars + HNL Lempiras + HRK Kuna + HTG Gourdes + HUF Forints + IDR Rupiahs + IEP Pounds + ILS New Shekels + IMP Pounds + INR Rupees + IQD Dinars + IRR Rials + ISK Kronur + ITL Lire + JEP Pounds + JMD Dollars + JOD Dinars + JPY Yen + KES Shillings + KGS Soms + KHR Riels + KMF Francs + KPW Won + KWD Dinars + KYD Dollars + KZT Tenge + LAK Kips + LBP Pounds + LKR Rupees + LRD Dollars + LSL Maloti + LTL Litai + LUF Francs + LVL Lati + LYD Dinars + MAD Dirhams + MDL Lei + MGF Malagasy Francs + MKD Denars + MMK Kyats + MNT Tugriks + MOP Patacas + MRO Ouguiyas + MTL Liri + MUR Rupees + MVR Rufiyaa + MWK Kwachas + MXN Pesos + MYR Ringgits + MZM Meticais + NAD Dollars + NGN Nairas + NIO Gold Cordobas + NLG Guilders + NOK Krone + NPR Nepal Rupees + NZD Dollars + OMR Rials + PAB Balboa + PEN Nuevos Soles + PGK Kina + PHP Pesos + PKR Rupees + PLN Zlotych + PTE Escudos + PYG Guarani + QAR Rials + ROL Lei + RUR Rubles + RWF Rwanda Francs + SAR Riyals + SBD Dollars + SCR Rupees + SDD Dinars + SEK Kronor + SGD Dollars + SHP Pounds + SIT Tolars + SKK Koruny + SLL Leones + SOS Shillings + SPL Luigini + SRG Guilders + STD Dobras + SVC Colones + SYP Pounds + SZL Emalangeni + THB Baht + TJR Rubles + TMM Manats + TND Dinars + TOP Pa'anga + TRL Liras + TTD Dollars + TVD Tuvalu Dollars + TWD New Dollars + TZS Shillings + UAH Hryvnia + UGX Shillings + USD Dollars + UYU Pesos + UZS Sums + VAL Lire + VEB Bolivares + VND Dong + VUV Vatu + WST Tala + XAF Francs + XAG Ounces + XAU Ounces + XCD Dollars + XDR Special Drawing Rights + XPD Ounces + XPF Francs + XPT Ounces + YER Rials + YUM New Dinars + ZAR Rand + ZMK Kwacha + ZWD Zimbabwe Dollars + + */ + + return getid3_lib::EmbeddedLookup($currencyid, $begin, __LINE__, __FILE__, 'id3v2-currency-units'); + } + + /** + * @param string $currencyid + * + * @return string + */ + public function LookupCurrencyCountry($currencyid) { + + $begin = __LINE__; + + /** This is not a comment! + + AED United Arab Emirates + AFA Afghanistan + ALL Albania + AMD Armenia + ANG Netherlands Antilles + AOA Angola + ARS Argentina + ATS Austria + AUD Australia + AWG Aruba + AZM Azerbaijan + BAM Bosnia and Herzegovina + BBD Barbados + BDT Bangladesh + BEF Belgium + BGL Bulgaria + BHD Bahrain + BIF Burundi + BMD Bermuda + BND Brunei Darussalam + BOB Bolivia + BRL Brazil + BSD Bahamas + BTN Bhutan + BWP Botswana + BYR Belarus + BZD Belize + CAD Canada + CDF Congo/Kinshasa + CHF Switzerland + CLP Chile + CNY China + COP Colombia + CRC Costa Rica + CUP Cuba + CVE Cape Verde + CYP Cyprus + CZK Czech Republic + DEM Germany + DJF Djibouti + DKK Denmark + DOP Dominican Republic + DZD Algeria + EEK Estonia + EGP Egypt + ERN Eritrea + ESP Spain + ETB Ethiopia + EUR Euro Member Countries + FIM Finland + FJD Fiji + FKP Falkland Islands (Malvinas) + FRF France + GBP United Kingdom + GEL Georgia + GGP Guernsey + GHC Ghana + GIP Gibraltar + GMD Gambia + GNF Guinea + GRD Greece + GTQ Guatemala + GYD Guyana + HKD Hong Kong + HNL Honduras + HRK Croatia + HTG Haiti + HUF Hungary + IDR Indonesia + IEP Ireland (Eire) + ILS Israel + IMP Isle of Man + INR India + IQD Iraq + IRR Iran + ISK Iceland + ITL Italy + JEP Jersey + JMD Jamaica + JOD Jordan + JPY Japan + KES Kenya + KGS Kyrgyzstan + KHR Cambodia + KMF Comoros + KPW Korea + KWD Kuwait + KYD Cayman Islands + KZT Kazakstan + LAK Laos + LBP Lebanon + LKR Sri Lanka + LRD Liberia + LSL Lesotho + LTL Lithuania + LUF Luxembourg + LVL Latvia + LYD Libya + MAD Morocco + MDL Moldova + MGF Madagascar + MKD Macedonia + MMK Myanmar (Burma) + MNT Mongolia + MOP Macau + MRO Mauritania + MTL Malta + MUR Mauritius + MVR Maldives (Maldive Islands) + MWK Malawi + MXN Mexico + MYR Malaysia + MZM Mozambique + NAD Namibia + NGN Nigeria + NIO Nicaragua + NLG Netherlands (Holland) + NOK Norway + NPR Nepal + NZD New Zealand + OMR Oman + PAB Panama + PEN Peru + PGK Papua New Guinea + PHP Philippines + PKR Pakistan + PLN Poland + PTE Portugal + PYG Paraguay + QAR Qatar + ROL Romania + RUR Russia + RWF Rwanda + SAR Saudi Arabia + SBD Solomon Islands + SCR Seychelles + SDD Sudan + SEK Sweden + SGD Singapore + SHP Saint Helena + SIT Slovenia + SKK Slovakia + SLL Sierra Leone + SOS Somalia + SPL Seborga + SRG Suriname + STD São Tome and Principe + SVC El Salvador + SYP Syria + SZL Swaziland + THB Thailand + TJR Tajikistan + TMM Turkmenistan + TND Tunisia + TOP Tonga + TRL Turkey + TTD Trinidad and Tobago + TVD Tuvalu + TWD Taiwan + TZS Tanzania + UAH Ukraine + UGX Uganda + USD United States of America + UYU Uruguay + UZS Uzbekistan + VAL Vatican City + VEB Venezuela + VND Viet Nam + VUV Vanuatu + WST Samoa + XAF Communauté Financière Africaine + XAG Silver + XAU Gold + XCD East Caribbean + XDR International Monetary Fund + XPD Palladium + XPF Comptoirs Français du Pacifique + XPT Platinum + YER Yemen + YUM Yugoslavia + ZAR South Africa + ZMK Zambia + ZWD Zimbabwe + + */ + + return getid3_lib::EmbeddedLookup($currencyid, $begin, __LINE__, __FILE__, 'id3v2-currency-country'); + } + + /** + * @param string $languagecode + * @param bool $casesensitive + * + * @return string + */ + public static function LanguageLookup($languagecode, $casesensitive=false) { + + if (!$casesensitive) { + $languagecode = strtolower($languagecode); + } + + // http://www.id3.org/id3v2.4.0-structure.txt + // [4. ID3v2 frame overview] + // The three byte language field, present in several frames, is used to + // describe the language of the frame's content, according to ISO-639-2 + // [ISO-639-2]. The language should be represented in lower case. If the + // language is not known the string "XXX" should be used. + + + // ISO 639-2 - http://www.id3.org/iso639-2.html + + $begin = __LINE__; + + /** This is not a comment! + + XXX unknown + xxx unknown + aar Afar + abk Abkhazian + ace Achinese + ach Acoli + ada Adangme + afa Afro-Asiatic (Other) + afh Afrihili + afr Afrikaans + aka Akan + akk Akkadian + alb Albanian + ale Aleut + alg Algonquian Languages + amh Amharic + ang English, Old (ca. 450-1100) + apa Apache Languages + ara Arabic + arc Aramaic + arm Armenian + arn Araucanian + arp Arapaho + art Artificial (Other) + arw Arawak + asm Assamese + ath Athapascan Languages + ava Avaric + ave Avestan + awa Awadhi + aym Aymara + aze Azerbaijani + bad Banda + bai Bamileke Languages + bak Bashkir + bal Baluchi + bam Bambara + ban Balinese + baq Basque + bas Basa + bat Baltic (Other) + bej Beja + bel Byelorussian + bem Bemba + ben Bengali + ber Berber (Other) + bho Bhojpuri + bih Bihari + bik Bikol + bin Bini + bis Bislama + bla Siksika + bnt Bantu (Other) + bod Tibetan + bra Braj + bre Breton + bua Buriat + bug Buginese + bul Bulgarian + bur Burmese + cad Caddo + cai Central American Indian (Other) + car Carib + cat Catalan + cau Caucasian (Other) + ceb Cebuano + cel Celtic (Other) + ces Czech + cha Chamorro + chb Chibcha + che Chechen + chg Chagatai + chi Chinese + chm Mari + chn Chinook jargon + cho Choctaw + chr Cherokee + chu Church Slavic + chv Chuvash + chy Cheyenne + cop Coptic + cor Cornish + cos Corsican + cpe Creoles and Pidgins, English-based (Other) + cpf Creoles and Pidgins, French-based (Other) + cpp Creoles and Pidgins, Portuguese-based (Other) + cre Cree + crp Creoles and Pidgins (Other) + cus Cushitic (Other) + cym Welsh + cze Czech + dak Dakota + dan Danish + del Delaware + deu German + din Dinka + div Divehi + doi Dogri + dra Dravidian (Other) + dua Duala + dum Dutch, Middle (ca. 1050-1350) + dut Dutch + dyu Dyula + dzo Dzongkha + efi Efik + egy Egyptian (Ancient) + eka Ekajuk + ell Greek, Modern (1453-) + elx Elamite + eng English + enm English, Middle (ca. 1100-1500) + epo Esperanto + esk Eskimo (Other) + esl Spanish + est Estonian + eus Basque + ewe Ewe + ewo Ewondo + fan Fang + fao Faroese + fas Persian + fat Fanti + fij Fijian + fin Finnish + fiu Finno-Ugrian (Other) + fon Fon + fra French + fre French + frm French, Middle (ca. 1400-1600) + fro French, Old (842- ca. 1400) + fry Frisian + ful Fulah + gaa Ga + gae Gaelic (Scots) + gai Irish + gay Gayo + gdh Gaelic (Scots) + gem Germanic (Other) + geo Georgian + ger German + gez Geez + gil Gilbertese + glg Gallegan + gmh German, Middle High (ca. 1050-1500) + goh German, Old High (ca. 750-1050) + gon Gondi + got Gothic + grb Grebo + grc Greek, Ancient (to 1453) + gre Greek, Modern (1453-) + grn Guarani + guj Gujarati + hai Haida + hau Hausa + haw Hawaiian + heb Hebrew + her Herero + hil Hiligaynon + him Himachali + hin Hindi + hmo Hiri Motu + hun Hungarian + hup Hupa + hye Armenian + iba Iban + ibo Igbo + ice Icelandic + ijo Ijo + iku Inuktitut + ilo Iloko + ina Interlingua (International Auxiliary language Association) + inc Indic (Other) + ind Indonesian + ine Indo-European (Other) + ine Interlingue + ipk Inupiak + ira Iranian (Other) + iri Irish + iro Iroquoian uages + isl Icelandic + ita Italian + jav Javanese + jaw Javanese + jpn Japanese + jpr Judeo-Persian + jrb Judeo-Arabic + kaa Kara-Kalpak + kab Kabyle + kac Kachin + kal Greenlandic + kam Kamba + kan Kannada + kar Karen + kas Kashmiri + kat Georgian + kau Kanuri + kaw Kawi + kaz Kazakh + kha Khasi + khi Khoisan (Other) + khm Khmer + kho Khotanese + kik Kikuyu + kin Kinyarwanda + kir Kirghiz + kok Konkani + kom Komi + kon Kongo + kor Korean + kpe Kpelle + kro Kru + kru Kurukh + kua Kuanyama + kum Kumyk + kur Kurdish + kus Kusaie + kut Kutenai + lad Ladino + lah Lahnda + lam Lamba + lao Lao + lat Latin + lav Latvian + lez Lezghian + lin Lingala + lit Lithuanian + lol Mongo + loz Lozi + ltz Letzeburgesch + lub Luba-Katanga + lug Ganda + lui Luiseno + lun Lunda + luo Luo (Kenya and Tanzania) + mac Macedonian + mad Madurese + mag Magahi + mah Marshall + mai Maithili + mak Macedonian + mak Makasar + mal Malayalam + man Mandingo + mao Maori + map Austronesian (Other) + mar Marathi + mas Masai + max Manx + may Malay + men Mende + mga Irish, Middle (900 - 1200) + mic Micmac + min Minangkabau + mis Miscellaneous (Other) + mkh Mon-Kmer (Other) + mlg Malagasy + mlt Maltese + mni Manipuri + mno Manobo Languages + moh Mohawk + mol Moldavian + mon Mongolian + mos Mossi + mri Maori + msa Malay + mul Multiple Languages + mun Munda Languages + mus Creek + mwr Marwari + mya Burmese + myn Mayan Languages + nah Aztec + nai North American Indian (Other) + nau Nauru + nav Navajo + nbl Ndebele, South + nde Ndebele, North + ndo Ndongo + nep Nepali + new Newari + nic Niger-Kordofanian (Other) + niu Niuean + nla Dutch + nno Norwegian (Nynorsk) + non Norse, Old + nor Norwegian + nso Sotho, Northern + nub Nubian Languages + nya Nyanja + nym Nyamwezi + nyn Nyankole + nyo Nyoro + nzi Nzima + oci Langue d'Oc (post 1500) + oji Ojibwa + ori Oriya + orm Oromo + osa Osage + oss Ossetic + ota Turkish, Ottoman (1500 - 1928) + oto Otomian Languages + paa Papuan-Australian (Other) + pag Pangasinan + pal Pahlavi + pam Pampanga + pan Panjabi + pap Papiamento + pau Palauan + peo Persian, Old (ca 600 - 400 B.C.) + per Persian + phn Phoenician + pli Pali + pol Polish + pon Ponape + por Portuguese + pra Prakrit uages + pro Provencal, Old (to 1500) + pus Pushto + que Quechua + raj Rajasthani + rar Rarotongan + roa Romance (Other) + roh Rhaeto-Romance + rom Romany + ron Romanian + rum Romanian + run Rundi + rus Russian + sad Sandawe + sag Sango + sah Yakut + sai South American Indian (Other) + sal Salishan Languages + sam Samaritan Aramaic + san Sanskrit + sco Scots + scr Serbo-Croatian + sel Selkup + sem Semitic (Other) + sga Irish, Old (to 900) + shn Shan + sid Sidamo + sin Singhalese + sio Siouan Languages + sit Sino-Tibetan (Other) + sla Slavic (Other) + slk Slovak + slo Slovak + slv Slovenian + smi Sami Languages + smo Samoan + sna Shona + snd Sindhi + sog Sogdian + som Somali + son Songhai + sot Sotho, Southern + spa Spanish + sqi Albanian + srd Sardinian + srr Serer + ssa Nilo-Saharan (Other) + ssw Siswant + ssw Swazi + suk Sukuma + sun Sudanese + sus Susu + sux Sumerian + sve Swedish + swa Swahili + swe Swedish + syr Syriac + tah Tahitian + tam Tamil + tat Tatar + tel Telugu + tem Timne + ter Tereno + tgk Tajik + tgl Tagalog + tha Thai + tib Tibetan + tig Tigre + tir Tigrinya + tiv Tivi + tli Tlingit + tmh Tamashek + tog Tonga (Nyasa) + ton Tonga (Tonga Islands) + tru Truk + tsi Tsimshian + tsn Tswana + tso Tsonga + tuk Turkmen + tum Tumbuka + tur Turkish + tut Altaic (Other) + twi Twi + tyv Tuvinian + uga Ugaritic + uig Uighur + ukr Ukrainian + umb Umbundu + und Undetermined + urd Urdu + uzb Uzbek + vai Vai + ven Venda + vie Vietnamese + vol Volapük + vot Votic + wak Wakashan Languages + wal Walamo + war Waray + was Washo + wel Welsh + wen Sorbian Languages + wol Wolof + xho Xhosa + yao Yao + yap Yap + yid Yiddish + yor Yoruba + zap Zapotec + zen Zenaga + zha Zhuang + zho Chinese + zul Zulu + zun Zuni + + */ + + return getid3_lib::EmbeddedLookup($languagecode, $begin, __LINE__, __FILE__, 'id3v2-languagecode'); + } + + /** + * @param int $index + * + * @return string + */ + public static function ETCOEventLookup($index) { + if (($index >= 0x17) && ($index <= 0xDF)) { + return 'reserved for future use'; + } + if (($index >= 0xE0) && ($index <= 0xEF)) { + return 'not predefined synch 0-F'; + } + if (($index >= 0xF0) && ($index <= 0xFC)) { + return 'reserved for future use'; + } + + static $EventLookup = array( + 0x00 => 'padding (has no meaning)', + 0x01 => 'end of initial silence', + 0x02 => 'intro start', + 0x03 => 'main part start', + 0x04 => 'outro start', + 0x05 => 'outro end', + 0x06 => 'verse start', + 0x07 => 'refrain start', + 0x08 => 'interlude start', + 0x09 => 'theme start', + 0x0A => 'variation start', + 0x0B => 'key change', + 0x0C => 'time change', + 0x0D => 'momentary unwanted noise (Snap, Crackle & Pop)', + 0x0E => 'sustained noise', + 0x0F => 'sustained noise end', + 0x10 => 'intro end', + 0x11 => 'main part end', + 0x12 => 'verse end', + 0x13 => 'refrain end', + 0x14 => 'theme end', + 0x15 => 'profanity', + 0x16 => 'profanity end', + 0xFD => 'audio end (start of silence)', + 0xFE => 'audio file ends', + 0xFF => 'one more byte of events follows' + ); + + return (isset($EventLookup[$index]) ? $EventLookup[$index] : ''); + } + + /** + * @param int $index + * + * @return string + */ + public static function SYTLContentTypeLookup($index) { + static $SYTLContentTypeLookup = array( + 0x00 => 'other', + 0x01 => 'lyrics', + 0x02 => 'text transcription', + 0x03 => 'movement/part name', // (e.g. 'Adagio') + 0x04 => 'events', // (e.g. 'Don Quijote enters the stage') + 0x05 => 'chord', // (e.g. 'Bb F Fsus') + 0x06 => 'trivia/\'pop up\' information', + 0x07 => 'URLs to webpages', + 0x08 => 'URLs to images' + ); + + return (isset($SYTLContentTypeLookup[$index]) ? $SYTLContentTypeLookup[$index] : ''); + } + + /** + * @param int $index + * @param bool $returnarray + * + * @return array|string + */ + public static function APICPictureTypeLookup($index, $returnarray=false) { + static $APICPictureTypeLookup = array( + 0x00 => 'Other', + 0x01 => '32x32 pixels \'file icon\' (PNG only)', + 0x02 => 'Other file icon', + 0x03 => 'Cover (front)', + 0x04 => 'Cover (back)', + 0x05 => 'Leaflet page', + 0x06 => 'Media (e.g. label side of CD)', + 0x07 => 'Lead artist/lead performer/soloist', + 0x08 => 'Artist/performer', + 0x09 => 'Conductor', + 0x0A => 'Band/Orchestra', + 0x0B => 'Composer', + 0x0C => 'Lyricist/text writer', + 0x0D => 'Recording Location', + 0x0E => 'During recording', + 0x0F => 'During performance', + 0x10 => 'Movie/video screen capture', + 0x11 => 'A bright coloured fish', + 0x12 => 'Illustration', + 0x13 => 'Band/artist logotype', + 0x14 => 'Publisher/Studio logotype' + ); + if ($returnarray) { + return $APICPictureTypeLookup; + } + return (isset($APICPictureTypeLookup[$index]) ? $APICPictureTypeLookup[$index] : ''); + } + + /** + * @param int $index + * + * @return string + */ + public static function COMRReceivedAsLookup($index) { + static $COMRReceivedAsLookup = array( + 0x00 => 'Other', + 0x01 => 'Standard CD album with other songs', + 0x02 => 'Compressed audio on CD', + 0x03 => 'File over the Internet', + 0x04 => 'Stream over the Internet', + 0x05 => 'As note sheets', + 0x06 => 'As note sheets in a book with other sheets', + 0x07 => 'Music on other media', + 0x08 => 'Non-musical merchandise' + ); + + return (isset($COMRReceivedAsLookup[$index]) ? $COMRReceivedAsLookup[$index] : ''); + } + + /** + * @param int $index + * + * @return string + */ + public static function RVA2ChannelTypeLookup($index) { + static $RVA2ChannelTypeLookup = array( + 0x00 => 'Other', + 0x01 => 'Master volume', + 0x02 => 'Front right', + 0x03 => 'Front left', + 0x04 => 'Back right', + 0x05 => 'Back left', + 0x06 => 'Front centre', + 0x07 => 'Back centre', + 0x08 => 'Subwoofer' + ); + + return (isset($RVA2ChannelTypeLookup[$index]) ? $RVA2ChannelTypeLookup[$index] : ''); + } + + /** + * @param string $framename + * + * @return string + */ + public static function FrameNameLongLookup($framename) { + + $begin = __LINE__; + + /** This is not a comment! + + AENC Audio encryption + APIC Attached picture + ASPI Audio seek point index + BUF Recommended buffer size + CNT Play counter + COM Comments + COMM Comments + COMR Commercial frame + CRA Audio encryption + CRM Encrypted meta frame + ENCR Encryption method registration + EQU Equalisation + EQU2 Equalisation (2) + EQUA Equalisation + ETC Event timing codes + ETCO Event timing codes + GEO General encapsulated object + GEOB General encapsulated object + GRID Group identification registration + IPL Involved people list + IPLS Involved people list + LINK Linked information + LNK Linked information + MCDI Music CD identifier + MCI Music CD Identifier + MLL MPEG location lookup table + MLLT MPEG location lookup table + OWNE Ownership frame + PCNT Play counter + PIC Attached picture + POP Popularimeter + POPM Popularimeter + POSS Position synchronisation frame + PRIV Private frame + RBUF Recommended buffer size + REV Reverb + RVA Relative volume adjustment + RVA2 Relative volume adjustment (2) + RVAD Relative volume adjustment + RVRB Reverb + SEEK Seek frame + SIGN Signature frame + SLT Synchronised lyric/text + STC Synced tempo codes + SYLT Synchronised lyric/text + SYTC Synchronised tempo codes + TAL Album/Movie/Show title + TALB Album/Movie/Show title + TBP BPM (Beats Per Minute) + TBPM BPM (beats per minute) + TCM Composer + TCMP Part of a compilation + TCO Content type + TCOM Composer + TCON Content type + TCOP Copyright message + TCP Part of a compilation + TCR Copyright message + TDA Date + TDAT Date + TDEN Encoding time + TDLY Playlist delay + TDOR Original release time + TDRC Recording time + TDRL Release time + TDTG Tagging time + TDY Playlist delay + TEN Encoded by + TENC Encoded by + TEXT Lyricist/Text writer + TFLT File type + TFT File type + TIM Time + TIME Time + TIPL Involved people list + TIT1 Content group description + TIT2 Title/songname/content description + TIT3 Subtitle/Description refinement + TKE Initial key + TKEY Initial key + TLA Language(s) + TLAN Language(s) + TLE Length + TLEN Length + TMCL Musician credits list + TMED Media type + TMOO Mood + TMT Media type + TOA Original artist(s)/performer(s) + TOAL Original album/movie/show title + TOF Original filename + TOFN Original filename + TOL Original Lyricist(s)/text writer(s) + TOLY Original lyricist(s)/text writer(s) + TOPE Original artist(s)/performer(s) + TOR Original release year + TORY Original release year + TOT Original album/Movie/Show title + TOWN File owner/licensee + TP1 Lead artist(s)/Lead performer(s)/Soloist(s)/Performing group + TP2 Band/Orchestra/Accompaniment + TP3 Conductor/Performer refinement + TP4 Interpreted, remixed, or otherwise modified by + TPA Part of a set + TPB Publisher + TPE1 Lead performer(s)/Soloist(s) + TPE2 Band/orchestra/accompaniment + TPE3 Conductor/performer refinement + TPE4 Interpreted, remixed, or otherwise modified by + TPOS Part of a set + TPRO Produced notice + TPUB Publisher + TRC ISRC (International Standard Recording Code) + TRCK Track number/Position in set + TRD Recording dates + TRDA Recording dates + TRK Track number/Position in set + TRSN Internet radio station name + TRSO Internet radio station owner + TS2 Album-Artist sort order + TSA Album sort order + TSC Composer sort order + TSI Size + TSIZ Size + TSO2 Album-Artist sort order + TSOA Album sort order + TSOC Composer sort order + TSOP Performer sort order + TSOT Title sort order + TSP Performer sort order + TSRC ISRC (international standard recording code) + TSS Software/hardware and settings used for encoding + TSSE Software/Hardware and settings used for encoding + TSST Set subtitle + TST Title sort order + TT1 Content group description + TT2 Title/Songname/Content description + TT3 Subtitle/Description refinement + TXT Lyricist/text writer + TXX User defined text information frame + TXXX User defined text information frame + TYE Year + TYER Year + UFI Unique file identifier + UFID Unique file identifier + ULT Unsynchronised lyric/text transcription + USER Terms of use + USLT Unsynchronised lyric/text transcription + WAF Official audio file webpage + WAR Official artist/performer webpage + WAS Official audio source webpage + WCM Commercial information + WCOM Commercial information + WCOP Copyright/Legal information + WCP Copyright/Legal information + WOAF Official audio file webpage + WOAR Official artist/performer webpage + WOAS Official audio source webpage + WORS Official Internet radio station homepage + WPAY Payment + WPB Publishers official webpage + WPUB Publishers official webpage + WXX User defined URL link frame + WXXX User defined URL link frame + TFEA Featured Artist + TSTU Recording Studio + rgad Replay Gain Adjustment + + */ + + return getid3_lib::EmbeddedLookup($framename, $begin, __LINE__, __FILE__, 'id3v2-framename_long'); + + // Last three: + // from Helium2 [www.helium2.com] + // from http://privatewww.essex.ac.uk/~djmrob/replaygain/file_format_id3v2.html + } + + /** + * @param string $framename + * + * @return string + */ + public static function FrameNameShortLookup($framename) { + + $begin = __LINE__; + + /** This is not a comment! + + AENC audio_encryption + APIC attached_picture + ASPI audio_seek_point_index + BUF recommended_buffer_size + CNT play_counter + COM comment + COMM comment + COMR commercial_frame + CRA audio_encryption + CRM encrypted_meta_frame + ENCR encryption_method_registration + EQU equalisation + EQU2 equalisation + EQUA equalisation + ETC event_timing_codes + ETCO event_timing_codes + GEO general_encapsulated_object + GEOB general_encapsulated_object + GRID group_identification_registration + IPL involved_people_list + IPLS involved_people_list + LINK linked_information + LNK linked_information + MCDI music_cd_identifier + MCI music_cd_identifier + MLL mpeg_location_lookup_table + MLLT mpeg_location_lookup_table + OWNE ownership_frame + PCNT play_counter + PIC attached_picture + POP popularimeter + POPM popularimeter + POSS position_synchronisation_frame + PRIV private_frame + RBUF recommended_buffer_size + REV reverb + RVA relative_volume_adjustment + RVA2 relative_volume_adjustment + RVAD relative_volume_adjustment + RVRB reverb + SEEK seek_frame + SIGN signature_frame + SLT synchronised_lyric + STC synced_tempo_codes + SYLT synchronised_lyric + SYTC synchronised_tempo_codes + TAL album + TALB album + TBP bpm + TBPM bpm + TCM composer + TCMP part_of_a_compilation + TCO genre + TCOM composer + TCON genre + TCOP copyright_message + TCP part_of_a_compilation + TCR copyright_message + TDA date + TDAT date + TDEN encoding_time + TDLY playlist_delay + TDOR original_release_time + TDRC recording_time + TDRL release_time + TDTG tagging_time + TDY playlist_delay + TEN encoded_by + TENC encoded_by + TEXT lyricist + TFLT file_type + TFT file_type + TIM time + TIME time + TIPL involved_people_list + TIT1 content_group_description + TIT2 title + TIT3 subtitle + TKE initial_key + TKEY initial_key + TLA language + TLAN language + TLE length + TLEN length + TMCL musician_credits_list + TMED media_type + TMOO mood + TMT media_type + TOA original_artist + TOAL original_album + TOF original_filename + TOFN original_filename + TOL original_lyricist + TOLY original_lyricist + TOPE original_artist + TOR original_year + TORY original_year + TOT original_album + TOWN file_owner + TP1 artist + TP2 band + TP3 conductor + TP4 remixer + TPA part_of_a_set + TPB publisher + TPE1 artist + TPE2 band + TPE3 conductor + TPE4 remixer + TPOS part_of_a_set + TPRO produced_notice + TPUB publisher + TRC isrc + TRCK track_number + TRD recording_dates + TRDA recording_dates + TRK track_number + TRSN internet_radio_station_name + TRSO internet_radio_station_owner + TS2 album_artist_sort_order + TSA album_sort_order + TSC composer_sort_order + TSI size + TSIZ size + TSO2 album_artist_sort_order + TSOA album_sort_order + TSOC composer_sort_order + TSOP performer_sort_order + TSOT title_sort_order + TSP performer_sort_order + TSRC isrc + TSS encoder_settings + TSSE encoder_settings + TSST set_subtitle + TST title_sort_order + TT1 content_group_description + TT2 title + TT3 subtitle + TXT lyricist + TXX text + TXXX text + TYE year + TYER year + UFI unique_file_identifier + UFID unique_file_identifier + ULT unsynchronised_lyric + USER terms_of_use + USLT unsynchronised_lyric + WAF url_file + WAR url_artist + WAS url_source + WCM commercial_information + WCOM commercial_information + WCOP copyright + WCP copyright + WOAF url_file + WOAR url_artist + WOAS url_source + WORS url_station + WPAY url_payment + WPB url_publisher + WPUB url_publisher + WXX url_user + WXXX url_user + TFEA featured_artist + TSTU recording_studio + rgad replay_gain_adjustment + + */ + + return getid3_lib::EmbeddedLookup($framename, $begin, __LINE__, __FILE__, 'id3v2-framename_short'); + } + + /** + * @param string $encoding + * + * @return string + */ + public static function TextEncodingTerminatorLookup($encoding) { + // http://www.id3.org/id3v2.4.0-structure.txt + // Frames that allow different types of text encoding contains a text encoding description byte. Possible encodings: + static $TextEncodingTerminatorLookup = array( + 0 => "\x00", // $00 ISO-8859-1. Terminated with $00. + 1 => "\x00\x00", // $01 UTF-16 encoded Unicode with BOM. All strings in the same frame SHALL have the same byteorder. Terminated with $00 00. + 2 => "\x00\x00", // $02 UTF-16BE encoded Unicode without BOM. Terminated with $00 00. + 3 => "\x00", // $03 UTF-8 encoded Unicode. Terminated with $00. + 255 => "\x00\x00" + ); + return (isset($TextEncodingTerminatorLookup[$encoding]) ? $TextEncodingTerminatorLookup[$encoding] : "\x00"); + } + + /** + * @param int $encoding + * + * @return string + */ + public static function TextEncodingNameLookup($encoding) { + // http://www.id3.org/id3v2.4.0-structure.txt + // Frames that allow different types of text encoding contains a text encoding description byte. Possible encodings: + static $TextEncodingNameLookup = array( + 0 => 'ISO-8859-1', // $00 ISO-8859-1. Terminated with $00. + 1 => 'UTF-16', // $01 UTF-16 encoded Unicode with BOM. All strings in the same frame SHALL have the same byteorder. Terminated with $00 00. + 2 => 'UTF-16BE', // $02 UTF-16BE encoded Unicode without BOM. Terminated with $00 00. + 3 => 'UTF-8', // $03 UTF-8 encoded Unicode. Terminated with $00. + 255 => 'UTF-16BE' + ); + return (isset($TextEncodingNameLookup[$encoding]) ? $TextEncodingNameLookup[$encoding] : 'ISO-8859-1'); + } + + /** + * @param string $string + * @param string $terminator + * + * @return string + */ + public static function RemoveStringTerminator($string, $terminator) { + // Null terminator at end of comment string is somewhat ambiguous in the specification, may or may not be implemented by various taggers. Remove terminator only if present. + // https://github.com/JamesHeinrich/getID3/issues/121 + // https://community.mp3tag.de/t/x-trailing-nulls-in-id3v2-comments/19227 + if (substr($string, -strlen($terminator), strlen($terminator)) === $terminator) { + $string = substr($string, 0, -strlen($terminator)); + } + return $string; + } + + /** + * @param string $string + * + * @return string + */ + public static function MakeUTF16emptyStringEmpty($string) { + if (in_array($string, array("\x00", "\x00\x00", "\xFF\xFE", "\xFE\xFF"))) { + // if string only contains a BOM or terminator then make it actually an empty string + $string = ''; + } + return $string; + } + + /** + * @param string $framename + * @param int $id3v2majorversion + * + * @return bool|int + */ + public static function IsValidID3v2FrameName($framename, $id3v2majorversion) { + switch ($id3v2majorversion) { + case 2: + return preg_match('#[A-Z][A-Z0-9]{2}#', $framename); + + case 3: + case 4: + return preg_match('#[A-Z][A-Z0-9]{3}#', $framename); + } + return false; + } + + /** + * @param string $numberstring + * @param bool $allowdecimal + * @param bool $allownegative + * + * @return bool + */ + public static function IsANumber($numberstring, $allowdecimal=false, $allownegative=false) { + for ($i = 0; $i < strlen($numberstring); $i++) { + if ((chr($numberstring[$i]) < chr('0')) || (chr($numberstring[$i]) > chr('9'))) { + if (($numberstring[$i] == '.') && $allowdecimal) { + // allowed + } elseif (($numberstring[$i] == '-') && $allownegative && ($i == 0)) { + // allowed + } else { + return false; + } + } + } + return true; + } + + /** + * @param string $datestamp + * + * @return bool + */ + public static function IsValidDateStampString($datestamp) { + if (strlen($datestamp) != 8) { + return false; + } + if (!self::IsANumber($datestamp, false)) { + return false; + } + $year = substr($datestamp, 0, 4); + $month = substr($datestamp, 4, 2); + $day = substr($datestamp, 6, 2); + if (($year == 0) || ($month == 0) || ($day == 0)) { + return false; + } + if ($month > 12) { + return false; + } + if ($day > 31) { + return false; + } + if (($day > 30) && (($month == 4) || ($month == 6) || ($month == 9) || ($month == 11))) { + return false; + } + if (($day > 29) && ($month == 2)) { + return false; + } + return true; + } + + /** + * @param int $majorversion + * + * @return int + */ + public static function ID3v2HeaderLength($majorversion) { + return (($majorversion == 2) ? 6 : 10); + } + + /** + * @param string $frame_name + * + * @return string|false + */ + public static function ID3v22iTunesBrokenFrameName($frame_name) { + // iTunes (multiple versions) has been known to write ID3v2.3 style frames + // but use ID3v2.2 frame names, right-padded using either [space] or [null] + // to make them fit in the 4-byte frame name space of the ID3v2.3 frame. + // This function will detect and translate the corrupt frame name into ID3v2.3 standard. + static $ID3v22_iTunes_BrokenFrames = array( + 'BUF' => 'RBUF', // Recommended buffer size + 'CNT' => 'PCNT', // Play counter + 'COM' => 'COMM', // Comments + 'CRA' => 'AENC', // Audio encryption + 'EQU' => 'EQUA', // Equalisation + 'ETC' => 'ETCO', // Event timing codes + 'GEO' => 'GEOB', // General encapsulated object + 'IPL' => 'IPLS', // Involved people list + 'LNK' => 'LINK', // Linked information + 'MCI' => 'MCDI', // Music CD identifier + 'MLL' => 'MLLT', // MPEG location lookup table + 'PIC' => 'APIC', // Attached picture + 'POP' => 'POPM', // Popularimeter + 'REV' => 'RVRB', // Reverb + 'RVA' => 'RVAD', // Relative volume adjustment + 'SLT' => 'SYLT', // Synchronised lyric/text + 'STC' => 'SYTC', // Synchronised tempo codes + 'TAL' => 'TALB', // Album/Movie/Show title + 'TBP' => 'TBPM', // BPM (beats per minute) + 'TCM' => 'TCOM', // Composer + 'TCO' => 'TCON', // Content type + 'TCP' => 'TCMP', // Part of a compilation + 'TCR' => 'TCOP', // Copyright message + 'TDA' => 'TDAT', // Date + 'TDY' => 'TDLY', // Playlist delay + 'TEN' => 'TENC', // Encoded by + 'TFT' => 'TFLT', // File type + 'TIM' => 'TIME', // Time + 'TKE' => 'TKEY', // Initial key + 'TLA' => 'TLAN', // Language(s) + 'TLE' => 'TLEN', // Length + 'TMT' => 'TMED', // Media type + 'TOA' => 'TOPE', // Original artist(s)/performer(s) + 'TOF' => 'TOFN', // Original filename + 'TOL' => 'TOLY', // Original lyricist(s)/text writer(s) + 'TOR' => 'TORY', // Original release year + 'TOT' => 'TOAL', // Original album/movie/show title + 'TP1' => 'TPE1', // Lead performer(s)/Soloist(s) + 'TP2' => 'TPE2', // Band/orchestra/accompaniment + 'TP3' => 'TPE3', // Conductor/performer refinement + 'TP4' => 'TPE4', // Interpreted, remixed, or otherwise modified by + 'TPA' => 'TPOS', // Part of a set + 'TPB' => 'TPUB', // Publisher + 'TRC' => 'TSRC', // ISRC (international standard recording code) + 'TRD' => 'TRDA', // Recording dates + 'TRK' => 'TRCK', // Track number/Position in set + 'TS2' => 'TSO2', // Album-Artist sort order + 'TSA' => 'TSOA', // Album sort order + 'TSC' => 'TSOC', // Composer sort order + 'TSI' => 'TSIZ', // Size + 'TSP' => 'TSOP', // Performer sort order + 'TSS' => 'TSSE', // Software/Hardware and settings used for encoding + 'TST' => 'TSOT', // Title sort order + 'TT1' => 'TIT1', // Content group description + 'TT2' => 'TIT2', // Title/songname/content description + 'TT3' => 'TIT3', // Subtitle/Description refinement + 'TXT' => 'TEXT', // Lyricist/Text writer + 'TXX' => 'TXXX', // User defined text information frame + 'TYE' => 'TYER', // Year + 'UFI' => 'UFID', // Unique file identifier + 'ULT' => 'USLT', // Unsynchronised lyric/text transcription + 'WAF' => 'WOAF', // Official audio file webpage + 'WAR' => 'WOAR', // Official artist/performer webpage + 'WAS' => 'WOAS', // Official audio source webpage + 'WCM' => 'WCOM', // Commercial information + 'WCP' => 'WCOP', // Copyright/Legal information + 'WPB' => 'WPUB', // Publishers official webpage + 'WXX' => 'WXXX', // User defined URL link frame + ); + if (strlen($frame_name) == 4) { + if ((substr($frame_name, 3, 1) == ' ') || (substr($frame_name, 3, 1) == "\x00")) { + if (isset($ID3v22_iTunes_BrokenFrames[substr($frame_name, 0, 3)])) { + return $ID3v22_iTunes_BrokenFrames[substr($frame_name, 0, 3)]; + } + } + } + return false; + } + +} + diff --git a/projects/tests/tests/performance/ID3/module.tag.lyrics3.php b/projects/tests/tests/performance/ID3/module.tag.lyrics3.php new file mode 100644 index 00000000..b2375057 --- /dev/null +++ b/projects/tests/tests/performance/ID3/module.tag.lyrics3.php @@ -0,0 +1,326 @@ + // +// available at https://github.com/JamesHeinrich/getID3 // +// or https://www.getid3.org // +// or http://getid3.sourceforge.net // +// see readme.txt for more details // +///////////////////////////////////////////////////////////////// +/// // +// module.tag.lyrics3.php // +// module for analyzing Lyrics3 tags // +// dependencies: module.tag.apetag.php (optional) // +// /// +///////////////////////////////////////////////////////////////// + +if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers + exit; +} +class getid3_lyrics3 extends getid3_handler +{ + /** + * @return bool + */ + public function Analyze() { + $info = &$this->getid3->info; + + // http://www.volweb.cz/str/tags.htm + + if (!getid3_lib::intValueSupported($info['filesize'])) { + $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); + return false; + } + + $this->fseek((0 - 128 - 9 - 6), SEEK_END); // end - ID3v1 - "LYRICSEND" - [Lyrics3size] + $lyrics3_id3v1 = $this->fread(128 + 9 + 6); + $lyrics3lsz = (int) substr($lyrics3_id3v1, 0, 6); // Lyrics3size + $lyrics3end = substr($lyrics3_id3v1, 6, 9); // LYRICSEND or LYRICS200 + $id3v1tag = substr($lyrics3_id3v1, 15, 128); // ID3v1 + + if ($lyrics3end == 'LYRICSEND') { + // Lyrics3v1, ID3v1, no APE + + $lyrics3size = 5100; + $lyrics3offset = $info['filesize'] - 128 - $lyrics3size; + $lyrics3version = 1; + + } elseif ($lyrics3end == 'LYRICS200') { + // Lyrics3v2, ID3v1, no APE + + // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' + $lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200'); + $lyrics3offset = $info['filesize'] - 128 - $lyrics3size; + $lyrics3version = 2; + + } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICSEND')) { + // Lyrics3v1, no ID3v1, no APE + + $lyrics3size = 5100; + $lyrics3offset = $info['filesize'] - $lyrics3size; + $lyrics3version = 1; + $lyrics3offset = $info['filesize'] - $lyrics3size; + + } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICS200')) { + + // Lyrics3v2, no ID3v1, no APE + + $lyrics3size = (int) strrev(substr(strrev($lyrics3_id3v1), 9, 6)) + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' + $lyrics3offset = $info['filesize'] - $lyrics3size; + $lyrics3version = 2; + + } else { + + if (isset($info['ape']['tag_offset_start']) && ($info['ape']['tag_offset_start'] > 15)) { + + $this->fseek($info['ape']['tag_offset_start'] - 15); + $lyrics3lsz = $this->fread(6); + $lyrics3end = $this->fread(9); + + if ($lyrics3end == 'LYRICSEND') { + // Lyrics3v1, APE, maybe ID3v1 + + $lyrics3size = 5100; + $lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size; + $info['avdataend'] = $lyrics3offset; + $lyrics3version = 1; + $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability'); + + } elseif ($lyrics3end == 'LYRICS200') { + // Lyrics3v2, APE, maybe ID3v1 + + $lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' + $lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size; + $lyrics3version = 2; + $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability'); + + } + + } + + } + + if (isset($lyrics3offset) && isset($lyrics3version) && isset($lyrics3size)) { + $info['avdataend'] = $lyrics3offset; + $this->getLyrics3Data($lyrics3offset, $lyrics3version, $lyrics3size); + + if (!isset($info['ape'])) { + if (isset($info['lyrics3']['tag_offset_start'])) { + $GETID3_ERRORARRAY = &$info['warning']; + getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.apetag.php', __FILE__, true); + $getid3_temp = new getID3(); + $getid3_temp->openfile($this->getid3->filename, $this->getid3->info['filesize'], $this->getid3->fp); + $getid3_apetag = new getid3_apetag($getid3_temp); + $getid3_apetag->overrideendoffset = $info['lyrics3']['tag_offset_start']; + $getid3_apetag->Analyze(); + if (!empty($getid3_temp->info['ape'])) { + $info['ape'] = $getid3_temp->info['ape']; + } + if (!empty($getid3_temp->info['replay_gain'])) { + $info['replay_gain'] = $getid3_temp->info['replay_gain']; + } + unset($getid3_temp, $getid3_apetag); + } else { + $this->warning('Lyrics3 and APE tags appear to have become entangled (most likely due to updating the APE tags with a non-Lyrics3-aware tagger)'); + } + } + + } + + return true; + } + + /** + * @param int $endoffset + * @param int $version + * @param int $length + * + * @return bool + */ + public function getLyrics3Data($endoffset, $version, $length) { + // http://www.volweb.cz/str/tags.htm + + $info = &$this->getid3->info; + + if (!getid3_lib::intValueSupported($endoffset)) { + $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); + return false; + } + + $this->fseek($endoffset); + if ($length <= 0) { + return false; + } + $rawdata = $this->fread($length); + + $ParsedLyrics3 = array(); + + $ParsedLyrics3['raw']['lyrics3version'] = $version; + $ParsedLyrics3['raw']['lyrics3tagsize'] = $length; + $ParsedLyrics3['tag_offset_start'] = $endoffset; + $ParsedLyrics3['tag_offset_end'] = $endoffset + $length - 1; + + if (substr($rawdata, 0, 11) != 'LYRICSBEGIN') { + if (strpos($rawdata, 'LYRICSBEGIN') !== false) { + + $this->warning('"LYRICSBEGIN" expected at '.$endoffset.' but actually found at '.($endoffset + strpos($rawdata, 'LYRICSBEGIN')).' - this is invalid for Lyrics3 v'.$version); + $info['avdataend'] = $endoffset + strpos($rawdata, 'LYRICSBEGIN'); + $rawdata = substr($rawdata, strpos($rawdata, 'LYRICSBEGIN')); + $length = strlen($rawdata); + $ParsedLyrics3['tag_offset_start'] = $info['avdataend']; + $ParsedLyrics3['raw']['lyrics3tagsize'] = $length; + + } else { + + $this->error('"LYRICSBEGIN" expected at '.$endoffset.' but found "'.substr($rawdata, 0, 11).'" instead'); + return false; + + } + + } + + switch ($version) { + + case 1: + if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICSEND') { + $ParsedLyrics3['raw']['LYR'] = trim(substr($rawdata, 11, strlen($rawdata) - 11 - 9)); + $this->Lyrics3LyricsTimestampParse($ParsedLyrics3); + } else { + $this->error('"LYRICSEND" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead'); + return false; + } + break; + + case 2: + if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICS200') { + $ParsedLyrics3['raw']['unparsed'] = substr($rawdata, 11, strlen($rawdata) - 11 - 9 - 6); // LYRICSBEGIN + LYRICS200 + LSZ + $rawdata = $ParsedLyrics3['raw']['unparsed']; + while (strlen($rawdata) > 0) { + $fieldname = substr($rawdata, 0, 3); + $fieldsize = (int) substr($rawdata, 3, 5); + $ParsedLyrics3['raw'][$fieldname] = substr($rawdata, 8, $fieldsize); + $rawdata = substr($rawdata, 3 + 5 + $fieldsize); + } + + if (isset($ParsedLyrics3['raw']['IND'])) { + $i = 0; + $flagnames = array('lyrics', 'timestamps', 'inhibitrandom'); + foreach ($flagnames as $flagname) { + if (strlen($ParsedLyrics3['raw']['IND']) > $i++) { + $ParsedLyrics3['flags'][$flagname] = $this->IntString2Bool(substr($ParsedLyrics3['raw']['IND'], $i, 1 - 1)); + } + } + } + + $fieldnametranslation = array('ETT'=>'title', 'EAR'=>'artist', 'EAL'=>'album', 'INF'=>'comment', 'AUT'=>'author'); + foreach ($fieldnametranslation as $key => $value) { + if (isset($ParsedLyrics3['raw'][$key])) { + $ParsedLyrics3['comments'][$value][] = trim($ParsedLyrics3['raw'][$key]); + } + } + + if (isset($ParsedLyrics3['raw']['IMG'])) { + $imagestrings = explode("\r\n", $ParsedLyrics3['raw']['IMG']); + foreach ($imagestrings as $key => $imagestring) { + if (strpos($imagestring, '||') !== false) { + $imagearray = explode('||', $imagestring); + $ParsedLyrics3['images'][$key]['filename'] = (isset($imagearray[0]) ? $imagearray[0] : ''); + $ParsedLyrics3['images'][$key]['description'] = (isset($imagearray[1]) ? $imagearray[1] : ''); + $ParsedLyrics3['images'][$key]['timestamp'] = $this->Lyrics3Timestamp2Seconds(isset($imagearray[2]) ? $imagearray[2] : ''); + } + } + } + if (isset($ParsedLyrics3['raw']['LYR'])) { + $this->Lyrics3LyricsTimestampParse($ParsedLyrics3); + } + } else { + $this->error('"LYRICS200" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead'); + return false; + } + break; + + default: + $this->error('Cannot process Lyrics3 version '.$version.' (only v1 and v2)'); + return false; + } + + + if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] <= $ParsedLyrics3['tag_offset_end'])) { + $this->warning('ID3v1 tag information ignored since it appears to be a false synch in Lyrics3 tag data'); + unset($info['id3v1']); + foreach ($info['warning'] as $key => $value) { + if ($value == 'Some ID3v1 fields do not use NULL characters for padding') { + unset($info['warning'][$key]); + sort($info['warning']); + break; + } + } + } + + $info['lyrics3'] = $ParsedLyrics3; + + return true; + } + + /** + * @param string $rawtimestamp + * + * @return int|false + */ + public function Lyrics3Timestamp2Seconds($rawtimestamp) { + if (preg_match('#^\\[([0-9]{2}):([0-9]{2})\\]$#', $rawtimestamp, $regs)) { + return (int) (($regs[1] * 60) + $regs[2]); + } + return false; + } + + /** + * @param array $Lyrics3data + * + * @return bool + */ + public function Lyrics3LyricsTimestampParse(&$Lyrics3data) { + $lyricsarray = explode("\r\n", $Lyrics3data['raw']['LYR']); + $notimestamplyricsarray = array(); + foreach ($lyricsarray as $key => $lyricline) { + $regs = array(); + unset($thislinetimestamps); + while (preg_match('#^(\\[[0-9]{2}:[0-9]{2}\\])#', $lyricline, $regs)) { + $thislinetimestamps[] = $this->Lyrics3Timestamp2Seconds($regs[0]); + $lyricline = str_replace($regs[0], '', $lyricline); + } + $notimestamplyricsarray[$key] = $lyricline; + if (isset($thislinetimestamps) && is_array($thislinetimestamps)) { + sort($thislinetimestamps); + foreach ($thislinetimestamps as $timestampkey => $timestamp) { + if (isset($Lyrics3data['synchedlyrics'][$timestamp])) { + // timestamps only have a 1-second resolution, it's possible that multiple lines + // could have the same timestamp, if so, append + $Lyrics3data['synchedlyrics'][$timestamp] .= "\r\n".$lyricline; + } else { + $Lyrics3data['synchedlyrics'][$timestamp] = $lyricline; + } + } + } + } + $Lyrics3data['unsynchedlyrics'] = implode("\r\n", $notimestamplyricsarray); + if (isset($Lyrics3data['synchedlyrics']) && is_array($Lyrics3data['synchedlyrics'])) { + ksort($Lyrics3data['synchedlyrics']); + } + return true; + } + + /** + * @param string $char + * + * @return bool|null + */ + public function IntString2Bool($char) { + if ($char == '1') { + return true; + } elseif ($char == '0') { + return false; + } + return null; + } +} diff --git a/projects/tests/tests/performance/ID3/readme.txt b/projects/tests/tests/performance/ID3/readme.txt new file mode 100644 index 00000000..e48bcef9 --- /dev/null +++ b/projects/tests/tests/performance/ID3/readme.txt @@ -0,0 +1,627 @@ +///////////////////////////////////////////////////////////////// +/// getID3() by James Heinrich // +// available at http://getid3.sourceforge.net // +// or https://www.getid3.org // +// also https://github.com/JamesHeinrich/getID3 // +///////////////////////////////////////////////////////////////// + +***************************************************************** +***************************************************************** + + getID3() is released under multiple licenses. You may choose + from the following licenses, and use getID3 according to the + terms of the license most suitable to your project. + +GNU GPL: https://gnu.org/licenses/gpl.html (v3) + https://gnu.org/licenses/old-licenses/gpl-2.0.html (v2) + https://gnu.org/licenses/old-licenses/gpl-1.0.html (v1) + +GNU LGPL: https://gnu.org/licenses/lgpl.html (v3) + +Mozilla MPL: https://www.mozilla.org/MPL/2.0/ (v2) + +getID3 Commercial License: https://www.getid3.org/#gCL (payment required) + +***************************************************************** +***************************************************************** +Copies of each of the above licenses are included in the 'licenses' +directory of the getID3 distribution. + + + +----------------------------------------------+ + | If you want to donate, there is a link on | + | https://www.getid3.org for PayPal donations. | + +----------------------------------------------+ + + +Quick Start +=========================================================================== + +Q: How can I check that getID3() works on my server/files? +A: Unzip getID3() to a directory, then access /demos/demo.browse.php + + + +Support +=========================================================================== + +Q: I have a question, or I found a bug. What do I do? +A: The preferred method of support requests and/or bug reports is the + forum at http://support.getid3.org/ + + + +Sourceforge Notification +=========================================================================== + +It's highly recommended that you sign up for notification from +Sourceforge for when new versions are released. Please visit: +http://sourceforge.net/project/showfiles.php?group_id=55859 +and click the little "monitor package" icon/link. If you're +previously signed up for the mailing list, be aware that it has +been discontinued, only the automated Sourceforge notification +will be used from now on. + + + +What does getID3() do? +=========================================================================== + +Reads & parses (to varying degrees): + ¤ tags: + * APE (v1 and v2) + * ID3v1 (& ID3v1.1) + * ID3v2 (v2.4, v2.3, v2.2) + * Lyrics3 (v1 & v2) + + ¤ audio-lossy: + * MP3/MP2/MP1 + * MPC / Musepack + * Ogg (Vorbis, OggFLAC, Speex, Opus) + * AAC / MP4 + * AC3 + * DTS + * RealAudio + * Speex + * DSS + * VQF + + ¤ audio-lossless: + * AIFF + * AU + * Bonk + * CD-audio (*.cda) + * FLAC + * LA (Lossless Audio) + * LiteWave + * LPAC + * MIDI + * Monkey's Audio + * OptimFROG + * RKAU + * Shorten + * TTA + * VOC + * WAV (RIFF) + * WavPack + + ¤ audio-video: + * ASF: ASF, Windows Media Audio (WMA), Windows Media Video (WMV) + * AVI (RIFF) + * Flash + * Matroska (MKV) + * MPEG-1 / MPEG-2 + * NSV (Nullsoft Streaming Video) + * Quicktime (including MP4) + * RealVideo + + ¤ still image: + * BMP + * GIF + * JPEG + * PNG + * TIFF + * SWF (Flash) + * PhotoCD + + ¤ data: + * ISO-9660 CD-ROM image (directory structure) + * SZIP (limited support) + * ZIP (directory structure) + * TAR + * CUE + + +Writes: + * ID3v1 (& ID3v1.1) + * ID3v2 (v2.3 & v2.4) + * VorbisComment on OggVorbis + * VorbisComment on FLAC (not OggFLAC) + * APE v2 + * Lyrics3 (delete only) + + + +Requirements +=========================================================================== + +* PHP 4.2.0 up to 5.2.x for getID3() 1.7.x (and earlier) +* PHP 5.0.5 (or higher) for getID3() 1.8.x (and up) +* PHP 5.3.0 (or higher) for getID3() 1.9.17 (and up) +* PHP 5.3.0 (or higher) for getID3() 2.0.x (and up) +* at least 4MB memory for PHP. 8MB or more is highly recommended. + 12MB is required with all modules loaded. + + + +Usage +=========================================================================== + +See /demos/demo.basic.php for a very basic use of getID3() with no +fancy output, just scanning one file. + +See structure.txt for the returned data structure. + +*> For an example of a complete directory-browsing, <* +*> file-scanning implementation of getID3(), please run <* +*> /demos/demo.browse.php <* + +See /demos/demo.mysql.php for a sample recursive scanning code that +scans every file in a given directory, and all sub-directories, stores +the results in a database and allows various analysis / maintenance +operations + +To analyze remote files over HTTP or FTP you need to copy the file +locally first before running getID3(). Your code would look something +like this: + +// Copy remote file locally to scan with getID3() +$remotefilename = 'http://www.example.com/filename.mp3'; +if ($fp_remote = fopen($remotefilename, 'rb')) { + $localtempfilename = tempnam('/tmp', 'getID3'); + if ($fp_local = fopen($localtempfilename, 'wb')) { + while ($buffer = fread($fp_remote, 32768)) { + fwrite($fp_local, $buffer); + } + fclose($fp_local); + + $remote_headers = array_change_key_case(get_headers($remotefilename, 1), CASE_LOWER); + $remote_filesize = (isset($remote_headers['content-length']) ? (is_array($remote_headers['content-length']) ? $remote_headers['content-length'][count($remote_headers['content-length']) - 1] : $remote_headers['content-length']) : null); + + // Initialize getID3 engine + $getID3 = new getID3; + + $ThisFileInfo = $getID3->analyze($localtempfilename, $remote_filesize, basename($remotefilename)); + + // Delete temporary file + unlink($localtempfilename); + } + fclose($fp_remote); +} + +Note: since v1.9.9-20150212 it is possible a second and third parameter +to $getID3->analyze(), for original filesize and original filename +respectively. This permits you to download only a portion of a large remote +file but get accurate playtime estimates, assuming the format only requires +the beginning of the file for correct format analysis. + +See /demos/demo.write.php for how to write tags. + + + +What does the returned data structure look like? +=========================================================================== + +See structure.txt + +It is recommended that you look at the output of +/demos/demo.browse.php scanning the file(s) you're interested in to +confirm what data is actually returned for any particular filetype in +general, and your files in particular, as the actual data returned +may vary considerably depending on what information is available in +the file itself. + + + +Notes +=========================================================================== + +getID3() 1.x: +If the format parser encounters a critical problem, it will return +something in $fileinfo['error'], describing the encountered error. If +a less critical error or notice is generated it will appear in +$fileinfo['warning']. Both keys may contain more than one warning or +error. If something is returned in ['error'] then the file was not +correctly parsed and returned data may or may not be correct and/or +complete. If something is returned in ['warning'] (and not ['error']) +then the data that is returned is OK - usually getID3() is reporting +errors in the file that have been worked around due to known bugs in +other programs. Some warnings may indicate that the data that is +returned is OK but that some data could not be extracted due to +errors in the file. + +getID3() 2.x: +See above except errors are thrown (so you will only get one error). + + + +Disclaimer +=========================================================================== + +getID3() has been tested on many systems, on many types of files, +under many operating systems, and is generally believe to be stable +and safe. That being said, there is still the chance there is an +undiscovered and/or unfixed bug that may potentially corrupt your +file, especially within the writing functions. By using getID3() you +agree that it's not my fault if any of your files are corrupted. +In fact, I'm not liable for anything :) + + + +License +=========================================================================== + +GNU General Public License - see license.txt + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to: +Free Software Foundation, Inc. +59 Temple Place - Suite 330 +Boston, MA 02111-1307, USA. + +FAQ: +Q: Can I use getID3() in my program? Do I need a commercial license? +A: You're generally free to use getID3 however you see fit. The only + case in which you would require a commercial license is if you're + selling your closed-source program that integrates getID3. If you + sell your program including a copy of getID3, that's fine as long + as you include a copy of the sourcecode when you sell it. Or you + can distribute your code without getID3 and say "download it from + getid3.sourceforge.net" + + + +Why is it called "getID3()" if it does so much more than just that? +=========================================================================== + +v0.1 did in fact just do that. I don't have a copy of code that old, but I +could essentially write it today with a one-line function: + function getID3($filename) { return unpack('a3TAG/a30title/a30artist/a30album/a4year/a28comment/c1track/c1genreid', substr(file_get_contents($filename), -128)); } + + +Future Plans +=========================================================================== +https://www.getid3.org/phpBB3/viewforum.php?f=7 + +* Better support for MP4 container format +* Scan for appended ID3v2 tag at end of file per ID3v2.4 specs (Section 5.0) +* Support for JPEG-2000 (http://www.morgan-multimedia.com/jpeg2000_overview.htm) +* Support for MOD (mod/stm/s3m/it/xm/mtm/ult/669) +* Support for ACE (thanks Vince) +* Support for Ogg other than Vorbis, Speex and OggFlac (ie. Ogg+Xvid) +* Ability to create Xing/LAME VBR header for VBR MP3s that are missing VBR header +* Ability to "clean" ID3v2 padding (replace invalid padding with valid padding) +* Warn if MP3s change version mid-stream (in full-scan mode) +* check for corrupt/broken mid-file MP3 streams in histogram scan +* Support for lossless-compression formats + (http://www.firstpr.com.au/audiocomp/lossless/#Links) + (http://compression.ca/act-sound.html) + (http://web.inter.nl.net/users/hvdh/lossless/lossless.htm) +* Support for RIFF-INFO chunks + * http://lotto.st-andrews.ac.uk/~njh/tag_interchange.html + (thanks Nick Humfrey ) + * http://abcavi.narod.ru/sof/abcavi/infotags.htm + (thanks Kibi) +* Better support for Bink video +* http://www.hr/josip/DSP/AudioFile2.html +* http://www.pcisys.net/~melanson/codecs/ +* Detect mp3PRO +* Support for PSD +* Support for JPC +* Support for JP2 +* Support for JPX +* Support for JB2 +* Support for IFF +* Support for ICO +* Support for ANI +* Support for EXE (comments, author, etc) (thanks p*quaedackersØplanet*nl) +* Support for DVD-IFO (region, subtitles, aspect ratio, etc) + (thanks p*quaedackersØplanet*nl) +* More complete support for SWF - parsing encapsulated MP3 and/or JPEG content + (thanks n8n8Øyahoo*com) +* Support for a2b +* Optional scan-through-frames for AVI verification + (thanks rockcohenØmassive-interactive*nl) +* Support for TTF (thanks infoØbutterflyx*com) +* Support for DSS (https://www.getid3.org/phpBB3/viewtopic.php?t=171) +* Support for SMAF (http://smaf-yamaha.com/what/demo.html) + https://www.getid3.org/phpBB3/viewtopic.php?t=182 +* Support for AMR (https://www.getid3.org/phpBB3/viewtopic.php?t=195) +* Support for 3gpp (https://www.getid3.org/phpBB3/viewtopic.php?t=195) +* Support for ID4 (http://www.wackysoft.cjb.net grizlyY2KØhotmail*com) +* Parse XML data returned in Ogg comments +* Parse XML data from Quicktime SMIL metafiles (klausrathØmac*com) +* ID3v2 genre string creator function +* More complete parsing of JPG +* Support for all old-style ASF packets +* ASF/WMA/WMV tag writing +* Parse declared T??? ID3v2 text information frames, where appropriate + (thanks Christian Fritz for the idea) +* Recognize encoder: + http://www.guerillasoft.com/EncSpot2/index.html + http://ff123.net/identify.html + http://www.hydrogenaudio.org/?act=ST&f=16&t=9414 + http://www.hydrogenaudio.org/?showtopic=11785 +* Support for other OS/2 bitmap structures: Bitmap Array('BA'), + Color Icon('CI'), Color Pointer('CP'), Icon('IC'), Pointer ('PT') + http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm +* Support for WavPack RAW mode +* ASF/WMA/WMV data packet parsing +* ID3v2FrameFlagsLookupTagAlter() +* ID3v2FrameFlagsLookupFileAlter() +* obey ID3v2 tag alter/preserve/discard rules +* http://www.geocities.com/SiliconValley/Sector/9654/Softdoc/Illyrium/Aolyr.htm +* proper checking for LINK/LNK frame validity in ID3v2 writing +* proper checking for ASPI-TLEN frame validity in ID3v2 writing +* proper checking for COMR frame validity in ID3v2 writing +* http://www.geocities.co.jp/SiliconValley-Oakland/3664/index.html +* decode GEOB ID3v2 structure as encoded by RealJukebox, + decode NCON ID3v2 structure as encoded by MusicMatch + (probably won't happen - the formats are proprietary) + + + +Known Bugs/Issues in getID3() that may be fixed eventually +=========================================================================== +https://www.getid3.org/phpBB3/viewtopic.php?t=25 + +* Cannot determine bitrate for MPEG video with VBR video data + (need documentation) +* Interlace/progressive cannot be determined for MPEG video + (need documentation) +* MIDI playtime is sometimes inaccurate +* AAC-RAW mode files cannot be identified +* WavPack-RAW mode files cannot be identified +* mp4 files report lots of "Unknown QuickTime atom type" + (need documentation) +* Encrypted ASF/WMA/WMV files warn about "unhandled GUID + ASF_Content_Encryption_Object" +* Bitrate split between audio and video cannot be calculated for + NSV, only the total bitrate. (need documentation) +* All Ogg formats (Vorbis, OggFLAC, Speex) are affected by the + problem of large VorbisComments spanning multiple Ogg pages, but + but only OggVorbis files can be processed with vorbiscomment. +* The version of "head" supplied with Mac OS 10.2.8 (maybe other + versions too) does only understands a single option (-n) and + therefore fails. getID3 ignores this and returns wrong md5_data. + + + +Known Bugs/Issues in getID3() that cannot be fixed +-------------------------------------------------- +https://www.getid3.org/phpBB3/viewtopic.php?t=25 + +* 32-bit PHP installations only: + Files larger than 2GB cannot always be parsed fully by getID3() + due to limitations in the 32-bit PHP filesystem functions. + NOTE: Since v1.7.8b3 there is partial support for larger-than- + 2GB files, most of which will parse OK, as long as no critical + data is located beyond the 2GB offset. + Known will-work: + * all file formats on 64-bit PHP + * ZIP (format doesn't support files >2GB) + * FLAC (current encoders don't support files >2GB) + Known will-not-work: + * ID3v1 tags (always located at end-of-file) + * Lyrics3 tags (always located at end-of-file) + * APE tags (always located at end-of-file) + Maybe-will-work: + * Quicktime (will work if needed metadata is before 2GB offset, + that is if the file has been hinted/optimized for streaming) + * RIFF.WAV (should work fine, but gives warnings about not being + able to parse all chunks) + * RIFF.AVI (playtime will probably be wrong, is only based on + "movi" chunk that fits in the first 2GB, should issue error + to show that playtime is incorrect. Other data should be mostly + correct, assuming that data is constant throughout the file) +* PHP <= v5 on Windows cannot read UTF-8 filenames + + +Known Bugs/Issues in other programs +----------------------------------- +https://www.getid3.org/phpBB3/viewtopic.php?t=25 + +* MusicBrainz Picard (at least up to v1.3.2) writes multiple + ID3v2.3 genres in non-standard forward-slash separated text + rather than parenthesis-numeric+refinement style per the ID3v2.3 + specs. Tags written in ID3v2.4 mode are written correctly. + (detected and worked around by getID3()) +* PZ TagEditor v4.53.408 has been known to insert ID3v2.3 frames + into an existing ID3v2.2 tag which, of course, breaks things +* Windows Media Player (up to v11) and iTunes (up to v10+) do + not correctly handle ID3v2.3 tags with UTF-16BE+BOM + encoding (they assume the data is UTF-16LE+BOM and either + crash (WMP) or output Asian character set (iTunes) +* Winamp (up to v2.80 at least) does not support ID3v2.4 tags, + only ID3v2.3 + see: http://forums.winamp.com/showthread.php?postid=387524 +* Some versions of Helium2 (www.helium2.com) do not write + ID3v2.4-compliant Frame Sizes, even though the tag is marked + as ID3v2.4) (detected by getID3()) +* MP3ext V3.3.17 places a non-compliant padding string at the end + of the ID3v2 header. This is supposedly fixed in v3.4b21 but + only if you manually add a registry key. This fix is not yet + confirmed. (detected by getID3()) +* CDex v1.40 (fixed by v1.50b7) writes non-compliant Ogg comment + strings, supposed to be in the format "NAME=value" but actually + written just "value" (detected by getID3()) +* Oggenc 0.9-rc3 flags the encoded file as ABR whether it's + actually ABR or VBR. +* iTunes (versions "v7.0.0.70" is known-guilty, probably + other versions are too) writes ID3v2.3 comment tags using an + ID3v2.2 frame name (3-bytes) null-padded to 4 bytes which is + not valid for ID3v2.3+ + (detected by getID3() since 1.9.12-201603221746) +* iTunes (versions "X v2.0.3", "v3.0.1" are known-guilty, probably + other versions are too) writes ID3v2.3 comment tags using a + frame name 'COM ' which is not valid for ID3v2.3+ (it's an + ID3v2.2-style frame name) (detected by getID3()) +* MP2enc does not encode mono CBR MP2 files properly (half speed + sound and double playtime) +* MP2enc does not encode mono VBR MP2 files properly (actually + encoded as stereo) +* tooLAME does not encode mono VBR MP2 files properly (actually + encoded as stereo) +* AACenc encodes files in VBR mode (actually ABR) even if CBR is + specified +* AAC/ADIF - bitrate_mode = cbr for vbr files +* LAME 3.90-3.92 prepends one frame of null data (space for the + LAME/VBR header, but it never gets written) when encoding in CBR + mode with the DLL +* Ahead Nero encodes TwinVQF with a DSIZ value (which is supposed + to be the filesize in bytes) of "0" for TwinVQF v1.0 and "1" for + TwinVQF v2.0 (detected by getID3()) +* Ahead Nero encodes TwinVQF files 1 second shorter than they + should be +* AAC-ADTS files are always actually encoded VBR, even if CBR mode + is specified (the CBR-mode switches on the encoder enable ABR + mode, not CBR as such, but it's not possible to tell the + difference between such ABR files and true VBR) +* STREAMINFO.audio_signature in OggFLAC is always null. "The reason + it's like that is because there is no seeking support in + libOggFLAC yet, so it has no way to go back and write the + computed sum after encoding. Seeking support in Ogg FLAC is the + #1 item for the next release." - Josh Coalson (FLAC developer) + NOTE: getID3() will calculate md5_data in a method similar to + other file formats, but that value cannot be compared to the + md5_data value from FLAC data in a FLAC file format. +* STREAMINFO.audio_signature is not calculated in FLAC v0.3.0 & + v0.4.0 - getID3() will calculate md5_data in a method similar to + other file formats, but that value cannot be compared to the + md5_data value from FLAC v0.5.0+ +* RioPort (various versions including 2.0 and 3.11) tags ID3v2 with + a WCOM frame that has no data portion +* Earlier versions of Coolplayer adds illegal ID3 tags to Ogg Vorbis + files, thus making them corrupt. +* Meracl ID3 Tag Writer v1.3.4 (and older) incorrectly truncates the + last byte of data from an MP3 file when appending a new ID3v1 tag. + (detected by getID3()) +* Lossless-Audio files encoded with and without the -noseek switch + do actually differ internally and therefore cannot match md5_data +* iTunes has been known to append a new ID3v1 tag on the end of an + existing ID3v1 tag when ID3v2 tag is also present + (detected by getID3()) +* MediaMonkey may write a blank RGAD ID3v2 frame but put actual + replay gain adjustments in a series of user-defined TXXX frames + (detected and handled by getID3() since v1.9.2) + + + + +Reference material: +=========================================================================== + +[www.id3.org material now mirrored at http://id3lib.sourceforge.net/id3/] +* http://www.id3.org/id3v2.4.0-structure.txt +* http://www.id3.org/id3v2.4.0-frames.txt +* http://www.id3.org/id3v2.4.0-changes.txt +* http://www.id3.org/id3v2.3.0.txt +* http://www.id3.org/id3v2-00.txt +* http://www.id3.org/mp3frame.html +* http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html +* http://www.dv.co.yu/mpgscript/mpeghdr.htm +* http://www.mp3-tech.org/programmer/frame_header.html +* http://users.belgacom.net/gc247244/extra/tag.html +* http://gabriel.mp3-tech.org/mp3infotag.html +* http://www.id3.org/iso4217.html +* http://www.unicode.org/Public/MAPPINGS/ISO8859/8859-1.TXT +* http://www.xiph.org/ogg/vorbis/doc/framing.html +* http://www.xiph.org/ogg/vorbis/doc/v-comment.html +* http://leknor.com/code/php/class.ogg.php.txt +* http://www.id3.org/iso639-2.html +* http://www.id3.org/lyrics3.html +* http://www.id3.org/lyrics3200.html +* http://www.psc.edu/general/software/packages/ieee/ieee.html +* http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html +* http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html +* http://www.jmcgowan.com/avi.html +* http://www.wotsit.org/ +* http://www.herdsoft.com/ti/davincie/davp3xo2.htm +* http://www.mathdogs.com/vorbis-illuminated/bitstream-appendix.html +* "Standard MIDI File Format" by Dustin Caldwell (from www.wotsit.org) +* http://midistudio.com/Help/GMSpecs_Patches.htm +* http://www.xiph.org/archives/vorbis/200109/0459.html +* http://www.replaygain.org/ +* http://www.lossless-audio.com/ +* http://download.microsoft.com/download/winmediatech40/Doc/1.0/WIN98MeXP/EN-US/ASF_Specification_v.1.0.exe +* http://mediaxw.sourceforge.net/files/doc/Active%20Streaming%20Format%20(ASF)%201.0%20Specification.pdf +* http://www.uni-jena.de/~pfk/mpp/sv8/ (archived at http://www.hydrogenaudio.org/musepack/klemm/www.personal.uni-jena.de/~pfk/mpp/sv8/) +* http://jfaul.de/atl/ +* http://www.uni-jena.de/~pfk/mpp/ (archived at http://www.hydrogenaudio.org/musepack/klemm/www.personal.uni-jena.de/~pfk/mpp/) +* http://www.libpng.org/pub/png/spec/png-1.2-pdg.html +* http://www.real.com/devzone/library/creating/rmsdk/doc/rmff.htm +* http://www.fastgraph.com/help/bmp_os2_header_format.html +* http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm +* http://flac.sourceforge.net/format.html +* http://www.research.att.com/projects/mpegaudio/mpeg2.html +* http://www.audiocoding.com/wiki/index.php?page=AAC +* http://libmpeg.org/mpeg4/doc/w2203tfs.pdf +* http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt +* http://developer.apple.com/techpubs/quicktime/qtdevdocs/RM/frameset.htm +* http://www.nullsoft.com/nsv/ +* http://www.wotsit.org/download.asp?f=iso9660 +* http://sandbox.mc.edu/~bennet/cs110/tc/tctod.html +* http://www.cdroller.com/htm/readdata.html +* http://www.speex.org/manual/node10.html +* http://www.harmony-central.com/Computer/Programming/aiff-file-format.doc +* http://www.faqs.org/rfcs/rfc2361.html +* http://ghido.shelter.ro/ +* http://www.ebu.ch/tech_t3285.pdf +* http://www.sr.se/utveckling/tu/bwf +* http://ftp.aessc.org/pub/aes46-2002.pdf +* http://cartchunk.org:8080/ +* http://www.broadcastpapers.com/radio/cartchunk01.htm +* http://www.hr/josip/DSP/AudioFile2.html +* http://home.attbi.com/~chris.bagwell/AudioFormats-11.html +* http://www.pure-mac.com/extkey.html +* http://cesnet.dl.sourceforge.net/sourceforge/bonkenc/bonk-binary-format-0.9.txt +* http://www.headbands.com/gspot/ +* http://www.openswf.org/spec/SWFfileformat.html +* http://j-faul.virtualave.net/ +* http://www.btinternet.com/~AnthonyJ/Atari/programming/avr_format.html +* http://cui.unige.ch/OSG/info/AudioFormats/ap11.html +* http://sswf.sourceforge.net/SWFalexref.html +* http://www.geocities.com/xhelmboyx/quicktime/formats/qti-layout.txt +* http://www-lehre.informatik.uni-osnabrueck.de/~fbstark/diplom/docs/swf/Flash_Uncovered.htm +* http://developer.apple.com/quicktime/icefloe/dispatch012.html +* http://www.csdn.net/Dev/Format/graphics/PCD.htm +* http://tta.iszf.irk.ru/ +* http://www.atsc.org/standards/a_52a.pdf +* http://www.alanwood.net/unicode/ +* http://www.freelists.org/archives/matroska-devel/07-2003/msg00010.html +* http://www.its.msstate.edu/net/real/reports/config/tags.stats +* http://homepages.slingshot.co.nz/~helmboy/quicktime/formats/qtm-layout.txt +* http://brennan.young.net/Comp/LiveStage/things.html +* http://www.multiweb.cz/twoinches/MP3inside.htm +* http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended +* http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/ +* http://www.unicode.org/unicode/faq/utf_bom.html +* http://tta.corecodec.org/?menu=format +* http://www.scvi.net/nsvformat.htm +* http://pda.etsi.org/pda/queryform.asp +* http://cpansearch.perl.org/src/RGIBSON/Audio-DSS-0.02/lib/Audio/DSS.pm +* http://trac.musepack.net/trac/wiki/SV8Specification +* http://wyday.com/cuesharp/specification.php +* http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html +* http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header +* http://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf diff --git a/projects/tests/tests/performance/benchmarks.txt b/projects/tests/tests/performance/benchmarks.txt new file mode 100644 index 00000000..0b51ceba --- /dev/null +++ b/projects/tests/tests/performance/benchmarks.txt @@ -0,0 +1,36 @@ +================================================================================= +./tests/real/wordpress/ + + setLimitTime: 300 + setLimitDefs: 100000 + setLimitSize: 1000000 + +real 217m58.642s +user 217m56.825s +sys 0m1.108s +================================================================================= + + +./tests/real/wordpress/ + + setLimitTime: 300 + setLimitDefs: 100000 + setLimitSize: 1000000 + +real 96m24.123s +user 96m10.106s +sys 0m1.532s + + + +================================================================================= +./tests/real/wordpress/ + + setLimitTime: 300 + setLimitDefs: 100000 + setLimitSize: 1000000 + + +real 56m16.871s +user 55m48.726s +sys 0m2.379s diff --git a/projects/tests/tests/performance/class-wp-query.php b/projects/tests/tests/performance/class-wp-query.php new file mode 100644 index 00000000..339acdb5 --- /dev/null +++ b/projects/tests/tests/performance/class-wp-query.php @@ -0,0 +1,4462 @@ +parent`. + * + * @since 1.5.0 + * @var WP_Post|null + */ + public $post; + + /** + * The list of comments for current post. + * + * @since 2.2.0 + * @var WP_Comment[] + */ + public $comments; + + /** + * The amount of comments for the posts. + * + * @since 2.2.0 + * @var int + */ + public $comment_count = 0; + + /** + * The index of the comment in the comment loop. + * + * @since 2.2.0 + * @var int + */ + public $current_comment = -1; + + /** + * Current comment object. + * + * @since 2.2.0 + * @var WP_Comment + */ + public $comment; + + /** + * The amount of found posts for the current query. + * + * If limit clause was not used, equals $post_count. + * + * @since 2.1.0 + * @var int + */ + public $found_posts = 0; + + /** + * The amount of pages. + * + * @since 2.1.0 + * @var int + */ + public $max_num_pages = 0; + + /** + * The amount of comment pages. + * + * @since 2.7.0 + * @var int + */ + public $max_num_comment_pages = 0; + + /** + * Signifies whether the current query is for a single post. + * + * @since 1.5.0 + * @var bool + */ + public $is_single = false; + + /** + * Signifies whether the current query is for a preview. + * + * @since 2.0.0 + * @var bool + */ + public $is_preview = false; + + /** + * Signifies whether the current query is for a page. + * + * @since 1.5.0 + * @var bool + */ + public $is_page = false; + + /** + * Signifies whether the current query is for an archive. + * + * @since 1.5.0 + * @var bool + */ + public $is_archive = false; + + /** + * Signifies whether the current query is for a date archive. + * + * @since 1.5.0 + * @var bool + */ + public $is_date = false; + + /** + * Signifies whether the current query is for a year archive. + * + * @since 1.5.0 + * @var bool + */ + public $is_year = false; + + /** + * Signifies whether the current query is for a month archive. + * + * @since 1.5.0 + * @var bool + */ + public $is_month = false; + + /** + * Signifies whether the current query is for a day archive. + * + * @since 1.5.0 + * @var bool + */ + public $is_day = false; + + /** + * Signifies whether the current query is for a specific time. + * + * @since 1.5.0 + * @var bool + */ + public $is_time = false; + + /** + * Signifies whether the current query is for an author archive. + * + * @since 1.5.0 + * @var bool + */ + public $is_author = false; + + /** + * Signifies whether the current query is for a category archive. + * + * @since 1.5.0 + * @var bool + */ + public $is_category = false; + + /** + * Signifies whether the current query is for a tag archive. + * + * @since 2.3.0 + * @var bool + */ + public $is_tag = false; + + /** + * Signifies whether the current query is for a taxonomy archive. + * + * @since 2.5.0 + * @var bool + */ + public $is_tax = false; + + /** + * Signifies whether the current query is for a search. + * + * @since 1.5.0 + * @var bool + */ + public $is_search = false; + + /** + * Signifies whether the current query is for a feed. + * + * @since 1.5.0 + * @var bool + */ + public $is_feed = false; + + /** + * Signifies whether the current query is for a comment feed. + * + * @since 2.2.0 + * @var bool + */ + public $is_comment_feed = false; + + /** + * Signifies whether the current query is for trackback endpoint call. + * + * @since 1.5.0 + * @var bool + */ + public $is_trackback = false; + + /** + * Signifies whether the current query is for the site homepage. + * + * @since 1.5.0 + * @var bool + */ + public $is_home = false; + + /** + * Signifies whether the current query is for the Privacy Policy page. + * + * @since 5.2.0 + * @var bool + */ + public $is_privacy_policy = false; + + /** + * Signifies whether the current query couldn't find anything. + * + * @since 1.5.0 + * @var bool + */ + public $is_404 = false; + + /** + * Signifies whether the current query is for an embed. + * + * @since 4.4.0 + * @var bool + */ + public $is_embed = false; + + /** + * Signifies whether the current query is for a paged result and not for the first page. + * + * @since 1.5.0 + * @var bool + */ + public $is_paged = false; + + /** + * Signifies whether the current query is for an administrative interface page. + * + * @since 1.5.0 + * @var bool + */ + public $is_admin = false; + + /** + * Signifies whether the current query is for an attachment page. + * + * @since 2.0.0 + * @var bool + */ + public $is_attachment = false; + + /** + * Signifies whether the current query is for an existing single post of any post type + * (post, attachment, page, custom post types). + * + * @since 2.1.0 + * @var bool + */ + public $is_singular = false; + + /** + * Signifies whether the current query is for the robots.txt file. + * + * @since 2.1.0 + * @var bool + */ + public $is_robots = false; + + /** + * Signifies whether the current query is for the favicon.ico file. + * + * @since 5.4.0 + * @var bool + */ + public $is_favicon = false; + + /** + * Signifies whether the current query is for the page_for_posts page. + * + * Basically, the homepage if the option isn't set for the static homepage. + * + * @since 2.1.0 + * @var bool + */ + public $is_posts_page = false; + + /** + * Signifies whether the current query is for a post type archive. + * + * @since 3.1.0 + * @var bool + */ + public $is_post_type_archive = false; + + /** + * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know + * whether we have to re-parse because something has changed + * + * @since 3.1.0 + * @var bool|string + */ + private $query_vars_hash = false; + + /** + * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made + * via pre_get_posts hooks. + * + * @since 3.1.1 + */ + private $query_vars_changed = true; + + /** + * Set if post thumbnails are cached + * + * @since 3.2.0 + * @var bool + */ + public $thumbnails_cached = false; + + /** + * Cached list of search stopwords. + * + * @since 3.7.0 + * @var array + */ + private $stopwords; + + private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' ); + + private $compat_methods = array( 'init_query_flags', 'parse_tax_query' ); + + /** + * Resets query flags to false. + * + * The query flags are what page info WordPress was able to figure out. + * + * @since 2.0.0 + */ + private function init_query_flags() { + $this->is_single = false; + $this->is_preview = false; + $this->is_page = false; + $this->is_archive = false; + $this->is_date = false; + $this->is_year = false; + $this->is_month = false; + $this->is_day = false; + $this->is_time = false; + $this->is_author = false; + $this->is_category = false; + $this->is_tag = false; + $this->is_tax = false; + $this->is_search = false; + $this->is_feed = false; + $this->is_comment_feed = false; + $this->is_trackback = false; + $this->is_home = false; + $this->is_privacy_policy = false; + $this->is_404 = false; + $this->is_paged = false; + $this->is_admin = false; + $this->is_attachment = false; + $this->is_singular = false; + $this->is_robots = false; + $this->is_favicon = false; + $this->is_posts_page = false; + $this->is_post_type_archive = false; + } + + /** + * Initiates object properties and sets default values. + * + * @since 1.5.0 + */ + public function init() { + unset( $this->posts ); + unset( $this->query ); + $this->query_vars = array(); + unset( $this->queried_object ); + unset( $this->queried_object_id ); + $this->post_count = 0; + $this->current_post = -1; + $this->in_the_loop = false; + unset( $this->request ); + unset( $this->post ); + unset( $this->comments ); + unset( $this->comment ); + $this->comment_count = 0; + $this->current_comment = -1; + $this->found_posts = 0; + $this->max_num_pages = 0; + $this->max_num_comment_pages = 0; + + $this->init_query_flags(); + } + + /** + * Reparse the query vars. + * + * @since 1.5.0 + */ + public function parse_query_vars() { + $this->parse_query(); + } + + /** + * Fills in the query variables, which do not exist within the parameter. + * + * @since 2.1.0 + * @since 4.5.0 Removed the `comments_popup` public query variable. + * + * @param array $array Defined query variables. + * @return array Complete query variables with undefined ones filled in empty. + */ + public function fill_query_vars( $array ) { + $keys = array( + 'error', + 'm', + 'p', + 'post_parent', + 'subpost', + 'subpost_id', + 'attachment', + 'attachment_id', + 'name', + 'pagename', + 'page_id', + 'second', + 'minute', + 'hour', + 'day', + 'monthnum', + 'year', + 'w', + 'category_name', + 'tag', + 'cat', + 'tag_id', + 'author', + 'author_name', + 'feed', + 'tb', + 'paged', + 'meta_key', + 'meta_value', + 'preview', + 's', + 'sentence', + 'title', + 'fields', + 'menu_order', + 'embed', + ); + + foreach ( $keys as $key ) { + if ( ! isset( $array[ $key ] ) ) { + $array[ $key ] = ''; + } + } + + $array_keys = array( + 'category__in', + 'category__not_in', + 'category__and', + 'post__in', + 'post__not_in', + 'post_name__in', + 'tag__in', + 'tag__not_in', + 'tag__and', + 'tag_slug__in', + 'tag_slug__and', + 'post_parent__in', + 'post_parent__not_in', + 'author__in', + 'author__not_in', + ); + + foreach ( $array_keys as $key ) { + if ( ! isset( $array[ $key ] ) ) { + $array[ $key ] = array(); + } + } + return $array; + } + + /** + * Parse a query string and set query type booleans. + * + * @since 1.5.0 + * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's + * array key to `$orderby`. + * @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded + * search terms, by prepending a hyphen. + * @since 4.5.0 Removed the `$comments_popup` parameter. + * Introduced the `$comment_status` and `$ping_status` parameters. + * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts. + * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument. + * @since 4.9.0 Introduced the `$comment_count` parameter. + * @since 5.1.0 Introduced the `$meta_compare_key` parameter. + * @since 5.3.0 Introduced the `$meta_type_key` parameter. + * + * @param string|array $query { + * Optional. Array or string of Query parameters. + * + * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type. + * @type int|string $author Author ID, or comma-separated list of IDs. + * @type string $author_name User 'user_nicename'. + * @type int[] $author__in An array of author IDs to query from. + * @type int[] $author__not_in An array of author IDs not to query from. + * @type bool $cache_results Whether to cache post information. Default true. + * @type int|string $cat Category ID or comma-separated list of IDs (this or any children). + * @type int[] $category__and An array of category IDs (AND in). + * @type int[] $category__in An array of category IDs (OR in, no children). + * @type int[] $category__not_in An array of category IDs (NOT in). + * @type string $category_name Use category slug (not name, this or any children). + * @type array|int $comment_count Filter results by comment count. Provide an integer to match + * comment count exactly. Provide an array with integer 'value' + * and 'compare' operator ('=', '!=', '>', '>=', '<', '<=' ) to + * compare against comment_count in a specific way. + * @type string $comment_status Comment status. + * @type int $comments_per_page The number of comments to return per page. + * Default 'comments_per_page' option. + * @type array $date_query An associative array of WP_Date_Query arguments. + * See WP_Date_Query::__construct(). + * @type int $day Day of the month. Default empty. Accepts numbers 1-31. + * @type bool $exact Whether to search by exact keyword. Default false. + * @type string $fields Post fields to query for. Accepts: + * - '' Returns an array of complete post objects (`WP_Post[]`). + * - 'ids' Returns an array of post IDs (`int[]`). + * - 'id=>parent' Returns an associative array of parent post IDs, + * keyed by post ID (`int[]`). + * Default ''. + * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23. + * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false + * excludes stickies from 'post__in'. Accepts 1|true, 0|false. + * Default false. + * @type int $m Combination YearMonth. Accepts any four-digit year and month + * numbers 1-12. Default empty. + * @type string $meta_compare Comparison operator to test the 'meta_value'. + * @type string $meta_compare_key Comparison operator to test the 'meta_key'. + * @type string $meta_key Custom field key. + * @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query. + * @type string $meta_value Custom field value. + * @type int $meta_value_num Custom field value number. + * @type string $meta_type_key Cast for 'meta_key'. See WP_Meta_Query::construct(). + * @type int $menu_order The menu order of the posts. + * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12. + * @type string $name Post slug. + * @type bool $nopaging Show all posts (true) or paginate (false). Default false. + * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve + * performance. Default false. + * @type int $offset The number of posts to offset before retrieval. + * @type string $order Designates ascending or descending order of posts. Default 'DESC'. + * Accepts 'ASC', 'DESC'. + * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be + * passed. To use 'meta_value', or 'meta_value_num', + * 'meta_key=keyname' must be also be defined. To sort by a + * specific `$meta_query` clause, use that clause's array key. + * Accepts 'none', 'name', 'author', 'date', 'title', + * 'modified', 'menu_order', 'parent', 'ID', 'rand', + * 'relevance', 'RAND(x)' (where 'x' is an integer seed value), + * 'comment_count', 'meta_value', 'meta_value_num', 'post__in', + * 'post_name__in', 'post_parent__in', and the array keys + * of `$meta_query`. Default is 'date', except when a search + * is being performed, when the default is 'relevance'. + * @type int $p Post ID. + * @type int $page Show the number of posts that would show up on page X of a + * static front page. + * @type int $paged The number of the current page. + * @type int $page_id Page ID. + * @type string $pagename Page slug. + * @type string $perm Show posts if user has the appropriate capability. + * @type string $ping_status Ping status. + * @type int[] $post__in An array of post IDs to retrieve, sticky posts will be included. + * @type int[] $post__not_in An array of post IDs not to retrieve. Note: a string of comma- + * separated IDs will NOT work. + * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type. + * @type string[] $post_name__in An array of post slugs that results must match. + * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve + * top-level pages. + * @type int[] $post_parent__in An array containing parent page IDs to query child pages from. + * @type int[] $post_parent__not_in An array containing parent page IDs not to query child pages from. + * @type string|array $post_type A post type slug (string) or array of post type slugs. + * Default 'any' if using 'tax_query'. + * @type string|array $post_status A post status (string) or array of post statuses. + * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts. + * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides + * 'posts_per_page' when is_archive(), or is_search() are true. + * @type string $s Search keyword(s). Prepending a term with a hyphen will + * exclude posts matching that term. Eg, 'pillow -sofa' will + * return posts containing 'pillow' but not 'sofa'. The + * character used for exclusion can be modified using the + * the 'wp_query_search_exclusion_prefix' filter. + * @type int $second Second of the minute. Default empty. Accepts numbers 0-60. + * @type bool $sentence Whether to search by phrase. Default false. + * @type bool $suppress_filters Whether to suppress filters. Default false. + * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all). + * @type int[] $tag__and An array of tag IDs (AND in). + * @type int[] $tag__in An array of tag IDs (OR in). + * @type int[] $tag__not_in An array of tag IDs (NOT in). + * @type int $tag_id Tag id or comma-separated list of IDs. + * @type string[] $tag_slug__and An array of tag slugs (AND in). + * @type string[] $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is + * true. Note: a string of comma-separated IDs will NOT work. + * @type array $tax_query An associative array of WP_Tax_Query arguments. + * See WP_Tax_Query->__construct(). + * @type string $title Post title. + * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true. + * @type bool $update_post_term_cache Whether to update the post term cache. Default true. + * @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will + * disable cache priming for term meta, so that each + * get_term_meta() call will hit the database. + * Defaults to the value of `$update_post_term_cache`. + * @type int $w The week number of the year. Default empty. Accepts numbers 0-53. + * @type int $year The four-digit year. Default empty. Accepts any four-digit year. + * } + */ + public function parse_query( $query = '' ) { + if ( ! empty( $query ) ) { + $this->init(); + $this->query = wp_parse_args( $query ); + $this->query_vars = $this->query; + } elseif ( ! isset( $this->query ) ) { + $this->query = $this->query_vars; + } + + $this->query_vars = $this->fill_query_vars( $this->query_vars ); + $qv = &$this->query_vars; + $this->query_vars_changed = true; + + if ( ! empty( $qv['robots'] ) ) { + $this->is_robots = true; + } elseif ( ! empty( $qv['favicon'] ) ) { + $this->is_favicon = true; + } + + if ( ! is_scalar( $qv['p'] ) || (int) $qv['p'] < 0 ) { + $qv['p'] = 0; + $qv['error'] = '404'; + } else { + $qv['p'] = (int) $qv['p']; + } + + $qv['page_id'] = absint( $qv['page_id'] ); + $qv['year'] = absint( $qv['year'] ); + $qv['monthnum'] = absint( $qv['monthnum'] ); + $qv['day'] = absint( $qv['day'] ); + $qv['w'] = absint( $qv['w'] ); + $qv['m'] = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : ''; + $qv['paged'] = absint( $qv['paged'] ); + $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // Comma-separated list of positive or negative integers. + $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // Comma-separated list of positive or negative integers. + $qv['pagename'] = trim( $qv['pagename'] ); + $qv['name'] = trim( $qv['name'] ); + $qv['title'] = trim( $qv['title'] ); + if ( '' !== $qv['hour'] ) { + $qv['hour'] = absint( $qv['hour'] ); + } + if ( '' !== $qv['minute'] ) { + $qv['minute'] = absint( $qv['minute'] ); + } + if ( '' !== $qv['second'] ) { + $qv['second'] = absint( $qv['second'] ); + } + if ( '' !== $qv['menu_order'] ) { + $qv['menu_order'] = absint( $qv['menu_order'] ); + } + + // Fairly large, potentially too large, upper bound for search string lengths. + if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) { + $qv['s'] = ''; + } + + // Compat. Map subpost to attachment. + if ( '' != $qv['subpost'] ) { + $qv['attachment'] = $qv['subpost']; + } + if ( '' != $qv['subpost_id'] ) { + $qv['attachment_id'] = $qv['subpost_id']; + } + + $qv['attachment_id'] = absint( $qv['attachment_id'] ); + + if ( ( '' !== $qv['attachment'] ) || ! empty( $qv['attachment_id'] ) ) { + $this->is_single = true; + $this->is_attachment = true; + } elseif ( '' !== $qv['name'] ) { + $this->is_single = true; + } elseif ( $qv['p'] ) { + $this->is_single = true; + } elseif ( '' !== $qv['pagename'] || ! empty( $qv['page_id'] ) ) { + $this->is_page = true; + $this->is_single = false; + } else { + // Look for archive queries. Dates, categories, authors, search, post type archives. + + if ( isset( $this->query['s'] ) ) { + $this->is_search = true; + } + + if ( '' !== $qv['second'] ) { + $this->is_time = true; + $this->is_date = true; + } + + if ( '' !== $qv['minute'] ) { + $this->is_time = true; + $this->is_date = true; + } + + if ( '' !== $qv['hour'] ) { + $this->is_time = true; + $this->is_date = true; + } + + if ( $qv['day'] ) { + if ( ! $this->is_date ) { + $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] ); + if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) { + $qv['error'] = '404'; + } else { + $this->is_day = true; + $this->is_date = true; + } + } + } + + if ( $qv['monthnum'] ) { + if ( ! $this->is_date ) { + if ( 12 < $qv['monthnum'] ) { + $qv['error'] = '404'; + } else { + $this->is_month = true; + $this->is_date = true; + } + } + } + + if ( $qv['year'] ) { + if ( ! $this->is_date ) { + $this->is_year = true; + $this->is_date = true; + } + } + + if ( $qv['m'] ) { + $this->is_date = true; + if ( strlen( $qv['m'] ) > 9 ) { + $this->is_time = true; + } elseif ( strlen( $qv['m'] ) > 7 ) { + $this->is_day = true; + } elseif ( strlen( $qv['m'] ) > 5 ) { + $this->is_month = true; + } else { + $this->is_year = true; + } + } + + if ( $qv['w'] ) { + $this->is_date = true; + } + + $this->query_vars_hash = false; + $this->parse_tax_query( $qv ); + + foreach ( $this->tax_query->queries as $tax_query ) { + if ( ! is_array( $tax_query ) ) { + continue; + } + + if ( isset( $tax_query['operator'] ) && 'NOT IN' !== $tax_query['operator'] ) { + switch ( $tax_query['taxonomy'] ) { + case 'category': + $this->is_category = true; + break; + case 'post_tag': + $this->is_tag = true; + break; + default: + $this->is_tax = true; + } + } + } + unset( $tax_query ); + + if ( empty( $qv['author'] ) || ( '0' == $qv['author'] ) ) { + $this->is_author = false; + } else { + $this->is_author = true; + } + + if ( '' !== $qv['author_name'] ) { + $this->is_author = true; + } + + if ( ! empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) { + $post_type_obj = get_post_type_object( $qv['post_type'] ); + if ( ! empty( $post_type_obj->has_archive ) ) { + $this->is_post_type_archive = true; + } + } + + if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) { + $this->is_archive = true; + } + } + + if ( '' != $qv['feed'] ) { + $this->is_feed = true; + } + + if ( '' != $qv['embed'] ) { + $this->is_embed = true; + } + + if ( '' != $qv['tb'] ) { + $this->is_trackback = true; + } + + if ( '' != $qv['paged'] && ( (int) $qv['paged'] > 1 ) ) { + $this->is_paged = true; + } + + // If we're previewing inside the write screen. + if ( '' != $qv['preview'] ) { + $this->is_preview = true; + } + + if ( is_admin() ) { + $this->is_admin = true; + } + + if ( false !== strpos( $qv['feed'], 'comments-' ) ) { + $qv['feed'] = str_replace( 'comments-', '', $qv['feed'] ); + $qv['withcomments'] = 1; + } + + $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; + + if ( $this->is_feed && ( ! empty( $qv['withcomments'] ) || ( empty( $qv['withoutcomments'] ) && $this->is_singular ) ) ) { + $this->is_comment_feed = true; + } + + if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed + || ( defined( 'REST_REQUEST' ) && REST_REQUEST && $this->is_main_query() ) + || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) { + $this->is_home = true; + } + + // Correct `is_*` for 'page_on_front' and 'page_for_posts'. + if ( $this->is_home && 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ) { + $_query = wp_parse_args( $this->query ); + // 'pagename' can be set and empty depending on matched rewrite rules. Ignore an empty 'pagename'. + if ( isset( $_query['pagename'] ) && '' === $_query['pagename'] ) { + unset( $_query['pagename'] ); + } + + unset( $_query['embed'] ); + + if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage' ) ) ) { + $this->is_page = true; + $this->is_home = false; + $qv['page_id'] = get_option( 'page_on_front' ); + // Correct for 'page_on_front'. + if ( ! empty( $qv['paged'] ) ) { + $qv['page'] = $qv['paged']; + unset( $qv['paged'] ); + } + } + } + + if ( '' !== $qv['pagename'] ) { + $this->queried_object = get_page_by_path( $qv['pagename'] ); + + if ( $this->queried_object && 'attachment' === $this->queried_object->post_type ) { + if ( preg_match( '/^[^%]*%(?:postname)%/', get_option( 'permalink_structure' ) ) ) { + // See if we also have a post with the same slug. + $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' ); + if ( $post ) { + $this->queried_object = $post; + $this->is_page = false; + $this->is_single = true; + } + } + } + + if ( ! empty( $this->queried_object ) ) { + $this->queried_object_id = (int) $this->queried_object->ID; + } else { + unset( $this->queried_object ); + } + + if ( 'page' === get_option( 'show_on_front' ) && isset( $this->queried_object_id ) && get_option( 'page_for_posts' ) == $this->queried_object_id ) { + $this->is_page = false; + $this->is_home = true; + $this->is_posts_page = true; + } + + if ( isset( $this->queried_object_id ) && get_option( 'wp_page_for_privacy_policy' ) == $this->queried_object_id ) { + $this->is_privacy_policy = true; + } + } + + if ( $qv['page_id'] ) { + if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) == $qv['page_id'] ) { + $this->is_page = false; + $this->is_home = true; + $this->is_posts_page = true; + } + + if ( get_option( 'wp_page_for_privacy_policy' ) == $qv['page_id'] ) { + $this->is_privacy_policy = true; + } + } + + if ( ! empty( $qv['post_type'] ) ) { + if ( is_array( $qv['post_type'] ) ) { + $qv['post_type'] = array_map( 'sanitize_key', $qv['post_type'] ); + } else { + $qv['post_type'] = sanitize_key( $qv['post_type'] ); + } + } + + if ( ! empty( $qv['post_status'] ) ) { + if ( is_array( $qv['post_status'] ) ) { + $qv['post_status'] = array_map( 'sanitize_key', $qv['post_status'] ); + } else { + $qv['post_status'] = preg_replace( '|[^a-z0-9_,-]|', '', $qv['post_status'] ); + } + } + + if ( $this->is_posts_page && ( ! isset( $qv['withcomments'] ) || ! $qv['withcomments'] ) ) { + $this->is_comment_feed = false; + } + + $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; + // Done correcting `is_*` for 'page_on_front' and 'page_for_posts'. + + if ( '404' == $qv['error'] ) { + $this->set_404(); + } + + $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 ); + + $this->query_vars_hash = md5( serialize( $this->query_vars ) ); + $this->query_vars_changed = false; + + /** + * Fires after the main query vars have been parsed. + * + * @since 1.5.0 + * + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + do_action_ref_array( 'parse_query', array( &$this ) ); + } + + /** + * Parses various taxonomy related query vars. + * + * For BC, this method is not marked as protected. See [28987]. + * + * @since 3.1.0 + * + * @param array $q The query variables. Passed by reference. + */ + public function parse_tax_query( &$q ) { + if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) { + $tax_query = $q['tax_query']; + } else { + $tax_query = array(); + } + + if ( ! empty( $q['taxonomy'] ) && ! empty( $q['term'] ) ) { + $tax_query[] = array( + 'taxonomy' => $q['taxonomy'], + 'terms' => array( $q['term'] ), + 'field' => 'slug', + ); + } + + foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) { + if ( 'post_tag' === $taxonomy ) { + continue; // Handled further down in the $q['tag'] block. + } + + if ( $t->query_var && ! empty( $q[ $t->query_var ] ) ) { + $tax_query_defaults = array( + 'taxonomy' => $taxonomy, + 'field' => 'slug', + ); + + if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) { + $q[ $t->query_var ] = wp_basename( $q[ $t->query_var ] ); + } + + $term = $q[ $t->query_var ]; + + if ( is_array( $term ) ) { + $term = implode( ',', $term ); + } + + if ( strpos( $term, '+' ) !== false ) { + $terms = preg_split( '/[+]+/', $term ); + foreach ( $terms as $term ) { + $tax_query[] = array_merge( + $tax_query_defaults, + array( + 'terms' => array( $term ), + ) + ); + } + } else { + $tax_query[] = array_merge( + $tax_query_defaults, + array( + 'terms' => preg_split( '/[,]+/', $term ), + ) + ); + } + } + } + + // If query string 'cat' is an array, implode it. + if ( is_array( $q['cat'] ) ) { + $q['cat'] = implode( ',', $q['cat'] ); + } + + // Category stuff. + + if ( ! empty( $q['cat'] ) && ! $this->is_singular ) { + $cat_in = array(); + $cat_not_in = array(); + + $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) ); + $cat_array = array_map( 'intval', $cat_array ); + $q['cat'] = implode( ',', $cat_array ); + + foreach ( $cat_array as $cat ) { + if ( $cat > 0 ) { + $cat_in[] = $cat; + } elseif ( $cat < 0 ) { + $cat_not_in[] = abs( $cat ); + } + } + + if ( ! empty( $cat_in ) ) { + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $cat_in, + 'field' => 'term_id', + 'include_children' => true, + ); + } + + if ( ! empty( $cat_not_in ) ) { + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $cat_not_in, + 'field' => 'term_id', + 'operator' => 'NOT IN', + 'include_children' => true, + ); + } + unset( $cat_array, $cat_in, $cat_not_in ); + } + + if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) { + $q['category__and'] = (array) $q['category__and']; + if ( ! isset( $q['category__in'] ) ) { + $q['category__in'] = array(); + } + $q['category__in'][] = absint( reset( $q['category__and'] ) ); + unset( $q['category__and'] ); + } + + if ( ! empty( $q['category__in'] ) ) { + $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $q['category__in'], + 'field' => 'term_id', + 'include_children' => false, + ); + } + + if ( ! empty( $q['category__not_in'] ) ) { + $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $q['category__not_in'], + 'operator' => 'NOT IN', + 'include_children' => false, + ); + } + + if ( ! empty( $q['category__and'] ) ) { + $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $q['category__and'], + 'field' => 'term_id', + 'operator' => 'AND', + 'include_children' => false, + ); + } + + // If query string 'tag' is array, implode it. + if ( is_array( $q['tag'] ) ) { + $q['tag'] = implode( ',', $q['tag'] ); + } + + // Tag stuff. + + if ( '' !== $q['tag'] && ! $this->is_singular && $this->query_vars_changed ) { + if ( strpos( $q['tag'], ',' ) !== false ) { + $tags = preg_split( '/[,\r\n\t ]+/', $q['tag'] ); + foreach ( (array) $tags as $tag ) { + $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); + $q['tag_slug__in'][] = $tag; + } + } elseif ( preg_match( '/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) { + $tags = preg_split( '/[+\r\n\t ]+/', $q['tag'] ); + foreach ( (array) $tags as $tag ) { + $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); + $q['tag_slug__and'][] = $tag; + } + } else { + $q['tag'] = sanitize_term_field( 'slug', $q['tag'], 0, 'post_tag', 'db' ); + $q['tag_slug__in'][] = $q['tag']; + } + } + + if ( ! empty( $q['tag_id'] ) ) { + $q['tag_id'] = absint( $q['tag_id'] ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_id'], + ); + } + + if ( ! empty( $q['tag__in'] ) ) { + $q['tag__in'] = array_map( 'absint', array_unique( (array) $q['tag__in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag__in'], + ); + } + + if ( ! empty( $q['tag__not_in'] ) ) { + $q['tag__not_in'] = array_map( 'absint', array_unique( (array) $q['tag__not_in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag__not_in'], + 'operator' => 'NOT IN', + ); + } + + if ( ! empty( $q['tag__and'] ) ) { + $q['tag__and'] = array_map( 'absint', array_unique( (array) $q['tag__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag__and'], + 'operator' => 'AND', + ); + } + + if ( ! empty( $q['tag_slug__in'] ) ) { + $q['tag_slug__in'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_slug__in'], + 'field' => 'slug', + ); + } + + if ( ! empty( $q['tag_slug__and'] ) ) { + $q['tag_slug__and'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_slug__and'], + 'field' => 'slug', + 'operator' => 'AND', + ); + } + + $this->tax_query = new WP_Tax_Query( $tax_query ); + + /** + * Fires after taxonomy-related query vars have been parsed. + * + * @since 3.7.0 + * + * @param WP_Query $query The WP_Query instance. + */ + do_action( 'parse_tax_query', $this ); + } + + /** + * Generates SQL for the WHERE clause based on passed search terms. + * + * @since 3.7.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param array $q Query variables. + * @return string WHERE clause. + */ + protected function parse_search( &$q ) { + global $wpdb; + + $search = ''; + + // Added slashes screw with quote grouping when done early, so done later. + $q['s'] = stripslashes( $q['s'] ); + if ( empty( $_GET['s'] ) && $this->is_main_query() ) { + $q['s'] = urldecode( $q['s'] ); + } + // There are no line breaks in fields. + $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] ); + $q['search_terms_count'] = 1; + if ( ! empty( $q['sentence'] ) ) { + $q['search_terms'] = array( $q['s'] ); + } else { + if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) { + $q['search_terms_count'] = count( $matches[0] ); + $q['search_terms'] = $this->parse_search_terms( $matches[0] ); + // If the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence. + if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ) { + $q['search_terms'] = array( $q['s'] ); + } + } else { + $q['search_terms'] = array( $q['s'] ); + } + } + + $n = ! empty( $q['exact'] ) ? '' : '%'; + $searchand = ''; + $q['search_orderby_title'] = array(); + + /** + * Filters the prefix that indicates that a search term should be excluded from results. + * + * @since 4.7.0 + * + * @param string $exclusion_prefix The prefix. Default '-'. Returning + * an empty value disables exclusions. + */ + $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' ); + + foreach ( $q['search_terms'] as $term ) { + // If there is an $exclusion_prefix, terms prefixed with it should be excluded. + $exclude = $exclusion_prefix && ( substr( $term, 0, 1 ) === $exclusion_prefix ); + if ( $exclude ) { + $like_op = 'NOT LIKE'; + $andor_op = 'AND'; + $term = substr( $term, 1 ); + } else { + $like_op = 'LIKE'; + $andor_op = 'OR'; + } + + if ( $n && ! $exclude ) { + $like = '%' . $wpdb->esc_like( $term ) . '%'; + $q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like ); + } + + $like = $n . $wpdb->esc_like( $term ) . $n; + $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like ); + $searchand = ' AND '; + } + + if ( ! empty( $search ) ) { + $search = " AND ({$search}) "; + if ( ! is_user_logged_in() ) { + $search .= " AND ({$wpdb->posts}.post_password = '') "; + } + } + + return $search; + } + + /** + * Check if the terms are suitable for searching. + * + * Uses an array of stopwords (terms) that are excluded from the separate + * term matching when searching for posts. The list of English stopwords is + * the approximate search engines list, and is translatable. + * + * @since 3.7.0 + * + * @param string[] $terms Array of terms to check. + * @return string[] Terms that are not stopwords. + */ + protected function parse_search_terms( $terms ) { + $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower'; + $checked = array(); + + $stopwords = $this->get_search_stopwords(); + + foreach ( $terms as $term ) { + // Keep before/after spaces when term is for exact match. + if ( preg_match( '/^".+"$/', $term ) ) { + $term = trim( $term, "\"'" ); + } else { + $term = trim( $term, "\"' " ); + } + + // Avoid single A-Z and single dashes. + if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) ) { + continue; + } + + if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) ) { + continue; + } + + $checked[] = $term; + } + + return $checked; + } + + /** + * Retrieve stopwords used when parsing search terms. + * + * @since 3.7.0 + * + * @return string[] Stopwords. + */ + protected function get_search_stopwords() { + if ( isset( $this->stopwords ) ) { + return $this->stopwords; + } + + /* + * translators: This is a comma-separated list of very common words that should be excluded from a search, + * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual + * words into your language. Instead, look for and provide commonly accepted stopwords in your language. + */ + $words = explode( + ',', + _x( + 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www', + 'Comma-separated list of search stopwords in your language' + ) + ); + + $stopwords = array(); + foreach ( $words as $word ) { + $word = trim( $word, "\r\n\t " ); + if ( $word ) { + $stopwords[] = $word; + } + } + + /** + * Filters stopwords used when parsing search terms. + * + * @since 3.7.0 + * + * @param string[] $stopwords Array of stopwords. + */ + $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); + return $this->stopwords; + } + + /** + * Generates SQL for the ORDER BY condition based on passed search terms. + * + * @since 3.7.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param array $q Query variables. + * @return string ORDER BY clause. + */ + protected function parse_search_order( &$q ) { + global $wpdb; + + if ( $q['search_terms_count'] > 1 ) { + $num_terms = count( $q['search_orderby_title'] ); + + // If the search terms contain negative queries, don't bother ordering by sentence matches. + $like = ''; + if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) { + $like = '%' . $wpdb->esc_like( $q['s'] ) . '%'; + } + + $search_orderby = ''; + + // Sentence match in 'post_title'. + if ( $like ) { + $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like ); + } + + // Sanity limit, sort as sentence when more than 6 terms + // (few searches are longer than 6 terms and most titles are not). + if ( $num_terms < 7 ) { + // All words in title. + $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 '; + // Any word in title, not needed when $num_terms == 1. + if ( $num_terms > 1 ) { + $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 '; + } + } + + // Sentence match in 'post_content' and 'post_excerpt'. + if ( $like ) { + $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like ); + $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like ); + } + + if ( $search_orderby ) { + $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)'; + } + } else { + // Single word or sentence search. + $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC'; + } + + return $search_orderby; + } + + /** + * Converts the given orderby alias (if allowed) to a properly-prefixed value. + * + * @since 4.0.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param string $orderby Alias for the field to order by. + * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise. + */ + protected function parse_orderby( $orderby ) { + global $wpdb; + + // Used to filter values. + $allowed_keys = array( + 'post_name', + 'post_author', + 'post_date', + 'post_title', + 'post_modified', + 'post_parent', + 'post_type', + 'name', + 'author', + 'date', + 'title', + 'modified', + 'parent', + 'type', + 'ID', + 'menu_order', + 'comment_count', + 'rand', + 'post__in', + 'post_parent__in', + 'post_name__in', + ); + + $primary_meta_key = ''; + $primary_meta_query = false; + $meta_clauses = $this->meta_query->get_clauses(); + if ( ! empty( $meta_clauses ) ) { + $primary_meta_query = reset( $meta_clauses ); + + if ( ! empty( $primary_meta_query['key'] ) ) { + $primary_meta_key = $primary_meta_query['key']; + $allowed_keys[] = $primary_meta_key; + } + + $allowed_keys[] = 'meta_value'; + $allowed_keys[] = 'meta_value_num'; + $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); + } + + // If RAND() contains a seed value, sanitize and add to allowed keys. + $rand_with_seed = false; + if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { + $orderby = sprintf( 'RAND(%s)', (int) $matches[1] ); + $allowed_keys[] = $orderby; + $rand_with_seed = true; + } + + if ( ! in_array( $orderby, $allowed_keys, true ) ) { + return false; + } + + $orderby_clause = ''; + + switch ( $orderby ) { + case 'post_name': + case 'post_author': + case 'post_date': + case 'post_title': + case 'post_modified': + case 'post_parent': + case 'post_type': + case 'ID': + case 'menu_order': + case 'comment_count': + $orderby_clause = "{$wpdb->posts}.{$orderby}"; + break; + case 'rand': + $orderby_clause = 'RAND()'; + break; + case $primary_meta_key: + case 'meta_value': + if ( ! empty( $primary_meta_query['type'] ) ) { + $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})"; + } else { + $orderby_clause = "{$primary_meta_query['alias']}.meta_value"; + } + break; + case 'meta_value_num': + $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0"; + break; + case 'post__in': + if ( ! empty( $this->query_vars['post__in'] ) ) { + $orderby_clause = "FIELD({$wpdb->posts}.ID," . implode( ',', array_map( 'absint', $this->query_vars['post__in'] ) ) . ')'; + } + break; + case 'post_parent__in': + if ( ! empty( $this->query_vars['post_parent__in'] ) ) { + $orderby_clause = "FIELD( {$wpdb->posts}.post_parent," . implode( ', ', array_map( 'absint', $this->query_vars['post_parent__in'] ) ) . ' )'; + } + break; + case 'post_name__in': + if ( ! empty( $this->query_vars['post_name__in'] ) ) { + $post_name__in = array_map( 'sanitize_title_for_query', $this->query_vars['post_name__in'] ); + $post_name__in_string = "'" . implode( "','", $post_name__in ) . "'"; + $orderby_clause = "FIELD( {$wpdb->posts}.post_name," . $post_name__in_string . ' )'; + } + break; + default: + if ( array_key_exists( $orderby, $meta_clauses ) ) { + // $orderby corresponds to a meta_query clause. + $meta_clause = $meta_clauses[ $orderby ]; + $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; + } elseif ( $rand_with_seed ) { + $orderby_clause = $orderby; + } else { + // Default: order by post field. + $orderby_clause = "{$wpdb->posts}.post_" . sanitize_key( $orderby ); + } + + break; + } + + return $orderby_clause; + } + + /** + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. + * + * @since 4.0.0 + * + * @param string $order The 'order' query variable. + * @return string The sanitized 'order' query variable. + */ + protected function parse_order( $order ) { + if ( ! is_string( $order ) || empty( $order ) ) { + return 'DESC'; + } + + if ( 'ASC' === strtoupper( $order ) ) { + return 'ASC'; + } else { + return 'DESC'; + } + } + + /** + * Sets the 404 property and saves whether query is feed. + * + * @since 2.0.0 + */ + public function set_404() { + $is_feed = $this->is_feed; + + $this->init_query_flags(); + $this->is_404 = true; + + $this->is_feed = $is_feed; + + /** + * Fires after a 404 is triggered. + * + * @since 5.5.0 + * + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + do_action_ref_array( 'set_404', array( $this ) ); + } + + /** + * Retrieves the value of a query variable. + * + * @since 1.5.0 + * @since 3.9.0 The `$default` argument was introduced. + * + * @param string $query_var Query variable key. + * @param mixed $default Optional. Value to return if the query variable is not set. Default empty string. + * @return mixed Contents of the query variable. + */ + public function get( $query_var, $default = '' ) { + if ( isset( $this->query_vars[ $query_var ] ) ) { + return $this->query_vars[ $query_var ]; + } + + return $default; + } + + /** + * Sets the value of a query variable. + * + * @since 1.5.0 + * + * @param string $query_var Query variable key. + * @param mixed $value Query variable value. + */ + public function set( $query_var, $value ) { + $this->query_vars[ $query_var ] = $value; + } + + /** + * Retrieves an array of posts based on query variables. + * + * There are a few filters and actions that can be used to modify the post + * database query. + * + * @since 1.5.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @return WP_Post[]|int[] Array of post objects or post IDs. + */ + public function get_posts() { + global $wpdb; + + $this->parse_query(); + + /** + * Fires after the query variable object is created, but before the actual query is run. + * + * Note: If using conditional tags, use the method versions within the passed instance + * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions + * like is_main_query() test against the global $wp_query instance, not the passed one. + * + * @since 2.0.0 + * + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + do_action_ref_array( 'pre_get_posts', array( &$this ) ); + + // Shorthand. + $q = &$this->query_vars; + + // Fill again in case 'pre_get_posts' unset some vars. + $q = $this->fill_query_vars( $q ); + + // Parse meta query. + $this->meta_query = new WP_Meta_Query(); + $this->meta_query->parse_query_vars( $q ); + + // Set a flag if a 'pre_get_posts' hook changed the query vars. + $hash = md5( serialize( $this->query_vars ) ); + if ( $hash != $this->query_vars_hash ) { + $this->query_vars_changed = true; + $this->query_vars_hash = $hash; + } + unset( $hash ); + + // First let's clear some variables. + $distinct = ''; + $whichauthor = ''; + $whichmimetype = ''; + $where = ''; + $limits = ''; + $join = ''; + $search = ''; + $groupby = ''; + $post_status_join = false; + $page = 1; + + if ( isset( $q['caller_get_posts'] ) ) { + _deprecated_argument( + 'WP_Query', + '3.1.0', + sprintf( + /* translators: 1: caller_get_posts, 2: ignore_sticky_posts */ + __( '%1$s is deprecated. Use %2$s instead.' ), + 'caller_get_posts', + 'ignore_sticky_posts' + ) + ); + + if ( ! isset( $q['ignore_sticky_posts'] ) ) { + $q['ignore_sticky_posts'] = $q['caller_get_posts']; + } + } + + if ( ! isset( $q['ignore_sticky_posts'] ) ) { + $q['ignore_sticky_posts'] = false; + } + + if ( ! isset( $q['suppress_filters'] ) ) { + $q['suppress_filters'] = false; + } + + if ( ! isset( $q['cache_results'] ) ) { + if ( wp_using_ext_object_cache() ) { + $q['cache_results'] = false; + } else { + $q['cache_results'] = true; + } + } + + if ( ! isset( $q['update_post_term_cache'] ) ) { + $q['update_post_term_cache'] = true; + } + + if ( ! isset( $q['lazy_load_term_meta'] ) ) { + $q['lazy_load_term_meta'] = $q['update_post_term_cache']; + } + + if ( ! isset( $q['update_post_meta_cache'] ) ) { + $q['update_post_meta_cache'] = true; + } + + if ( ! isset( $q['post_type'] ) ) { + if ( $this->is_search ) { + $q['post_type'] = 'any'; + } else { + $q['post_type'] = ''; + } + } + $post_type = $q['post_type']; + if ( empty( $q['posts_per_page'] ) ) { + $q['posts_per_page'] = get_option( 'posts_per_page' ); + } + if ( isset( $q['showposts'] ) && $q['showposts'] ) { + $q['showposts'] = (int) $q['showposts']; + $q['posts_per_page'] = $q['showposts']; + } + if ( ( isset( $q['posts_per_archive_page'] ) && 0 != $q['posts_per_archive_page'] ) && ( $this->is_archive || $this->is_search ) ) { + $q['posts_per_page'] = $q['posts_per_archive_page']; + } + if ( ! isset( $q['nopaging'] ) ) { + if ( -1 == $q['posts_per_page'] ) { + $q['nopaging'] = true; + } else { + $q['nopaging'] = false; + } + } + + if ( $this->is_feed ) { + // This overrides 'posts_per_page'. + if ( ! empty( $q['posts_per_rss'] ) ) { + $q['posts_per_page'] = $q['posts_per_rss']; + } else { + $q['posts_per_page'] = get_option( 'posts_per_rss' ); + } + $q['nopaging'] = false; + } + $q['posts_per_page'] = (int) $q['posts_per_page']; + if ( $q['posts_per_page'] < -1 ) { + $q['posts_per_page'] = abs( $q['posts_per_page'] ); + } elseif ( 0 == $q['posts_per_page'] ) { + $q['posts_per_page'] = 1; + } + + if ( ! isset( $q['comments_per_page'] ) || 0 == $q['comments_per_page'] ) { + $q['comments_per_page'] = get_option( 'comments_per_page' ); + } + + if ( $this->is_home && ( empty( $this->query ) || 'true' === $q['preview'] ) && ( 'page' === get_option( 'show_on_front' ) ) && get_option( 'page_on_front' ) ) { + $this->is_page = true; + $this->is_home = false; + $q['page_id'] = get_option( 'page_on_front' ); + } + + if ( isset( $q['page'] ) ) { + $q['page'] = trim( $q['page'], '/' ); + $q['page'] = absint( $q['page'] ); + } + + // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. + if ( isset( $q['no_found_rows'] ) ) { + $q['no_found_rows'] = (bool) $q['no_found_rows']; + } else { + $q['no_found_rows'] = false; + } + + switch ( $q['fields'] ) { + case 'ids': + $fields = "{$wpdb->posts}.ID"; + break; + case 'id=>parent': + $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent"; + break; + default: + $fields = "{$wpdb->posts}.*"; + } + + if ( '' !== $q['menu_order'] ) { + $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order']; + } + // The "m" parameter is meant for months but accepts datetimes of varying specificity. + if ( $q['m'] ) { + $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr( $q['m'], 0, 4 ); + if ( strlen( $q['m'] ) > 5 ) { + $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 4, 2 ); + } + if ( strlen( $q['m'] ) > 7 ) { + $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 6, 2 ); + } + if ( strlen( $q['m'] ) > 9 ) { + $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr( $q['m'], 8, 2 ); + } + if ( strlen( $q['m'] ) > 11 ) { + $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr( $q['m'], 10, 2 ); + } + if ( strlen( $q['m'] ) > 13 ) { + $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr( $q['m'], 12, 2 ); + } + } + + // Handle the other individual date parameters. + $date_parameters = array(); + + if ( '' !== $q['hour'] ) { + $date_parameters['hour'] = $q['hour']; + } + + if ( '' !== $q['minute'] ) { + $date_parameters['minute'] = $q['minute']; + } + + if ( '' !== $q['second'] ) { + $date_parameters['second'] = $q['second']; + } + + if ( $q['year'] ) { + $date_parameters['year'] = $q['year']; + } + + if ( $q['monthnum'] ) { + $date_parameters['monthnum'] = $q['monthnum']; + } + + if ( $q['w'] ) { + $date_parameters['week'] = $q['w']; + } + + if ( $q['day'] ) { + $date_parameters['day'] = $q['day']; + } + + if ( $date_parameters ) { + $date_query = new WP_Date_Query( array( $date_parameters ) ); + $where .= $date_query->get_sql(); + } + unset( $date_parameters, $date_query ); + + // Handle complex date queries. + if ( ! empty( $q['date_query'] ) ) { + $this->date_query = new WP_Date_Query( $q['date_query'] ); + $where .= $this->date_query->get_sql(); + } + + // If we've got a post_type AND it's not "any" post_type. + if ( ! empty( $q['post_type'] ) && 'any' !== $q['post_type'] ) { + foreach ( (array) $q['post_type'] as $_post_type ) { + $ptype_obj = get_post_type_object( $_post_type ); + if ( ! $ptype_obj || ! $ptype_obj->query_var || empty( $q[ $ptype_obj->query_var ] ) ) { + continue; + } + + if ( ! $ptype_obj->hierarchical ) { + // Non-hierarchical post types can directly use 'name'. + $q['name'] = $q[ $ptype_obj->query_var ]; + } else { + // Hierarchical post types will operate through 'pagename'. + $q['pagename'] = $q[ $ptype_obj->query_var ]; + $q['name'] = ''; + } + + // Only one request for a slug is possible, this is why name & pagename are overwritten above. + break; + } // End foreach. + unset( $ptype_obj ); + } + + if ( '' !== $q['title'] ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) ); + } + + // Parameters related to 'post_name'. + if ( '' !== $q['name'] ) { + $q['name'] = sanitize_title_for_query( $q['name'] ); + $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; + } elseif ( '' !== $q['pagename'] ) { + if ( isset( $this->queried_object_id ) ) { + $reqpage = $this->queried_object_id; + } else { + if ( 'page' !== $q['post_type'] ) { + foreach ( (array) $q['post_type'] as $_post_type ) { + $ptype_obj = get_post_type_object( $_post_type ); + if ( ! $ptype_obj || ! $ptype_obj->hierarchical ) { + continue; + } + + $reqpage = get_page_by_path( $q['pagename'], OBJECT, $_post_type ); + if ( $reqpage ) { + break; + } + } + unset( $ptype_obj ); + } else { + $reqpage = get_page_by_path( $q['pagename'] ); + } + if ( ! empty( $reqpage ) ) { + $reqpage = $reqpage->ID; + } else { + $reqpage = 0; + } + } + + $page_for_posts = get_option( 'page_for_posts' ); + if ( ( 'page' !== get_option( 'show_on_front' ) ) || empty( $page_for_posts ) || ( $reqpage != $page_for_posts ) ) { + $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); + $q['name'] = $q['pagename']; + $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; + $reqpage_obj = get_post( $reqpage ); + if ( is_object( $reqpage_obj ) && 'attachment' === $reqpage_obj->post_type ) { + $this->is_attachment = true; + $post_type = 'attachment'; + $q['post_type'] = 'attachment'; + $this->is_page = true; + $q['attachment_id'] = $reqpage; + } + } + } elseif ( '' !== $q['attachment'] ) { + $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); + $q['name'] = $q['attachment']; + $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; + } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { + $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); + $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; + $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; + } + + // If an attachment is requested by number, let it supersede any post number. + if ( $q['attachment_id'] ) { + $q['p'] = absint( $q['attachment_id'] ); + } + + // If a post number is specified, load that post. + if ( $q['p'] ) { + $where .= " AND {$wpdb->posts}.ID = " . $q['p']; + } elseif ( $q['post__in'] ) { + $post__in = implode( ',', array_map( 'absint', $q['post__in'] ) ); + $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; + } elseif ( $q['post__not_in'] ) { + $post__not_in = implode( ',', array_map( 'absint', $q['post__not_in'] ) ); + $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; + } + + if ( is_numeric( $q['post_parent'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); + } elseif ( $q['post_parent__in'] ) { + $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); + $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; + } elseif ( $q['post_parent__not_in'] ) { + $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) ); + $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; + } + + if ( $q['page_id'] ) { + if ( ( 'page' !== get_option( 'show_on_front' ) ) || ( get_option( 'page_for_posts' ) != $q['page_id'] ) ) { + $q['p'] = $q['page_id']; + $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; + } + } + + // If a search pattern is specified, load the posts that match. + if ( strlen( $q['s'] ) ) { + $search = $this->parse_search( $q ); + } + + if ( ! $q['suppress_filters'] ) { + /** + * Filters the search SQL that is used in the WHERE clause of WP_Query. + * + * @since 3.0.0 + * + * @param string $search Search SQL for WHERE clause. + * @param WP_Query $query The current WP_Query object. + */ + $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); + } + + // Taxonomies. + if ( ! $this->is_singular ) { + $this->parse_tax_query( $q ); + + $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); + + $join .= $clauses['join']; + $where .= $clauses['where']; + } + + if ( $this->is_tax ) { + if ( empty( $post_type ) ) { + // Do a fully inclusive search for currently registered post types of queried taxonomies. + $post_type = array(); + $taxonomies = array_keys( $this->tax_query->queried_terms ); + foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) { + $object_taxonomies = 'attachment' === $pt ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt ); + if ( array_intersect( $taxonomies, $object_taxonomies ) ) { + $post_type[] = $pt; + } + } + if ( ! $post_type ) { + $post_type = 'any'; + } elseif ( count( $post_type ) == 1 ) { + $post_type = $post_type[0]; + } + + $post_status_join = true; + } elseif ( in_array( 'attachment', (array) $post_type, true ) ) { + $post_status_join = true; + } + } + + /* + * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and + * 'category_name' vars are set for backward compatibility. + */ + if ( ! empty( $this->tax_query->queried_terms ) ) { + + /* + * Set 'taxonomy', 'term', and 'term_id' to the + * first taxonomy other than 'post_tag' or 'category'. + */ + if ( ! isset( $q['taxonomy'] ) ) { + foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { + if ( empty( $queried_items['terms'][0] ) ) { + continue; + } + + if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ), true ) ) { + $q['taxonomy'] = $queried_taxonomy; + + if ( 'slug' === $queried_items['field'] ) { + $q['term'] = $queried_items['terms'][0]; + } else { + $q['term_id'] = $queried_items['terms'][0]; + } + + // Take the first one we find. + break; + } + } + } + + // 'cat', 'category_name', 'tag_id'. + foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { + if ( empty( $queried_items['terms'][0] ) ) { + continue; + } + + if ( 'category' === $queried_taxonomy ) { + $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' ); + if ( $the_cat ) { + $this->set( 'cat', $the_cat->term_id ); + $this->set( 'category_name', $the_cat->slug ); + } + unset( $the_cat ); + } + + if ( 'post_tag' === $queried_taxonomy ) { + $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' ); + if ( $the_tag ) { + $this->set( 'tag_id', $the_tag->term_id ); + } + unset( $the_tag ); + } + } + } + + if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) { + $groupby = "{$wpdb->posts}.ID"; + } + + // Author/user stuff. + + if ( ! empty( $q['author'] ) && '0' != $q['author'] ) { + $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); + $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) ); + foreach ( $authors as $author ) { + $key = $author > 0 ? 'author__in' : 'author__not_in'; + $q[ $key ][] = abs( $author ); + } + $q['author'] = implode( ',', $authors ); + } + + if ( ! empty( $q['author__not_in'] ) ) { + $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); + $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; + } elseif ( ! empty( $q['author__in'] ) ) { + $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); + $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; + } + + // Author stuff for nice URLs. + + if ( '' !== $q['author_name'] ) { + if ( strpos( $q['author_name'], '/' ) !== false ) { + $q['author_name'] = explode( '/', $q['author_name'] ); + if ( $q['author_name'][ count( $q['author_name'] ) - 1 ] ) { + $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 1 ]; // No trailing slash. + } else { + $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 2 ]; // There was a trailing slash. + } + } + $q['author_name'] = sanitize_title_for_query( $q['author_name'] ); + $q['author'] = get_user_by( 'slug', $q['author_name'] ); + if ( $q['author'] ) { + $q['author'] = $q['author']->ID; + } + $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint( $q['author'] ) . ')'; + } + + // Matching by comment count. + if ( isset( $q['comment_count'] ) ) { + // Numeric comment count is converted to array format. + if ( is_numeric( $q['comment_count'] ) ) { + $q['comment_count'] = array( + 'value' => (int) $q['comment_count'], + ); + } + + if ( isset( $q['comment_count']['value'] ) ) { + $q['comment_count'] = array_merge( + array( + 'compare' => '=', + ), + $q['comment_count'] + ); + + // Fallback for invalid compare operators is '='. + $compare_operators = array( '=', '!=', '>', '>=', '<', '<=' ); + if ( ! in_array( $q['comment_count']['compare'], $compare_operators, true ) ) { + $q['comment_count']['compare'] = '='; + } + + $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count {$q['comment_count']['compare']} %d", $q['comment_count']['value'] ); + } + } + + // MIME-Type stuff for attachment browsing. + + if ( isset( $q['post_mime_type'] ) && '' !== $q['post_mime_type'] ) { + $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts ); + } + $where .= $search . $whichauthor . $whichmimetype; + + if ( ! empty( $this->meta_query->queries ) ) { + $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this ); + $join .= $clauses['join']; + $where .= $clauses['where']; + } + + $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] ); + if ( ! isset( $q['order'] ) ) { + $q['order'] = $rand ? '' : 'DESC'; + } else { + $q['order'] = $rand ? '' : $this->parse_order( $q['order'] ); + } + + // These values of orderby should ignore the 'order' parameter. + $force_asc = array( 'post__in', 'post_name__in', 'post_parent__in' ); + if ( isset( $q['orderby'] ) && in_array( $q['orderby'], $force_asc, true ) ) { + $q['order'] = ''; + } + + // Order by. + if ( empty( $q['orderby'] ) ) { + /* + * Boolean false or empty array blanks out ORDER BY, + * while leaving the value unset or otherwise empty sets the default. + */ + if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) { + $orderby = ''; + } else { + $orderby = "{$wpdb->posts}.post_date " . $q['order']; + } + } elseif ( 'none' === $q['orderby'] ) { + $orderby = ''; + } else { + $orderby_array = array(); + if ( is_array( $q['orderby'] ) ) { + foreach ( $q['orderby'] as $_orderby => $order ) { + $orderby = addslashes_gpc( urldecode( $_orderby ) ); + $parsed = $this->parse_orderby( $orderby ); + + if ( ! $parsed ) { + continue; + } + + $orderby_array[] = $parsed . ' ' . $this->parse_order( $order ); + } + $orderby = implode( ', ', $orderby_array ); + + } else { + $q['orderby'] = urldecode( $q['orderby'] ); + $q['orderby'] = addslashes_gpc( $q['orderby'] ); + + foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) { + $parsed = $this->parse_orderby( $orderby ); + // Only allow certain values for safety. + if ( ! $parsed ) { + continue; + } + + $orderby_array[] = $parsed; + } + $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); + + if ( empty( $orderby ) ) { + $orderby = "{$wpdb->posts}.post_date " . $q['order']; + } elseif ( ! empty( $q['order'] ) ) { + $orderby .= " {$q['order']}"; + } + } + } + + // Order search results by relevance only when another "orderby" is not specified in the query. + if ( ! empty( $q['s'] ) ) { + $search_orderby = ''; + if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) ) { + $search_orderby = $this->parse_search_order( $q ); + } + + if ( ! $q['suppress_filters'] ) { + /** + * Filters the ORDER BY used when ordering search results. + * + * @since 3.7.0 + * + * @param string $search_orderby The ORDER BY clause. + * @param WP_Query $query The current WP_Query instance. + */ + $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); + } + + if ( $search_orderby ) { + $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; + } + } + + if ( is_array( $post_type ) && count( $post_type ) > 1 ) { + $post_type_cap = 'multiple_post_type'; + } else { + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $post_type_object = get_post_type_object( $post_type ); + if ( empty( $post_type_object ) ) { + $post_type_cap = $post_type; + } + } + + if ( isset( $q['post_password'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $q['post_password'] ); + if ( empty( $q['perm'] ) ) { + $q['perm'] = 'readable'; + } + } elseif ( isset( $q['has_password'] ) ) { + $where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' ); + } + + if ( ! empty( $q['comment_status'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] ); + } + + if ( ! empty( $q['ping_status'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] ); + } + + if ( 'any' === $post_type ) { + $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) ); + if ( empty( $in_search_post_types ) ) { + $where .= ' AND 1=0 '; + } else { + $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; + } + } elseif ( ! empty( $post_type ) && is_array( $post_type ) ) { + $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')"; + } elseif ( ! empty( $post_type ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); + $post_type_object = get_post_type_object( $post_type ); + } elseif ( $this->is_attachment ) { + $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; + $post_type_object = get_post_type_object( 'attachment' ); + } elseif ( $this->is_page ) { + $where .= " AND {$wpdb->posts}.post_type = 'page'"; + $post_type_object = get_post_type_object( 'page' ); + } else { + $where .= " AND {$wpdb->posts}.post_type = 'post'"; + $post_type_object = get_post_type_object( 'post' ); + } + + $edit_cap = 'edit_post'; + $read_cap = 'read_post'; + + if ( ! empty( $post_type_object ) ) { + $edit_others_cap = $post_type_object->cap->edit_others_posts; + $read_private_cap = $post_type_object->cap->read_private_posts; + } else { + $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; + $read_private_cap = 'read_private_' . $post_type_cap . 's'; + } + + $user_id = get_current_user_id(); + + $q_status = array(); + if ( ! empty( $q['post_status'] ) ) { + $statuswheres = array(); + $q_status = $q['post_status']; + if ( ! is_array( $q_status ) ) { + $q_status = explode( ',', $q_status ); + } + $r_status = array(); + $p_status = array(); + $e_status = array(); + if ( in_array( 'any', $q_status, true ) ) { + foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { + if ( ! in_array( $status, $q_status, true ) ) { + $e_status[] = "{$wpdb->posts}.post_status <> '$status'"; + } + } + } else { + foreach ( get_post_stati() as $status ) { + if ( in_array( $status, $q_status, true ) ) { + if ( 'private' === $status ) { + $p_status[] = "{$wpdb->posts}.post_status = '$status'"; + } else { + $r_status[] = "{$wpdb->posts}.post_status = '$status'"; + } + } + } + } + + if ( empty( $q['perm'] ) || 'readable' !== $q['perm'] ) { + $r_status = array_merge( $r_status, $p_status ); + unset( $p_status ); + } + + if ( ! empty( $e_status ) ) { + $statuswheres[] = '(' . implode( ' AND ', $e_status ) . ')'; + } + if ( ! empty( $r_status ) ) { + if ( ! empty( $q['perm'] ) && 'editable' === $q['perm'] && ! current_user_can( $edit_others_cap ) ) { + $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $r_status ) . '))'; + } else { + $statuswheres[] = '(' . implode( ' OR ', $r_status ) . ')'; + } + } + if ( ! empty( $p_status ) ) { + if ( ! empty( $q['perm'] ) && 'readable' === $q['perm'] && ! current_user_can( $read_private_cap ) ) { + $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $p_status ) . '))'; + } else { + $statuswheres[] = '(' . implode( ' OR ', $p_status ) . ')'; + } + } + if ( $post_status_join ) { + $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; + foreach ( $statuswheres as $index => $statuswhere ) { + $statuswheres[ $index ] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . '))'; + } + } + $where_status = implode( ' OR ', $statuswheres ); + if ( ! empty( $where_status ) ) { + $where .= " AND ($where_status)"; + } + } elseif ( ! $this->is_singular ) { + $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; + + // Add public states. + $public_states = get_post_stati( array( 'public' => true ) ); + foreach ( (array) $public_states as $state ) { + if ( 'publish' === $state ) { // Publish is hard-coded above. + continue; + } + $where .= " OR {$wpdb->posts}.post_status = '$state'"; + } + + if ( $this->is_admin ) { + // Add protected states that should show in the admin all list. + $admin_all_states = get_post_stati( + array( + 'protected' => true, + 'show_in_admin_all_list' => true, + ) + ); + foreach ( (array) $admin_all_states as $state ) { + $where .= " OR {$wpdb->posts}.post_status = '$state'"; + } + } + + if ( is_user_logged_in() ) { + // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. + $private_states = get_post_stati( array( 'private' => true ) ); + foreach ( (array) $private_states as $state ) { + $where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'"; + } + } + + $where .= ')'; + } + + /* + * Apply filters on where and join prior to paging so that any + * manipulations to them are reflected in the paging by day queries. + */ + if ( ! $q['suppress_filters'] ) { + /** + * Filters the WHERE clause of the query. + * + * @since 1.5.0 + * + * @param string $where The WHERE clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) ); + + /** + * Filters the JOIN clause of the query. + * + * @since 1.5.0 + * + * @param string $join The JOIN clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) ); + } + + // Paging. + if ( empty( $q['nopaging'] ) && ! $this->is_singular ) { + $page = absint( $q['paged'] ); + if ( ! $page ) { + $page = 1; + } + + // If 'offset' is provided, it takes precedence over 'paged'. + if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) { + $q['offset'] = absint( $q['offset'] ); + $pgstrt = $q['offset'] . ', '; + } else { + $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; + } + $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; + } + + // Comments feeds. + if ( $this->is_comment_feed && ! $this->is_singular ) { + if ( $this->is_archive || $this->is_search ) { + $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join "; + $cwhere = "WHERE comment_approved = '1' $where"; + $cgroupby = "{$wpdb->comments}.comment_id"; + } else { // Other non-singular, e.g. front. + $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; + $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'"; + $cgroupby = ''; + } + + if ( ! $q['suppress_filters'] ) { + /** + * Filters the JOIN clause of the comments feed query before sending. + * + * @since 2.2.0 + * + * @param string $cjoin The JOIN clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) ); + + /** + * Filters the WHERE clause of the comments feed query before sending. + * + * @since 2.2.0 + * + * @param string $cwhere The WHERE clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) ); + + /** + * Filters the GROUP BY clause of the comments feed query before sending. + * + * @since 2.2.0 + * + * @param string $cgroupby The GROUP BY clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) ); + + /** + * Filters the ORDER BY clause of the comments feed query before sending. + * + * @since 2.8.0 + * + * @param string $corderby The ORDER BY clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); + + /** + * Filters the LIMIT clause of the comments feed query before sending. + * + * @since 2.8.0 + * + * @param string $climits The JOIN clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); + } + + $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; + $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; + $climits = ( ! empty( $climits ) ) ? $climits : ''; + + $comments = (array) $wpdb->get_results( "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits" ); + // Convert to WP_Comment. + /** @var WP_Comment[] */ + $this->comments = array_map( 'get_comment', $comments ); + $this->comment_count = count( $this->comments ); + + $post_ids = array(); + + foreach ( $this->comments as $comment ) { + $post_ids[] = (int) $comment->comment_post_ID; + } + + $post_ids = implode( ',', $post_ids ); + $join = ''; + if ( $post_ids ) { + $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; + } else { + $where = 'AND 0'; + } + } + + $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); + + /* + * Apply post-paging filters on where and join. Only plugins that + * manipulate paging queries should use these hooks. + */ + if ( ! $q['suppress_filters'] ) { + /** + * Filters the WHERE clause of the query. + * + * Specifically for manipulating paging queries. + * + * @since 1.5.0 + * + * @param string $where The WHERE clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); + + /** + * Filters the GROUP BY clause of the query. + * + * @since 2.0.0 + * + * @param string $groupby The GROUP BY clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); + + /** + * Filters the JOIN clause of the query. + * + * Specifically for manipulating paging queries. + * + * @since 1.5.0 + * + * @param string $join The JOIN clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); + + /** + * Filters the ORDER BY clause of the query. + * + * @since 1.5.1 + * + * @param string $orderby The ORDER BY clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); + + /** + * Filters the DISTINCT clause of the query. + * + * @since 2.1.0 + * + * @param string $distinct The DISTINCT clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); + + /** + * Filters the LIMIT clause of the query. + * + * @since 2.1.0 + * + * @param string $limits The LIMIT clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); + + /** + * Filters the SELECT clause of the query. + * + * @since 2.1.0 + * + * @param string $fields The SELECT clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); + + /** + * Filters all query clauses at once, for convenience. + * + * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, + * fields (SELECT), and LIMITS clauses. + * + * @since 3.1.0 + * + * @param string[] $clauses Associative array of the clauses for the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); + + $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; + $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; + $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; + $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; + $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; + $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; + $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; + } + + /** + * Fires to announce the query's current selection parameters. + * + * For use by caching plugins. + * + * @since 2.3.0 + * + * @param string $selection The assembled selection query. + */ + do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join ); + + /* + * Filters again for the benefit of caching plugins. + * Regular plugins should use the hooks above. + */ + if ( ! $q['suppress_filters'] ) { + /** + * Filters the WHERE clause of the query. + * + * For use by caching plugins. + * + * @since 2.5.0 + * + * @param string $where The WHERE clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); + + /** + * Filters the GROUP BY clause of the query. + * + * For use by caching plugins. + * + * @since 2.5.0 + * + * @param string $groupby The GROUP BY clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); + + /** + * Filters the JOIN clause of the query. + * + * For use by caching plugins. + * + * @since 2.5.0 + * + * @param string $join The JOIN clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); + + /** + * Filters the ORDER BY clause of the query. + * + * For use by caching plugins. + * + * @since 2.5.0 + * + * @param string $orderby The ORDER BY clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); + + /** + * Filters the DISTINCT clause of the query. + * + * For use by caching plugins. + * + * @since 2.5.0 + * + * @param string $distinct The DISTINCT clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) ); + + /** + * Filters the SELECT clause of the query. + * + * For use by caching plugins. + * + * @since 2.5.0 + * + * @param string $fields The SELECT clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) ); + + /** + * Filters the LIMIT clause of the query. + * + * For use by caching plugins. + * + * @since 2.5.0 + * + * @param string $limits The LIMIT clause of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); + + /** + * Filters all query clauses at once, for convenience. + * + * For use by caching plugins. + * + * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, + * fields (SELECT), and LIMITS clauses. + * + * @since 3.1.0 + * + * @param string[] $pieces Associative array of the pieces of the query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); + + $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; + $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; + $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; + $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; + $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; + $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; + $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; + } + + if ( ! empty( $groupby ) ) { + $groupby = 'GROUP BY ' . $groupby; + } + if ( ! empty( $orderby ) ) { + $orderby = 'ORDER BY ' . $orderby; + } + + $found_rows = ''; + if ( ! $q['no_found_rows'] && ! empty( $limits ) ) { + $found_rows = 'SQL_CALC_FOUND_ROWS'; + } + + $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + $this->request = $old_request; + + if ( ! $q['suppress_filters'] ) { + /** + * Filters the completed SQL query before sending. + * + * @since 2.0.0 + * + * @param string $request The complete SQL query. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) ); + } + + /** + * Filters the posts array before the query takes place. + * + * Return a non-null value to bypass WordPress' default post queries. + * + * Filtering functions that require pagination information are encouraged to set + * the `found_posts` and `max_num_pages` properties of the WP_Query object, + * passed to the filter by reference. If WP_Query does not perform a database + * query, it will not have enough information to generate these values itself. + * + * @since 4.6.0 + * + * @param WP_Post[]|int[]|null $posts Return an array of post data to short-circuit WP's query, + * or null to allow WP to run its normal queries. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) ); + + if ( 'ids' === $q['fields'] ) { + if ( null === $this->posts ) { + $this->posts = $wpdb->get_col( $this->request ); + } + + /** @var int[] */ + $this->posts = array_map( 'intval', $this->posts ); + $this->post_count = count( $this->posts ); + $this->set_found_posts( $q, $limits ); + + return $this->posts; + } + + if ( 'id=>parent' === $q['fields'] ) { + if ( null === $this->posts ) { + $this->posts = $wpdb->get_results( $this->request ); + } + + $this->post_count = count( $this->posts ); + $this->set_found_posts( $q, $limits ); + + /** @var int[] */ + $r = array(); + foreach ( $this->posts as $key => $post ) { + $this->posts[ $key ]->ID = (int) $post->ID; + $this->posts[ $key ]->post_parent = (int) $post->post_parent; + + $r[ (int) $post->ID ] = (int) $post->post_parent; + } + + return $r; + } + + if ( null === $this->posts ) { + $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" === $fields && ! empty( $limits ) && $q['posts_per_page'] < 500 ); + + /** + * Filters whether to split the query. + * + * Splitting the query will cause it to fetch just the IDs of the found posts + * (and then individually fetch each post by ID), rather than fetching every + * complete row at once. One massive result vs. many small results. + * + * @since 3.4.0 + * + * @param bool $split_the_query Whether or not to split the query. + * @param WP_Query $query The WP_Query instance. + */ + $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this ); + + if ( $split_the_query ) { + // First get the IDs and then fill in the objects. + + $this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + + /** + * Filters the Post IDs SQL request before sending. + * + * @since 3.4.0 + * + * @param string $request The post ID request. + * @param WP_Query $query The WP_Query instance. + */ + $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); + + $ids = $wpdb->get_col( $this->request ); + + if ( $ids ) { + $this->posts = $ids; + $this->set_found_posts( $q, $limits ); + _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + } else { + $this->posts = array(); + } + } else { + $this->posts = $wpdb->get_results( $this->request ); + $this->set_found_posts( $q, $limits ); + } + } + + // Convert to WP_Post objects. + if ( $this->posts ) { + /** @var WP_Post[] */ + $this->posts = array_map( 'get_post', $this->posts ); + } + + if ( ! $q['suppress_filters'] ) { + /** + * Filters the raw post results array, prior to status checks. + * + * @since 2.3.0 + * + * @param WP_Post[] $posts Array of post objects. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); + } + + if ( ! empty( $this->posts ) && $this->is_comment_feed && $this->is_singular ) { + /** This filter is documented in wp-includes/query.php */ + $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) ); + + /** This filter is documented in wp-includes/query.php */ + $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) ); + + /** This filter is documented in wp-includes/query.php */ + $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) ); + $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; + + /** This filter is documented in wp-includes/query.php */ + $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); + $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; + + /** This filter is documented in wp-includes/query.php */ + $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); + + $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; + $comments = $wpdb->get_results( $comments_request ); + // Convert to WP_Comment. + /** @var WP_Comment[] */ + $this->comments = array_map( 'get_comment', $comments ); + $this->comment_count = count( $this->comments ); + } + + // Check post status to determine if post should be displayed. + if ( ! empty( $this->posts ) && ( $this->is_single || $this->is_page ) ) { + $status = get_post_status( $this->posts[0] ); + + if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { + $this->is_page = false; + $this->is_single = true; + $this->is_attachment = true; + } + + // If the post_status was specifically requested, let it pass through. + if ( ! in_array( $status, $q_status, true ) ) { + $post_status_obj = get_post_status_object( $status ); + + if ( $post_status_obj && ! $post_status_obj->public ) { + if ( ! is_user_logged_in() ) { + // User must be logged in to view unpublished posts. + $this->posts = array(); + } else { + if ( $post_status_obj->protected ) { + // User must have edit permissions on the draft to preview. + if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } else { + $this->is_preview = true; + if ( 'future' !== $status ) { + $this->posts[0]->post_date = current_time( 'mysql' ); + } + } + } elseif ( $post_status_obj->private ) { + if ( ! current_user_can( $read_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } + } else { + $this->posts = array(); + } + } + } elseif ( ! $post_status_obj ) { + // Post status is not registered, assume it's not public. + if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } + } + } + + if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + /** + * Filters the single post for preview mode. + * + * @since 2.7.0 + * + * @param WP_Post $post_preview The Post object. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); + } + } + + // Put sticky posts at the top of the posts array. + $sticky_posts = get_option( 'sticky_posts' ); + if ( $this->is_home && $page <= 1 && is_array( $sticky_posts ) && ! empty( $sticky_posts ) && ! $q['ignore_sticky_posts'] ) { + $num_posts = count( $this->posts ); + $sticky_offset = 0; + // Loop over posts and relocate stickies to the front. + for ( $i = 0; $i < $num_posts; $i++ ) { + if ( in_array( $this->posts[ $i ]->ID, $sticky_posts, true ) ) { + $sticky_post = $this->posts[ $i ]; + // Remove sticky from current position. + array_splice( $this->posts, $i, 1 ); + // Move to front, after other stickies. + array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); + // Increment the sticky offset. The next sticky will be placed at this offset. + $sticky_offset++; + // Remove post from sticky posts array. + $offset = array_search( $sticky_post->ID, $sticky_posts, true ); + unset( $sticky_posts[ $offset ] ); + } + } + + // If any posts have been excluded specifically, Ignore those that are sticky. + if ( ! empty( $sticky_posts ) && ! empty( $q['post__not_in'] ) ) { + $sticky_posts = array_diff( $sticky_posts, $q['post__not_in'] ); + } + + // Fetch sticky posts that weren't in the query results. + if ( ! empty( $sticky_posts ) ) { + $stickies = get_posts( + array( + 'post__in' => $sticky_posts, + 'post_type' => $post_type, + 'post_status' => 'publish', + 'nopaging' => true, + ) + ); + + foreach ( $stickies as $sticky_post ) { + array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); + $sticky_offset++; + } + } + } + + // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up. + if ( ! empty( $this->comments ) ) { + wp_queue_comments_for_comment_meta_lazyload( $this->comments ); + } + + if ( ! $q['suppress_filters'] ) { + /** + * Filters the array of retrieved posts after they've been fetched and + * internally processed. + * + * @since 1.5.0 + * + * @param WP_Post[] $posts Array of post objects. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) ); + } + + // Ensure that any posts added/modified via one of the filters above are + // of the type WP_Post and are filtered. + if ( $this->posts ) { + $this->post_count = count( $this->posts ); + + /** @var WP_Post[] */ + $this->posts = array_map( 'get_post', $this->posts ); + + if ( $q['cache_results'] ) { + update_post_caches( $this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + } + + /** @var WP_Post */ + $this->post = reset( $this->posts ); + } else { + $this->post_count = 0; + $this->posts = array(); + } + + if ( $q['lazy_load_term_meta'] ) { + wp_queue_posts_for_term_meta_lazyload( $this->posts ); + } + + return $this->posts; + } + + /** + * Set up the amount of found posts and the number of pages (if limit clause was used) + * for the current query. + * + * @since 3.5.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param array $q Query variables. + * @param string $limits LIMIT clauses of the query. + */ + private function set_found_posts( $q, $limits ) { + global $wpdb; + + // Bail if posts is an empty array. Continue if posts is an empty string, + // null, or false to accommodate caching plugins that fill posts later. + if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) ) { + return; + } + + if ( ! empty( $limits ) ) { + /** + * Filters the query to run for retrieving the found posts. + * + * @since 2.1.0 + * + * @param string $found_posts_query The query to run to find the found posts. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); + + $this->found_posts = (int) $wpdb->get_var( $found_posts_query ); + } else { + if ( is_array( $this->posts ) ) { + $this->found_posts = count( $this->posts ); + } else { + if ( null === $this->posts ) { + $this->found_posts = 0; + } else { + $this->found_posts = 1; + } + } + } + + /** + * Filters the number of found posts for the query. + * + * @since 2.1.0 + * + * @param int $found_posts The number of posts found. + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); + + if ( ! empty( $limits ) ) { + $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); + } + } + + /** + * Set up the next post and iterate current post index. + * + * @since 1.5.0 + * + * @return WP_Post Next post. + */ + public function next_post() { + + $this->current_post++; + + /** @var WP_Post */ + $this->post = $this->posts[ $this->current_post ]; + return $this->post; + } + + /** + * Sets up the current post. + * + * Retrieves the next post, sets up the post, sets the 'in the loop' + * property to true. + * + * @since 1.5.0 + * + * @global WP_Post $post Global post object. + */ + public function the_post() { + global $post; + $this->in_the_loop = true; + + if ( -1 == $this->current_post ) { // Loop has just started. + /** + * Fires once the loop is started. + * + * @since 2.0.0 + * + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + do_action_ref_array( 'loop_start', array( &$this ) ); + } + + $post = $this->next_post(); + $this->setup_postdata( $post ); + } + + /** + * Determines whether there are more posts available in the loop. + * + * Calls the {@see 'loop_end'} action when the loop is complete. + * + * @since 1.5.0 + * + * @return bool True if posts are available, false if end of the loop. + */ + public function have_posts() { + if ( $this->current_post + 1 < $this->post_count ) { + return true; + } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) { + /** + * Fires once the loop has ended. + * + * @since 2.0.0 + * + * @param WP_Query $query The WP_Query instance (passed by reference). + */ + do_action_ref_array( 'loop_end', array( &$this ) ); + // Do some cleaning up after the loop. + $this->rewind_posts(); + } elseif ( 0 === $this->post_count ) { + /** + * Fires if no results are found in a post query. + * + * @since 4.9.0 + * + * @param WP_Query $query The WP_Query instance. + */ + do_action( 'loop_no_results', $this ); + } + + $this->in_the_loop = false; + return false; + } + + /** + * Rewind the posts and reset post index. + * + * @since 1.5.0 + */ + public function rewind_posts() { + $this->current_post = -1; + if ( $this->post_count > 0 ) { + $this->post = $this->posts[0]; + } + } + + /** + * Iterate current comment index and return WP_Comment object. + * + * @since 2.2.0 + * + * @return WP_Comment Comment object. + */ + public function next_comment() { + $this->current_comment++; + + /** @var WP_Comment */ + $this->comment = $this->comments[ $this->current_comment ]; + return $this->comment; + } + + /** + * Sets up the current comment. + * + * @since 2.2.0 + * + * @global WP_Comment $comment Global comment object. + */ + public function the_comment() { + global $comment; + + $comment = $this->next_comment(); + + if ( 0 == $this->current_comment ) { + /** + * Fires once the comment loop is started. + * + * @since 2.2.0 + */ + do_action( 'comment_loop_start' ); + } + } + + /** + * Whether there are more comments available. + * + * Automatically rewinds comments when finished. + * + * @since 2.2.0 + * + * @return bool True if comments are available, false if no more comments. + */ + public function have_comments() { + if ( $this->current_comment + 1 < $this->comment_count ) { + return true; + } elseif ( $this->current_comment + 1 == $this->comment_count ) { + $this->rewind_comments(); + } + + return false; + } + + /** + * Rewind the comments, resets the comment index and comment to first. + * + * @since 2.2.0 + */ + public function rewind_comments() { + $this->current_comment = -1; + if ( $this->comment_count > 0 ) { + $this->comment = $this->comments[0]; + } + } + + /** + * Sets up the WordPress query by parsing query string. + * + * @since 1.5.0 + * + * @see WP_Query::parse_query() for all available arguments. + * + * @param string|array $query URL query string or array of query arguments. + * @return WP_Post[]|int[] Array of post objects or post IDs. + */ + public function query( $query ) { + $this->init(); + $this->query = wp_parse_args( $query ); + $this->query_vars = $this->query; + return $this->get_posts(); + } + + /** + * Retrieves the currently queried object. + * + * If queried object is not set, then the queried object will be set from + * the category, tag, taxonomy, posts page, single post, page, or author + * query variable. After it is set up, it will be returned. + * + * @since 1.5.0 + * + * @return WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object. + */ + public function get_queried_object() { + if ( isset( $this->queried_object ) ) { + return $this->queried_object; + } + + $this->queried_object = null; + $this->queried_object_id = null; + + if ( $this->is_category || $this->is_tag || $this->is_tax ) { + if ( $this->is_category ) { + if ( $this->get( 'cat' ) ) { + $term = get_term( $this->get( 'cat' ), 'category' ); + } elseif ( $this->get( 'category_name' ) ) { + $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' ); + } + } elseif ( $this->is_tag ) { + if ( $this->get( 'tag_id' ) ) { + $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); + } elseif ( $this->get( 'tag' ) ) { + $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' ); + } + } else { + // For other tax queries, grab the first term from the first clause. + if ( ! empty( $this->tax_query->queried_terms ) ) { + $queried_taxonomies = array_keys( $this->tax_query->queried_terms ); + $matched_taxonomy = reset( $queried_taxonomies ); + $query = $this->tax_query->queried_terms[ $matched_taxonomy ]; + + if ( ! empty( $query['terms'] ) ) { + if ( 'term_id' === $query['field'] ) { + $term = get_term( reset( $query['terms'] ), $matched_taxonomy ); + } else { + $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy ); + } + } + } + } + + if ( ! empty( $term ) && ! is_wp_error( $term ) ) { + $this->queried_object = $term; + $this->queried_object_id = (int) $term->term_id; + + if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) { + _make_cat_compat( $this->queried_object ); + } + } + } elseif ( $this->is_post_type_archive ) { + $post_type = $this->get( 'post_type' ); + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $this->queried_object = get_post_type_object( $post_type ); + } elseif ( $this->is_posts_page ) { + $page_for_posts = get_option( 'page_for_posts' ); + $this->queried_object = get_post( $page_for_posts ); + $this->queried_object_id = (int) $this->queried_object->ID; + } elseif ( $this->is_singular && ! empty( $this->post ) ) { + $this->queried_object = $this->post; + $this->queried_object_id = (int) $this->post->ID; + } elseif ( $this->is_author ) { + $this->queried_object_id = (int) $this->get( 'author' ); + $this->queried_object = get_userdata( $this->queried_object_id ); + } + + return $this->queried_object; + } + + /** + * Retrieves the ID of the currently queried object. + * + * @since 1.5.0 + * + * @return int + */ + public function get_queried_object_id() { + $this->get_queried_object(); + + if ( isset( $this->queried_object_id ) ) { + return $this->queried_object_id; + } + + return 0; + } + + /** + * Constructor. + * + * Sets up the WordPress query, if parameter is not empty. + * + * @since 1.5.0 + * + * @see WP_Query::parse_query() for all available arguments. + * + * @param string|array $query URL query string or array of vars. + */ + public function __construct( $query = '' ) { + if ( ! empty( $query ) ) { + $this->query( $query ); + } + } + + /** + * Make private properties readable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Property to get. + * @return mixed Property. + */ + public function __get( $name ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + return $this->$name; + } + } + + /** + * Make private properties checkable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Property to check if set. + * @return bool Whether the property is set. + */ + public function __isset( $name ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + return isset( $this->$name ); + } + } + + /** + * Make private/protected methods readable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Method to call. + * @param array $arguments Arguments to pass when calling. + * @return mixed|false Return value of the callback, false otherwise. + */ + public function __call( $name, $arguments ) { + if ( in_array( $name, $this->compat_methods, true ) ) { + return $this->$name( ...$arguments ); + } + return false; + } + + /** + * Is the query for an existing archive page? + * + * Archive pages include category, tag, author, date, custom post type, + * and custom taxonomy based archives. + * + * @since 3.1.0 + * + * @see WP_Query::is_category() + * @see WP_Query::is_tag() + * @see WP_Query::is_author() + * @see WP_Query::is_date() + * @see WP_Query::is_post_type_archive() + * @see WP_Query::is_tax() + * + * @return bool Whether the query is for an existing archive page. + */ + public function is_archive() { + return (bool) $this->is_archive; + } + + /** + * Is the query for an existing post type archive page? + * + * @since 3.1.0 + * + * @param string|string[] $post_types Optional. Post type or array of posts types + * to check against. Default empty. + * @return bool Whether the query is for an existing post type archive page. + */ + public function is_post_type_archive( $post_types = '' ) { + if ( empty( $post_types ) || ! $this->is_post_type_archive ) { + return (bool) $this->is_post_type_archive; + } + + $post_type = $this->get( 'post_type' ); + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $post_type_object = get_post_type_object( $post_type ); + + return in_array( $post_type_object->name, (array) $post_types, true ); + } + + /** + * Is the query for an existing attachment page? + * + * @since 3.1.0 + * + * @param int|string|int[]|string[] $attachment Optional. Attachment ID, title, slug, or array of such + * to check against. Default empty. + * @return bool Whether the query is for an existing attachment page. + */ + public function is_attachment( $attachment = '' ) { + if ( ! $this->is_attachment ) { + return false; + } + + if ( empty( $attachment ) ) { + return true; + } + + $attachment = array_map( 'strval', (array) $attachment ); + + $post_obj = $this->get_queried_object(); + + if ( in_array( (string) $post_obj->ID, $attachment, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_title, $attachment, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_name, $attachment, true ) ) { + return true; + } + return false; + } + + /** + * Is the query for an existing author archive page? + * + * If the $author parameter is specified, this function will additionally + * check if the query is for one of the authors specified. + * + * @since 3.1.0 + * + * @param int|string|int[]|string[] $author Optional. User ID, nickname, nicename, or array of such + * to check against. Default empty. + * @return bool Whether the query is for an existing author archive page. + */ + public function is_author( $author = '' ) { + if ( ! $this->is_author ) { + return false; + } + + if ( empty( $author ) ) { + return true; + } + + $author_obj = $this->get_queried_object(); + + $author = array_map( 'strval', (array) $author ); + + if ( in_array( (string) $author_obj->ID, $author, true ) ) { + return true; + } elseif ( in_array( $author_obj->nickname, $author, true ) ) { + return true; + } elseif ( in_array( $author_obj->user_nicename, $author, true ) ) { + return true; + } + + return false; + } + + /** + * Is the query for an existing category archive page? + * + * If the $category parameter is specified, this function will additionally + * check if the query is for one of the categories specified. + * + * @since 3.1.0 + * + * @param int|string|int[]|string[] $category Optional. Category ID, name, slug, or array of such + * to check against. Default empty. + * @return bool Whether the query is for an existing category archive page. + */ + public function is_category( $category = '' ) { + if ( ! $this->is_category ) { + return false; + } + + if ( empty( $category ) ) { + return true; + } + + $cat_obj = $this->get_queried_object(); + + $category = array_map( 'strval', (array) $category ); + + if ( in_array( (string) $cat_obj->term_id, $category, true ) ) { + return true; + } elseif ( in_array( $cat_obj->name, $category, true ) ) { + return true; + } elseif ( in_array( $cat_obj->slug, $category, true ) ) { + return true; + } + + return false; + } + + /** + * Is the query for an existing tag archive page? + * + * If the $tag parameter is specified, this function will additionally + * check if the query is for one of the tags specified. + * + * @since 3.1.0 + * + * @param int|string|int[]|string[] $tag Optional. Tag ID, name, slug, or array of such + * to check against. Default empty. + * @return bool Whether the query is for an existing tag archive page. + */ + public function is_tag( $tag = '' ) { + if ( ! $this->is_tag ) { + return false; + } + + if ( empty( $tag ) ) { + return true; + } + + $tag_obj = $this->get_queried_object(); + + $tag = array_map( 'strval', (array) $tag ); + + if ( in_array( (string) $tag_obj->term_id, $tag, true ) ) { + return true; + } elseif ( in_array( $tag_obj->name, $tag, true ) ) { + return true; + } elseif ( in_array( $tag_obj->slug, $tag, true ) ) { + return true; + } + + return false; + } + + /** + * Is the query for an existing custom taxonomy archive page? + * + * If the $taxonomy parameter is specified, this function will additionally + * check if the query is for that specific $taxonomy. + * + * If the $term parameter is specified in addition to the $taxonomy parameter, + * this function will additionally check if the query is for one of the terms + * specified. + * + * @since 3.1.0 + * + * @global array $wp_taxonomies + * + * @param string|string[] $taxonomy Optional. Taxonomy slug or slugs to check against. + * Default empty. + * @param int|string|int[]|string[] $term Optional. Term ID, name, slug, or array of such + * to check against. Default empty. + * @return bool Whether the query is for an existing custom taxonomy archive page. + * True for custom taxonomy archive pages, false for built-in taxonomies + * (category and tag archives). + */ + public function is_tax( $taxonomy = '', $term = '' ) { + global $wp_taxonomies; + + if ( ! $this->is_tax ) { + return false; + } + + if ( empty( $taxonomy ) ) { + return true; + } + + $queried_object = $this->get_queried_object(); + $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy ); + $term_array = (array) $term; + + // Check that the taxonomy matches. + if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array, true ) ) ) { + return false; + } + + // Only a taxonomy provided. + if ( empty( $term ) ) { + return true; + } + + return isset( $queried_object->term_id ) && + count( + array_intersect( + array( $queried_object->term_id, $queried_object->name, $queried_object->slug ), + $term_array + ) + ); + } + + /** + * Whether the current URL is within the comments popup window. + * + * @since 3.1.0 + * @deprecated 4.5.0 + * + * @return false Always returns false. + */ + public function is_comments_popup() { + _deprecated_function( __FUNCTION__, '4.5.0' ); + + return false; + } + + /** + * Is the query for an existing date archive? + * + * @since 3.1.0 + * + * @return bool Whether the query is for an existing date archive. + */ + public function is_date() { + return (bool) $this->is_date; + } + + /** + * Is the query for an existing day archive? + * + * @since 3.1.0 + * + * @return bool Whether the query is for an existing day archive. + */ + public function is_day() { + return (bool) $this->is_day; + } + + /** + * Is the query for a feed? + * + * @since 3.1.0 + * + * @param string|string[] $feeds Optional. Feed type or array of feed types + * to check against. Default empty. + * @return bool Whether the query is for a feed. + */ + public function is_feed( $feeds = '' ) { + if ( empty( $feeds ) || ! $this->is_feed ) { + return (bool) $this->is_feed; + } + + $qv = $this->get( 'feed' ); + if ( 'feed' === $qv ) { + $qv = get_default_feed(); + } + + return in_array( $qv, (array) $feeds, true ); + } + + /** + * Is the query for a comments feed? + * + * @since 3.1.0 + * + * @return bool Whether the query is for a comments feed. + */ + public function is_comment_feed() { + return (bool) $this->is_comment_feed; + } + + /** + * Is the query for the front page of the site? + * + * This is for what is displayed at your site's main URL. + * + * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. + * + * If you set a static page for the front page of your site, this function will return + * true when viewing that page. + * + * Otherwise the same as @see WP_Query::is_home() + * + * @since 3.1.0 + * + * @return bool Whether the query is for the front page of the site. + */ + public function is_front_page() { + // Most likely case. + if ( 'posts' === get_option( 'show_on_front' ) && $this->is_home() ) { + return true; + } elseif ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) + && $this->is_page( get_option( 'page_on_front' ) ) + ) { + return true; + } else { + return false; + } + } + + /** + * Is the query for the blog homepage? + * + * This is the page which shows the time based blog content of your site. + * + * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'. + * + * If you set a static page for the front page of your site, this function will return + * true only on the page you set as the "Posts page". + * + * @since 3.1.0 + * + * @see WP_Query::is_front_page() + * + * @return bool Whether the query is for the blog homepage. + */ + public function is_home() { + return (bool) $this->is_home; + } + + /** + * Is the query for the Privacy Policy page? + * + * This is the page which shows the Privacy Policy content of your site. + * + * Depends on the site's "Change your Privacy Policy page" Privacy Settings 'wp_page_for_privacy_policy'. + * + * This function will return true only on the page you set as the "Privacy Policy page". + * + * @since 5.2.0 + * + * @return bool Whether the query is for the Privacy Policy page. + */ + public function is_privacy_policy() { + if ( get_option( 'wp_page_for_privacy_policy' ) + && $this->is_page( get_option( 'wp_page_for_privacy_policy' ) ) + ) { + return true; + } else { + return false; + } + } + + /** + * Is the query for an existing month archive? + * + * @since 3.1.0 + * + * @return bool Whether the query is for an existing month archive. + */ + public function is_month() { + return (bool) $this->is_month; + } + + /** + * Is the query for an existing single page? + * + * If the $page parameter is specified, this function will additionally + * check if the query is for one of the pages specified. + * + * @since 3.1.0 + * + * @see WP_Query::is_single() + * @see WP_Query::is_singular() + * + * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, path, or array of such + * to check against. Default empty. + * @return bool Whether the query is for an existing single page. + */ + public function is_page( $page = '' ) { + if ( ! $this->is_page ) { + return false; + } + + if ( empty( $page ) ) { + return true; + } + + $page_obj = $this->get_queried_object(); + + $page = array_map( 'strval', (array) $page ); + + if ( in_array( (string) $page_obj->ID, $page, true ) ) { + return true; + } elseif ( in_array( $page_obj->post_title, $page, true ) ) { + return true; + } elseif ( in_array( $page_obj->post_name, $page, true ) ) { + return true; + } else { + foreach ( $page as $pagepath ) { + if ( ! strpos( $pagepath, '/' ) ) { + continue; + } + $pagepath_obj = get_page_by_path( $pagepath ); + + if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) { + return true; + } + } + } + + return false; + } + + /** + * Is the query for a paged result and not for the first page? + * + * @since 3.1.0 + * + * @return bool Whether the query is for a paged result. + */ + public function is_paged() { + return (bool) $this->is_paged; + } + + /** + * Is the query for a post or page preview? + * + * @since 3.1.0 + * + * @return bool Whether the query is for a post or page preview. + */ + public function is_preview() { + return (bool) $this->is_preview; + } + + /** + * Is the query for the robots.txt file? + * + * @since 3.1.0 + * + * @return bool Whether the query is for the robots.txt file. + */ + public function is_robots() { + return (bool) $this->is_robots; + } + + /** + * Is the query for the favicon.ico file? + * + * @since 5.4.0 + * + * @return bool Whether the query is for the favicon.ico file. + */ + public function is_favicon() { + return (bool) $this->is_favicon; + } + + /** + * Is the query for a search? + * + * @since 3.1.0 + * + * @return bool Whether the query is for a search. + */ + public function is_search() { + return (bool) $this->is_search; + } + + /** + * Is the query for an existing single post? + * + * Works for any post type excluding pages. + * + * If the $post parameter is specified, this function will additionally + * check if the query is for one of the Posts specified. + * + * @since 3.1.0 + * + * @see WP_Query::is_page() + * @see WP_Query::is_singular() + * + * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, path, or array of such + * to check against. Default empty. + * @return bool Whether the query is for an existing single post. + */ + public function is_single( $post = '' ) { + if ( ! $this->is_single ) { + return false; + } + + if ( empty( $post ) ) { + return true; + } + + $post_obj = $this->get_queried_object(); + + $post = array_map( 'strval', (array) $post ); + + if ( in_array( (string) $post_obj->ID, $post, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_title, $post, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_name, $post, true ) ) { + return true; + } else { + foreach ( $post as $postpath ) { + if ( ! strpos( $postpath, '/' ) ) { + continue; + } + $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type ); + + if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) { + return true; + } + } + } + return false; + } + + /** + * Is the query for an existing single post of any post type (post, attachment, page, + * custom post types)? + * + * If the $post_types parameter is specified, this function will additionally + * check if the query is for one of the Posts Types specified. + * + * @since 3.1.0 + * + * @see WP_Query::is_page() + * @see WP_Query::is_single() + * + * @param string|string[] $post_types Optional. Post type or array of post types + * to check against. Default empty. + * @return bool Whether the query is for an existing single post + * or any of the given post types. + */ + public function is_singular( $post_types = '' ) { + if ( empty( $post_types ) || ! $this->is_singular ) { + return (bool) $this->is_singular; + } + + $post_obj = $this->get_queried_object(); + + return in_array( $post_obj->post_type, (array) $post_types, true ); + } + + /** + * Is the query for a specific time? + * + * @since 3.1.0 + * + * @return bool Whether the query is for a specific time. + */ + public function is_time() { + return (bool) $this->is_time; + } + + /** + * Is the query for a trackback endpoint call? + * + * @since 3.1.0 + * + * @return bool Whether the query is for a trackback endpoint call. + */ + public function is_trackback() { + return (bool) $this->is_trackback; + } + + /** + * Is the query for an existing year archive? + * + * @since 3.1.0 + * + * @return bool Whether the query is for an existing year archive. + */ + public function is_year() { + return (bool) $this->is_year; + } + + /** + * Is the query a 404 (returns no results)? + * + * @since 3.1.0 + * + * @return bool Whether the query is a 404 error. + */ + public function is_404() { + return (bool) $this->is_404; + } + + /** + * Is the query for an embedded post? + * + * @since 4.4.0 + * + * @return bool Whether the query is for an embedded post. + */ + public function is_embed() { + return (bool) $this->is_embed; + } + + /** + * Is the query the main query? + * + * @since 3.3.0 + * + * @global WP_Query $wp_query WordPress Query object. + * + * @return bool Whether the query is the main query. + */ + public function is_main_query() { + global $wp_the_query; + return $wp_the_query === $this; + } + + /** + * Set up global post data. + * + * @since 4.1.0 + * @since 4.4.0 Added the ability to pass a post ID to `$post`. + * + * @global int $id + * @global WP_User $authordata + * @global string $currentday + * @global string $currentmonth + * @global int $page + * @global array $pages + * @global int $multipage + * @global int $more + * @global int $numpages + * + * @param WP_Post|object|int $post WP_Post instance or Post ID/object. + * @return true True when finished. + */ + public function setup_postdata( $post ) { + global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages; + + if ( ! ( $post instanceof WP_Post ) ) { + $post = get_post( $post ); + } + + if ( ! $post ) { + return; + } + + $elements = $this->generate_postdata( $post ); + if ( false === $elements ) { + return; + } + + $id = $elements['id']; + $authordata = $elements['authordata']; + $currentday = $elements['currentday']; + $currentmonth = $elements['currentmonth']; + $page = $elements['page']; + $pages = $elements['pages']; + $multipage = $elements['multipage']; + $more = $elements['more']; + $numpages = $elements['numpages']; + + /** + * Fires once the post data has been set up. + * + * @since 2.8.0 + * @since 4.1.0 Introduced `$query` parameter. + * + * @param WP_Post $post The Post object (passed by reference). + * @param WP_Query $query The current Query object (passed by reference). + */ + do_action_ref_array( 'the_post', array( &$post, &$this ) ); + + return true; + } + + /** + * Generate post data. + * + * @since 5.2.0 + * + * @param WP_Post|object|int $post WP_Post instance or Post ID/object. + * @return array|false Elements of post or false on failure. + */ + public function generate_postdata( $post ) { + + if ( ! ( $post instanceof WP_Post ) ) { + $post = get_post( $post ); + } + + if ( ! $post ) { + return false; + } + + $id = (int) $post->ID; + + $authordata = get_userdata( $post->post_author ); + + $currentday = mysql2date( 'd.m.y', $post->post_date, false ); + $currentmonth = mysql2date( 'm', $post->post_date, false ); + $numpages = 1; + $multipage = 0; + $page = $this->get( 'page' ); + if ( ! $page ) { + $page = 1; + } + + /* + * Force full post content when viewing the permalink for the $post, + * or when on an RSS feed. Otherwise respect the 'more' tag. + */ + if ( get_queried_object_id() === $post->ID && ( $this->is_page() || $this->is_single() ) ) { + $more = 1; + } elseif ( $this->is_feed() ) { + $more = 1; + } else { + $more = 0; + } + + $content = $post->post_content; + if ( false !== strpos( $content, '' ) ) { + $content = str_replace( "\n\n", '', $content ); + $content = str_replace( "\n", '', $content ); + $content = str_replace( "\n", '', $content ); + + // Remove the nextpage block delimiters, to avoid invalid block structures in the split content. + $content = str_replace( '', '', $content ); + $content = str_replace( '', '', $content ); + + // Ignore nextpage at the beginning of the content. + if ( 0 === strpos( $content, '' ) ) { + $content = substr( $content, 15 ); + } + + $pages = explode( '', $content ); + } else { + $pages = array( $post->post_content ); + } + + /** + * Filters the "pages" derived from splitting the post content. + * + * "Pages" are determined by splitting the post content based on the presence + * of `` tags. + * + * @since 4.4.0 + * + * @param string[] $pages Array of "pages" from the post content split by `` tags. + * @param WP_Post $post Current post object. + */ + $pages = apply_filters( 'content_pagination', $pages, $post ); + + $numpages = count( $pages ); + + if ( $numpages > 1 ) { + if ( $page > 1 ) { + $more = 1; + } + $multipage = 1; + } else { + $multipage = 0; + } + + $elements = compact( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); + + return $elements; + } + /** + * After looping through a nested query, this function + * restores the $post global to the current post in this query. + * + * @since 3.7.0 + * + * @global WP_Post $post Global post object. + */ + public function reset_postdata() { + if ( ! empty( $this->post ) ) { + $GLOBALS['post'] = $this->post; + $this->setup_postdata( $this->post ); + } + } + + /** + * Lazyload term meta for posts in the loop. + * + * @since 4.4.0 + * @deprecated 4.5.0 See wp_queue_posts_for_term_meta_lazyload(). + * + * @param mixed $check + * @param int $term_id + * @return mixed + */ + public function lazyload_term_meta( $check, $term_id ) { + _deprecated_function( __METHOD__, '4.5.0' ); + return $check; + } + + /** + * Lazyload comment meta for comments in the loop. + * + * @since 4.4.0 + * @deprecated 4.5.0 See wp_queue_comments_for_comment_meta_lazyload(). + * + * @param mixed $check + * @param int $comment_id + * @return mixed + */ + public function lazyload_comment_meta( $check, $comment_id ) { + _deprecated_function( __METHOD__, '4.5.0' ); + return $check; + } +} diff --git a/projects/tests/tests/performance/defs.php b/projects/tests/tests/performance/defs.php new file mode 100644 index 00000000..df859fc6 --- /dev/null +++ b/projects/tests/tests/performance/defs.php @@ -0,0 +1,161 @@ + 'home', 'name' => 'Home', 'url' => '.' ); + $menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' ); + $menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup / Reset DB', 'url' => 'setup.php' ); + } else { + $menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup DVWA', 'url' => 'setup.php' ); + $menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' ); + } + + if (dvwaIsLoggedIn()) { + $menuBlocks[ 'vulnerabilities' ] = array(); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'brute', 'name' => 'Brute Force', 'url' => 'vulnerabilities/brute/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'exec', 'name' => 'Command Injection', 'url' => 'vulnerabilities/exec/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'csrf', 'name' => 'CSRF', 'url' => 'vulnerabilities/csrf/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'fi', 'name' => 'File Inclusion', 'url' => 'vulnerabilities/fi/.?page=include.php' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'upload', 'name' => 'File Upload', 'url' => 'vulnerabilities/upload/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'captcha', 'name' => 'Insecure CAPTCHA', 'url' => 'vulnerabilities/captcha/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli', 'name' => 'SQL Injection', 'url' => 'vulnerabilities/sqli/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli_blind', 'name' => 'SQL Injection (Blind)', 'url' => 'vulnerabilities/sqli_blind/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'weak_id', 'name' => 'Weak Session IDs', 'url' => 'vulnerabilities/weak_id/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_d', 'name' => 'XSS (DOM)', 'url' => 'vulnerabilities/xss_d/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_r', 'name' => 'XSS (Reflected)', 'url' => 'vulnerabilities/xss_r/' ); + $menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_s', 'name' => 'XSS (Stored)', 'url' => 'vulnerabilities/xss_s/' ); + } + + $menuBlocks[ 'meta' ] = array(); + if (dvwaIsLoggedIn()) { + $menuBlocks[ 'meta' ][] = array( 'id' => 'security', 'name' => 'DVWA Security', 'url' => 'security.php' ); + $menuBlocks[ 'meta' ][] = array( 'id' => 'phpinfo', 'name' => 'PHP Info', 'url' => 'phpinfo.php' ); + } + $menuBlocks[ 'meta' ][] = array( 'id' => 'about', 'name' => 'About', 'url' => 'about.php' ); + + if (dvwaIsLoggedIn()) { + $menuBlocks[ 'logout' ] = array(); + $menuBlocks[ 'logout' ][] = array( 'id' => 'logout', 'name' => 'Logout', 'url' => 'logout.php' ); + } + + $menuHtml = ''; + + foreach ($menuBlocks as $menuBlock) { + $menuBlockHtml = ''; + foreach ($menuBlock as $menuItem) { + $selectedClass = ($menuItem[ 'id' ] == $pPage[ 'page_id' ]) ? 'selected' : ''; + $fixedUrl = DVWA_WEB_PAGE_TO_ROOT.$menuItem[ 'url' ]; + $menuBlockHtml .= "
  • {$menuItem[ 'name' ]}
  • \n"; + } + $menuHtml .= "
      {$menuBlockHtml}
    "; + } + + // Get security cookie -- + $securityLevelHtml = ''; + switch (dvwaSecurityLevelGet()) { + case 'low': + $securityLevelHtml = 'low'; + break; + case 'medium': + $securityLevelHtml = 'medium'; + break; + case 'high': + $securityLevelHtml = 'high'; + break; + default: + $securityLevelHtml = 'impossible'; + break; + } + // -- END (security cookie) + + $phpIdsHtml = 'PHPIDS: ' . (dvwaPhpIdsIsEnabled() ? 'enabled' : 'disabled'); + $userInfoHtml = 'Username: ' . (dvwaCurrentUser()); + + $messagesHtml = messagesPopAllToHtml(); + if ($messagesHtml) { + $messagesHtml = "
    {$messagesHtml}
    "; + } + + $systemInfoHtml = ""; + if (dvwaIsLoggedIn()) { + $systemInfoHtml = "
    {$userInfoHtml}
    Security Level: {$securityLevelHtml}
    {$phpIdsHtml}
    "; + } + if ($pPage[ 'source_button' ]) { + $systemInfoHtml = dvwaButtonSourceHtmlGet($pPage[ 'source_button' ]) . " $systemInfoHtml"; + } + if ($pPage[ 'help_button' ]) { + $systemInfoHtml = dvwaButtonHelpHtmlGet($pPage[ 'help_button' ]) . " $systemInfoHtml"; + } + + // Send Headers + main HTML code + Header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 + Header('Content-Type: text/html;charset=utf-8'); // TODO- proper XHTML headers... + Header('Expires: Tue, 23 Jun 2009 12:00:00 GMT'); // Date in the past + + echo " + + + + + + + + {$pPage[ 'title' ]} + + + + + + + + + + +
    + +
    + + \"Damn + +
    + +
    + +
    + {$menuHtml} +
    + +
    + +
    + + {$pPage[ 'body' ]} +

    + {$messagesHtml} + +
    + +
    +
    + +
    + {$systemInfoHtml} +
    + +
    + +

    Damn Vulnerable Web Application (DVWA) v" . dvwaVersionGet() . "

    + +
    + +
    + + + +"; +} + diff --git a/projects/tests/tests/phpwander/test4.php b/projects/tests/tests/phpwander/test4.php index fa062d2c..ec3f5e3b 100644 --- a/projects/tests/tests/phpwander/test4.php +++ b/projects/tests/tests/phpwander/test4.php @@ -1,6 +1,9 @@ __isset( $key ) ) { + $post[ $key ] = $this->__get( $key ); + } + } + + return $post; + } diff --git a/projects/tests/tests/real/class-wp-filesystem-direct.php b/projects/tests/tests/real/class-wp-filesystem-direct.php new file mode 100644 index 00000000..b6aa4b53 --- /dev/null +++ b/projects/tests/tests/real/class-wp-filesystem-direct.php @@ -0,0 +1,44 @@ +is_file( $file ) ) { + $mode = FS_CHMOD_FILE; + } elseif ( $this->is_dir( $file ) ) { + $mode = FS_CHMOD_DIR; + } else { + return false; + } + } + + if ( ! $recursive || ! $this->is_dir( $file ) ) { + return chmod( $file, $mode ); + } + + $filelist = $this->dirlist( $file ); + + + return true; + } + + public function copy( $source, $destination, $overwrite = false, $mode = false ) { + $this->chmod( $destination, $mode ); + + return $rtval; + } + + public function dirlist($path) { + + $ret = array(); + + $struc = array(); + + $struc['files'] = $this->dirlist( "foo"); + + $ret[ $struc['name'] ] = $struc; + + return $ret; + } +} diff --git a/projects/tests/tests/real/composer/index.php b/projects/tests/tests/real/composer/index.php index 9b40c79d..8ca769ae 100644 --- a/projects/tests/tests/real/composer/index.php +++ b/projects/tests/tests/real/composer/index.php @@ -1,6 +1,6 @@ register(true); - +*/ if ($useStaticLoader) { $includeFiles = Composer\Autoload\ComposerStaticInit025959f4676b9e3c73718d111acaa853::$files; } else { diff --git a/projects/tests/tests/real/composer/vendor/composer/autoload_static.php b/projects/tests/tests/real/composer/notexcluded_vendor/composer/autoload_static.php similarity index 100% rename from projects/tests/tests/real/composer/vendor/composer/autoload_static.php rename to projects/tests/tests/real/composer/notexcluded_vendor/composer/autoload_static.php diff --git a/projects/tests/tests/real/composer/vendor/composer/installed.json b/projects/tests/tests/real/composer/notexcluded_vendor/composer/installed.json similarity index 100% rename from projects/tests/tests/real/composer/vendor/composer/installed.json rename to projects/tests/tests/real/composer/notexcluded_vendor/composer/installed.json diff --git a/projects/tests/tests/real/composer/vendor/progpilot/tests/test_package b/projects/tests/tests/real/composer/notexcluded_vendor/progpilot/tests/test_package similarity index 100% rename from projects/tests/tests/real/composer/vendor/progpilot/tests/test_package rename to projects/tests/tests/real/composer/notexcluded_vendor/progpilot/tests/test_package diff --git a/projects/tests/tests/real/executiontime/Item.php b/projects/tests/tests/real/executiontime/Item.php new file mode 100644 index 00000000..f82cb430 --- /dev/null +++ b/projects/tests/tests/real/executiontime/Item.php @@ -0,0 +1,2585 @@ +feed = $feed; + $this->data = $data; + } + + public function set_registry(SimplePie_Registry $registry) + { + $this->registry = $registry; + } + + public function __toString() + { + return md5(serialize($this->data)); + } + + public function __destruct() + { + if (!gc_enabled()) + { + unset($this->feed); + } + } + + public function get_item_tags($namespace, $tag) + { + if (isset($this->data['child'][$namespace][$tag])) + { + return $this->data['child'][$namespace][$tag]; + } + + return null; + } + + public function get_base($element = array()) + { + return $this->feed->get_base($element); + } + + public function sanitize($data, $type, $base = '') + { + return $this->feed->sanitize($data, $type, $base); + } + + public function get_feed() + { + return $this->feed; + } + + public function get_id($hash = false, $fn = 'md5') + { + if (!$hash) + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'id')) + { + return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'id')) + { + return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'guid')) + { + return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'identifier')) + { + return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'identifier')) + { + return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif (isset($this->data['attribs'][SIMPLEPIE_NAMESPACE_RDF]['about'])) + { + return $this->sanitize($this->data['attribs'][SIMPLEPIE_NAMESPACE_RDF]['about'], SIMPLEPIE_CONSTRUCT_TEXT); + } + } + if ($fn === false) + { + return null; + } + elseif (!is_callable($fn)) + { + trigger_error('User-supplied function $fn must be callable', E_USER_WARNING); + $fn = 'md5'; + } + return call_user_func($fn, + $this->get_permalink().$this->get_title().$this->get_content()); + } + + public function get_title() + { + if (!isset($this->data['title'])) + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'title')) + { + $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0])); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'title')) + { + $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0])); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title')) + { + $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title')) + { + $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title')) + { + $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title')) + { + $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title')) + { + $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $this->data['title'] = null; + } + } + return $this->data['title']; + } + + public function get_description($description_only = false) + { + if (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'summary')) && + ($return = $this->sanitize($tags[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($tags[0]['attribs'])), $this->get_base($tags[0])))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'summary')) && + ($return = $this->sanitize($tags[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($tags[0]['attribs'])), $this->get_base($tags[0])))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'description')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($tags[0])))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'description')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($tags[0])))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'description')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'description')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'summary')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($tags[0])))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'subtitle')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'description')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_HTML))) + { + return $return; + } + + elseif (!$description_only) + { + return $this->get_content(true); + } + + return null; + } + + public function get_content($content_only = false) + { + if (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'content')) && + ($return = $this->sanitize($tags[0]['data'], $this->registry->call('Misc', 'atom_10_content_construct_type', array($tags[0]['attribs'])), $this->get_base($tags[0])))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'content')) && + ($return = $this->sanitize($tags[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($tags[0]['attribs'])), $this->get_base($tags[0])))) + { + return $return; + } + elseif (($tags = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT, 'encoded')) && + ($return = $this->sanitize($tags[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($tags[0])))) + { + return $return; + } + elseif (!$content_only) + { + return $this->get_description(true); + } + + return null; + } + + public function get_thumbnail() + { + if (!isset($this->data['thumbnail'])) + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'thumbnail')) + { + $this->data['thumbnail'] = $return[0]['attribs']['']; + } + else + { + $this->data['thumbnail'] = null; + } + } + return $this->data['thumbnail']; + } + + public function get_category($key = 0) + { + $categories = $this->get_categories(); + if (isset($categories[$key])) + { + return $categories[$key]; + } + + return null; + } + + public function get_categories() + { + $categories = array(); + + $type = 'category'; + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, $type) as $category) + { + $term = null; + $scheme = null; + $label = null; + if (isset($category['attribs']['']['term'])) + { + $term = $this->sanitize($category['attribs']['']['term'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($category['attribs']['']['scheme'])) + { + $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($category['attribs']['']['label'])) + { + $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories[] = $this->registry->create('Category', array($term, $scheme, $label, $type)); + } + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, $type) as $category) + { + // This is really the label, but keep this as the term also for BC. + // Label will also work on retrieving because that falls back to term. + $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT); + if (isset($category['attribs']['']['domain'])) + { + $scheme = $this->sanitize($category['attribs']['']['domain'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $scheme = null; + } + $categories[] = $this->registry->create('Category', array($term, $scheme, null, $type)); + } + + $type = 'subject'; + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, $type) as $category) + { + $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null, $type)); + } + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, $type) as $category) + { + $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null, $type)); + } + + if (!empty($categories)) + { + return array_unique($categories); + } + + return null; + } + + public function get_author($key = 0) + { + $authors = $this->get_authors(); + if (isset($authors[$key])) + { + return $authors[$key]; + } + + return null; + } + + public function get_contributor($key = 0) + { + $contributors = $this->get_contributors(); + if (isset($contributors[$key])) + { + return $contributors[$key]; + } + + return null; + } + + public function get_contributors() + { + $contributors = array(); + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'contributor') as $contributor) + { + $name = null; + $uri = null; + $email = null; + if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])) + { + $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])) + { + $uri = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0])); + } + if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'])) + { + $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if ($name !== null || $email !== null || $uri !== null) + { + $contributors[] = $this->registry->create('Author', array($name, $uri, $email)); + } + } + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'contributor') as $contributor) + { + $name = null; + $url = null; + $email = null; + if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'])) + { + $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'])) + { + $url = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0])); + } + if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'])) + { + $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if ($name !== null || $email !== null || $url !== null) + { + $contributors[] = $this->registry->create('Author', array($name, $url, $email)); + } + } + + if (!empty($contributors)) + { + return array_unique($contributors); + } + + return null; + } + + public function get_authors() + { + $authors = array(); + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author') as $author) + { + $name = null; + $uri = null; + $email = null; + if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])) + { + $name = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])) + { + $uri = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0])); + } + if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'])) + { + $email = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if ($name !== null || $email !== null || $uri !== null) + { + $authors[] = $this->registry->create('Author', array($name, $uri, $email)); + } + } + if ($author = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'author')) + { + $name = null; + $url = null; + $email = null; + if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'])) + { + $name = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'])) + { + $url = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0])); + } + if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'])) + { + $email = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if ($name !== null || $email !== null || $url !== null) + { + $authors[] = $this->registry->create('Author', array($name, $url, $email)); + } + } + if ($author = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'author')) + { + $authors[] = $this->registry->create('Author', array(null, null, $this->sanitize($author[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT))); + } + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'creator') as $author) + { + $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null)); + } + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'creator') as $author) + { + $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null)); + } + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'author') as $author) + { + $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null)); + } + + if (!empty($authors)) + { + return array_unique($authors); + } + elseif (($source = $this->get_source()) && ($authors = $source->get_authors())) + { + return $authors; + } + elseif ($authors = $this->feed->get_authors()) + { + return $authors; + } + + return null; + } + + public function get_copyright() + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'rights')) + { + return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0])); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'rights')) + { + return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'rights')) + { + return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + + return null; + } + + public function get_date($date_format = 'j F Y, g:i a') + { + if (!isset($this->data['date'])) + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'published')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'pubDate')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'date')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'date')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'issued')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'created')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'modified')) + { + $this->data['date']['raw'] = $return[0]['data']; + } + + if (!empty($this->data['date']['raw'])) + { + $parser = $this->registry->call('Parse_Date', 'get'); + $this->data['date']['parsed'] = $parser->parse($this->data['date']['raw']); + } + else + { + $this->data['date'] = null; + } + } + if ($this->data['date']) + { + $date_format = (string) $date_format; + switch ($date_format) + { + case '': + return $this->sanitize($this->data['date']['raw'], SIMPLEPIE_CONSTRUCT_TEXT); + + case 'U': + return $this->data['date']['parsed']; + + default: + return date($date_format, $this->data['date']['parsed']); + } + } + + return null; + } + + public function get_updated_date($date_format = 'j F Y, g:i a') + { + if (!isset($this->data['updated'])) + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated')) + { + $this->data['updated']['raw'] = $return[0]['data']; + } + + if (!empty($this->data['updated']['raw'])) + { + $parser = $this->registry->call('Parse_Date', 'get'); + $this->data['updated']['parsed'] = $parser->parse($this->data['updated']['raw']); + } + else + { + $this->data['updated'] = null; + } + } + if ($this->data['updated']) + { + $date_format = (string) $date_format; + switch ($date_format) + { + case '': + return $this->sanitize($this->data['updated']['raw'], SIMPLEPIE_CONSTRUCT_TEXT); + + case 'U': + return $this->data['updated']['parsed']; + + default: + return date($date_format, $this->data['updated']['parsed']); + } + } + + return null; + } + + public function get_local_date($date_format = '%c') + { + if (!$date_format) + { + return $this->sanitize($this->get_date(''), SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif (($date = $this->get_date('U')) !== null && $date !== false) + { + return strftime($date_format, $date); + } + + return null; + } + + public function get_gmdate($date_format = 'j F Y, g:i a') + { + $date = $this->get_date('U'); + if ($date === null) + { + return null; + } + + return gmdate($date_format, $date); + } + + public function get_updated_gmdate($date_format = 'j F Y, g:i a') + { + $date = $this->get_updated_date('U'); + if ($date === null) + { + return null; + } + + return gmdate($date_format, $date); + } + + public function get_permalink() + { + $link = $this->get_link(); + $enclosure = $this->get_enclosure(0); + if ($link !== null) + { + return $link; + } + elseif ($enclosure !== null) + { + return $enclosure->get_link(); + } + + return null; + } + + public function get_link($key = 0, $rel = 'alternate') + { + $links = $this->get_links($rel); + if ($links && $links[$key] !== null) + { + return $links[$key]; + } + + return null; + } + + public function get_links($rel = 'alternate') + { + if (!isset($this->data['links'])) + { + $this->data['links'] = array(); + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link') as $link) + { + if (isset($link['attribs']['']['href'])) + { + $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; + $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link)); + + } + } + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link') as $link) + { + if (isset($link['attribs']['']['href'])) + { + $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; + $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link)); + } + } + if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link')) + { + $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0])); + } + if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link')) + { + $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0])); + } + if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link')) + { + $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0])); + } + if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'guid')) + { + if (!isset($links[0]['attribs']['']['isPermaLink']) || strtolower(trim($links[0]['attribs']['']['isPermaLink'])) === 'true') + { + $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0])); + } + } + + $keys = array_keys($this->data['links']); + foreach ($keys as $key) + { + if ($this->registry->call('Misc', 'is_isegment_nz_nc', array($key))) + { + if (isset($this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key])) + { + $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]); + $this->data['links'][$key] =& $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]; + } + else + { + $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] =& $this->data['links'][$key]; + } + } + elseif (substr($key, 0, 41) === SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY) + { + $this->data['links'][substr($key, 41)] =& $this->data['links'][$key]; + } + $this->data['links'][$key] = array_unique($this->data['links'][$key]); + } + } + if (isset($this->data['links'][$rel])) + { + return $this->data['links'][$rel]; + } + + return null; + } + + public function get_enclosure($key = 0, $prefer = null) + { + $enclosures = $this->get_enclosures(); + if (isset($enclosures[$key])) + { + return $enclosures[$key]; + } + + return null; + }*/ + + public function get_enclosures() + { + if (!isset($this->data['enclosures'])) + { + $this->data['enclosures'] = array(); + + // Elements + $captions_parent = null; + $categories_parent = null; + $copyrights_parent = null; + $credits_parent = null; + $description_parent = null; + $duration_parent = null; + $hashes_parent = null; + $keywords_parent = null; + $player_parent = null; + $ratings_parent = null; + $restrictions_parent = null; + $thumbnails_parent = null; + $title_parent = null; + + // Let's do the channel and item-level ones first, and just re-use them if we need to. + $parent = $this->get_feed(); + + // CAPTIONS + if ($captions = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'text')) + { + foreach ($captions as $caption) + { + $caption_type = null; + $caption_lang = null; + $caption_startTime = null; + $caption_endTime = null; + $caption_text = null; + if (isset($caption['attribs']['']['type'])) + { + $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['lang'])) + { + $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['start'])) + { + $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['end'])) + { + $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['data'])) + { + $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $captions_parent[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text)); + } + } + elseif ($captions = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'text')) + { + foreach ($captions as $caption) + { + $caption_type = null; + $caption_lang = null; + $caption_startTime = null; + $caption_endTime = null; + $caption_text = null; + if (isset($caption['attribs']['']['type'])) + { + $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['lang'])) + { + $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['start'])) + { + $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['end'])) + { + $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['data'])) + { + $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $captions_parent[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text)); + } + } + if (is_array($captions_parent)) + { + $captions_parent = array_values(array_unique($captions_parent)); + } + + // CATEGORIES + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'category') as $category) + { + $term = null; + $scheme = null; + $label = null; + if (isset($category['data'])) + { + $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($category['attribs']['']['scheme'])) + { + $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $scheme = 'http://search.yahoo.com/mrss/category_schema'; + } + if (isset($category['attribs']['']['label'])) + { + $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label)); + } + foreach ((array) $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'category') as $category) + { + $term = null; + $scheme = null; + $label = null; + if (isset($category['data'])) + { + $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($category['attribs']['']['scheme'])) + { + $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $scheme = 'http://search.yahoo.com/mrss/category_schema'; + } + if (isset($category['attribs']['']['label'])) + { + $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label)); + } + foreach ((array) $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'category') as $category) + { + $term = null; + $scheme = 'http://www.itunes.com/dtds/podcast-1.0.dtd'; + $label = null; + if (isset($category['attribs']['']['text'])) + { + $label = $this->sanitize($category['attribs']['']['text'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label)); + + if (isset($category['child'][SIMPLEPIE_NAMESPACE_ITUNES]['category'])) + { + foreach ((array) $category['child'][SIMPLEPIE_NAMESPACE_ITUNES]['category'] as $subcategory) + { + if (isset($subcategory['attribs']['']['text'])) + { + $label = $this->sanitize($subcategory['attribs']['']['text'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label)); + } + } + } + if (is_array($categories_parent)) + { + $categories_parent = array_values(array_unique($categories_parent)); + } + + // COPYRIGHT + if ($copyright = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'copyright')) + { + $copyright_url = null; + $copyright_label = null; + if (isset($copyright[0]['attribs']['']['url'])) + { + $copyright_url = $this->sanitize($copyright[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($copyright[0]['data'])) + { + $copyright_label = $this->sanitize($copyright[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $copyrights_parent = $this->registry->create('Copyright', array($copyright_url, $copyright_label)); + } + elseif ($copyright = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'copyright')) + { + $copyright_url = null; + $copyright_label = null; + if (isset($copyright[0]['attribs']['']['url'])) + { + $copyright_url = $this->sanitize($copyright[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($copyright[0]['data'])) + { + $copyright_label = $this->sanitize($copyright[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $copyrights_parent = $this->registry->create('Copyright', array($copyright_url, $copyright_label)); + } + + // CREDITS + if ($credits = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'credit')) + { + foreach ($credits as $credit) + { + $credit_role = null; + $credit_scheme = null; + $credit_name = null; + if (isset($credit['attribs']['']['role'])) + { + $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($credit['attribs']['']['scheme'])) + { + $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $credit_scheme = 'urn:ebu'; + } + if (isset($credit['data'])) + { + $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $credits_parent[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name)); + } + } + elseif ($credits = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'credit')) + { + foreach ($credits as $credit) + { + $credit_role = null; + $credit_scheme = null; + $credit_name = null; + if (isset($credit['attribs']['']['role'])) + { + $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($credit['attribs']['']['scheme'])) + { + $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $credit_scheme = 'urn:ebu'; + } + if (isset($credit['data'])) + { + $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $credits_parent[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name)); + } + } + if (is_array($credits_parent)) + { + $credits_parent = array_values(array_unique($credits_parent)); + } + + // DESCRIPTION + if ($description_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'description')) + { + if (isset($description_parent[0]['data'])) + { + $description_parent = $this->sanitize($description_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + } + elseif ($description_parent = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'description')) + { + if (isset($description_parent[0]['data'])) + { + $description_parent = $this->sanitize($description_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + } + + // DURATION + if ($duration_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'duration')) + { + $seconds = null; + $minutes = null; + $hours = null; + if (isset($duration_parent[0]['data'])) + { + $temp = explode(':', $this->sanitize($duration_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + if (sizeof($temp) > 0) + { + $seconds = (int) array_pop($temp); + } + if (sizeof($temp) > 0) + { + $minutes = (int) array_pop($temp); + $seconds += $minutes * 60; + } + if (sizeof($temp) > 0) + { + $hours = (int) array_pop($temp); + $seconds += $hours * 3600; + } + unset($temp); + $duration_parent = $seconds; + } + } + + // HASHES + if ($hashes_iterator = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'hash')) + { + foreach ($hashes_iterator as $hash) + { + $value = null; + $algo = null; + if (isset($hash['data'])) + { + $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($hash['attribs']['']['algo'])) + { + $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $algo = 'md5'; + } + $hashes_parent[] = $algo.':'.$value; + } + } + elseif ($hashes_iterator = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'hash')) + { + foreach ($hashes_iterator as $hash) + { + $value = null; + $algo = null; + if (isset($hash['data'])) + { + $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($hash['attribs']['']['algo'])) + { + $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $algo = 'md5'; + } + $hashes_parent[] = $algo.':'.$value; + } + } + if (is_array($hashes_parent)) + { + $hashes_parent = array_values(array_unique($hashes_parent)); + } + + // KEYWORDS + if ($keywords = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'keywords')) + { + if (isset($keywords[0]['data'])) + { + $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + foreach ($temp as $word) + { + $keywords_parent[] = trim($word); + } + } + unset($temp); + } + elseif ($keywords = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'keywords')) + { + if (isset($keywords[0]['data'])) + { + $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + foreach ($temp as $word) + { + $keywords_parent[] = trim($word); + } + } + unset($temp); + } + elseif ($keywords = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'keywords')) + { + if (isset($keywords[0]['data'])) + { + $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + foreach ($temp as $word) + { + $keywords_parent[] = trim($word); + } + } + unset($temp); + } + elseif ($keywords = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'keywords')) + { + if (isset($keywords[0]['data'])) + { + $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + foreach ($temp as $word) + { + $keywords_parent[] = trim($word); + } + } + unset($temp); + } + if (is_array($keywords_parent)) + { + $keywords_parent = array_values(array_unique($keywords_parent)); + } + + // PLAYER + if ($player_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'player')) + { + if (isset($player_parent[0]['attribs']['']['url'])) + { + $player_parent = $this->sanitize($player_parent[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + } + elseif ($player_parent = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'player')) + { + if (isset($player_parent[0]['attribs']['']['url'])) + { + $player_parent = $this->sanitize($player_parent[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + } + + // RATINGS + if ($ratings = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'rating')) + { + foreach ($ratings as $rating) + { + $rating_scheme = null; + $rating_value = null; + if (isset($rating['attribs']['']['scheme'])) + { + $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $rating_scheme = 'urn:simple'; + } + if (isset($rating['data'])) + { + $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value)); + } + } + elseif ($ratings = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'explicit')) + { + foreach ($ratings as $rating) + { + $rating_scheme = 'urn:itunes'; + $rating_value = null; + if (isset($rating['data'])) + { + $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value)); + } + } + elseif ($ratings = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'rating')) + { + foreach ($ratings as $rating) + { + $rating_scheme = null; + $rating_value = null; + if (isset($rating['attribs']['']['scheme'])) + { + $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $rating_scheme = 'urn:simple'; + } + if (isset($rating['data'])) + { + $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value)); + } + } + elseif ($ratings = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'explicit')) + { + foreach ($ratings as $rating) + { + $rating_scheme = 'urn:itunes'; + $rating_value = null; + if (isset($rating['data'])) + { + $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value)); + } + } + if (is_array($ratings_parent)) + { + $ratings_parent = array_values(array_unique($ratings_parent)); + } + + // RESTRICTIONS + if ($restrictions = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'restriction')) + { + foreach ($restrictions as $restriction) + { + $restriction_relationship = null; + $restriction_type = null; + $restriction_value = null; + if (isset($restriction['attribs']['']['relationship'])) + { + $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['attribs']['']['type'])) + { + $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['data'])) + { + $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value)); + } + } + elseif ($restrictions = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'block')) + { + foreach ($restrictions as $restriction) + { + $restriction_relationship = 'allow'; + $restriction_type = null; + $restriction_value = 'itunes'; + if (isset($restriction['data']) && strtolower($restriction['data']) === 'yes') + { + $restriction_relationship = 'deny'; + } + $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value)); + } + } + elseif ($restrictions = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'restriction')) + { + foreach ($restrictions as $restriction) + { + $restriction_relationship = null; + $restriction_type = null; + $restriction_value = null; + if (isset($restriction['attribs']['']['relationship'])) + { + $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['attribs']['']['type'])) + { + $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['data'])) + { + $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value)); + } + } + elseif ($restrictions = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'block')) + { + foreach ($restrictions as $restriction) + { + $restriction_relationship = 'allow'; + $restriction_type = null; + $restriction_value = 'itunes'; + if (isset($restriction['data']) && strtolower($restriction['data']) === 'yes') + { + $restriction_relationship = 'deny'; + } + $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value)); + } + } + if (is_array($restrictions_parent)) + { + $restrictions_parent = array_values(array_unique($restrictions_parent)); + } + else + { + $restrictions_parent = array(new SimplePie_Restriction('allow', null, 'default')); + } + + // THUMBNAILS + if ($thumbnails = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'thumbnail')) + { + foreach ($thumbnails as $thumbnail) + { + if (isset($thumbnail['attribs']['']['url'])) + { + $thumbnails_parent[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + } + } + elseif ($thumbnails = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'thumbnail')) + { + foreach ($thumbnails as $thumbnail) + { + if (isset($thumbnail['attribs']['']['url'])) + { + $thumbnails_parent[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + } + } + + // TITLES + if ($title_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'title')) + { + if (isset($title_parent[0]['data'])) + { + $title_parent = $this->sanitize($title_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + } + elseif ($title_parent = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'title')) + { + if (isset($title_parent[0]['data'])) + { + $title_parent = $this->sanitize($title_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + } + + // Clear the memory + unset($parent); + + // Attributes + $bitrate = null; + $channels = null; + $duration = null; + $expression = null; + $framerate = null; + $height = null; + $javascript = null; + $lang = null; + $length = null; + $medium = null; + $samplingrate = null; + $type = null; + $url = null; + $width = null; + + // Elements + $captions = null; + $categories = null; + $copyrights = null; + $credits = null; + $description = null; + $hashes = null; + $keywords = null; + $player = null; + $ratings = null; + $restrictions = null; + $thumbnails = null; + $title = null; + + // If we have media:group tags, loop through them. + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'group') as $group) + { + if(isset($group['child']) && isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'])) + { + // If we have media:content tags, loop through them. + foreach ((array) $group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'] as $content) + { + if (isset($content['attribs']['']['url'])) + { + // Attributes + $bitrate = null; + $channels = null; + $duration = null; + $expression = null; + $framerate = null; + $height = null; + $javascript = null; + $lang = null; + $length = null; + $medium = null; + $samplingrate = null; + $type = null; + $url = null; + $width = null; + + // Elements + $captions = null; + $categories = null; + $copyrights = null; + $credits = null; + $description = null; + $hashes = null; + $keywords = null; + $player = null; + $ratings = null; + $restrictions = null; + $thumbnails = null; + $title = null; + + // Start checking the attributes of media:content + if (isset($content['attribs']['']['bitrate'])) + { + $bitrate = $this->sanitize($content['attribs']['']['bitrate'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['channels'])) + { + $channels = $this->sanitize($content['attribs']['']['channels'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['duration'])) + { + $duration = $this->sanitize($content['attribs']['']['duration'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $duration = $duration_parent; + } + if (isset($content['attribs']['']['expression'])) + { + $expression = $this->sanitize($content['attribs']['']['expression'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['framerate'])) + { + $framerate = $this->sanitize($content['attribs']['']['framerate'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['height'])) + { + $height = $this->sanitize($content['attribs']['']['height'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['lang'])) + { + $lang = $this->sanitize($content['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['fileSize'])) + { + $length = ceil($content['attribs']['']['fileSize']); + } + if (isset($content['attribs']['']['medium'])) + { + $medium = $this->sanitize($content['attribs']['']['medium'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['samplingrate'])) + { + $samplingrate = $this->sanitize($content['attribs']['']['samplingrate'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['type'])) + { + $type = $this->sanitize($content['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['width'])) + { + $width = $this->sanitize($content['attribs']['']['width'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $url = $this->sanitize($content['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + + // Checking the other optional media: elements. Priority: media:content, media:group, item, channel + + // CAPTIONS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'] as $caption) + { + $caption_type = null; + $caption_lang = null; + $caption_startTime = null; + $caption_endTime = null; + $caption_text = null; + if (isset($caption['attribs']['']['type'])) + { + $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['lang'])) + { + $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['start'])) + { + $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['end'])) + { + $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['data'])) + { + $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $captions[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text)); + } + if (is_array($captions)) + { + $captions = array_values(array_unique($captions)); + } + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'])) + { + foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'] as $caption) + { + $caption_type = null; + $caption_lang = null; + $caption_startTime = null; + $caption_endTime = null; + $caption_text = null; + if (isset($caption['attribs']['']['type'])) + { + $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['lang'])) + { + $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['start'])) + { + $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['end'])) + { + $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['data'])) + { + $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $captions[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text)); + } + if (is_array($captions)) + { + $captions = array_values(array_unique($captions)); + } + } + else + { + $captions = $captions_parent; + } + + // CATEGORIES + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'])) + { + foreach ((array) $content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'] as $category) + { + $term = null; + $scheme = null; + $label = null; + if (isset($category['data'])) + { + $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($category['attribs']['']['scheme'])) + { + $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $scheme = 'http://search.yahoo.com/mrss/category_schema'; + } + if (isset($category['attribs']['']['label'])) + { + $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories[] = $this->registry->create('Category', array($term, $scheme, $label)); + } + } + if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'])) + { + foreach ((array) $group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'] as $category) + { + $term = null; + $scheme = null; + $label = null; + if (isset($category['data'])) + { + $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($category['attribs']['']['scheme'])) + { + $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $scheme = 'http://search.yahoo.com/mrss/category_schema'; + } + if (isset($category['attribs']['']['label'])) + { + $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories[] = $this->registry->create('Category', array($term, $scheme, $label)); + } + } + if (is_array($categories) && is_array($categories_parent)) + { + $categories = array_values(array_unique(array_merge($categories, $categories_parent))); + } + elseif (is_array($categories)) + { + $categories = array_values(array_unique($categories)); + } + elseif (is_array($categories_parent)) + { + $categories = array_values(array_unique($categories_parent)); + } + + // COPYRIGHTS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'])) + { + $copyright_url = null; + $copyright_label = null; + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'])) + { + $copyright_url = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'])) + { + $copyright_label = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $copyrights = $this->registry->create('Copyright', array($copyright_url, $copyright_label)); + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'])) + { + $copyright_url = null; + $copyright_label = null; + if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'])) + { + $copyright_url = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'])) + { + $copyright_label = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $copyrights = $this->registry->create('Copyright', array($copyright_url, $copyright_label)); + } + else + { + $copyrights = $copyrights_parent; + } + + // CREDITS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'] as $credit) + { + $credit_role = null; + $credit_scheme = null; + $credit_name = null; + if (isset($credit['attribs']['']['role'])) + { + $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($credit['attribs']['']['scheme'])) + { + $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $credit_scheme = 'urn:ebu'; + } + if (isset($credit['data'])) + { + $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $credits[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name)); + } + if (is_array($credits)) + { + $credits = array_values(array_unique($credits)); + } + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'])) + { + foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'] as $credit) + { + $credit_role = null; + $credit_scheme = null; + $credit_name = null; + if (isset($credit['attribs']['']['role'])) + { + $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($credit['attribs']['']['scheme'])) + { + $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $credit_scheme = 'urn:ebu'; + } + if (isset($credit['data'])) + { + $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $credits[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name)); + } + if (is_array($credits)) + { + $credits = array_values(array_unique($credits)); + } + } + else + { + $credits = $credits_parent; + } + + // DESCRIPTION + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'])) + { + $description = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'])) + { + $description = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $description = $description_parent; + } + + // HASHES + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'] as $hash) + { + $value = null; + $algo = null; + if (isset($hash['data'])) + { + $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($hash['attribs']['']['algo'])) + { + $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $algo = 'md5'; + } + $hashes[] = $algo.':'.$value; + } + if (is_array($hashes)) + { + $hashes = array_values(array_unique($hashes)); + } + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'])) + { + foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'] as $hash) + { + $value = null; + $algo = null; + if (isset($hash['data'])) + { + $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($hash['attribs']['']['algo'])) + { + $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $algo = 'md5'; + } + $hashes[] = $algo.':'.$value; + } + if (is_array($hashes)) + { + $hashes = array_values(array_unique($hashes)); + } + } + else + { + $hashes = $hashes_parent; + } + + // KEYWORDS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'])) + { + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'])) + { + $temp = explode(',', $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + foreach ($temp as $word) + { + $keywords[] = trim($word); + } + unset($temp); + } + if (is_array($keywords)) + { + $keywords = array_values(array_unique($keywords)); + } + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'])) + { + if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'])) + { + $temp = explode(',', $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + foreach ($temp as $word) + { + $keywords[] = trim($word); + } + unset($temp); + } + if (is_array($keywords)) + { + $keywords = array_values(array_unique($keywords)); + } + } + else + { + $keywords = $keywords_parent; + } + + // PLAYER + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'])) + { + $player = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'])) + { + $player = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + else + { + $player = $player_parent; + } + + // RATINGS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'] as $rating) + { + $rating_scheme = null; + $rating_value = null; + if (isset($rating['attribs']['']['scheme'])) + { + $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $rating_scheme = 'urn:simple'; + } + if (isset($rating['data'])) + { + $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $ratings[] = $this->registry->create('Rating', array($rating_scheme, $rating_value)); + } + if (is_array($ratings)) + { + $ratings = array_values(array_unique($ratings)); + } + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'])) + { + foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'] as $rating) + { + $rating_scheme = null; + $rating_value = null; + if (isset($rating['attribs']['']['scheme'])) + { + $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $rating_scheme = 'urn:simple'; + } + if (isset($rating['data'])) + { + $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $ratings[] = $this->registry->create('Rating', array($rating_scheme, $rating_value)); + } + if (is_array($ratings)) + { + $ratings = array_values(array_unique($ratings)); + } + } + else + { + $ratings = $ratings_parent; + } + + // RESTRICTIONS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'] as $restriction) + { + $restriction_relationship = null; + $restriction_type = null; + $restriction_value = null; + if (isset($restriction['attribs']['']['relationship'])) + { + $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['attribs']['']['type'])) + { + $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['data'])) + { + $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $restrictions[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value)); + } + if (is_array($restrictions)) + { + $restrictions = array_values(array_unique($restrictions)); + } + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'])) + { + foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'] as $restriction) + { + $restriction_relationship = null; + $restriction_type = null; + $restriction_value = null; + if (isset($restriction['attribs']['']['relationship'])) + { + $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['attribs']['']['type'])) + { + $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['data'])) + { + $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $restrictions[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value)); + } + if (is_array($restrictions)) + { + $restrictions = array_values(array_unique($restrictions)); + } + } + else + { + $restrictions = $restrictions_parent; + } + + // THUMBNAILS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail) + { + $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + if (is_array($thumbnails)) + { + $thumbnails = array_values(array_unique($thumbnails)); + } + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'])) + { + foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail) + { + $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + if (is_array($thumbnails)) + { + $thumbnails = array_values(array_unique($thumbnails)); + } + } + else + { + $thumbnails = $thumbnails_parent; + } + + // TITLES + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'])) + { + $title = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'])) + { + $title = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $title = $title_parent; + } + + $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions, $categories, $channels, $copyrights, $credits, $description, $duration, $expression, $framerate, $hashes, $height, $keywords, $lang, $medium, $player, $ratings, $restrictions, $samplingrate, $thumbnails, $title, $width)); + } + } + } + } + + // If we have standalone media:content tags, loop through them. + if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'])) + { + foreach ((array) $this->data['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'] as $content) + { + if (isset($content['attribs']['']['url']) || isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'])) + { + // Attributes + $bitrate = null; + $channels = null; + $duration = null; + $expression = null; + $framerate = null; + $height = null; + $javascript = null; + $lang = null; + $length = null; + $medium = null; + $samplingrate = null; + $type = null; + $url = null; + $width = null; + + // Elements + $captions = null; + $categories = null; + $copyrights = null; + $credits = null; + $description = null; + $hashes = null; + $keywords = null; + $player = null; + $ratings = null; + $restrictions = null; + $thumbnails = null; + $title = null; + + // Start checking the attributes of media:content + if (isset($content['attribs']['']['bitrate'])) + { + $bitrate = $this->sanitize($content['attribs']['']['bitrate'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['channels'])) + { + $channels = $this->sanitize($content['attribs']['']['channels'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['duration'])) + { + $duration = $this->sanitize($content['attribs']['']['duration'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $duration = $duration_parent; + } + if (isset($content['attribs']['']['expression'])) + { + $expression = $this->sanitize($content['attribs']['']['expression'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['framerate'])) + { + $framerate = $this->sanitize($content['attribs']['']['framerate'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['height'])) + { + $height = $this->sanitize($content['attribs']['']['height'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['lang'])) + { + $lang = $this->sanitize($content['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['fileSize'])) + { + $length = ceil($content['attribs']['']['fileSize']); + } + if (isset($content['attribs']['']['medium'])) + { + $medium = $this->sanitize($content['attribs']['']['medium'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['samplingrate'])) + { + $samplingrate = $this->sanitize($content['attribs']['']['samplingrate'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['type'])) + { + $type = $this->sanitize($content['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['width'])) + { + $width = $this->sanitize($content['attribs']['']['width'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['attribs']['']['url'])) + { + $url = $this->sanitize($content['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + // Checking the other optional media: elements. Priority: media:content, media:group, item, channel + + // CAPTIONS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'] as $caption) + { + $caption_type = null; + $caption_lang = null; + $caption_startTime = null; + $caption_endTime = null; + $caption_text = null; + if (isset($caption['attribs']['']['type'])) + { + $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['lang'])) + { + $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['start'])) + { + $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['attribs']['']['end'])) + { + $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($caption['data'])) + { + $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $captions[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text)); + } + if (is_array($captions)) + { + $captions = array_values(array_unique($captions)); + } + } + else + { + $captions = $captions_parent; + } + + // CATEGORIES + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'])) + { + foreach ((array) $content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'] as $category) + { + $term = null; + $scheme = null; + $label = null; + if (isset($category['data'])) + { + $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($category['attribs']['']['scheme'])) + { + $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $scheme = 'http://search.yahoo.com/mrss/category_schema'; + } + if (isset($category['attribs']['']['label'])) + { + $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $categories[] = $this->registry->create('Category', array($term, $scheme, $label)); + } + } + if (is_array($categories) && is_array($categories_parent)) + { + $categories = array_values(array_unique(array_merge($categories, $categories_parent))); + } + elseif (is_array($categories)) + { + $categories = array_values(array_unique($categories)); + } + elseif (is_array($categories_parent)) + { + $categories = array_values(array_unique($categories_parent)); + } + else + { + $categories = null; + } + + // COPYRIGHTS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'])) + { + $copyright_url = null; + $copyright_label = null; + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'])) + { + $copyright_url = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'])) + { + $copyright_label = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $copyrights = $this->registry->create('Copyright', array($copyright_url, $copyright_label)); + } + else + { + $copyrights = $copyrights_parent; + } + + // CREDITS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'] as $credit) + { + $credit_role = null; + $credit_scheme = null; + $credit_name = null; + if (isset($credit['attribs']['']['role'])) + { + $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($credit['attribs']['']['scheme'])) + { + $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $credit_scheme = 'urn:ebu'; + } + if (isset($credit['data'])) + { + $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $credits[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name)); + } + if (is_array($credits)) + { + $credits = array_values(array_unique($credits)); + } + } + else + { + $credits = $credits_parent; + } + + // DESCRIPTION + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'])) + { + $description = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $description = $description_parent; + } + + // HASHES + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'] as $hash) + { + $value = null; + $algo = null; + if (isset($hash['data'])) + { + $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($hash['attribs']['']['algo'])) + { + $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $algo = 'md5'; + } + $hashes[] = $algo.':'.$value; + } + if (is_array($hashes)) + { + $hashes = array_values(array_unique($hashes)); + } + } + else + { + $hashes = $hashes_parent; + } + + // KEYWORDS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'])) + { + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'])) + { + $temp = explode(',', $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)); + foreach ($temp as $word) + { + $keywords[] = trim($word); + } + unset($temp); + } + if (is_array($keywords)) + { + $keywords = array_values(array_unique($keywords)); + } + } + else + { + $keywords = $keywords_parent; + } + + // PLAYER + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'])) + { + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'])) { + $player = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + } + else + { + $player = $player_parent; + } + + // RATINGS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'] as $rating) + { + $rating_scheme = null; + $rating_value = null; + if (isset($rating['attribs']['']['scheme'])) + { + $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $rating_scheme = 'urn:simple'; + } + if (isset($rating['data'])) + { + $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $ratings[] = $this->registry->create('Rating', array($rating_scheme, $rating_value)); + } + if (is_array($ratings)) + { + $ratings = array_values(array_unique($ratings)); + } + } + else + { + $ratings = $ratings_parent; + } + + // RESTRICTIONS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'] as $restriction) + { + $restriction_relationship = null; + $restriction_type = null; + $restriction_value = null; + if (isset($restriction['attribs']['']['relationship'])) + { + $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['attribs']['']['type'])) + { + $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($restriction['data'])) + { + $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + $restrictions[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value)); + } + if (is_array($restrictions)) + { + $restrictions = array_values(array_unique($restrictions)); + } + } + else + { + $restrictions = $restrictions_parent; + } + + // THUMBNAILS + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'])) + { + foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail) + { + if (isset($thumbnail['attribs']['']['url'])) { + $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI); + } + } + if (is_array($thumbnails)) + { + $thumbnails = array_values(array_unique($thumbnails)); + } + } + else + { + $thumbnails = $thumbnails_parent; + } + + // TITLES + if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'])) + { + $title = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $title = $title_parent; + } + + $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions, $categories, $channels, $copyrights, $credits, $description, $duration, $expression, $framerate, $hashes, $height, $keywords, $lang, $medium, $player, $ratings, $restrictions, $samplingrate, $thumbnails, $title, $width)); + } + } + } + + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link') as $link) + { + if (isset($link['attribs']['']['href']) && !empty($link['attribs']['']['rel']) && $link['attribs']['']['rel'] === 'enclosure') + { + // Attributes + $bitrate = null; + $channels = null; + $duration = null; + $expression = null; + $framerate = null; + $height = null; + $javascript = null; + $lang = null; + $length = null; + $medium = null; + $samplingrate = null; + $type = null; + $url = null; + $width = null; + + $url = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link)); + if (isset($link['attribs']['']['type'])) + { + $type = $this->sanitize($link['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($link['attribs']['']['length'])) + { + $length = ceil($link['attribs']['']['length']); + } + if (isset($link['attribs']['']['title'])) + { + $title = $this->sanitize($link['attribs']['']['title'], SIMPLEPIE_CONSTRUCT_TEXT); + } + else + { + $title = $title_parent; + } + + // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor + $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title, $width)); + } + } + + foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link') as $link) + { + if (isset($link['attribs']['']['href']) && !empty($link['attribs']['']['rel']) && $link['attribs']['']['rel'] === 'enclosure') + { + // Attributes + $bitrate = null; + $channels = null; + $duration = null; + $expression = null; + $framerate = null; + $height = null; + $javascript = null; + $lang = null; + $length = null; + $medium = null; + $samplingrate = null; + $type = null; + $url = null; + $width = null; + + $url = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link)); + if (isset($link['attribs']['']['type'])) + { + $type = $this->sanitize($link['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($link['attribs']['']['length'])) + { + $length = ceil($link['attribs']['']['length']); + } + + // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor + $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width)); + } + } + + if ($enclosure = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'enclosure')) + { + if (isset($enclosure[0]['attribs']['']['url'])) + { + // Attributes + $bitrate = null; + $channels = null; + $duration = null; + $expression = null; + $framerate = null; + $height = null; + $javascript = null; + $lang = null; + $length = null; + $medium = null; + $samplingrate = null; + $type = null; + $url = null; + $width = null; + + $url = $this->sanitize($enclosure[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($enclosure[0])); + if (isset($enclosure[0]['attribs']['']['type'])) + { + $type = $this->sanitize($enclosure[0]['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT); + } + if (isset($enclosure[0]['attribs']['']['length'])) + { + $length = ceil($enclosure[0]['attribs']['']['length']); + } + + // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor + $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width)); + } + } + + if (sizeof($this->data['enclosures']) === 0 && ($url || $type || $length || $bitrate || $captions_parent || $categories_parent || $channels || $copyrights_parent || $credits_parent || $description_parent || $duration_parent || $expression || $framerate || $hashes_parent || $height || $keywords_parent || $lang || $medium || $player_parent || $ratings_parent || $restrictions_parent || $samplingrate || $thumbnails_parent || $title_parent || $width)) + { + // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor + $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width)); + } + + $this->data['enclosures'] = array_values(array_unique($this->data['enclosures'])); + } + if (!empty($this->data['enclosures'])) + { + return $this->data['enclosures']; + } + + return null; + } +/* + public function get_latitude() + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lat')) + { + return (float) $return[0]['data']; + } + elseif (($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) + { + return (float) $match[1]; + } + + return null; + } + + public function get_longitude() + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'long')) + { + return (float) $return[0]['data']; + } + elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lon')) + { + return (float) $return[0]['data']; + } + elseif (($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) + { + return (float) $match[2]; + } + + return null; + } + + public function get_source() + { + if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'source')) + { + return $this->registry->create('Source', array($this, $return[0])); + } + + return null; + }*/ +} diff --git a/projects/tests/tests/real/executiontime/class-wp-query.php b/projects/tests/tests/real/executiontime/class-wp-query.php new file mode 100644 index 00000000..51853140 --- /dev/null +++ b/projects/tests/tests/real/executiontime/class-wp-query.php @@ -0,0 +1,1447 @@ +parse_query(); + + do_action_ref_array( 'pre_get_posts', array( &$this ) ); + + // Shorthand. + $q = &$this->query_vars; + + // Fill again in case 'pre_get_posts' unset some vars. + $q = $this->fill_query_vars( $q ); + + // Parse meta query. + $this->meta_query = new WP_Meta_Query(); + $this->meta_query->parse_query_vars( $q ); + + // Set a flag if a 'pre_get_posts' hook changed the query vars. + $hash = md5( serialize( $this->query_vars ) ); + if ( $hash != $this->query_vars_hash ) { + $this->query_vars_changed = true; + $this->query_vars_hash = $hash; + } + unset( $hash ); + + // First let's clear some variables. + $distinct = ''; + $whichauthor = ''; + $whichmimetype = ''; + $where = ''; + $limits = ''; + $join = ''; + $search = ''; + $groupby = ''; + $post_status_join = false; + $page = 1; + + if ( isset( $q['caller_get_posts'] ) ) { + _deprecated_argument( + 'WP_Query', + '3.1.0', + sprintf( + __( '%1$s is deprecated. Use %2$s instead.' ), + 'caller_get_posts', + 'ignore_sticky_posts' + ) + ); + + if ( ! isset( $q['ignore_sticky_posts'] ) ) { + $q['ignore_sticky_posts'] = $q['caller_get_posts']; + } + } + + if ( ! isset( $q['ignore_sticky_posts'] ) ) { + $q['ignore_sticky_posts'] = false; + } + + if ( ! isset( $q['suppress_filters'] ) ) { + $q['suppress_filters'] = false; + } + + if ( ! isset( $q['cache_results'] ) ) { + if ( wp_using_ext_object_cache() ) { + $q['cache_results'] = false; + } else { + $q['cache_results'] = true; + } + } + + if ( ! isset( $q['update_post_term_cache'] ) ) { + $q['update_post_term_cache'] = true; + } + + if ( ! isset( $q['lazy_load_term_meta'] ) ) { + $q['lazy_load_term_meta'] = $q['update_post_term_cache']; + } + + if ( ! isset( $q['update_post_meta_cache'] ) ) { + $q['update_post_meta_cache'] = true; + } + + if ( ! isset( $q['post_type'] ) ) { + if ( $this->is_search ) { + $q['post_type'] = 'any'; + } else { + $q['post_type'] = ''; + } + } + $post_type = $q['post_type']; + if ( empty( $q['posts_per_page'] ) ) { + $q['posts_per_page'] = get_option( 'posts_per_page' ); + } + if ( isset( $q['showposts'] ) && $q['showposts'] ) { + $q['showposts'] = (int) $q['showposts']; + $q['posts_per_page'] = $q['showposts']; + } + if ( ( isset( $q['posts_per_archive_page'] ) && 0 != $q['posts_per_archive_page'] ) && ( $this->is_archive || $this->is_search ) ) { + $q['posts_per_page'] = $q['posts_per_archive_page']; + } + if ( ! isset( $q['nopaging'] ) ) { + if ( -1 == $q['posts_per_page'] ) { + $q['nopaging'] = true; + } else { + $q['nopaging'] = false; + } + } + + if ( $this->is_feed ) { + // This overrides 'posts_per_page'. + if ( ! empty( $q['posts_per_rss'] ) ) { + $q['posts_per_page'] = $q['posts_per_rss']; + } else { + $q['posts_per_page'] = get_option( 'posts_per_rss' ); + } + $q['nopaging'] = false; + } + $q['posts_per_page'] = (int) $q['posts_per_page']; + if ( $q['posts_per_page'] < -1 ) { + $q['posts_per_page'] = abs( $q['posts_per_page'] ); + } elseif ( 0 == $q['posts_per_page'] ) { + $q['posts_per_page'] = 1; + } + + if ( ! isset( $q['comments_per_page'] ) || 0 == $q['comments_per_page'] ) { + $q['comments_per_page'] = get_option( 'comments_per_page' ); + } + + if ( $this->is_home && ( empty( $this->query ) || 'true' === $q['preview'] ) && ( 'page' === get_option( 'show_on_front' ) ) && get_option( 'page_on_front' ) ) { + $this->is_page = true; + $this->is_home = false; + $q['page_id'] = get_option( 'page_on_front' ); + } + + if ( isset( $q['page'] ) ) { + $q['page'] = trim( $q['page'], '/' ); + $q['page'] = absint( $q['page'] ); + } + + // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. + if ( isset( $q['no_found_rows'] ) ) { + $q['no_found_rows'] = (bool) $q['no_found_rows']; + } else { + $q['no_found_rows'] = false; + } + + switch ( $q['fields'] ) { + case 'ids': + $fields = "{$wpdb->posts}.ID"; + break; + case 'id=>parent': + $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent"; + break; + default: + $fields = "{$wpdb->posts}.*"; + } + + if ( '' !== $q['menu_order'] ) { + $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order']; + } + // The "m" parameter is meant for months but accepts datetimes of varying specificity. + if ( $q['m'] ) { + $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr( $q['m'], 0, 4 ); + if ( strlen( $q['m'] ) > 5 ) { + $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 4, 2 ); + } + if ( strlen( $q['m'] ) > 7 ) { + $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 6, 2 ); + } + if ( strlen( $q['m'] ) > 9 ) { + $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr( $q['m'], 8, 2 ); + } + if ( strlen( $q['m'] ) > 11 ) { + $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr( $q['m'], 10, 2 ); + } + if ( strlen( $q['m'] ) > 13 ) { + $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr( $q['m'], 12, 2 ); + } + } + + // Handle the other individual date parameters. + $date_parameters = array(); + + if ( '' !== $q['hour'] ) { + $date_parameters['hour'] = $q['hour']; + } + + if ( '' !== $q['minute'] ) { + $date_parameters['minute'] = $q['minute']; + } + + if ( '' !== $q['second'] ) { + $date_parameters['second'] = $q['second']; + } + + if ( $q['year'] ) { + $date_parameters['year'] = $q['year']; + } + + if ( $q['monthnum'] ) { + $date_parameters['monthnum'] = $q['monthnum']; + } + + if ( $q['w'] ) { + $date_parameters['week'] = $q['w']; + } + + if ( $q['day'] ) { + $date_parameters['day'] = $q['day']; + } + + if ( $date_parameters ) { + $date_query = new WP_Date_Query( array( $date_parameters ) ); + $where .= $date_query->get_sql(); + } + unset( $date_parameters, $date_query ); + + // Handle complex date queries. + if ( ! empty( $q['date_query'] ) ) { + $this->date_query = new WP_Date_Query( $q['date_query'] ); + $where .= $this->date_query->get_sql(); + } + + // If we've got a post_type AND it's not "any" post_type. + if ( ! empty( $q['post_type'] ) && 'any' !== $q['post_type'] ) { + foreach ( (array) $q['post_type'] as $_post_type ) { + $ptype_obj = get_post_type_object( $_post_type ); + if ( ! $ptype_obj || ! $ptype_obj->query_var || empty( $q[ $ptype_obj->query_var ] ) ) { + continue; + } + + if ( ! $ptype_obj->hierarchical ) { + // Non-hierarchical post types can directly use 'name'. + $q['name'] = $q[ $ptype_obj->query_var ]; + } else { + // Hierarchical post types will operate through 'pagename'. + $q['pagename'] = $q[ $ptype_obj->query_var ]; + $q['name'] = ''; + } + + // Only one request for a slug is possible, this is why name & pagename are overwritten above. + break; + } // End foreach. + unset( $ptype_obj ); + } + + if ( '' !== $q['title'] ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) ); + } + + // Parameters related to 'post_name'. + if ( '' !== $q['name'] ) { + $q['name'] = sanitize_title_for_query( $q['name'] ); + $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; + } elseif ( '' !== $q['pagename'] ) { + if ( isset( $this->queried_object_id ) ) { + $reqpage = $this->queried_object_id; + } else { + if ( 'page' !== $q['post_type'] ) { + foreach ( (array) $q['post_type'] as $_post_type ) { + $ptype_obj = get_post_type_object( $_post_type ); + if ( ! $ptype_obj || ! $ptype_obj->hierarchical ) { + continue; + } + + $reqpage = get_page_by_path( $q['pagename'], OBJECT, $_post_type ); + if ( $reqpage ) { + break; + } + } + unset( $ptype_obj ); + } else { + $reqpage = get_page_by_path( $q['pagename'] ); + } + if ( ! empty( $reqpage ) ) { + $reqpage = $reqpage->ID; + } else { + $reqpage = 0; + } + } + + $page_for_posts = get_option( 'page_for_posts' ); + if ( ( 'page' !== get_option( 'show_on_front' ) ) || empty( $page_for_posts ) || ( $reqpage != $page_for_posts ) ) { + $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); + $q['name'] = $q['pagename']; + $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; + $reqpage_obj = get_post( $reqpage ); + if ( is_object( $reqpage_obj ) && 'attachment' === $reqpage_obj->post_type ) { + $this->is_attachment = true; + $post_type = 'attachment'; + $q['post_type'] = 'attachment'; + $this->is_page = true; + $q['attachment_id'] = $reqpage; + } + } + } elseif ( '' !== $q['attachment'] ) { + $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); + $q['name'] = $q['attachment']; + $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; + } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { + $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); + $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; + $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; + } + + // If an attachment is requested by number, let it supersede any post number. + if ( $q['attachment_id'] ) { + $q['p'] = absint( $q['attachment_id'] ); + } + + // If a post number is specified, load that post. + if ( $q['p'] ) { + $where .= " AND {$wpdb->posts}.ID = " . $q['p']; + } elseif ( $q['post__in'] ) { + $post__in = implode( ',', array_map( 'absint', $q['post__in'] ) ); + $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; + } elseif ( $q['post__not_in'] ) { + $post__not_in = implode( ',', array_map( 'absint', $q['post__not_in'] ) ); + $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; + } + + if ( is_numeric( $q['post_parent'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); + } elseif ( $q['post_parent__in'] ) { + $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); + $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; + } elseif ( $q['post_parent__not_in'] ) { + $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) ); + $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; + } + + if ( $q['page_id'] ) { + if ( ( 'page' !== get_option( 'show_on_front' ) ) || ( get_option( 'page_for_posts' ) != $q['page_id'] ) ) { + $q['p'] = $q['page_id']; + $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; + } + } + + // If a search pattern is specified, load the posts that match. + if ( strlen( $q['s'] ) ) { + $search = $this->parse_search( $q ); + } + + if ( ! $q['suppress_filters'] ) { + + $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); + } + + // Taxonomies. + if ( ! $this->is_singular ) { + $this->parse_tax_query( $q ); + + $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); + + $join .= $clauses['join']; + $where .= $clauses['where']; + } + + if ( $this->is_tax ) { + if ( empty( $post_type ) ) { + // Do a fully inclusive search for currently registered post types of queried taxonomies. + $post_type = array(); + $taxonomies = array_keys( $this->tax_query->queried_terms ); + foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) { + $object_taxonomies = 'attachment' === $pt ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt ); + if ( array_intersect( $taxonomies, $object_taxonomies ) ) { + $post_type[] = $pt; + } + } + if ( ! $post_type ) { + $post_type = 'any'; + } elseif ( count( $post_type ) == 1 ) { + $post_type = $post_type[0]; + } + + $post_status_join = true; + } elseif ( in_array( 'attachment', (array) $post_type, true ) ) { + $post_status_join = true; + } + } + + if ( ! empty( $this->tax_query->queried_terms ) ) { + + if ( ! isset( $q['taxonomy'] ) ) { + foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { + if ( empty( $queried_items['terms'][0] ) ) { + continue; + } + + if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ), true ) ) { + $q['taxonomy'] = $queried_taxonomy; + + if ( 'slug' === $queried_items['field'] ) { + $q['term'] = $queried_items['terms'][0]; + } else { + $q['term_id'] = $queried_items['terms'][0]; + } + + // Take the first one we find. + break; + } + } + } + + // 'cat', 'category_name', 'tag_id'. + foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { + if ( empty( $queried_items['terms'][0] ) ) { + continue; + } + + if ( 'category' === $queried_taxonomy ) { + $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' ); + if ( $the_cat ) { + $this->set( 'cat', $the_cat->term_id ); + $this->set( 'category_name', $the_cat->slug ); + } + unset( $the_cat ); + } + + if ( 'post_tag' === $queried_taxonomy ) { + $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' ); + if ( $the_tag ) { + $this->set( 'tag_id', $the_tag->term_id ); + } + unset( $the_tag ); + } + } + } + + if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) { + $groupby = "{$wpdb->posts}.ID"; + } + + // Author/user stuff. + + if ( ! empty( $q['author'] ) && '0' != $q['author'] ) { + $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); + $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) ); + foreach ( $authors as $author ) { + $key = $author > 0 ? 'author__in' : 'author__not_in'; + $q[ $key ][] = abs( $author ); + } + $q['author'] = implode( ',', $authors ); + } + + if ( ! empty( $q['author__not_in'] ) ) { + $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); + $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; + } elseif ( ! empty( $q['author__in'] ) ) { + $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); + $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; + } + + // Author stuff for nice URLs. + + if ( '' !== $q['author_name'] ) { + if ( strpos( $q['author_name'], '/' ) !== false ) { + $q['author_name'] = explode( '/', $q['author_name'] ); + if ( $q['author_name'][ count( $q['author_name'] ) - 1 ] ) { + $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 1 ]; // No trailing slash. + } else { + $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 2 ]; // There was a trailing slash. + } + } + $q['author_name'] = sanitize_title_for_query( $q['author_name'] ); + $q['author'] = get_user_by( 'slug', $q['author_name'] ); + if ( $q['author'] ) { + $q['author'] = $q['author']->ID; + } + $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint( $q['author'] ) . ')'; + } + + // Matching by comment count. + if ( isset( $q['comment_count'] ) ) { + // Numeric comment count is converted to array format. + if ( is_numeric( $q['comment_count'] ) ) { + $q['comment_count'] = array( + 'value' => (int) $q['comment_count'], + ); + } + + if ( isset( $q['comment_count']['value'] ) ) { + $q['comment_count'] = array_merge( + array( + 'compare' => '=', + ), + $q['comment_count'] + ); + + // Fallback for invalid compare operators is '='. + $compare_operators = array( '=', '!=', '>', '>=', '<', '<=' ); + if ( ! in_array( $q['comment_count']['compare'], $compare_operators, true ) ) { + $q['comment_count']['compare'] = '='; + } + + $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count {$q['comment_count']['compare']} %d", $q['comment_count']['value'] ); + } + } + + // MIME-Type stuff for attachment browsing. + + if ( isset( $q['post_mime_type'] ) && '' !== $q['post_mime_type'] ) { + $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts ); + } + $where .= $search . $whichauthor . $whichmimetype; + + if ( ! empty( $this->meta_query->queries ) ) { + $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this ); + $join .= $clauses['join']; + $where .= $clauses['where']; + } + + $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] ); + if ( ! isset( $q['order'] ) ) { + $q['order'] = $rand ? '' : 'DESC'; + } else { + $q['order'] = $rand ? '' : $this->parse_order( $q['order'] ); + } + + // These values of orderby should ignore the 'order' parameter. + $force_asc = array( 'post__in', 'post_name__in', 'post_parent__in' ); + if ( isset( $q['orderby'] ) && in_array( $q['orderby'], $force_asc, true ) ) { + $q['order'] = ''; + } + + // Order by. + if ( empty( $q['orderby'] ) ) { + + if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) { + $orderby = ''; + } else { + $orderby = "{$wpdb->posts}.post_date " . $q['order']; + } + } elseif ( 'none' === $q['orderby'] ) { + $orderby = ''; + } else { + $orderby_array = array(); + if ( is_array( $q['orderby'] ) ) { + foreach ( $q['orderby'] as $_orderby => $order ) { + $orderby = addslashes_gpc( urldecode( $_orderby ) ); + $parsed = $this->parse_orderby( $orderby ); + + if ( ! $parsed ) { + continue; + } + + $orderby_array[] = $parsed . ' ' . $this->parse_order( $order ); + } + $orderby = implode( ', ', $orderby_array ); + + } else { + $q['orderby'] = urldecode( $q['orderby'] ); + $q['orderby'] = addslashes_gpc( $q['orderby'] ); + + foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) { + $parsed = $this->parse_orderby( $orderby ); + // Only allow certain values for safety. + if ( ! $parsed ) { + continue; + } + + $orderby_array[] = $parsed; + } + $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); + + if ( empty( $orderby ) ) { + $orderby = "{$wpdb->posts}.post_date " . $q['order']; + } elseif ( ! empty( $q['order'] ) ) { + $orderby .= " {$q['order']}"; + } + } + } + + // Order search results by relevance only when another "orderby" is not specified in the query. + if ( ! empty( $q['s'] ) ) { + $search_orderby = ''; + if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) ) { + $search_orderby = $this->parse_search_order( $q ); + } + + if ( ! $q['suppress_filters'] ) { + + $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); + } + + if ( $search_orderby ) { + $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; + } + } + + if ( is_array( $post_type ) && count( $post_type ) > 1 ) { + $post_type_cap = 'multiple_post_type'; + } else { + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $post_type_object = get_post_type_object( $post_type ); + if ( empty( $post_type_object ) ) { + $post_type_cap = $post_type; + } + } + + if ( isset( $q['post_password'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $q['post_password'] ); + if ( empty( $q['perm'] ) ) { + $q['perm'] = 'readable'; + } + } elseif ( isset( $q['has_password'] ) ) { + $where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' ); + } + + if ( ! empty( $q['comment_status'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] ); + } + + if ( ! empty( $q['ping_status'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] ); + } + + if ( 'any' === $post_type ) { + $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) ); + if ( empty( $in_search_post_types ) ) { + $where .= ' AND 1=0 '; + } else { + $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; + } + } elseif ( ! empty( $post_type ) && is_array( $post_type ) ) { + $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')"; + } elseif ( ! empty( $post_type ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); + $post_type_object = get_post_type_object( $post_type ); + } elseif ( $this->is_attachment ) { + $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; + $post_type_object = get_post_type_object( 'attachment' ); + } elseif ( $this->is_page ) { + $where .= " AND {$wpdb->posts}.post_type = 'page'"; + $post_type_object = get_post_type_object( 'page' ); + } else { + $where .= " AND {$wpdb->posts}.post_type = 'post'"; + $post_type_object = get_post_type_object( 'post' ); + } + + $edit_cap = 'edit_post'; + $read_cap = 'read_post'; + + if ( ! empty( $post_type_object ) ) { + $edit_others_cap = $post_type_object->cap->edit_others_posts; + $read_private_cap = $post_type_object->cap->read_private_posts; + } else { + $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; + $read_private_cap = 'read_private_' . $post_type_cap . 's'; + } + + $user_id = get_current_user_id(); + + $q_status = array(); + if ( ! empty( $q['post_status'] ) ) { + $statuswheres = array(); + $q_status = $q['post_status']; + if ( ! is_array( $q_status ) ) { + $q_status = explode( ',', $q_status ); + } + $r_status = array(); + $p_status = array(); + $e_status = array(); + if ( in_array( 'any', $q_status, true ) ) { + foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { + if ( ! in_array( $status, $q_status, true ) ) { + $e_status[] = "{$wpdb->posts}.post_status <> '$status'"; + } + } + } else { + foreach ( get_post_stati() as $status ) { + if ( in_array( $status, $q_status, true ) ) { + if ( 'private' === $status ) { + $p_status[] = "{$wpdb->posts}.post_status = '$status'"; + } else { + $r_status[] = "{$wpdb->posts}.post_status = '$status'"; + } + } + } + } + + if ( empty( $q['perm'] ) || 'readable' !== $q['perm'] ) { + $r_status = array_merge( $r_status, $p_status ); + unset( $p_status ); + } + + if ( ! empty( $e_status ) ) { + $statuswheres[] = '(' . implode( ' AND ', $e_status ) . ')'; + } + if ( ! empty( $r_status ) ) { + if ( ! empty( $q['perm'] ) && 'editable' === $q['perm'] && ! current_user_can( $edit_others_cap ) ) { + $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $r_status ) . '))'; + } else { + $statuswheres[] = '(' . implode( ' OR ', $r_status ) . ')'; + } + } + if ( ! empty( $p_status ) ) { + if ( ! empty( $q['perm'] ) && 'readable' === $q['perm'] && ! current_user_can( $read_private_cap ) ) { + $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $p_status ) . '))'; + } else { + $statuswheres[] = '(' . implode( ' OR ', $p_status ) . ')'; + } + } + if ( $post_status_join ) { + $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; + foreach ( $statuswheres as $index => $statuswhere ) { + $statuswheres[ $index ] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . '))'; + } + } + $where_status = implode( ' OR ', $statuswheres ); + if ( ! empty( $where_status ) ) { + $where .= " AND ($where_status)"; + } + } elseif ( ! $this->is_singular ) { + $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; + + // Add public states. + $public_states = get_post_stati( array( 'public' => true ) ); + foreach ( (array) $public_states as $state ) { + if ( 'publish' === $state ) { // Publish is hard-coded above. + continue; + } + $where .= " OR {$wpdb->posts}.post_status = '$state'"; + } + + if ( $this->is_admin ) { + // Add protected states that should show in the admin all list. + $admin_all_states = get_post_stati( + array( + 'protected' => true, + 'show_in_admin_all_list' => true, + ) + ); + foreach ( (array) $admin_all_states as $state ) { + $where .= " OR {$wpdb->posts}.post_status = '$state'"; + } + } + + if ( is_user_logged_in() ) { + // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. + $private_states = get_post_stati( array( 'private' => true ) ); + foreach ( (array) $private_states as $state ) { + $where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'"; + } + } + + $where .= ')'; + } + + + if ( ! $q['suppress_filters'] ) { + + $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) ); + + + $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) ); + } + + // Paging. + if ( empty( $q['nopaging'] ) && ! $this->is_singular ) { + $page = absint( $q['paged'] ); + if ( ! $page ) { + $page = 1; + } + + // If 'offset' is provided, it takes precedence over 'paged'. + if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) { + $q['offset'] = absint( $q['offset'] ); + $pgstrt = $q['offset'] . ', '; + } else { + $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; + } + $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; + } + + // Comments feeds. + if ( $this->is_comment_feed && ! $this->is_singular ) { + if ( $this->is_archive || $this->is_search ) { + $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join "; + $cwhere = "WHERE comment_approved = '1' $where"; + $cgroupby = "{$wpdb->comments}.comment_id"; + } else { // Other non-singular, e.g. front. + $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; + $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'"; + $cgroupby = ''; + } + + if ( ! $q['suppress_filters'] ) { + + $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) ); + + + $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) ); + + + $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) ); + + + $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); + + + $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); + } + + $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; + $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; + $climits = ( ! empty( $climits ) ) ? $climits : ''; + + $comments = (array) $wpdb->get_results( "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits" ); + // Convert to WP_Comment. + $this->comments = array_map( 'get_comment', $comments ); + $this->comment_count = count( $this->comments ); + + $post_ids = array(); + + foreach ( $this->comments as $comment ) { + $post_ids[] = (int) $comment->comment_post_ID; + } + + $post_ids = implode( ',', $post_ids ); + $join = ''; + if ( $post_ids ) { + $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; + } else { + $where = 'AND 0'; + } + } + + $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); + + if ( ! $q['suppress_filters'] ) { + + $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); + + + $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); + + + $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); + + + $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); + + + $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); + + + $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); + + + $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); + + $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); + + $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; + $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; + $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; + $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; + $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; + $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; + $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; + } + + + do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join ); + + if ( ! $q['suppress_filters'] ) { + + $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); + + $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); + + $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); + + $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); + + $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) ); + + $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) ); + + $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); + + $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); + + $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; + $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; + $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; + $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; + $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; + $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; + $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; + } + + if ( ! empty( $groupby ) ) { + $groupby = 'GROUP BY ' . $groupby; + } + if ( ! empty( $orderby ) ) { + $orderby = 'ORDER BY ' . $orderby; + } + + $found_rows = ''; + if ( ! $q['no_found_rows'] && ! empty( $limits ) ) { + $found_rows = 'SQL_CALC_FOUND_ROWS'; + } + + $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + $this->request = $old_request; + + if ( ! $q['suppress_filters'] ) { + $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) ); + } + + $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) ); + + if ( 'ids' === $q['fields'] ) { + if ( null === $this->posts ) { + $this->posts = $wpdb->get_col( $this->request ); + } + + $this->posts = array_map( 'intval', $this->posts ); + $this->post_count = count( $this->posts ); + $this->set_found_posts( $q, $limits ); + + return $this->posts; + } + + if ( 'id=>parent' === $q['fields'] ) { + if ( null === $this->posts ) { + $this->posts = $wpdb->get_results( $this->request ); + } + + $this->post_count = count( $this->posts ); + $this->set_found_posts( $q, $limits ); + + $r = array(); + foreach ( $this->posts as $key => $post ) { + $this->posts[ $key ]->ID = (int) $post->ID; + $this->posts[ $key ]->post_parent = (int) $post->post_parent; + + $r[ (int) $post->ID ] = (int) $post->post_parent; + } + + return $r; + } + + if ( null === $this->posts ) { + $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" === $fields && ! empty( $limits ) && $q['posts_per_page'] < 500 ); + + + $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this ); + + if ( $split_the_query ) { + // First get the IDs and then fill in the objects. + + $this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + + $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); + + $ids = $wpdb->get_col( $this->request ); + + if ( $ids ) { + $this->posts = $ids; + $this->set_found_posts( $q, $limits ); + _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + } else { + $this->posts = array(); + } + } else { + $this->posts = $wpdb->get_results( $this->request ); + $this->set_found_posts( $q, $limits ); + } + } + + // Convert to WP_Post objects. + if ( $this->posts ) { + $this->posts = array_map( 'get_post', $this->posts ); + } + + if ( ! $q['suppress_filters'] ) { + + $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); + } + + if ( ! empty( $this->posts ) && $this->is_comment_feed && $this->is_singular ) { + $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) ); + + $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) ); + + $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) ); + $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; + + $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); + $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; + + $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); + + $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; + $comments = $wpdb->get_results( $comments_request ); + // Convert to WP_Comment. + $this->comments = array_map( 'get_comment', $comments ); + $this->comment_count = count( $this->comments ); + } + + // Check post status to determine if post should be displayed. + if ( ! empty( $this->posts ) && ( $this->is_single || $this->is_page ) ) { + $status = get_post_status( $this->posts[0] ); + + if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { + $this->is_page = false; + $this->is_single = true; + $this->is_attachment = true; + } + + // If the post_status was specifically requested, let it pass through. + if ( ! in_array( $status, $q_status, true ) ) { + $post_status_obj = get_post_status_object( $status ); + + if ( $post_status_obj && ! $post_status_obj->public ) { + if ( ! is_user_logged_in() ) { + // User must be logged in to view unpublished posts. + $this->posts = array(); + } else { + if ( $post_status_obj->protected ) { + // User must have edit permissions on the draft to preview. + if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } else { + $this->is_preview = true; + if ( 'future' !== $status ) { + $this->posts[0]->post_date = current_time( 'mysql' ); + } + } + } elseif ( $post_status_obj->private ) { + if ( ! current_user_can( $read_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } + } else { + $this->posts = array(); + } + } + } elseif ( ! $post_status_obj ) { + // Post status is not registered, assume it's not public. + if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } + } + } + + if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); + } + } + + // Put sticky posts at the top of the posts array. + $sticky_posts = get_option( 'sticky_posts' ); + if ( $this->is_home && $page <= 1 && is_array( $sticky_posts ) && ! empty( $sticky_posts ) && ! $q['ignore_sticky_posts'] ) { + $num_posts = count( $this->posts ); + $sticky_offset = 0; + // Loop over posts and relocate stickies to the front. + for ( $i = 0; $i < $num_posts; $i++ ) { + if ( in_array( $this->posts[ $i ]->ID, $sticky_posts, true ) ) { + $sticky_post = $this->posts[ $i ]; + // Remove sticky from current position. + array_splice( $this->posts, $i, 1 ); + // Move to front, after other stickies. + array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); + // Increment the sticky offset. The next sticky will be placed at this offset. + $sticky_offset++; + // Remove post from sticky posts array. + $offset = array_search( $sticky_post->ID, $sticky_posts, true ); + unset( $sticky_posts[ $offset ] ); + } + } + + // If any posts have been excluded specifically, Ignore those that are sticky. + if ( ! empty( $sticky_posts ) && ! empty( $q['post__not_in'] ) ) { + $sticky_posts = array_diff( $sticky_posts, $q['post__not_in'] ); + } + + // Fetch sticky posts that weren't in the query results. + if ( ! empty( $sticky_posts ) ) { + $stickies = get_posts( + array( + 'post__in' => $sticky_posts, + 'post_type' => $post_type, + 'post_status' => 'publish', + 'nopaging' => true, + ) + ); + + foreach ( $stickies as $sticky_post ) { + array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); + $sticky_offset++; + } + } + } + + // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up. + if ( ! empty( $this->comments ) ) { + wp_queue_comments_for_comment_meta_lazyload( $this->comments ); + } + + if ( ! $q['suppress_filters'] ) { + $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) ); + } + + // Ensure that any posts added/modified via one of the filters above are + // of the type WP_Post and are filtered. + if ( $this->posts ) { + $this->post_count = count( $this->posts ); + + $this->posts = array_map( 'get_post', $this->posts ); + + if ( $q['cache_results'] ) { + update_post_caches( $this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + } + + $this->post = reset( $this->posts ); + } else { + $this->post_count = 0; + $this->posts = array(); + } + + if ( $q['lazy_load_term_meta'] ) { + wp_queue_posts_for_term_meta_lazyload( $this->posts ); + } + + return $this->posts; + } + + + private function set_found_posts( $q, $limits ) { + global $wpdb; + + // Bail if posts is an empty array. Continue if posts is an empty string, + // null, or false to accommodate caching plugins that fill posts later. + if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) ) { + return; + } + + if ( ! empty( $limits ) ) { + $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); + + $this->found_posts = (int) $wpdb->get_var( $found_posts_query ); + } else { + if ( is_array( $this->posts ) ) { + $this->found_posts = count( $this->posts ); + } else { + if ( null === $this->posts ) { + $this->found_posts = 0; + } else { + $this->found_posts = 1; + } + } + } + + $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); + + if ( ! empty( $limits ) ) { + $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); + } + } + + public function next_post() { + + $this->current_post++; + + $this->post = $this->posts[ $this->current_post ]; + return $this->post; + } + + public function the_post() { + global $post; + $this->in_the_loop = true; + + if ( -1 == $this->current_post ) { // Loop has just started. + do_action_ref_array( 'loop_start', array( &$this ) ); + } + + $post = $this->next_post(); + $this->setup_postdata( $post ); + } + + public function have_posts() { + if ( $this->current_post + 1 < $this->post_count ) { + return true; + } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) { + do_action_ref_array( 'loop_end', array( &$this ) ); + // Do some cleaning up after the loop. + $this->rewind_posts(); + } elseif ( 0 === $this->post_count ) { + do_action( 'loop_no_results', $this ); + } + + $this->in_the_loop = false; + return false; + } + + public function rewind_posts() { + $this->current_post = -1; + if ( $this->post_count > 0 ) { + $this->post = $this->posts[0]; + } + } + + public function next_comment() { + $this->current_comment++; + + $this->comment = $this->comments[ $this->current_comment ]; + return $this->comment; + } + + public function the_comment() { + global $comment; + + $comment = $this->next_comment(); + + if ( 0 == $this->current_comment ) { + do_action( 'comment_loop_start' ); + } + } + + public function have_comments() { + if ( $this->current_comment + 1 < $this->comment_count ) { + return true; + } elseif ( $this->current_comment + 1 == $this->comment_count ) { + $this->rewind_comments(); + } + + return false; + } + + public function rewind_comments() { + $this->current_comment = -1; + if ( $this->comment_count > 0 ) { + $this->comment = $this->comments[0]; + } + } + + public function query( $query ) { + $this->init(); + $this->query = wp_parse_args( $query ); + $this->query_vars = $this->query; + return $this->get_posts(); + } + + public function get_queried_object() { + if ( isset( $this->queried_object ) ) { + return $this->queried_object; + } + + $this->queried_object = null; + $this->queried_object_id = null; + + if ( $this->is_category || $this->is_tag || $this->is_tax ) { + if ( $this->is_category ) { + if ( $this->get( 'cat' ) ) { + $term = get_term( $this->get( 'cat' ), 'category' ); + } elseif ( $this->get( 'category_name' ) ) { + $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' ); + } + } elseif ( $this->is_tag ) { + if ( $this->get( 'tag_id' ) ) { + $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); + } elseif ( $this->get( 'tag' ) ) { + $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' ); + } + } else { + // For other tax queries, grab the first term from the first clause. + if ( ! empty( $this->tax_query->queried_terms ) ) { + $queried_taxonomies = array_keys( $this->tax_query->queried_terms ); + $matched_taxonomy = reset( $queried_taxonomies ); + $query = $this->tax_query->queried_terms[ $matched_taxonomy ]; + + if ( ! empty( $query['terms'] ) ) { + if ( 'term_id' === $query['field'] ) { + $term = get_term( reset( $query['terms'] ), $matched_taxonomy ); + } else { + $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy ); + } + } + } + } + + if ( ! empty( $term ) && ! is_wp_error( $term ) ) { + $this->queried_object = $term; + $this->queried_object_id = (int) $term->term_id; + + if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) { + _make_cat_compat( $this->queried_object ); + } + } + } elseif ( $this->is_post_type_archive ) { + $post_type = $this->get( 'post_type' ); + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $this->queried_object = get_post_type_object( $post_type ); + } elseif ( $this->is_posts_page ) { + $page_for_posts = get_option( 'page_for_posts' ); + $this->queried_object = get_post( $page_for_posts ); + $this->queried_object_id = (int) $this->queried_object->ID; + } elseif ( $this->is_singular && ! empty( $this->post ) ) { + $this->queried_object = $this->post; + $this->queried_object_id = (int) $this->post->ID; + } elseif ( $this->is_author ) { + $this->queried_object_id = (int) $this->get( 'author' ); + $this->queried_object = get_userdata( $this->queried_object_id ); + } + + return $this->queried_object; + } + + + + public function get_queried_object_id() { + $this->get_queried_object(); + + if ( isset( $this->queried_object_id ) ) { + return $this->queried_object_id; + } + + return 0; + } + + public function __construct( $query = '' ) { + if ( ! empty( $query ) ) { + $this->query( $query ); + } + } + +} diff --git a/projects/tests/tests/real/executiontime/class-wp-query3.php b/projects/tests/tests/real/executiontime/class-wp-query3.php new file mode 100644 index 00000000..960a16d6 --- /dev/null +++ b/projects/tests/tests/real/executiontime/class-wp-query3.php @@ -0,0 +1,2923 @@ +is_single = false; + $this->is_preview = false; + $this->is_page = false; + $this->is_archive = false; + $this->is_date = false; + $this->is_year = false; + $this->is_month = false; + $this->is_day = false; + $this->is_time = false; + $this->is_author = false; + $this->is_category = false; + $this->is_tag = false; + $this->is_tax = false; + $this->is_search = false; + $this->is_feed = false; + $this->is_comment_feed = false; + $this->is_trackback = false; + $this->is_home = false; + $this->is_privacy_policy = false; + $this->is_404 = false; + $this->is_paged = false; + $this->is_admin = false; + $this->is_attachment = false; + $this->is_singular = false; + $this->is_robots = false; + $this->is_favicon = false; + $this->is_posts_page = false; + $this->is_post_type_archive = false; + } + + public function init() { + unset( $this->posts ); + unset( $this->query ); + $this->query_vars = array(); + unset( $this->queried_object ); + unset( $this->queried_object_id ); + $this->post_count = 0; + $this->current_post = -1; + $this->in_the_loop = false; + unset( $this->request ); + unset( $this->post ); + unset( $this->comments ); + unset( $this->comment ); + $this->comment_count = 0; + $this->current_comment = -1; + $this->found_posts = 0; + $this->max_num_pages = 0; + $this->max_num_comment_pages = 0; + + $this->init_query_flags(); + } + + public function parse_query_vars() { + $this->parse_query(); + } + + public function fill_query_vars( $array ) { + $keys = array( + 'error', + 'm', + 'p', + 'post_parent', + 'subpost', + 'subpost_id', + 'attachment', + 'attachment_id', + 'name', + 'pagename', + 'page_id', + 'second', + 'minute', + 'hour', + 'day', + 'monthnum', + 'year', + 'w', + 'category_name', + 'tag', + 'cat', + 'tag_id', + 'author', + 'author_name', + 'feed', + 'tb', + 'paged', + 'meta_key', + 'meta_value', + 'preview', + 's', + 'sentence', + 'title', + 'fields', + 'menu_order', + 'embed', + ); + + foreach ( $keys as $key ) { + if ( ! isset( $array[ $key ] ) ) { + $array[ $key ] = ''; + } + } + + $array_keys = array( + 'category__in', + 'category__not_in', + 'category__and', + 'post__in', + 'post__not_in', + 'post_name__in', + 'tag__in', + 'tag__not_in', + 'tag__and', + 'tag_slug__in', + 'tag_slug__and', + 'post_parent__in', + 'post_parent__not_in', + 'author__in', + 'author__not_in', + ); + + foreach ( $array_keys as $key ) { + if ( ! isset( $array[ $key ] ) ) { + $array[ $key ] = array(); + } + } + return $array; + } + + public function parse_query( $query = '' ) { + if ( ! empty( $query ) ) { + $this->init(); + $this->query = wp_parse_args( $query ); + $this->query_vars = $this->query; + } elseif ( ! isset( $this->query ) ) { + $this->query = $this->query_vars; + } + + $this->query_vars = $this->fill_query_vars( $this->query_vars ); + $qv = &$this->query_vars; + $this->query_vars_changed = true; + + if ( ! empty( $qv['robots'] ) ) { + $this->is_robots = true; + } elseif ( ! empty( $qv['favicon'] ) ) { + $this->is_favicon = true; + } + + if ( ! is_scalar( $qv['p'] ) || (int) $qv['p'] < 0 ) { + $qv['p'] = 0; + $qv['error'] = '404'; + } else { + $qv['p'] = (int) $qv['p']; + } + + $qv['page_id'] = absint( $qv['page_id'] ); + $qv['year'] = absint( $qv['year'] ); + $qv['monthnum'] = absint( $qv['monthnum'] ); + $qv['day'] = absint( $qv['day'] ); + $qv['w'] = absint( $qv['w'] ); + $qv['m'] = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : ''; + $qv['paged'] = absint( $qv['paged'] ); + $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // Comma-separated list of positive or negative integers. + $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // Comma-separated list of positive or negative integers. + $qv['pagename'] = trim( $qv['pagename'] ); + $qv['name'] = trim( $qv['name'] ); + $qv['title'] = trim( $qv['title'] ); + if ( '' !== $qv['hour'] ) { + $qv['hour'] = absint( $qv['hour'] ); + } + if ( '' !== $qv['minute'] ) { + $qv['minute'] = absint( $qv['minute'] ); + } + if ( '' !== $qv['second'] ) { + $qv['second'] = absint( $qv['second'] ); + } + if ( '' !== $qv['menu_order'] ) { + $qv['menu_order'] = absint( $qv['menu_order'] ); + } + + // Fairly large, potentially too large, upper bound for search string lengths. + if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) { + $qv['s'] = ''; + } + + // Compat. Map subpost to attachment. + if ( '' != $qv['subpost'] ) { + $qv['attachment'] = $qv['subpost']; + } + if ( '' != $qv['subpost_id'] ) { + $qv['attachment_id'] = $qv['subpost_id']; + } + + $qv['attachment_id'] = absint( $qv['attachment_id'] ); + + if ( ( '' !== $qv['attachment'] ) || ! empty( $qv['attachment_id'] ) ) { + $this->is_single = true; + $this->is_attachment = true; + } elseif ( '' !== $qv['name'] ) { + $this->is_single = true; + } elseif ( $qv['p'] ) { + $this->is_single = true; + } elseif ( '' !== $qv['pagename'] || ! empty( $qv['page_id'] ) ) { + $this->is_page = true; + $this->is_single = false; + } else { + // Look for archive queries. Dates, categories, authors, search, post type archives. + + if ( isset( $this->query['s'] ) ) { + $this->is_search = true; + } + + if ( '' !== $qv['second'] ) { + $this->is_time = true; + $this->is_date = true; + } + + if ( '' !== $qv['minute'] ) { + $this->is_time = true; + $this->is_date = true; + } + + if ( '' !== $qv['hour'] ) { + $this->is_time = true; + $this->is_date = true; + } + + if ( $qv['day'] ) { + if ( ! $this->is_date ) { + $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] ); + if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) { + $qv['error'] = '404'; + } else { + $this->is_day = true; + $this->is_date = true; + } + } + } + + if ( $qv['monthnum'] ) { + if ( ! $this->is_date ) { + if ( 12 < $qv['monthnum'] ) { + $qv['error'] = '404'; + } else { + $this->is_month = true; + $this->is_date = true; + } + } + } + + if ( $qv['year'] ) { + if ( ! $this->is_date ) { + $this->is_year = true; + $this->is_date = true; + } + } + + if ( $qv['m'] ) { + $this->is_date = true; + if ( strlen( $qv['m'] ) > 9 ) { + $this->is_time = true; + } elseif ( strlen( $qv['m'] ) > 7 ) { + $this->is_day = true; + } elseif ( strlen( $qv['m'] ) > 5 ) { + $this->is_month = true; + } else { + $this->is_year = true; + } + } + + if ( $qv['w'] ) { + $this->is_date = true; + } + + $this->query_vars_hash = false; + $this->parse_tax_query( $qv ); + + foreach ( $this->tax_query->queries as $tax_query ) { + if ( ! is_array( $tax_query ) ) { + continue; + } + + if ( isset( $tax_query['operator'] ) && 'NOT IN' !== $tax_query['operator'] ) { + switch ( $tax_query['taxonomy'] ) { + case 'category': + $this->is_category = true; + break; + case 'post_tag': + $this->is_tag = true; + break; + default: + $this->is_tax = true; + } + } + } + unset( $tax_query ); + + if ( empty( $qv['author'] ) || ( '0' == $qv['author'] ) ) { + $this->is_author = false; + } else { + $this->is_author = true; + } + + if ( '' !== $qv['author_name'] ) { + $this->is_author = true; + } + + if ( ! empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) { + $post_type_obj = get_post_type_object( $qv['post_type'] ); + if ( ! empty( $post_type_obj->has_archive ) ) { + $this->is_post_type_archive = true; + } + } + + if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) { + $this->is_archive = true; + } + } + + if ( '' != $qv['feed'] ) { + $this->is_feed = true; + } + + if ( '' != $qv['embed'] ) { + $this->is_embed = true; + } + + if ( '' != $qv['tb'] ) { + $this->is_trackback = true; + } + + if ( '' != $qv['paged'] && ( (int) $qv['paged'] > 1 ) ) { + $this->is_paged = true; + } + + // If we're previewing inside the write screen. + if ( '' != $qv['preview'] ) { + $this->is_preview = true; + } + + if ( is_admin() ) { + $this->is_admin = true; + } + + if ( false !== strpos( $qv['feed'], 'comments-' ) ) { + $qv['feed'] = str_replace( 'comments-', '', $qv['feed'] ); + $qv['withcomments'] = 1; + } + + $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; + + if ( $this->is_feed && ( ! empty( $qv['withcomments'] ) || ( empty( $qv['withoutcomments'] ) && $this->is_singular ) ) ) { + $this->is_comment_feed = true; + } + + if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed + || ( defined( 'REST_REQUEST' ) && REST_REQUEST && $this->is_main_query() ) + || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) { + $this->is_home = true; + } + + // Correct `is_*` for 'page_on_front' and 'page_for_posts'. + if ( $this->is_home && 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ) { + $_query = wp_parse_args( $this->query ); + // 'pagename' can be set and empty depending on matched rewrite rules. Ignore an empty 'pagename'. + if ( isset( $_query['pagename'] ) && '' === $_query['pagename'] ) { + unset( $_query['pagename'] ); + } + + unset( $_query['embed'] ); + + if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage' ) ) ) { + $this->is_page = true; + $this->is_home = false; + $qv['page_id'] = get_option( 'page_on_front' ); + // Correct for 'page_on_front'. + if ( ! empty( $qv['paged'] ) ) { + $qv['page'] = $qv['paged']; + unset( $qv['paged'] ); + } + } + } + + if ( '' !== $qv['pagename'] ) { + $this->queried_object = get_page_by_path( $qv['pagename'] ); + + if ( $this->queried_object && 'attachment' === $this->queried_object->post_type ) { + if ( preg_match( '/^[^%]*%(?:postname)%/', get_option( 'permalink_structure' ) ) ) { + // See if we also have a post with the same slug. + $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' ); + if ( $post ) { + $this->queried_object = $post; + $this->is_page = false; + $this->is_single = true; + } + } + } + + if ( ! empty( $this->queried_object ) ) { + $this->queried_object_id = (int) $this->queried_object->ID; + } else { + unset( $this->queried_object ); + } + + if ( 'page' === get_option( 'show_on_front' ) && isset( $this->queried_object_id ) && get_option( 'page_for_posts' ) == $this->queried_object_id ) { + $this->is_page = false; + $this->is_home = true; + $this->is_posts_page = true; + } + + if ( isset( $this->queried_object_id ) && get_option( 'wp_page_for_privacy_policy' ) == $this->queried_object_id ) { + $this->is_privacy_policy = true; + } + } + + if ( $qv['page_id'] ) { + if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) == $qv['page_id'] ) { + $this->is_page = false; + $this->is_home = true; + $this->is_posts_page = true; + } + + if ( get_option( 'wp_page_for_privacy_policy' ) == $qv['page_id'] ) { + $this->is_privacy_policy = true; + } + } + + if ( ! empty( $qv['post_type'] ) ) { + if ( is_array( $qv['post_type'] ) ) { + $qv['post_type'] = array_map( 'sanitize_key', $qv['post_type'] ); + } else { + $qv['post_type'] = sanitize_key( $qv['post_type'] ); + } + } + + if ( ! empty( $qv['post_status'] ) ) { + if ( is_array( $qv['post_status'] ) ) { + $qv['post_status'] = array_map( 'sanitize_key', $qv['post_status'] ); + } else { + $qv['post_status'] = preg_replace( '|[^a-z0-9_,-]|', '', $qv['post_status'] ); + } + } + + if ( $this->is_posts_page && ( ! isset( $qv['withcomments'] ) || ! $qv['withcomments'] ) ) { + $this->is_comment_feed = false; + } + + $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; + // Done correcting `is_*` for 'page_on_front' and 'page_for_posts'. + + if ( '404' == $qv['error'] ) { + $this->set_404(); + } + + $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 ); + + $this->query_vars_hash = md5( serialize( $this->query_vars ) ); + $this->query_vars_changed = false; + + + do_action_ref_array( 'parse_query', array( &$this ) ); + } + + public function parse_tax_query( &$q ) { + if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) { + $tax_query = $q['tax_query']; + } else { + $tax_query = array(); + } + + if ( ! empty( $q['taxonomy'] ) && ! empty( $q['term'] ) ) { + $tax_query[] = array( + 'taxonomy' => $q['taxonomy'], + 'terms' => array( $q['term'] ), + 'field' => 'slug', + ); + } + + foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) { + if ( 'post_tag' === $taxonomy ) { + continue; // Handled further down in the $q['tag'] block. + } + + if ( $t->query_var && ! empty( $q[ $t->query_var ] ) ) { + $tax_query_defaults = array( + 'taxonomy' => $taxonomy, + 'field' => 'slug', + ); + + if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) { + $q[ $t->query_var ] = wp_basename( $q[ $t->query_var ] ); + } + + $term = $q[ $t->query_var ]; + + if ( is_array( $term ) ) { + $term = implode( ',', $term ); + } + + if ( strpos( $term, '+' ) !== false ) { + $terms = preg_split( '/[+]+/', $term ); + foreach ( $terms as $term ) { + $tax_query[] = array_merge( + $tax_query_defaults, + array( + 'terms' => array( $term ), + ) + ); + } + } else { + $tax_query[] = array_merge( + $tax_query_defaults, + array( + 'terms' => preg_split( '/[,]+/', $term ), + ) + ); + } + } + } + + // If query string 'cat' is an array, implode it. + if ( is_array( $q['cat'] ) ) { + $q['cat'] = implode( ',', $q['cat'] ); + } + + // Category stuff. + + if ( ! empty( $q['cat'] ) && ! $this->is_singular ) { + $cat_in = array(); + $cat_not_in = array(); + + $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) ); + $cat_array = array_map( 'intval', $cat_array ); + $q['cat'] = implode( ',', $cat_array ); + + foreach ( $cat_array as $cat ) { + if ( $cat > 0 ) { + $cat_in[] = $cat; + } elseif ( $cat < 0 ) { + $cat_not_in[] = abs( $cat ); + } + } + + if ( ! empty( $cat_in ) ) { + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $cat_in, + 'field' => 'term_id', + 'include_children' => true, + ); + } + + if ( ! empty( $cat_not_in ) ) { + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $cat_not_in, + 'field' => 'term_id', + 'operator' => 'NOT IN', + 'include_children' => true, + ); + } + unset( $cat_array, $cat_in, $cat_not_in ); + } + + if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) { + $q['category__and'] = (array) $q['category__and']; + if ( ! isset( $q['category__in'] ) ) { + $q['category__in'] = array(); + } + $q['category__in'][] = absint( reset( $q['category__and'] ) ); + unset( $q['category__and'] ); + } + + if ( ! empty( $q['category__in'] ) ) { + $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $q['category__in'], + 'field' => 'term_id', + 'include_children' => false, + ); + } + + if ( ! empty( $q['category__not_in'] ) ) { + $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $q['category__not_in'], + 'operator' => 'NOT IN', + 'include_children' => false, + ); + } + + if ( ! empty( $q['category__and'] ) ) { + $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => $q['category__and'], + 'field' => 'term_id', + 'operator' => 'AND', + 'include_children' => false, + ); + } + + // If query string 'tag' is array, implode it. + if ( is_array( $q['tag'] ) ) { + $q['tag'] = implode( ',', $q['tag'] ); + } + + // Tag stuff. + + if ( '' !== $q['tag'] && ! $this->is_singular && $this->query_vars_changed ) { + if ( strpos( $q['tag'], ',' ) !== false ) { + $tags = preg_split( '/[,\r\n\t ]+/', $q['tag'] ); + foreach ( (array) $tags as $tag ) { + $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); + $q['tag_slug__in'][] = $tag; + } + } elseif ( preg_match( '/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) { + $tags = preg_split( '/[+\r\n\t ]+/', $q['tag'] ); + foreach ( (array) $tags as $tag ) { + $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); + $q['tag_slug__and'][] = $tag; + } + } else { + $q['tag'] = sanitize_term_field( 'slug', $q['tag'], 0, 'post_tag', 'db' ); + $q['tag_slug__in'][] = $q['tag']; + } + } + + if ( ! empty( $q['tag_id'] ) ) { + $q['tag_id'] = absint( $q['tag_id'] ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_id'], + ); + } + + if ( ! empty( $q['tag__in'] ) ) { + $q['tag__in'] = array_map( 'absint', array_unique( (array) $q['tag__in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag__in'], + ); + } + + if ( ! empty( $q['tag__not_in'] ) ) { + $q['tag__not_in'] = array_map( 'absint', array_unique( (array) $q['tag__not_in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag__not_in'], + 'operator' => 'NOT IN', + ); + } + + if ( ! empty( $q['tag__and'] ) ) { + $q['tag__and'] = array_map( 'absint', array_unique( (array) $q['tag__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag__and'], + 'operator' => 'AND', + ); + } + + if ( ! empty( $q['tag_slug__in'] ) ) { + $q['tag_slug__in'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_slug__in'], + 'field' => 'slug', + ); + } + + if ( ! empty( $q['tag_slug__and'] ) ) { + $q['tag_slug__and'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) ); + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => $q['tag_slug__and'], + 'field' => 'slug', + 'operator' => 'AND', + ); + } + + $this->tax_query = new WP_Tax_Query( $tax_query ); + + do_action( 'parse_tax_query', $this ); + } + + protected function parse_search( &$q ) { + global $wpdb; + + $search = ''; + + // Added slashes screw with quote grouping when done early, so done later. + $q['s'] = stripslashes( $q['s'] ); + if ( empty( $_GET['s'] ) && $this->is_main_query() ) { + $q['s'] = urldecode( $q['s'] ); + } + // There are no line breaks in fields. + $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] ); + $q['search_terms_count'] = 1; + if ( ! empty( $q['sentence'] ) ) { + $q['search_terms'] = array( $q['s'] ); + } else { + if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) { + $q['search_terms_count'] = count( $matches[0] ); + $q['search_terms'] = $this->parse_search_terms( $matches[0] ); + // If the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence. + if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ) { + $q['search_terms'] = array( $q['s'] ); + } + } else { + $q['search_terms'] = array( $q['s'] ); + } + } + + $n = ! empty( $q['exact'] ) ? '' : '%'; + $searchand = ''; + $q['search_orderby_title'] = array(); + + $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' ); + + foreach ( $q['search_terms'] as $term ) { + // If there is an $exclusion_prefix, terms prefixed with it should be excluded. + $exclude = $exclusion_prefix && ( substr( $term, 0, 1 ) === $exclusion_prefix ); + if ( $exclude ) { + $like_op = 'NOT LIKE'; + $andor_op = 'AND'; + $term = substr( $term, 1 ); + } else { + $like_op = 'LIKE'; + $andor_op = 'OR'; + } + + if ( $n && ! $exclude ) { + $like = '%' . $wpdb->esc_like( $term ) . '%'; + $q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like ); + } + + $like = $n . $wpdb->esc_like( $term ) . $n; + $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like ); + $searchand = ' AND '; + } + + if ( ! empty( $search ) ) { + $search = " AND ({$search}) "; + if ( ! is_user_logged_in() ) { + $search .= " AND ({$wpdb->posts}.post_password = '') "; + } + } + + return $search; + } + + protected function parse_search_terms( $terms ) { + $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower'; + $checked = array(); + + $stopwords = $this->get_search_stopwords(); + + foreach ( $terms as $term ) { + // Keep before/after spaces when term is for exact match. + if ( preg_match( '/^".+"$/', $term ) ) { + $term = trim( $term, "\"'" ); + } else { + $term = trim( $term, "\"' " ); + } + + // Avoid single A-Z and single dashes. + if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) ) { + continue; + } + + if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) ) { + continue; + } + + $checked[] = $term; + } + + return $checked; + } + + protected function get_search_stopwords() { + if ( isset( $this->stopwords ) ) { + return $this->stopwords; + } + + $words = explode( + ',', + _x( + 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www', + 'Comma-separated list of search stopwords in your language' + ) + ); + + $stopwords = array(); + foreach ( $words as $word ) { + $word = trim( $word, "\r\n\t " ); + if ( $word ) { + $stopwords[] = $word; + } + } + + $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); + return $this->stopwords; + } + + protected function parse_search_order( &$q ) { + global $wpdb; + + if ( $q['search_terms_count'] > 1 ) { + $num_terms = count( $q['search_orderby_title'] ); + + // If the search terms contain negative queries, don't bother ordering by sentence matches. + $like = ''; + if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) { + $like = '%' . $wpdb->esc_like( $q['s'] ) . '%'; + } + + $search_orderby = ''; + + // Sentence match in 'post_title'. + if ( $like ) { + $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like ); + } + + // Sanity limit, sort as sentence when more than 6 terms + // (few searches are longer than 6 terms and most titles are not). + if ( $num_terms < 7 ) { + // All words in title. + $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 '; + // Any word in title, not needed when $num_terms == 1. + if ( $num_terms > 1 ) { + $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 '; + } + } + + // Sentence match in 'post_content' and 'post_excerpt'. + if ( $like ) { + $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like ); + $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like ); + } + + if ( $search_orderby ) { + $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)'; + } + } else { + // Single word or sentence search. + $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC'; + } + + return $search_orderby; + } + + protected function parse_orderby( $orderby ) { + global $wpdb; + + // Used to filter values. + $allowed_keys = array( + 'post_name', + 'post_author', + 'post_date', + 'post_title', + 'post_modified', + 'post_parent', + 'post_type', + 'name', + 'author', + 'date', + 'title', + 'modified', + 'parent', + 'type', + 'ID', + 'menu_order', + 'comment_count', + 'rand', + 'post__in', + 'post_parent__in', + 'post_name__in', + ); + + $primary_meta_key = ''; + $primary_meta_query = false; + $meta_clauses = $this->meta_query->get_clauses(); + if ( ! empty( $meta_clauses ) ) { + $primary_meta_query = reset( $meta_clauses ); + + if ( ! empty( $primary_meta_query['key'] ) ) { + $primary_meta_key = $primary_meta_query['key']; + $allowed_keys[] = $primary_meta_key; + } + + $allowed_keys[] = 'meta_value'; + $allowed_keys[] = 'meta_value_num'; + $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); + } + + // If RAND() contains a seed value, sanitize and add to allowed keys. + $rand_with_seed = false; + if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { + $orderby = sprintf( 'RAND(%s)', (int) $matches[1] ); + $allowed_keys[] = $orderby; + $rand_with_seed = true; + } + + if ( ! in_array( $orderby, $allowed_keys, true ) ) { + return false; + } + + $orderby_clause = ''; + + switch ( $orderby ) { + case 'post_name': + case 'post_author': + case 'post_date': + case 'post_title': + case 'post_modified': + case 'post_parent': + case 'post_type': + case 'ID': + case 'menu_order': + case 'comment_count': + $orderby_clause = "{$wpdb->posts}.{$orderby}"; + break; + case 'rand': + $orderby_clause = 'RAND()'; + break; + case $primary_meta_key: + case 'meta_value': + if ( ! empty( $primary_meta_query['type'] ) ) { + $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})"; + } else { + $orderby_clause = "{$primary_meta_query['alias']}.meta_value"; + } + break; + case 'meta_value_num': + $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0"; + break; + case 'post__in': + if ( ! empty( $this->query_vars['post__in'] ) ) { + $orderby_clause = "FIELD({$wpdb->posts}.ID," . implode( ',', array_map( 'absint', $this->query_vars['post__in'] ) ) . ')'; + } + break; + case 'post_parent__in': + if ( ! empty( $this->query_vars['post_parent__in'] ) ) { + $orderby_clause = "FIELD( {$wpdb->posts}.post_parent," . implode( ', ', array_map( 'absint', $this->query_vars['post_parent__in'] ) ) . ' )'; + } + break; + case 'post_name__in': + if ( ! empty( $this->query_vars['post_name__in'] ) ) { + $post_name__in = array_map( 'sanitize_title_for_query', $this->query_vars['post_name__in'] ); + $post_name__in_string = "'" . implode( "','", $post_name__in ) . "'"; + $orderby_clause = "FIELD( {$wpdb->posts}.post_name," . $post_name__in_string . ' )'; + } + break; + default: + if ( array_key_exists( $orderby, $meta_clauses ) ) { + // $orderby corresponds to a meta_query clause. + $meta_clause = $meta_clauses[ $orderby ]; + $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; + } elseif ( $rand_with_seed ) { + $orderby_clause = $orderby; + } else { + // Default: order by post field. + $orderby_clause = "{$wpdb->posts}.post_" . sanitize_key( $orderby ); + } + + break; + } + + return $orderby_clause; + } + + protected function parse_order( $order ) { + if ( ! is_string( $order ) || empty( $order ) ) { + return 'DESC'; + } + + if ( 'ASC' === strtoupper( $order ) ) { + return 'ASC'; + } else { + return 'DESC'; + } + } + + public function set_404() { + $is_feed = $this->is_feed; + + $this->init_query_flags(); + $this->is_404 = true; + + $this->is_feed = $is_feed; + + do_action_ref_array( 'set_404', array( $this ) ); + } + + public function get( $query_var, $default = '' ) { + if ( isset( $this->query_vars[ $query_var ] ) ) { + return $this->query_vars[ $query_var ]; + } + + return $default; + } + + public function set( $query_var, $value ) { + $this->query_vars[ $query_var ] = $value; + } + + public function get_posts() { + global $wpdb; + + $this->parse_query(); + + do_action_ref_array( 'pre_get_posts', array( &$this ) ); + + // Shorthand. + $q = &$this->query_vars; + + // Fill again in case 'pre_get_posts' unset some vars. + $q = $this->fill_query_vars( $q ); + + // Parse meta query. + $this->meta_query = new WP_Meta_Query(); + $this->meta_query->parse_query_vars( $q ); + + // Set a flag if a 'pre_get_posts' hook changed the query vars. + $hash = md5( serialize( $this->query_vars ) ); + if ( $hash != $this->query_vars_hash ) { + $this->query_vars_changed = true; + $this->query_vars_hash = $hash; + } + unset( $hash ); + + // First let's clear some variables. + $distinct = ''; + $whichauthor = ''; + $whichmimetype = ''; + $where = ''; + $limits = ''; + $join = ''; + $search = ''; + $groupby = ''; + $post_status_join = false; + $page = 1; + + if ( isset( $q['caller_get_posts'] ) ) { + _deprecated_argument( + 'WP_Query', + '3.1.0', + sprintf( + __( '%1$s is deprecated. Use %2$s instead.' ), + 'caller_get_posts', + 'ignore_sticky_posts' + ) + ); + + if ( ! isset( $q['ignore_sticky_posts'] ) ) { + $q['ignore_sticky_posts'] = $q['caller_get_posts']; + } + } + + if ( ! isset( $q['ignore_sticky_posts'] ) ) { + $q['ignore_sticky_posts'] = false; + } + + if ( ! isset( $q['suppress_filters'] ) ) { + $q['suppress_filters'] = false; + } + + if ( ! isset( $q['cache_results'] ) ) { + if ( wp_using_ext_object_cache() ) { + $q['cache_results'] = false; + } else { + $q['cache_results'] = true; + } + } + + if ( ! isset( $q['update_post_term_cache'] ) ) { + $q['update_post_term_cache'] = true; + } + + if ( ! isset( $q['lazy_load_term_meta'] ) ) { + $q['lazy_load_term_meta'] = $q['update_post_term_cache']; + } + + if ( ! isset( $q['update_post_meta_cache'] ) ) { + $q['update_post_meta_cache'] = true; + } + + if ( ! isset( $q['post_type'] ) ) { + if ( $this->is_search ) { + $q['post_type'] = 'any'; + } else { + $q['post_type'] = ''; + } + } + $post_type = $q['post_type']; + if ( empty( $q['posts_per_page'] ) ) { + $q['posts_per_page'] = get_option( 'posts_per_page' ); + } + if ( isset( $q['showposts'] ) && $q['showposts'] ) { + $q['showposts'] = (int) $q['showposts']; + $q['posts_per_page'] = $q['showposts']; + } + if ( ( isset( $q['posts_per_archive_page'] ) && 0 != $q['posts_per_archive_page'] ) && ( $this->is_archive || $this->is_search ) ) { + $q['posts_per_page'] = $q['posts_per_archive_page']; + } + if ( ! isset( $q['nopaging'] ) ) { + if ( -1 == $q['posts_per_page'] ) { + $q['nopaging'] = true; + } else { + $q['nopaging'] = false; + } + } + + if ( $this->is_feed ) { + // This overrides 'posts_per_page'. + if ( ! empty( $q['posts_per_rss'] ) ) { + $q['posts_per_page'] = $q['posts_per_rss']; + } else { + $q['posts_per_page'] = get_option( 'posts_per_rss' ); + } + $q['nopaging'] = false; + } + $q['posts_per_page'] = (int) $q['posts_per_page']; + if ( $q['posts_per_page'] < -1 ) { + $q['posts_per_page'] = abs( $q['posts_per_page'] ); + } elseif ( 0 == $q['posts_per_page'] ) { + $q['posts_per_page'] = 1; + } + + if ( ! isset( $q['comments_per_page'] ) || 0 == $q['comments_per_page'] ) { + $q['comments_per_page'] = get_option( 'comments_per_page' ); + } + + if ( $this->is_home && ( empty( $this->query ) || 'true' === $q['preview'] ) && ( 'page' === get_option( 'show_on_front' ) ) && get_option( 'page_on_front' ) ) { + $this->is_page = true; + $this->is_home = false; + $q['page_id'] = get_option( 'page_on_front' ); + } + + if ( isset( $q['page'] ) ) { + $q['page'] = trim( $q['page'], '/' ); + $q['page'] = absint( $q['page'] ); + } + + // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. + if ( isset( $q['no_found_rows'] ) ) { + $q['no_found_rows'] = (bool) $q['no_found_rows']; + } else { + $q['no_found_rows'] = false; + } + + switch ( $q['fields'] ) { + case 'ids': + $fields = "{$wpdb->posts}.ID"; + break; + case 'id=>parent': + $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent"; + break; + default: + $fields = "{$wpdb->posts}.*"; + } + + if ( '' !== $q['menu_order'] ) { + $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order']; + } + // The "m" parameter is meant for months but accepts datetimes of varying specificity. + if ( $q['m'] ) { + $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr( $q['m'], 0, 4 ); + if ( strlen( $q['m'] ) > 5 ) { + $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 4, 2 ); + } + if ( strlen( $q['m'] ) > 7 ) { + $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 6, 2 ); + } + if ( strlen( $q['m'] ) > 9 ) { + $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr( $q['m'], 8, 2 ); + } + if ( strlen( $q['m'] ) > 11 ) { + $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr( $q['m'], 10, 2 ); + } + if ( strlen( $q['m'] ) > 13 ) { + $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr( $q['m'], 12, 2 ); + } + } + + // Handle the other individual date parameters. + $date_parameters = array(); + + if ( '' !== $q['hour'] ) { + $date_parameters['hour'] = $q['hour']; + } + + if ( '' !== $q['minute'] ) { + $date_parameters['minute'] = $q['minute']; + } + + if ( '' !== $q['second'] ) { + $date_parameters['second'] = $q['second']; + } + + if ( $q['year'] ) { + $date_parameters['year'] = $q['year']; + } + + if ( $q['monthnum'] ) { + $date_parameters['monthnum'] = $q['monthnum']; + } + + if ( $q['w'] ) { + $date_parameters['week'] = $q['w']; + } + + if ( $q['day'] ) { + $date_parameters['day'] = $q['day']; + } + + if ( $date_parameters ) { + $date_query = new WP_Date_Query( array( $date_parameters ) ); + $where .= $date_query->get_sql(); + } + unset( $date_parameters, $date_query ); + + // Handle complex date queries. + if ( ! empty( $q['date_query'] ) ) { + $this->date_query = new WP_Date_Query( $q['date_query'] ); + $where .= $this->date_query->get_sql(); + } + + // If we've got a post_type AND it's not "any" post_type. + if ( ! empty( $q['post_type'] ) && 'any' !== $q['post_type'] ) { + foreach ( (array) $q['post_type'] as $_post_type ) { + $ptype_obj = get_post_type_object( $_post_type ); + if ( ! $ptype_obj || ! $ptype_obj->query_var || empty( $q[ $ptype_obj->query_var ] ) ) { + continue; + } + + if ( ! $ptype_obj->hierarchical ) { + // Non-hierarchical post types can directly use 'name'. + $q['name'] = $q[ $ptype_obj->query_var ]; + } else { + // Hierarchical post types will operate through 'pagename'. + $q['pagename'] = $q[ $ptype_obj->query_var ]; + $q['name'] = ''; + } + + // Only one request for a slug is possible, this is why name & pagename are overwritten above. + break; + } // End foreach. + unset( $ptype_obj ); + } + + if ( '' !== $q['title'] ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) ); + } + + // Parameters related to 'post_name'. + if ( '' !== $q['name'] ) { + $q['name'] = sanitize_title_for_query( $q['name'] ); + $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; + } elseif ( '' !== $q['pagename'] ) { + if ( isset( $this->queried_object_id ) ) { + $reqpage = $this->queried_object_id; + } else { + if ( 'page' !== $q['post_type'] ) { + foreach ( (array) $q['post_type'] as $_post_type ) { + $ptype_obj = get_post_type_object( $_post_type ); + if ( ! $ptype_obj || ! $ptype_obj->hierarchical ) { + continue; + } + + $reqpage = get_page_by_path( $q['pagename'], OBJECT, $_post_type ); + if ( $reqpage ) { + break; + } + } + unset( $ptype_obj ); + } else { + $reqpage = get_page_by_path( $q['pagename'] ); + } + if ( ! empty( $reqpage ) ) { + $reqpage = $reqpage->ID; + } else { + $reqpage = 0; + } + } + + $page_for_posts = get_option( 'page_for_posts' ); + if ( ( 'page' !== get_option( 'show_on_front' ) ) || empty( $page_for_posts ) || ( $reqpage != $page_for_posts ) ) { + $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); + $q['name'] = $q['pagename']; + $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; + $reqpage_obj = get_post( $reqpage ); + if ( is_object( $reqpage_obj ) && 'attachment' === $reqpage_obj->post_type ) { + $this->is_attachment = true; + $post_type = 'attachment'; + $q['post_type'] = 'attachment'; + $this->is_page = true; + $q['attachment_id'] = $reqpage; + } + } + } elseif ( '' !== $q['attachment'] ) { + $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); + $q['name'] = $q['attachment']; + $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; + } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { + $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); + $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; + $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; + } + + // If an attachment is requested by number, let it supersede any post number. + if ( $q['attachment_id'] ) { + $q['p'] = absint( $q['attachment_id'] ); + } + + // If a post number is specified, load that post. + if ( $q['p'] ) { + $where .= " AND {$wpdb->posts}.ID = " . $q['p']; + } elseif ( $q['post__in'] ) { + $post__in = implode( ',', array_map( 'absint', $q['post__in'] ) ); + $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; + } elseif ( $q['post__not_in'] ) { + $post__not_in = implode( ',', array_map( 'absint', $q['post__not_in'] ) ); + $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; + } + + if ( is_numeric( $q['post_parent'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); + } elseif ( $q['post_parent__in'] ) { + $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); + $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; + } elseif ( $q['post_parent__not_in'] ) { + $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) ); + $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; + } + + if ( $q['page_id'] ) { + if ( ( 'page' !== get_option( 'show_on_front' ) ) || ( get_option( 'page_for_posts' ) != $q['page_id'] ) ) { + $q['p'] = $q['page_id']; + $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; + } + } + + // If a search pattern is specified, load the posts that match. + if ( strlen( $q['s'] ) ) { + $search = $this->parse_search( $q ); + } + + if ( ! $q['suppress_filters'] ) { + + $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); + } + + // Taxonomies. + if ( ! $this->is_singular ) { + $this->parse_tax_query( $q ); + + $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); + + $join .= $clauses['join']; + $where .= $clauses['where']; + } + + if ( $this->is_tax ) { + if ( empty( $post_type ) ) { + // Do a fully inclusive search for currently registered post types of queried taxonomies. + $post_type = array(); + $taxonomies = array_keys( $this->tax_query->queried_terms ); + foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) { + $object_taxonomies = 'attachment' === $pt ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt ); + if ( array_intersect( $taxonomies, $object_taxonomies ) ) { + $post_type[] = $pt; + } + } + if ( ! $post_type ) { + $post_type = 'any'; + } elseif ( count( $post_type ) == 1 ) { + $post_type = $post_type[0]; + } + + $post_status_join = true; + } elseif ( in_array( 'attachment', (array) $post_type, true ) ) { + $post_status_join = true; + } + } + + if ( ! empty( $this->tax_query->queried_terms ) ) { + + if ( ! isset( $q['taxonomy'] ) ) { + foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { + if ( empty( $queried_items['terms'][0] ) ) { + continue; + } + + if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ), true ) ) { + $q['taxonomy'] = $queried_taxonomy; + + if ( 'slug' === $queried_items['field'] ) { + $q['term'] = $queried_items['terms'][0]; + } else { + $q['term_id'] = $queried_items['terms'][0]; + } + + // Take the first one we find. + break; + } + } + } + + // 'cat', 'category_name', 'tag_id'. + foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { + if ( empty( $queried_items['terms'][0] ) ) { + continue; + } + + if ( 'category' === $queried_taxonomy ) { + $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' ); + if ( $the_cat ) { + $this->set( 'cat', $the_cat->term_id ); + $this->set( 'category_name', $the_cat->slug ); + } + unset( $the_cat ); + } + + if ( 'post_tag' === $queried_taxonomy ) { + $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' ); + if ( $the_tag ) { + $this->set( 'tag_id', $the_tag->term_id ); + } + unset( $the_tag ); + } + } + } + + if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) { + $groupby = "{$wpdb->posts}.ID"; + } + + // Author/user stuff. + + if ( ! empty( $q['author'] ) && '0' != $q['author'] ) { + $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); + $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) ); + foreach ( $authors as $author ) { + $key = $author > 0 ? 'author__in' : 'author__not_in'; + $q[ $key ][] = abs( $author ); + } + $q['author'] = implode( ',', $authors ); + } + + if ( ! empty( $q['author__not_in'] ) ) { + $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); + $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; + } elseif ( ! empty( $q['author__in'] ) ) { + $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); + $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; + } + + // Author stuff for nice URLs. + + if ( '' !== $q['author_name'] ) { + if ( strpos( $q['author_name'], '/' ) !== false ) { + $q['author_name'] = explode( '/', $q['author_name'] ); + if ( $q['author_name'][ count( $q['author_name'] ) - 1 ] ) { + $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 1 ]; // No trailing slash. + } else { + $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 2 ]; // There was a trailing slash. + } + } + $q['author_name'] = sanitize_title_for_query( $q['author_name'] ); + $q['author'] = get_user_by( 'slug', $q['author_name'] ); + if ( $q['author'] ) { + $q['author'] = $q['author']->ID; + } + $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint( $q['author'] ) . ')'; + } + + // Matching by comment count. + if ( isset( $q['comment_count'] ) ) { + // Numeric comment count is converted to array format. + if ( is_numeric( $q['comment_count'] ) ) { + $q['comment_count'] = array( + 'value' => (int) $q['comment_count'], + ); + } + + if ( isset( $q['comment_count']['value'] ) ) { + $q['comment_count'] = array_merge( + array( + 'compare' => '=', + ), + $q['comment_count'] + ); + + // Fallback for invalid compare operators is '='. + $compare_operators = array( '=', '!=', '>', '>=', '<', '<=' ); + if ( ! in_array( $q['comment_count']['compare'], $compare_operators, true ) ) { + $q['comment_count']['compare'] = '='; + } + + $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count {$q['comment_count']['compare']} %d", $q['comment_count']['value'] ); + } + } + + // MIME-Type stuff for attachment browsing. + + if ( isset( $q['post_mime_type'] ) && '' !== $q['post_mime_type'] ) { + $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts ); + } + $where .= $search . $whichauthor . $whichmimetype; + + if ( ! empty( $this->meta_query->queries ) ) { + $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this ); + $join .= $clauses['join']; + $where .= $clauses['where']; + } + + $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] ); + if ( ! isset( $q['order'] ) ) { + $q['order'] = $rand ? '' : 'DESC'; + } else { + $q['order'] = $rand ? '' : $this->parse_order( $q['order'] ); + } + + // These values of orderby should ignore the 'order' parameter. + $force_asc = array( 'post__in', 'post_name__in', 'post_parent__in' ); + if ( isset( $q['orderby'] ) && in_array( $q['orderby'], $force_asc, true ) ) { + $q['order'] = ''; + } + + // Order by. + if ( empty( $q['orderby'] ) ) { + + if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) { + $orderby = ''; + } else { + $orderby = "{$wpdb->posts}.post_date " . $q['order']; + } + } elseif ( 'none' === $q['orderby'] ) { + $orderby = ''; + } else { + $orderby_array = array(); + if ( is_array( $q['orderby'] ) ) { + foreach ( $q['orderby'] as $_orderby => $order ) { + $orderby = addslashes_gpc( urldecode( $_orderby ) ); + $parsed = $this->parse_orderby( $orderby ); + + if ( ! $parsed ) { + continue; + } + + $orderby_array[] = $parsed . ' ' . $this->parse_order( $order ); + } + $orderby = implode( ', ', $orderby_array ); + + } else { + $q['orderby'] = urldecode( $q['orderby'] ); + $q['orderby'] = addslashes_gpc( $q['orderby'] ); + + foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) { + $parsed = $this->parse_orderby( $orderby ); + // Only allow certain values for safety. + if ( ! $parsed ) { + continue; + } + + $orderby_array[] = $parsed; + } + $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); + + if ( empty( $orderby ) ) { + $orderby = "{$wpdb->posts}.post_date " . $q['order']; + } elseif ( ! empty( $q['order'] ) ) { + $orderby .= " {$q['order']}"; + } + } + } + + // Order search results by relevance only when another "orderby" is not specified in the query. + if ( ! empty( $q['s'] ) ) { + $search_orderby = ''; + if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) ) { + $search_orderby = $this->parse_search_order( $q ); + } + + if ( ! $q['suppress_filters'] ) { + + $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); + } + + if ( $search_orderby ) { + $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; + } + } + + if ( is_array( $post_type ) && count( $post_type ) > 1 ) { + $post_type_cap = 'multiple_post_type'; + } else { + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $post_type_object = get_post_type_object( $post_type ); + if ( empty( $post_type_object ) ) { + $post_type_cap = $post_type; + } + } + + if ( isset( $q['post_password'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $q['post_password'] ); + if ( empty( $q['perm'] ) ) { + $q['perm'] = 'readable'; + } + } elseif ( isset( $q['has_password'] ) ) { + $where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' ); + } + + if ( ! empty( $q['comment_status'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] ); + } + + if ( ! empty( $q['ping_status'] ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] ); + } + + if ( 'any' === $post_type ) { + $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) ); + if ( empty( $in_search_post_types ) ) { + $where .= ' AND 1=0 '; + } else { + $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; + } + } elseif ( ! empty( $post_type ) && is_array( $post_type ) ) { + $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')"; + } elseif ( ! empty( $post_type ) ) { + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); + $post_type_object = get_post_type_object( $post_type ); + } elseif ( $this->is_attachment ) { + $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; + $post_type_object = get_post_type_object( 'attachment' ); + } elseif ( $this->is_page ) { + $where .= " AND {$wpdb->posts}.post_type = 'page'"; + $post_type_object = get_post_type_object( 'page' ); + } else { + $where .= " AND {$wpdb->posts}.post_type = 'post'"; + $post_type_object = get_post_type_object( 'post' ); + } + + $edit_cap = 'edit_post'; + $read_cap = 'read_post'; + + if ( ! empty( $post_type_object ) ) { + $edit_others_cap = $post_type_object->cap->edit_others_posts; + $read_private_cap = $post_type_object->cap->read_private_posts; + } else { + $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; + $read_private_cap = 'read_private_' . $post_type_cap . 's'; + } + + $user_id = get_current_user_id(); + + $q_status = array(); + if ( ! empty( $q['post_status'] ) ) { + $statuswheres = array(); + $q_status = $q['post_status']; + if ( ! is_array( $q_status ) ) { + $q_status = explode( ',', $q_status ); + } + $r_status = array(); + $p_status = array(); + $e_status = array(); + if ( in_array( 'any', $q_status, true ) ) { + foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { + if ( ! in_array( $status, $q_status, true ) ) { + $e_status[] = "{$wpdb->posts}.post_status <> '$status'"; + } + } + } else { + foreach ( get_post_stati() as $status ) { + if ( in_array( $status, $q_status, true ) ) { + if ( 'private' === $status ) { + $p_status[] = "{$wpdb->posts}.post_status = '$status'"; + } else { + $r_status[] = "{$wpdb->posts}.post_status = '$status'"; + } + } + } + } + + if ( empty( $q['perm'] ) || 'readable' !== $q['perm'] ) { + $r_status = array_merge( $r_status, $p_status ); + unset( $p_status ); + } + + if ( ! empty( $e_status ) ) { + $statuswheres[] = '(' . implode( ' AND ', $e_status ) . ')'; + } + if ( ! empty( $r_status ) ) { + if ( ! empty( $q['perm'] ) && 'editable' === $q['perm'] && ! current_user_can( $edit_others_cap ) ) { + $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $r_status ) . '))'; + } else { + $statuswheres[] = '(' . implode( ' OR ', $r_status ) . ')'; + } + } + if ( ! empty( $p_status ) ) { + if ( ! empty( $q['perm'] ) && 'readable' === $q['perm'] && ! current_user_can( $read_private_cap ) ) { + $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $p_status ) . '))'; + } else { + $statuswheres[] = '(' . implode( ' OR ', $p_status ) . ')'; + } + } + if ( $post_status_join ) { + $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; + foreach ( $statuswheres as $index => $statuswhere ) { + $statuswheres[ $index ] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . '))'; + } + } + $where_status = implode( ' OR ', $statuswheres ); + if ( ! empty( $where_status ) ) { + $where .= " AND ($where_status)"; + } + } elseif ( ! $this->is_singular ) { + $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; + + // Add public states. + $public_states = get_post_stati( array( 'public' => true ) ); + foreach ( (array) $public_states as $state ) { + if ( 'publish' === $state ) { // Publish is hard-coded above. + continue; + } + $where .= " OR {$wpdb->posts}.post_status = '$state'"; + } + + if ( $this->is_admin ) { + // Add protected states that should show in the admin all list. + $admin_all_states = get_post_stati( + array( + 'protected' => true, + 'show_in_admin_all_list' => true, + ) + ); + foreach ( (array) $admin_all_states as $state ) { + $where .= " OR {$wpdb->posts}.post_status = '$state'"; + } + } + + if ( is_user_logged_in() ) { + // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. + $private_states = get_post_stati( array( 'private' => true ) ); + foreach ( (array) $private_states as $state ) { + $where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'"; + } + } + + $where .= ')'; + } + + + if ( ! $q['suppress_filters'] ) { + + $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) ); + + + $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) ); + } + + // Paging. + if ( empty( $q['nopaging'] ) && ! $this->is_singular ) { + $page = absint( $q['paged'] ); + if ( ! $page ) { + $page = 1; + } + + // If 'offset' is provided, it takes precedence over 'paged'. + if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) { + $q['offset'] = absint( $q['offset'] ); + $pgstrt = $q['offset'] . ', '; + } else { + $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; + } + $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; + } + + // Comments feeds. + if ( $this->is_comment_feed && ! $this->is_singular ) { + if ( $this->is_archive || $this->is_search ) { + $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join "; + $cwhere = "WHERE comment_approved = '1' $where"; + $cgroupby = "{$wpdb->comments}.comment_id"; + } else { // Other non-singular, e.g. front. + $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; + $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'"; + $cgroupby = ''; + } + + if ( ! $q['suppress_filters'] ) { + + $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) ); + + + $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) ); + + + $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) ); + + + $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); + + + $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); + } + + $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; + $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; + $climits = ( ! empty( $climits ) ) ? $climits : ''; + + $comments = (array) $wpdb->get_results( "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits" ); + // Convert to WP_Comment. + $this->comments = array_map( 'get_comment', $comments ); + $this->comment_count = count( $this->comments ); + + $post_ids = array(); + + foreach ( $this->comments as $comment ) { + $post_ids[] = (int) $comment->comment_post_ID; + } + + $post_ids = implode( ',', $post_ids ); + $join = ''; + if ( $post_ids ) { + $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; + } else { + $where = 'AND 0'; + } + } + + $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); + + if ( ! $q['suppress_filters'] ) { + + $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); + + + $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); + + + $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); + + + $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); + + + $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); + + + $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); + + + $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); + + $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); + + $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; + $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; + $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; + $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; + $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; + $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; + $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; + } + + + do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join ); + + if ( ! $q['suppress_filters'] ) { + + $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); + + $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); + + $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); + + $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); + + $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) ); + + $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) ); + + $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); + + $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); + + $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; + $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; + $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; + $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; + $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; + $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; + $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; + } + + if ( ! empty( $groupby ) ) { + $groupby = 'GROUP BY ' . $groupby; + } + if ( ! empty( $orderby ) ) { + $orderby = 'ORDER BY ' . $orderby; + } + + $found_rows = ''; + if ( ! $q['no_found_rows'] && ! empty( $limits ) ) { + $found_rows = 'SQL_CALC_FOUND_ROWS'; + } + + $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + $this->request = $old_request; + + if ( ! $q['suppress_filters'] ) { + $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) ); + } + + $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) ); + + if ( 'ids' === $q['fields'] ) { + if ( null === $this->posts ) { + $this->posts = $wpdb->get_col( $this->request ); + } + + $this->posts = array_map( 'intval', $this->posts ); + $this->post_count = count( $this->posts ); + $this->set_found_posts( $q, $limits ); + + return $this->posts; + } + + if ( 'id=>parent' === $q['fields'] ) { + if ( null === $this->posts ) { + $this->posts = $wpdb->get_results( $this->request ); + } + + $this->post_count = count( $this->posts ); + $this->set_found_posts( $q, $limits ); + + $r = array(); + foreach ( $this->posts as $key => $post ) { + $this->posts[ $key ]->ID = (int) $post->ID; + $this->posts[ $key ]->post_parent = (int) $post->post_parent; + + $r[ (int) $post->ID ] = (int) $post->post_parent; + } + + return $r; + } + + if ( null === $this->posts ) { + $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" === $fields && ! empty( $limits ) && $q['posts_per_page'] < 500 ); + + + $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this ); + + if ( $split_the_query ) { + // First get the IDs and then fill in the objects. + + $this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + + $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); + + $ids = $wpdb->get_col( $this->request ); + + if ( $ids ) { + $this->posts = $ids; + $this->set_found_posts( $q, $limits ); + _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + } else { + $this->posts = array(); + } + } else { + $this->posts = $wpdb->get_results( $this->request ); + $this->set_found_posts( $q, $limits ); + } + } + + // Convert to WP_Post objects. + if ( $this->posts ) { + $this->posts = array_map( 'get_post', $this->posts ); + } + + if ( ! $q['suppress_filters'] ) { + + $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); + } + + if ( ! empty( $this->posts ) && $this->is_comment_feed && $this->is_singular ) { + $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) ); + + $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) ); + + $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) ); + $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; + + $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); + $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; + + $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); + + $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; + $comments = $wpdb->get_results( $comments_request ); + // Convert to WP_Comment. + $this->comments = array_map( 'get_comment', $comments ); + $this->comment_count = count( $this->comments ); + } + + // Check post status to determine if post should be displayed. + if ( ! empty( $this->posts ) && ( $this->is_single || $this->is_page ) ) { + $status = get_post_status( $this->posts[0] ); + + if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { + $this->is_page = false; + $this->is_single = true; + $this->is_attachment = true; + } + + // If the post_status was specifically requested, let it pass through. + if ( ! in_array( $status, $q_status, true ) ) { + $post_status_obj = get_post_status_object( $status ); + + if ( $post_status_obj && ! $post_status_obj->public ) { + if ( ! is_user_logged_in() ) { + // User must be logged in to view unpublished posts. + $this->posts = array(); + } else { + if ( $post_status_obj->protected ) { + // User must have edit permissions on the draft to preview. + if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } else { + $this->is_preview = true; + if ( 'future' !== $status ) { + $this->posts[0]->post_date = current_time( 'mysql' ); + } + } + } elseif ( $post_status_obj->private ) { + if ( ! current_user_can( $read_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } + } else { + $this->posts = array(); + } + } + } elseif ( ! $post_status_obj ) { + // Post status is not registered, assume it's not public. + if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts = array(); + } + } + } + + if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) { + $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); + } + } + + // Put sticky posts at the top of the posts array. + $sticky_posts = get_option( 'sticky_posts' ); + if ( $this->is_home && $page <= 1 && is_array( $sticky_posts ) && ! empty( $sticky_posts ) && ! $q['ignore_sticky_posts'] ) { + $num_posts = count( $this->posts ); + $sticky_offset = 0; + // Loop over posts and relocate stickies to the front. + for ( $i = 0; $i < $num_posts; $i++ ) { + if ( in_array( $this->posts[ $i ]->ID, $sticky_posts, true ) ) { + $sticky_post = $this->posts[ $i ]; + // Remove sticky from current position. + array_splice( $this->posts, $i, 1 ); + // Move to front, after other stickies. + array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); + // Increment the sticky offset. The next sticky will be placed at this offset. + $sticky_offset++; + // Remove post from sticky posts array. + $offset = array_search( $sticky_post->ID, $sticky_posts, true ); + unset( $sticky_posts[ $offset ] ); + } + } + + // If any posts have been excluded specifically, Ignore those that are sticky. + if ( ! empty( $sticky_posts ) && ! empty( $q['post__not_in'] ) ) { + $sticky_posts = array_diff( $sticky_posts, $q['post__not_in'] ); + } + + // Fetch sticky posts that weren't in the query results. + if ( ! empty( $sticky_posts ) ) { + $stickies = get_posts( + array( + 'post__in' => $sticky_posts, + 'post_type' => $post_type, + 'post_status' => 'publish', + 'nopaging' => true, + ) + ); + + foreach ( $stickies as $sticky_post ) { + array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); + $sticky_offset++; + } + } + } + + // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up. + if ( ! empty( $this->comments ) ) { + wp_queue_comments_for_comment_meta_lazyload( $this->comments ); + } + + if ( ! $q['suppress_filters'] ) { + $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) ); + } + + // Ensure that any posts added/modified via one of the filters above are + // of the type WP_Post and are filtered. + if ( $this->posts ) { + $this->post_count = count( $this->posts ); + + $this->posts = array_map( 'get_post', $this->posts ); + + if ( $q['cache_results'] ) { + update_post_caches( $this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + } + + $this->post = reset( $this->posts ); + } else { + $this->post_count = 0; + $this->posts = array(); + } + + if ( $q['lazy_load_term_meta'] ) { + wp_queue_posts_for_term_meta_lazyload( $this->posts ); + } + + return $this->posts; + } + + + private function set_found_posts( $q, $limits ) { + global $wpdb; + + // Bail if posts is an empty array. Continue if posts is an empty string, + // null, or false to accommodate caching plugins that fill posts later. + if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) ) { + return; + } + + if ( ! empty( $limits ) ) { + $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); + + $this->found_posts = (int) $wpdb->get_var( $found_posts_query ); + } else { + if ( is_array( $this->posts ) ) { + $this->found_posts = count( $this->posts ); + } else { + if ( null === $this->posts ) { + $this->found_posts = 0; + } else { + $this->found_posts = 1; + } + } + } + + $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); + + if ( ! empty( $limits ) ) { + $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); + } + } + + public function next_post() { + + $this->current_post++; + + $this->post = $this->posts[ $this->current_post ]; + return $this->post; + } + + public function the_post() { + global $post; + $this->in_the_loop = true; + + if ( -1 == $this->current_post ) { // Loop has just started. + do_action_ref_array( 'loop_start', array( &$this ) ); + } + + $post = $this->next_post(); + $this->setup_postdata( $post ); + } + + public function have_posts() { + if ( $this->current_post + 1 < $this->post_count ) { + return true; + } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) { + do_action_ref_array( 'loop_end', array( &$this ) ); + // Do some cleaning up after the loop. + $this->rewind_posts(); + } elseif ( 0 === $this->post_count ) { + do_action( 'loop_no_results', $this ); + } + + $this->in_the_loop = false; + return false; + } + + public function rewind_posts() { + $this->current_post = -1; + if ( $this->post_count > 0 ) { + $this->post = $this->posts[0]; + } + } + + public function next_comment() { + $this->current_comment++; + + $this->comment = $this->comments[ $this->current_comment ]; + return $this->comment; + } + + public function the_comment() { + global $comment; + + $comment = $this->next_comment(); + + if ( 0 == $this->current_comment ) { + do_action( 'comment_loop_start' ); + } + } + + public function have_comments() { + if ( $this->current_comment + 1 < $this->comment_count ) { + return true; + } elseif ( $this->current_comment + 1 == $this->comment_count ) { + $this->rewind_comments(); + } + + return false; + } + + public function rewind_comments() { + $this->current_comment = -1; + if ( $this->comment_count > 0 ) { + $this->comment = $this->comments[0]; + } + } + + public function query( $query ) { + $this->init(); + $this->query = wp_parse_args( $query ); + $this->query_vars = $this->query; + return $this->get_posts(); + } + + public function get_queried_object() { + if ( isset( $this->queried_object ) ) { + return $this->queried_object; + } + + $this->queried_object = null; + $this->queried_object_id = null; + + if ( $this->is_category || $this->is_tag || $this->is_tax ) { + if ( $this->is_category ) { + if ( $this->get( 'cat' ) ) { + $term = get_term( $this->get( 'cat' ), 'category' ); + } elseif ( $this->get( 'category_name' ) ) { + $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' ); + } + } elseif ( $this->is_tag ) { + if ( $this->get( 'tag_id' ) ) { + $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); + } elseif ( $this->get( 'tag' ) ) { + $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' ); + } + } else { + // For other tax queries, grab the first term from the first clause. + if ( ! empty( $this->tax_query->queried_terms ) ) { + $queried_taxonomies = array_keys( $this->tax_query->queried_terms ); + $matched_taxonomy = reset( $queried_taxonomies ); + $query = $this->tax_query->queried_terms[ $matched_taxonomy ]; + + if ( ! empty( $query['terms'] ) ) { + if ( 'term_id' === $query['field'] ) { + $term = get_term( reset( $query['terms'] ), $matched_taxonomy ); + } else { + $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy ); + } + } + } + } + + if ( ! empty( $term ) && ! is_wp_error( $term ) ) { + $this->queried_object = $term; + $this->queried_object_id = (int) $term->term_id; + + if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) { + _make_cat_compat( $this->queried_object ); + } + } + } elseif ( $this->is_post_type_archive ) { + $post_type = $this->get( 'post_type' ); + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $this->queried_object = get_post_type_object( $post_type ); + } elseif ( $this->is_posts_page ) { + $page_for_posts = get_option( 'page_for_posts' ); + $this->queried_object = get_post( $page_for_posts ); + $this->queried_object_id = (int) $this->queried_object->ID; + } elseif ( $this->is_singular && ! empty( $this->post ) ) { + $this->queried_object = $this->post; + $this->queried_object_id = (int) $this->post->ID; + } elseif ( $this->is_author ) { + $this->queried_object_id = (int) $this->get( 'author' ); + $this->queried_object = get_userdata( $this->queried_object_id ); + } + + return $this->queried_object; + } + + public function get_queried_object_id() { + $this->get_queried_object(); + + if ( isset( $this->queried_object_id ) ) { + return $this->queried_object_id; + } + + return 0; + } + + public function __construct( $query = '' ) { + if ( ! empty( $query ) ) { + $this->query( $query ); + } + } + + public function __get( $name ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + return $this->$name; + } + } + + public function __isset( $name ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + return isset( $this->$name ); + } + } + + public function __call( $name, $arguments ) { + if ( in_array( $name, $this->compat_methods, true ) ) { + return $this->$name( ...$arguments ); + } + return false; + } + + public function is_archive() { + return (bool) $this->is_archive; + } + + public function is_post_type_archive( $post_types = '' ) { + if ( empty( $post_types ) || ! $this->is_post_type_archive ) { + return (bool) $this->is_post_type_archive; + } + + $post_type = $this->get( 'post_type' ); + if ( is_array( $post_type ) ) { + $post_type = reset( $post_type ); + } + $post_type_object = get_post_type_object( $post_type ); + + return in_array( $post_type_object->name, (array) $post_types, true ); + } + + public function is_attachment( $attachment = '' ) { + if ( ! $this->is_attachment ) { + return false; + } + + if ( empty( $attachment ) ) { + return true; + } + + $attachment = array_map( 'strval', (array) $attachment ); + + $post_obj = $this->get_queried_object(); + + if ( in_array( (string) $post_obj->ID, $attachment, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_title, $attachment, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_name, $attachment, true ) ) { + return true; + } + return false; + } + + public function is_author( $author = '' ) { + if ( ! $this->is_author ) { + return false; + } + + if ( empty( $author ) ) { + return true; + } + + $author_obj = $this->get_queried_object(); + + $author = array_map( 'strval', (array) $author ); + + if ( in_array( (string) $author_obj->ID, $author, true ) ) { + return true; + } elseif ( in_array( $author_obj->nickname, $author, true ) ) { + return true; + } elseif ( in_array( $author_obj->user_nicename, $author, true ) ) { + return true; + } + + return false; + } + + public function is_category( $category = '' ) { + if ( ! $this->is_category ) { + return false; + } + + if ( empty( $category ) ) { + return true; + } + + $cat_obj = $this->get_queried_object(); + + $category = array_map( 'strval', (array) $category ); + + if ( in_array( (string) $cat_obj->term_id, $category, true ) ) { + return true; + } elseif ( in_array( $cat_obj->name, $category, true ) ) { + return true; + } elseif ( in_array( $cat_obj->slug, $category, true ) ) { + return true; + } + + return false; + } + + public function is_tag( $tag = '' ) { + if ( ! $this->is_tag ) { + return false; + } + + if ( empty( $tag ) ) { + return true; + } + + $tag_obj = $this->get_queried_object(); + + $tag = array_map( 'strval', (array) $tag ); + + if ( in_array( (string) $tag_obj->term_id, $tag, true ) ) { + return true; + } elseif ( in_array( $tag_obj->name, $tag, true ) ) { + return true; + } elseif ( in_array( $tag_obj->slug, $tag, true ) ) { + return true; + } + + return false; + } + + public function is_tax( $taxonomy = '', $term = '' ) { + global $wp_taxonomies; + + if ( ! $this->is_tax ) { + return false; + } + + if ( empty( $taxonomy ) ) { + return true; + } + + $queried_object = $this->get_queried_object(); + $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy ); + $term_array = (array) $term; + + // Check that the taxonomy matches. + if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array, true ) ) ) { + return false; + } + + // Only a taxonomy provided. + if ( empty( $term ) ) { + return true; + } + + return isset( $queried_object->term_id ) && + count( + array_intersect( + array( $queried_object->term_id, $queried_object->name, $queried_object->slug ), + $term_array + ) + ); + } + + public function is_comments_popup() { + _deprecated_function( __FUNCTION__, '4.5.0' ); + + return false; + } + + public function is_date() { + return (bool) $this->is_date; + } + + public function is_day() { + return (bool) $this->is_day; + } + + public function is_feed( $feeds = '' ) { + if ( empty( $feeds ) || ! $this->is_feed ) { + return (bool) $this->is_feed; + } + + $qv = $this->get( 'feed' ); + if ( 'feed' === $qv ) { + $qv = get_default_feed(); + } + + return in_array( $qv, (array) $feeds, true ); + } + + public function is_comment_feed() { + return (bool) $this->is_comment_feed; + } + + public function is_front_page() { + // Most likely case. + if ( 'posts' === get_option( 'show_on_front' ) && $this->is_home() ) { + return true; + } elseif ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) + && $this->is_page( get_option( 'page_on_front' ) ) + ) { + return true; + } else { + return false; + } + } + + public function is_home() { + return (bool) $this->is_home; + } + + public function is_privacy_policy() { + if ( get_option( 'wp_page_for_privacy_policy' ) + && $this->is_page( get_option( 'wp_page_for_privacy_policy' ) ) + ) { + return true; + } else { + return false; + } + } + + public function is_month() { + return (bool) $this->is_month; + } + + public function is_page( $page = '' ) { + if ( ! $this->is_page ) { + return false; + } + + if ( empty( $page ) ) { + return true; + } + + $page_obj = $this->get_queried_object(); + + $page = array_map( 'strval', (array) $page ); + + if ( in_array( (string) $page_obj->ID, $page, true ) ) { + return true; + } elseif ( in_array( $page_obj->post_title, $page, true ) ) { + return true; + } elseif ( in_array( $page_obj->post_name, $page, true ) ) { + return true; + } else { + foreach ( $page as $pagepath ) { + if ( ! strpos( $pagepath, '/' ) ) { + continue; + } + $pagepath_obj = get_page_by_path( $pagepath ); + + if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) { + return true; + } + } + } + + return false; + } + + public function is_paged() { + return (bool) $this->is_paged; + } + + public function is_preview() { + return (bool) $this->is_preview; + } + + public function is_robots() { + return (bool) $this->is_robots; + } + + public function is_favicon() { + return (bool) $this->is_favicon; + } + + public function is_search() { + return (bool) $this->is_search; + } + + public function is_single( $post = '' ) { + if ( ! $this->is_single ) { + return false; + } + + if ( empty( $post ) ) { + return true; + } + + $post_obj = $this->get_queried_object(); + + $post = array_map( 'strval', (array) $post ); + + if ( in_array( (string) $post_obj->ID, $post, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_title, $post, true ) ) { + return true; + } elseif ( in_array( $post_obj->post_name, $post, true ) ) { + return true; + } else { + foreach ( $post as $postpath ) { + if ( ! strpos( $postpath, '/' ) ) { + continue; + } + $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type ); + + if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) { + return true; + } + } + } + return false; + } + + public function is_singular( $post_types = '' ) { + if ( empty( $post_types ) || ! $this->is_singular ) { + return (bool) $this->is_singular; + } + + $post_obj = $this->get_queried_object(); + + return in_array( $post_obj->post_type, (array) $post_types, true ); + } + + public function is_time() { + return (bool) $this->is_time; + } + + public function is_trackback() { + return (bool) $this->is_trackback; + } + + public function is_year() { + return (bool) $this->is_year; + } + + public function is_404() { + return (bool) $this->is_404; + } + + public function is_embed() { + return (bool) $this->is_embed; + } + + public function is_main_query() { + global $wp_the_query; + return $wp_the_query === $this; + } + + public function setup_postdata( $post ) { + global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages; + + if ( ! ( $post instanceof WP_Post ) ) { + $post = get_post( $post ); + } + + if ( ! $post ) { + return; + } + + $elements = $this->generate_postdata( $post ); + if ( false === $elements ) { + return; + } + + $id = $elements['id']; + $authordata = $elements['authordata']; + $currentday = $elements['currentday']; + $currentmonth = $elements['currentmonth']; + $page = $elements['page']; + $pages = $elements['pages']; + $multipage = $elements['multipage']; + $more = $elements['more']; + $numpages = $elements['numpages']; + + do_action_ref_array( 'the_post', array( &$post, &$this ) ); + + return true; + } + + public function generate_postdata( $post ) { + + if ( ! ( $post instanceof WP_Post ) ) { + $post = get_post( $post ); + } + + if ( ! $post ) { + return false; + } + + $id = (int) $post->ID; + + $authordata = get_userdata( $post->post_author ); + + $currentday = mysql2date( 'd.m.y', $post->post_date, false ); + $currentmonth = mysql2date( 'm', $post->post_date, false ); + $numpages = 1; + $multipage = 0; + $page = $this->get( 'page' ); + if ( ! $page ) { + $page = 1; + } + + if ( get_queried_object_id() === $post->ID && ( $this->is_page() || $this->is_single() ) ) { + $more = 1; + } elseif ( $this->is_feed() ) { + $more = 1; + } else { + $more = 0; + } + + $content = $post->post_content; + if ( false !== strpos( $content, '' ) ) { + $content = str_replace( "\n\n", '', $content ); + $content = str_replace( "\n", '', $content ); + $content = str_replace( "\n", '', $content ); + + // Remove the nextpage block delimiters, to avoid invalid block structures in the split content. + $content = str_replace( '', '', $content ); + $content = str_replace( '', '', $content ); + + // Ignore nextpage at the beginning of the content. + if ( 0 === strpos( $content, '' ) ) { + $content = substr( $content, 15 ); + } + + $pages = explode( '', $content ); + } else { + $pages = array( $post->post_content ); + } + + $pages = apply_filters( 'content_pagination', $pages, $post ); + + $numpages = count( $pages ); + + if ( $numpages > 1 ) { + if ( $page > 1 ) { + $more = 1; + } + $multipage = 1; + } else { + $multipage = 0; + } + + $elements = compact( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); + + return $elements; + } + + public function reset_postdata() { + if ( ! empty( $this->post ) ) { + $GLOBALS['post'] = $this->post; + $this->setup_postdata( $this->post ); + } + } + + public function lazyload_term_meta( $check, $term_id ) { + _deprecated_function( __METHOD__, '4.5.0' ); + return $check; + } + + public function lazyload_comment_meta( $check, $comment_id ) { + _deprecated_function( __METHOD__, '4.5.0' ); + return $check; + } +} diff --git a/projects/tests/tests/real/incallstack/class-ftp-pure.php b/projects/tests/tests/real/incallstack/class-ftp-pure.php new file mode 100644 index 00000000..4c51876e --- /dev/null +++ b/projects/tests/tests/real/incallstack/class-ftp-pure.php @@ -0,0 +1,186 @@ + +// +// + + function _settimeout($sock) { + if(!@stream_set_timeout($sock, $this->_timeout)) { + $this->PushError('_settimeout','socket set send timeout'); + $this->_quit(); + return FALSE; + } + return TRUE; + } + + function _connect($host, $port) { + $this->SendMSG("Creating socket"); + $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); + if (!$sock) { + $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")"); + return FALSE; + } + $this->_connected=true; + return $sock; + } + + function _readmsg($fnction="_readmsg"){ + if(!$this->_connected) { + $this->PushError($fnction, 'Connect first'); + return FALSE; + } + $result=true; + $this->_message=""; + $this->_code=0; + $go=true; + do { + $tmp=@fgets($this->_ftp_control_sock, 512); + if($tmp===false) { + $go=$result=false; + $this->PushError($fnction,'Read failed'); + } else { + $this->_message.=$tmp; + if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false; + } + } while($go); + if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF; + $this->_code=(int)$regs[1]; + return $result; + } + + function _exec($cmd, $fnction="_exec") { + if(!$this->_ready) { + $this->PushError($fnction,'Connect first'); + return FALSE; + } + if($this->LocalEcho) echo "PUT > ",$cmd,CRLF; + $status=@fputs($this->_ftp_control_sock, $cmd.CRLF); + if($status===false) { + $this->PushError($fnction,'socket write failed'); + return FALSE; + } + $this->_lastaction=time(); + if(!$this->_readmsg($fnction)) return FALSE; + return TRUE; + } + + function _data_prepare($mode=FTP_ASCII) { + if(!$this->_settype($mode)) return FALSE; + if($this->_passive) { + if(!$this->_exec("PASV", "pasv")) { + $this->_data_close(); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + return FALSE; + } + $ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message)); + $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3]; + $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]); + $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); + $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout); + if(!$this->_ftp_data_sock) { + $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")"); + $this->_data_close(); + return FALSE; + } + else $this->_ftp_data_sock; + } else { + $this->SendMSG("Only passive connections available!"); + return FALSE; + } + return TRUE; + } + + function _data_read($mode=FTP_ASCII, $fp=NULL) { + if(is_resource($fp)) $out=0; + else $out=""; + if(!$this->_passive) { + $this->SendMSG("Only passive connections available!"); + return FALSE; + } + while (!feof($this->_ftp_data_sock)) { + $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size); + if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block); + if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block)); + else $out.=$block; + } + return $out; + } + + function _data_write($mode=FTP_ASCII, $fp=NULL) { + if(is_resource($fp)) $out=0; + else $out=""; + if(!$this->_passive) { + $this->SendMSG("Only passive connections available!"); + return FALSE; + } + if(is_resource($fp)) { + while(!feof($fp)) { + $block=fread($fp, $this->_ftp_buff_size); + if(!$this->_data_write_block($mode, $block)) return false; + } + } elseif(!$this->_data_write_block($mode, $fp)) return false; + return TRUE; + } + + function _data_write_block($mode, $block) { + if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block); + do { + if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) { + $this->PushError("_data_write","Can't write to socket"); + return FALSE; + } + $block=substr($block, $t); + } while(!empty($block)); + return true; + } + + function _data_close() { + @fclose($this->_ftp_data_sock); + $this->SendMSG("Disconnected data from remote host"); + return TRUE; + } + + function _quit($force=FALSE) { + if($this->_connected or $force) { + @fclose($this->_ftp_control_sock); + $this->_connected=false; + $this->SendMSG("Socket closed"); + } + } +} + +?> diff --git a/projects/tests/tests/real/incallstack/class-ftp-sockets.php b/projects/tests/tests/real/incallstack/class-ftp-sockets.php new file mode 100644 index 00000000..6d802f5f --- /dev/null +++ b/projects/tests/tests/real/incallstack/class-ftp-sockets.php @@ -0,0 +1,246 @@ + +// +// + + function _settimeout($sock) { + if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) { + $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock))); + @socket_close($sock); + return FALSE; + } + if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) { + $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock))); + @socket_close($sock); + return FALSE; + } + return true; + } + + function _connect($host, $port) { + $this->SendMSG("Creating socket"); + if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) { + $this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock))); + return FALSE; + } + if(!$this->_settimeout($sock)) return FALSE; + $this->SendMSG("Connecting to \"".$host.":".$port."\""); + if (!($res = @socket_connect($sock, $host, $port))) { + $this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock))); + @socket_close($sock); + return FALSE; + } + $this->_connected=true; + return $sock; + } + + function _readmsg($fnction="_readmsg"){ + if(!$this->_connected) { + $this->PushError($fnction,'Connect first'); + return FALSE; + } + $result=true; + $this->_message=""; + $this->_code=0; + $go=true; + do { + $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ); + if($tmp===false) { + $go=$result=false; + $this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock))); + } else { + $this->_message.=$tmp; + $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs); + } + } while($go); + if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF; + $this->_code=(int)$regs[1]; + return $result; + } + + function _exec($cmd, $fnction="_exec") { + if(!$this->_ready) { + $this->PushError($fnction,'Connect first'); + return FALSE; + } + if($this->LocalEcho) echo "PUT > ",$cmd,CRLF; + $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF); + if($status===false) { + $this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream))); + return FALSE; + } + $this->_lastaction=time(); + if(!$this->_readmsg($fnction)) return FALSE; + return TRUE; + } + + function _data_prepare($mode=FTP_ASCII) { + if(!$this->_settype($mode)) return FALSE; + $this->SendMSG("Creating data socket"); + $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + if ($this->_ftp_data_sock < 0) { + $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock))); + return FALSE; + } + if(!$this->_settimeout($this->_ftp_data_sock)) { + $this->_data_close(); + return FALSE; + } + if($this->_passive) { + if(!$this->_exec("PASV", "pasv")) { + $this->_data_close(); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + return FALSE; + } + $ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message)); + $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3]; + $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]); + $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); + if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) { + $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock))); + $this->_data_close(); + return FALSE; + } + else $this->_ftp_temp_sock=$this->_ftp_data_sock; + } else { + if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) { + $this->PushError("_data_prepare","can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock))); + $this->_data_close(); + return FALSE; + } + if(!@socket_bind($this->_ftp_data_sock,$addr)){ + $this->PushError("_data_prepare","can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock))); + $this->_data_close(); + return FALSE; + } + if(!@socket_listen($this->_ftp_data_sock)) { + $this->PushError("_data_prepare","can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock))); + $this->_data_close(); + return FALSE; + } + if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) { + $this->PushError("_data_prepare","can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock))); + $this->_data_close(); + return FALSE; + } + if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) { + $this->_data_close(); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + return FALSE; + } + } + return TRUE; + } + + function _data_read($mode=FTP_ASCII, $fp=NULL) { + $NewLine=$this->_eol_code[$this->OS_local]; + if(is_resource($fp)) $out=0; + else $out=""; + if(!$this->_passive) { + $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); + $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock); + if($this->_ftp_temp_sock===FALSE) { + $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock))); + $this->_data_close(); + return FALSE; + } + } + + while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) { + if($block==="") break; + if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block); + if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block)); + else $out.=$block; + } + return $out; + } + + function _data_write($mode=FTP_ASCII, $fp=NULL) { + $NewLine=$this->_eol_code[$this->OS_local]; + if(is_resource($fp)) $out=0; + else $out=""; + if(!$this->_passive) { + $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); + $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock); + if($this->_ftp_temp_sock===FALSE) { + $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock))); + $this->_data_close(); + return false; + } + } + if(is_resource($fp)) { + while(!feof($fp)) { + $block=fread($fp, $this->_ftp_buff_size); + if(!$this->_data_write_block($mode, $block)) return false; + } + } elseif(!$this->_data_write_block($mode, $fp)) return false; + return true; + } + + function _data_write_block($mode, $block) { + if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block); + do { + if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) { + $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock))); + $this->_data_close(); + return FALSE; + } + $block=substr($block, $t); + } while(!empty($block)); + return true; + } + + function _data_close() { + @socket_close($this->_ftp_temp_sock); + @socket_close($this->_ftp_data_sock); + $this->SendMSG("Disconnected data from remote host"); + return TRUE; + } + + function _quit() { + if($this->_connected) { + @socket_close($this->_ftp_control_sock); + $this->_connected=false; + $this->SendMSG("Socket closed"); + } + } +} +?> diff --git a/projects/tests/tests/real/incallstack/class-ftp.php b/projects/tests/tests/real/incallstack/class-ftp.php new file mode 100644 index 00000000..cb8a0336 --- /dev/null +++ b/projects/tests/tests/real/incallstack/class-ftp.php @@ -0,0 +1,912 @@ +LocalEcho=$le; + $this->Verbose=$verb; + $this->_lastaction=NULL; + $this->_error_array=array(); + $this->_eol_code=array(FTP_OS_Unix=>"\n", FTP_OS_Mac=>"\r", FTP_OS_Windows=>"\r\n"); + $this->AuthorizedTransferMode=array(FTP_AUTOASCII, FTP_ASCII, FTP_BINARY); + $this->OS_FullName=array(FTP_OS_Unix => 'UNIX', FTP_OS_Windows => 'WINDOWS', FTP_OS_Mac => 'MACOS'); + $this->AutoAsciiExt=array("ASP","BAT","C","CPP","CSS","CSV","JS","H","HTM","HTML","SHTML","INI","LOG","PHP3","PHTML","PL","PERL","SH","SQL","TXT"); + $this->_port_available=($port_mode==TRUE); + $this->SendMSG("Staring FTP client class".($this->_port_available?"":" without PORT mode support")); + $this->_connected=FALSE; + $this->_ready=FALSE; + $this->_can_restore=FALSE; + $this->_code=0; + $this->_message=""; + $this->_ftp_buff_size=4096; + $this->_curtype=NULL; + $this->SetUmask(0022); + $this->SetType(FTP_AUTOASCII); + $this->SetTimeout(30); + $this->Passive(!$this->_port_available); + $this->_login="anonymous"; + $this->_password="anon@ftp.com"; + $this->_features=array(); + $this->OS_local=FTP_OS_Unix; + $this->OS_remote=FTP_OS_Unix; + $this->features=array(); + if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $this->OS_local=FTP_OS_Windows; + elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') $this->OS_local=FTP_OS_Mac; + } + + function ftp_base($port_mode=FALSE) { + $this->__construct($port_mode); + } + +// +// +// + + function parselisting($line) { + $is_windows = ($this->OS_remote == FTP_OS_Windows); + if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|) +(.+)/",$line,$lucifer)) { + $b = array(); + if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix + $b['isdir'] = ($lucifer[7]==""); + if ( $b['isdir'] ) + $b['type'] = 'd'; + else + $b['type'] = 'f'; + $b['size'] = $lucifer[7]; + $b['month'] = $lucifer[1]; + $b['day'] = $lucifer[2]; + $b['year'] = $lucifer[3]; + $b['hour'] = $lucifer[4]; + $b['minute'] = $lucifer[5]; + $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]); + $b['am/pm'] = $lucifer[6]; + $b['name'] = $lucifer[8]; + } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) { + //echo $line."\n"; + $lcount=count($lucifer); + if ($lcount<8) return ''; + $b = array(); + $b['isdir'] = $lucifer[0][0] === "d"; + $b['islink'] = $lucifer[0][0] === "l"; + if ( $b['isdir'] ) + $b['type'] = 'd'; + elseif ( $b['islink'] ) + $b['type'] = 'l'; + else + $b['type'] = 'f'; + $b['perms'] = $lucifer[0]; + $b['number'] = $lucifer[1]; + $b['owner'] = $lucifer[2]; + $b['group'] = $lucifer[3]; + $b['size'] = $lucifer[4]; + if ($lcount==8) { + sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']); + sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']); + $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']); + $b['name'] = $lucifer[7]; + } else { + $b['month'] = $lucifer[5]; + $b['day'] = $lucifer[6]; + if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) { + $b['year'] = gmdate("Y"); + $b['hour'] = $l2[1]; + $b['minute'] = $l2[2]; + } else { + $b['year'] = $lucifer[7]; + $b['hour'] = 0; + $b['minute'] = 0; + } + $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute'])); + $b['name'] = $lucifer[8]; + } + } + + return $b; + } + + function SendMSG($message = "", $crlf=true) { + if ($this->Verbose) { + echo $message.($crlf?CRLF:""); + flush(); + } + return TRUE; + } + + function SetType($mode=FTP_AUTOASCII) { + if(!in_array($mode, $this->AuthorizedTransferMode)) { + $this->SendMSG("Wrong type"); + return FALSE; + } + $this->_type=$mode; + $this->SendMSG("Transfer type: ".($this->_type==FTP_BINARY?"binary":($this->_type==FTP_ASCII?"ASCII":"auto ASCII") ) ); + return TRUE; + } + + function _settype($mode=FTP_ASCII) { + if($this->_ready) { + if($mode==FTP_BINARY) { + if($this->_curtype!=FTP_BINARY) { + if(!$this->_exec("TYPE I", "SetType")) return FALSE; + $this->_curtype=FTP_BINARY; + } + } elseif($this->_curtype!=FTP_ASCII) { + if(!$this->_exec("TYPE A", "SetType")) return FALSE; + $this->_curtype=FTP_ASCII; + } + } else return FALSE; + return TRUE; + } + + function Passive($pasv=NULL) { + if(is_null($pasv)) $this->_passive=!$this->_passive; + else $this->_passive=$pasv; + if(!$this->_port_available and !$this->_passive) { + $this->SendMSG("Only passive connections available!"); + $this->_passive=TRUE; + return FALSE; + } + $this->SendMSG("Passive mode ".($this->_passive?"on":"off")); + return TRUE; + } + + function SetServer($host, $port=21, $reconnect=true) { + if(!is_long($port)) { + $this->verbose=true; + $this->SendMSG("Incorrect port syntax"); + return FALSE; + } else { + $ip=@gethostbyname($host); + $dns=@gethostbyaddr($host); + if(!$ip) $ip=$host; + if(!$dns) $dns=$host; + // Validate the IPAddress PHP4 returns -1 for invalid, PHP5 false + // -1 === "255.255.255.255" which is the broadcast address which is also going to be invalid + $ipaslong = ip2long($ip); + if ( ($ipaslong == false) || ($ipaslong === -1) ) { + $this->SendMSG("Wrong host name/address \"".$host."\""); + return FALSE; + } + $this->_host=$ip; + $this->_fullhost=$dns; + $this->_port=$port; + $this->_dataport=$port-1; + } + $this->SendMSG("Host \"".$this->_fullhost."(".$this->_host."):".$this->_port."\""); + if($reconnect){ + if($this->_connected) { + $this->SendMSG("Reconnecting"); + if(!$this->quit(FTP_FORCE)) return FALSE; + if(!$this->connect()) return FALSE; + } + } + return TRUE; + } + + function SetUmask($umask=0022) { + $this->_umask=$umask; + umask($this->_umask); + $this->SendMSG("UMASK 0".decoct($this->_umask)); + return TRUE; + } + + function SetTimeout($timeout=30) { + $this->_timeout=$timeout; + $this->SendMSG("Timeout ".$this->_timeout); + if($this->_connected) + if(!$this->_settimeout($this->_ftp_control_sock)) return FALSE; + return TRUE; + } + + function connect($server=NULL) { + if(!empty($server)) { + if(!$this->SetServer($server)) return false; + } + if($this->_ready) return true; + $this->SendMsg('Local OS : '.$this->OS_FullName[$this->OS_local]); + if(!($this->_ftp_control_sock = $this->_connect($this->_host, $this->_port))) { + $this->SendMSG("Error : Cannot connect to remote host \"".$this->_fullhost." :".$this->_port."\""); + return FALSE; + } + $this->SendMSG("Connected to remote host \"".$this->_fullhost.":".$this->_port."\". Waiting for greeting."); + do { + if(!$this->_readmsg()) return FALSE; + if(!$this->_checkCode()) return FALSE; + $this->_lastaction=time(); + } while($this->_code<200); + $this->_ready=true; + $syst=$this->systype(); + if(!$syst) $this->SendMSG("Can't detect remote OS"); + else { + if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows; + elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac; + elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix; + else $this->OS_remote=FTP_OS_Mac; + $this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]); + } + if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled"); + else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features))); + return TRUE; + } + + function quit($force=false) { + if($this->_ready) { + if(!$this->_exec("QUIT") and !$force) return FALSE; + if(!$this->_checkCode() and !$force) return FALSE; + $this->_ready=false; + $this->SendMSG("Session finished"); + } + $this->_quit(); + return TRUE; + } + + function login($user=NULL, $pass=NULL) { + if(!is_null($user)) $this->_login=$user; + else $this->_login="anonymous"; + if(!is_null($pass)) $this->_password=$pass; + else $this->_password="anon@anon.com"; + if(!$this->_exec("USER ".$this->_login, "login")) return FALSE; + if(!$this->_checkCode()) return FALSE; + if($this->_code!=230) { + if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE; + if(!$this->_checkCode()) return FALSE; + } + $this->SendMSG("Authentication succeeded"); + if(empty($this->_features)) { + if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled"); + else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features))); + } + return TRUE; + } + + function pwd() { + if(!$this->_exec("PWD", "pwd")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return preg_replace("/^[0-9]{3} \"(.+)\".*$/s", "\\1", $this->_message); + } + + function cdup() { + if(!$this->_exec("CDUP", "cdup")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return true; + } + + function chdir($pathname) { + if(!$this->_exec("CWD ".$pathname, "chdir")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return TRUE; + } + + function rmdir($pathname) { + if(!$this->_exec("RMD ".$pathname, "rmdir")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return TRUE; + } + + function mkdir($pathname) { + if(!$this->_exec("MKD ".$pathname, "mkdir")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return TRUE; + } + + function rename($from, $to) { + if(!$this->_exec("RNFR ".$from, "rename")) return FALSE; + if(!$this->_checkCode()) return FALSE; + if($this->_code==350) { + if(!$this->_exec("RNTO ".$to, "rename")) return FALSE; + if(!$this->_checkCode()) return FALSE; + } else return FALSE; + return TRUE; + } + + function filesize($pathname) { + if(!isset($this->_features["SIZE"])) { + $this->PushError("filesize", "not supported by server"); + return FALSE; + } + if(!$this->_exec("SIZE ".$pathname, "filesize")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return preg_replace("/^[0-9]{3} ([0-9]+).*$/s", "\\1", $this->_message); + } + + function abort() { + if(!$this->_exec("ABOR", "abort")) return FALSE; + if(!$this->_checkCode()) { + if($this->_code!=426) return FALSE; + if(!$this->_readmsg("abort")) return FALSE; + if(!$this->_checkCode()) return FALSE; + } + return true; + } + + function mdtm($pathname) { + if(!isset($this->_features["MDTM"])) { + $this->PushError("mdtm", "not supported by server"); + return FALSE; + } + if(!$this->_exec("MDTM ".$pathname, "mdtm")) return FALSE; + if(!$this->_checkCode()) return FALSE; + $mdtm = preg_replace("/^[0-9]{3} ([0-9]+).*$/s", "\\1", $this->_message); + $date = sscanf($mdtm, "%4d%2d%2d%2d%2d%2d"); + $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]); + return $timestamp; + } + + function systype() { + if(!$this->_exec("SYST", "systype")) return FALSE; + if(!$this->_checkCode()) return FALSE; + $DATA = explode(" ", $this->_message); + return array($DATA[1], $DATA[3]); + } + + function delete($pathname) { + if(!$this->_exec("DELE ".$pathname, "delete")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return TRUE; + } + + function site($command, $fnction="site") { + if(!$this->_exec("SITE ".$command, $fnction)) return FALSE; + if(!$this->_checkCode()) return FALSE; + return TRUE; + } + + function chmod($pathname, $mode) { + if(!$this->site( sprintf('CHMOD %o %s', $mode, $pathname), "chmod")) return FALSE; + return TRUE; + } + + function restore($from) { + if(!isset($this->_features["REST"])) { + $this->PushError("restore", "not supported by server"); + return FALSE; + } + if($this->_curtype!=FTP_BINARY) { + $this->PushError("restore", "can't restore in ASCII mode"); + return FALSE; + } + if(!$this->_exec("REST ".$from, "resore")) return FALSE; + if(!$this->_checkCode()) return FALSE; + return TRUE; + } + + function features() { + if(!$this->_exec("FEAT", "features")) return FALSE; + if(!$this->_checkCode()) return FALSE; + $f=preg_split("/[".CRLF."]+/", preg_replace("/[0-9]{3}[ -].*[".CRLF."]+/", "", $this->_message), -1, PREG_SPLIT_NO_EMPTY); + $this->_features=array(); + foreach($f as $k=>$v) { + $v=explode(" ", trim($v)); + $this->_features[array_shift($v)]=$v; + } + return true; + } + + function rawlist($pathname="", $arg="") { + return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "LIST", "rawlist"); + } + + function nlist($pathname="", $arg="") { + return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "NLST", "nlist"); + } + + function is_exists($pathname) { + return $this->file_exists($pathname); + } + + function file_exists($pathname) { + $exists=true; + if(!$this->_exec("RNFR ".$pathname, "rename")) $exists=FALSE; + else { + if(!$this->_checkCode()) $exists=FALSE; + $this->abort(); + } + if($exists) $this->SendMSG("Remote file ".$pathname." exists"); + else $this->SendMSG("Remote file ".$pathname." does not exist"); + return $exists; + } + + function fget($fp, $remotefile, $rest=0) { + if($this->_can_restore and $rest!=0) fseek($fp, $rest); + $pi=pathinfo($remotefile); + if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII; + else $mode=FTP_BINARY; + if(!$this->_data_prepare($mode)) { + return FALSE; + } + if($this->_can_restore and $rest!=0) $this->restore($rest); + if(!$this->_exec("RETR ".$remotefile, "get")) { + $this->_data_close(); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + return FALSE; + } + $out=$this->_data_read($mode, $fp); + $this->_data_close(); + if(!$this->_readmsg()) return FALSE; + if(!$this->_checkCode()) return FALSE; + return $out; + } + + function get($remotefile, $localfile=NULL, $rest=0) { + if(is_null($localfile)) $localfile=$remotefile; + if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten"); + $fp = @fopen($localfile, "w"); + if (!$fp) { + $this->PushError("get","can't open local file", "Cannot create \"".$localfile."\""); + return FALSE; + } + if($this->_can_restore and $rest!=0) fseek($fp, $rest); + $pi=pathinfo($remotefile); + if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII; + else $mode=FTP_BINARY; + if(!$this->_data_prepare($mode)) { + fclose($fp); + return FALSE; + } + if($this->_can_restore and $rest!=0) $this->restore($rest); + if(!$this->_exec("RETR ".$remotefile, "get")) { + $this->_data_close(); + fclose($fp); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + fclose($fp); + return FALSE; + } + $out=$this->_data_read($mode, $fp); + fclose($fp); + $this->_data_close(); + if(!$this->_readmsg()) return FALSE; + if(!$this->_checkCode()) return FALSE; + return $out; + } + + function fput($remotefile, $fp, $rest=0) { + if($this->_can_restore and $rest!=0) fseek($fp, $rest); + $pi=pathinfo($remotefile); + if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII; + else $mode=FTP_BINARY; + if(!$this->_data_prepare($mode)) { + return FALSE; + } + if($this->_can_restore and $rest!=0) $this->restore($rest); + if(!$this->_exec("STOR ".$remotefile, "put")) { + $this->_data_close(); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + return FALSE; + } + $ret=$this->_data_write($mode, $fp); + $this->_data_close(); + if(!$this->_readmsg()) return FALSE; + if(!$this->_checkCode()) return FALSE; + return $ret; + } + + function put($localfile, $remotefile=NULL, $rest=0) { + if(is_null($remotefile)) $remotefile=$localfile; + if (!file_exists($localfile)) { + $this->PushError("put","can't open local file", "No such file or directory \"".$localfile."\""); + return FALSE; + } + $fp = @fopen($localfile, "r"); + + if (!$fp) { + $this->PushError("put","can't open local file", "Cannot read file \"".$localfile."\""); + return FALSE; + } + if($this->_can_restore and $rest!=0) fseek($fp, $rest); + $pi=pathinfo($localfile); + if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII; + else $mode=FTP_BINARY; + if(!$this->_data_prepare($mode)) { + fclose($fp); + return FALSE; + } + if($this->_can_restore and $rest!=0) $this->restore($rest); + if(!$this->_exec("STOR ".$remotefile, "put")) { + $this->_data_close(); + fclose($fp); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + fclose($fp); + return FALSE; + } + $ret=$this->_data_write($mode, $fp); + fclose($fp); + $this->_data_close(); + if(!$this->_readmsg()) return FALSE; + if(!$this->_checkCode()) return FALSE; + return $ret; + } + + function mput($local=".", $remote=NULL, $continious=false) { + $local=realpath($local); + if(!@file_exists($local)) { + $this->PushError("mput","can't open local folder", "Cannot stat folder \"".$local."\""); + return FALSE; + } + if(!is_dir($local)) return $this->put($local, $remote); + if(empty($remote)) $remote="."; + elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE; + if($handle = opendir($local)) { + $list=array(); + while (false !== ($file = readdir($handle))) { + if ($file != "." && $file != "..") $list[]=$file; + } + closedir($handle); + } else { + $this->PushError("mput","can't open local folder", "Cannot read folder \"".$local."\""); + return FALSE; + } + if(empty($list)) return TRUE; + $ret=true; + foreach($list as $el) { + if(is_dir($local."/".$el)) $t=$this->mput($local."/".$el, $remote."/".$el); + else $t=$this->put($local."/".$el, $remote."/".$el); + if(!$t) { + $ret=FALSE; + if(!$continious) break; + } + } + return $ret; + + } + + function mget($remote, $local=".", $continious=false) { + $list=$this->rawlist($remote, "-lA"); + if($list===false) { + $this->PushError("mget","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents"); + return FALSE; + } + if(empty($list)) return true; + if(!@file_exists($local)) { + if(!@mkdir($local)) { + $this->PushError("mget","can't create local folder", "Cannot create folder \"".$local."\""); + return FALSE; + } + } + foreach($list as $k=>$v) { + $list[$k]=$this->parselisting($v); + if( ! $list[$k] or $list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]); + } + $ret=true; + foreach($list as $el) { + if($el["type"]=="d") { + if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) { + $this->PushError("mget", "can't copy folder", "Can't copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\""); + $ret=false; + if(!$continious) break; + } + } else { + if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) { + $this->PushError("mget", "can't copy file", "Can't copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\""); + $ret=false; + if(!$continious) break; + } + } + @chmod($local."/".$el["name"], $el["perms"]); + $t=strtotime($el["date"]); + if($t!==-1 and $t!==false) @touch($local."/".$el["name"], $t); + } + return $ret; + } + + function mdel($remote, $continious=false) { + $list=$this->rawlist($remote, "-la"); + if($list===false) { + $this->PushError("mdel","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents"); + return false; + } + + foreach($list as $k=>$v) { + $list[$k]=$this->parselisting($v); + if( ! $list[$k] or $list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]); + } + $ret=true; + + foreach($list as $el) { + if ( empty($el) ) + continue; + + if($el["type"]=="d") { + if(!$this->mdel($remote."/".$el["name"], $continious)) { + $ret=false; + if(!$continious) break; + } + } else { + if (!$this->delete($remote."/".$el["name"])) { + $this->PushError("mdel", "can't delete file", "Can't delete remote file \"".$remote."/".$el["name"]."\""); + $ret=false; + if(!$continious) break; + } + } + } + + if(!$this->rmdir($remote)) { + $this->PushError("mdel", "can't delete folder", "Can't delete remote folder \"".$remote."/".$el["name"]."\""); + $ret=false; + } + return $ret; + } + + function mmkdir($dir, $mode = 0777) { + if(empty($dir)) return FALSE; + if($this->is_exists($dir) or $dir == "/" ) return TRUE; + if(!$this->mmkdir(dirname($dir), $mode)) return false; + $r=$this->mkdir($dir, $mode); + $this->chmod($dir,$mode); + return $r; + } + + function glob($pattern, $handle=NULL) { + $path=$output=null; + if(PHP_OS=='WIN32') $slash='\\'; + else $slash='/'; + $lastpos=strrpos($pattern,$slash); + if(!($lastpos===false)) { + $path=substr($pattern,0,-$lastpos-1); + $pattern=substr($pattern,$lastpos); + } else $path=getcwd(); + if(is_array($handle) and !empty($handle)) { + foreach($handle as $dir) { + if($this->glob_pattern_match($pattern,$dir)) + $output[]=$dir; + } + } else { + $handle=@opendir($path); + if($handle===false) return false; + while($dir=readdir($handle)) { + if($this->glob_pattern_match($pattern,$dir)) + $output[]=$dir; + } + closedir($handle); + } + if(is_array($output)) return $output; + return false; + } + + function glob_pattern_match($pattern,$string) { + $out=null; + $chunks=explode(';',$pattern); + foreach($chunks as $pattern) { + $escape=array('$','^','.','{','}','(',')','[',']','|'); + while(strpos($pattern,'**')!==false) + $pattern=str_replace('**','*',$pattern); + foreach($escape as $probe) + $pattern=str_replace($probe,"\\$probe",$pattern); + $pattern=str_replace('?*','*', + str_replace('*?','*', + str_replace('*',".*", + str_replace('?','.{1,1}',$pattern)))); + $out[]=$pattern; + } + if(count($out)==1) return($this->glob_regexp("^$out[0]$",$string)); + else { + foreach($out as $tester) + if($this->my_regexp("^$tester$",$string)) return true; + } + return false; + } + + function glob_regexp($pattern,$probe) { + $sensitive=(PHP_OS!='WIN32'); + return ($sensitive? + preg_match( '/' . preg_quote( $pattern, '/' ) . '/', $probe ) : + preg_match( '/' . preg_quote( $pattern, '/' ) . '/i', $probe ) + ); + } + + function dirlist($remote) { + $list=$this->rawlist($remote, "-la"); + if($list===false) { + $this->PushError("dirlist","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents"); + return false; + } + + $dirlist = array(); + foreach($list as $k=>$v) { + $entry=$this->parselisting($v); + if ( empty($entry) ) + continue; + + if($entry["name"]=="." or $entry["name"]=="..") + continue; + + $dirlist[$entry['name']] = $entry; + } + + return $dirlist; + } +// +// +// + function _checkCode() { + return ($this->_code<400 and $this->_code>0); + } + + function _list($arg="", $cmd="LIST", $fnction="_list") { + if(!$this->_data_prepare()) return false; + if(!$this->_exec($cmd.$arg, $fnction)) { + $this->_data_close(); + return FALSE; + } + if(!$this->_checkCode()) { + $this->_data_close(); + return FALSE; + } + $out=""; + if($this->_code<200) { + $out=$this->_data_read(); + $this->_data_close(); + if(!$this->_readmsg()) return FALSE; + if(!$this->_checkCode()) return FALSE; + if($out === FALSE ) return FALSE; + $out=preg_split("/[".CRLF."]+/", $out, -1, PREG_SPLIT_NO_EMPTY); +// $this->SendMSG(implode($this->_eol_code[$this->OS_local], $out)); + } + return $out; + } + +// +// +// +// Gnre une erreur pour traitement externe la classe + function PushError($fctname,$msg,$desc=false){ + $error=array(); + $error['time']=time(); + $error['fctname']=$fctname; + $error['msg']=$msg; + $error['desc']=$desc; + if($desc) $tmp=' ('.$desc.')'; else $tmp=''; + $this->SendMSG($fctname.': '.$msg.$tmp); + return(array_push($this->_error_array,$error)); + } + +// Rcupre une erreur externe + function PopError(){ + if(count($this->_error_array)) return(array_pop($this->_error_array)); + else return(false); + } +} + +$mod_sockets = extension_loaded( 'sockets' ); +if ( ! $mod_sockets && function_exists( 'dl' ) && is_callable( 'dl' ) ) { + $prefix = ( PHP_SHLIB_SUFFIX == 'dll' ) ? 'php_' : ''; + @dl( $prefix . 'sockets.' . PHP_SHLIB_SUFFIX ); // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.dlDeprecated + $mod_sockets = extension_loaded( 'sockets' ); +} + +require_once __DIR__ . "/class-ftp-" . ( $mod_sockets ? "sockets" : "pure" ) . ".php"; + +if ( $mod_sockets ) { + class ftp extends ftp_sockets {} +} else { + class ftp extends ftp_pure {} +} diff --git a/projects/tests/tests/real/multiplecalls/test.php b/projects/tests/tests/real/multiplecalls/test.php new file mode 100644 index 00000000..899dc8c1 --- /dev/null +++ b/projects/tests/tests/real/multiplecalls/test.php @@ -0,0 +1,20 @@ +xssvuln($tainted); + $f1->xssvuln($_GET["p"]); + } +} + + diff --git a/projects/tests/tests/real/namespaces1/test.php b/projects/tests/tests/real/namespaces1/test.php new file mode 100644 index 00000000..486a352d --- /dev/null +++ b/projects/tests/tests/real/namespaces1/test.php @@ -0,0 +1,7 @@ +callfoo1("toto"); +$f2->callfoo1($_GET["t"]); diff --git a/projects/tests/tests/real/sqli.php b/projects/tests/tests/real/sqli.php new file mode 100644 index 00000000..4f79ef36 --- /dev/null +++ b/projects/tests/tests/real/sqli.php @@ -0,0 +1,7 @@ +escape($descriptions[$i]); + +$result = $wpdb->query($cat_id); diff --git a/projects/tests/tests/real/wordpress/index.php b/projects/tests/tests/real/wordpress/index.php new file mode 100644 index 00000000..3e3db402 --- /dev/null +++ b/projects/tests/tests/real/wordpress/index.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/license.txt b/projects/tests/tests/real/wordpress/license.txt new file mode 100644 index 00000000..88822058 --- /dev/null +++ b/projects/tests/tests/real/wordpress/license.txt @@ -0,0 +1,280 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 675 Mass Ave, Cambridge, MA 02139, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + diff --git a/projects/tests/tests/real/wordpress/readme.html b/projects/tests/tests/real/wordpress/readme.html new file mode 100644 index 00000000..2a630165 --- /dev/null +++ b/projects/tests/tests/real/wordpress/readme.html @@ -0,0 +1,120 @@ + + + +WordPress › ReadMe + + + + +

    WordPress
    + Version 2.0

    +

    Semantic Personal Publishing Platform

    +

    First Things First

    +

    Welcome. WordPress is a very special project to me. Every developer and contributor adds something unique to the mix, and together we create something beautiful that I'm proud to be a part of. Thousands of hours have gone into WordPress, and we're dedicated to making it better every day. Thank you for making it part of your world.

    +

    — Matt Mullenweg

    + +

    Installation: Famous 5-minute install

    +
      +
    1. Unzip the package in an empty directory
    2. +
    3. Open up wp-config-sample.php with a text editor like WordPad or similar and fill in your database connection details
    4. +
    5. Save the file as wp-config.php
    6. +
    7. Upload everything.
    8. +
    9. Open /wp-admin/install.php in your browser. This should setup the tables needed for your blog. If there is an error, double check your wp-config.php file, and try again. If it fails again, please go to the support forums with as much data as you can gather.
    10. +
    11. Note the password given to you.
    12. +
    13. The install script should then send you to the login page. Sign in with the username admin and the password generated during the installation. You can then click on 'Profile' to change the password.
    14. +
    + +

    Upgrading

    +

    Before you upgrade anything, make sure you have backup copies of any files you may have modified such as index.php.

    +

    Upgrading from any previous WordPress to 2.0:

    +
      +
    1. Delete your old WP files, saving ones you've modified
    2. +
    3. Upload the new files
    4. +
    5. Point your browser to /wp-admin/upgrade.php
    6. +
    7. You wanted more, perhaps? That's it!
    8. +
    +

    Template Changes

    +

    If you have customized your templates you will probably have to make some changes to them. If you're converting your 1.2 or earlier templates, we've created a special guide for you.

    +

    Online Resources

    +

    If you have any questions that aren't addressed in this document, please take advantage of WordPress' numerous online resources:

    +
    +
    The WordPress Codex
    +
    The Codex is the encyclopedia of all things WordPress. It is the most comprehensive source of information for WordPress available.
    +
    The Development Blog
    +
    This is where you'll find the latest updates and news related to WordPress. Bookmark and check often.
    +
    WordPress Planet
    +
    The WordPress Planet is a news aggregator that brings together posts from WordPress blogs around the web.
    +
    WordPress Support Forums
    +
    If you've looked everywhere and still can't find an answer, the support forums are very active and have a large community ready to help. To help them help you be sure to use a descriptive thread title and describe your question in as much detail as possible.
    +
    WordPress IRC Channel
    +
    Finally, there is an online chat channel that is used for discussion amoung people who use WordPress and occasionally support topics. The above wiki page should point you in the right direction. (irc.freenode.net #wordpresss)
    +
    + +

    System Recommendations

    +
      +
    • PHP version 4.1 or higher
    • +
    • MySQL version 3.23.23 or higher
    • +
    • ... and a link to http://wordpress.org on your site.
    • +
    +

    WordPress is the official continuation of b2/cafélog, which came from Michel V. The work has been continued by the WordPress developers. If you would like to support WordPress, please consider donating.

    + +

    Upgrading from another system

    +

    WordPress can import from a number of systems. First you need to get WordPress installed and working as described above.

    + +

    XML-RPC Interface

    +

    You can now post to your WordPress blog with tools like Ecto, BlogBuddy, Bloggar, WapBlogger (post from your Wap cellphone!), Radio Userland (which means you can use Radio's email-to-blog feature), Zempt, NewzCrawler, and other tools that support the Blogging APIs! :) You can read more about XML-RPC support on the Codex.

    + +

    Post via Email

    +

    You can post from an email client! To set this up go to your "Writing" options screen and fill in the connection details for your secret POP3 account. Then you need to set up wp-mail.php to execute periodically to check the mailbox for new posts. You can do it with Cron-jobs, or if your host doesn't support it you can look into the various website-monitoring services, and make them check your wp-mail.php URL.

    +

    Posting is easy: Any email sent to the address you specify will be posted, with the subject as the title. It is best to keep the address dicrete. The script will delete emails that are successfully posted.

    +

    User Roles

    + +

    We've eliminated user levels in order to make way for the much more flexible roles system introduced in 2.0. You can read more about Roles and Capabilities on the Codex.

    + +

    Final notes

    +
      +
    • If you have any suggestions, ideas, comments, or if you (gasp!) found a bug, join us in the Support Forums
    • +
    • WordPress now has a robust plugin API that makes extending the code easy. If you are a developer interested in utilizing this see the plugin documentation in the Codex. In most all cases you shouldn't modify any of the core code.
    • +
    + +

    Share the Love

    +

    WordPress has no multi-million dollar marketing campaign or celebrity sponsors, but we do have something even better—you. If you enjoy WordPress please consider telling a friend, setting it up for someone less knowledgable than yourself, or writing the author of a media article that overlooks us.

    + +

    Copyright

    +

    WordPress is released under the GPL (see license.txt).

    + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/admin-db.php b/projects/tests/tests/real/wordpress/wp-admin/admin-db.php new file mode 100644 index 00000000..bf8692ab --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/admin-db.php @@ -0,0 +1,349 @@ +posts WHERE post_status = 'draft' AND post_author = $user_id ORDER BY ID DESC"; + $query = apply_filters('get_users_drafts', $query); + return $wpdb->get_results( $query ); +} + +function get_others_drafts( $user_id ) { + global $wpdb; + $user = get_userdata( $user_id ); + $level_key = $wpdb->prefix . 'user_level'; + + $editable = get_editable_user_ids( $user_id ); + + if( !$editable ) { + $other_drafts = ''; + } else { + $editable = join(',', $editable); + $other_drafts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author IN ($editable) AND post_author != '$user_id' "); + } + + return apply_filters('get_others_drafts', $other_drafts); +} + +function get_editable_authors( $user_id ) { + global $wpdb; + + $editable = get_editable_user_ids( $user_id ); + + if( !$editable ) { + return false; + } else { + $editable = join(',', $editable); + $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable)" ); + } + + return apply_filters('get_editable_authors', $authors); +} + +function get_editable_user_ids( $user_id, $exclude_zeros = true ) { + global $wpdb; + + $user = new WP_User( $user_id ); + + if ( ! $user->has_cap('edit_others_posts') ) { + if ( $user->has_cap('edit_posts') || $exclude_zeros == false ) + return array($user->id); + else + return false; + } + + $level_key = $wpdb->prefix . 'user_level'; + + $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'"; + if ( $exclude_zeros ) + $query .= " AND meta_value != '0'"; + + return $wpdb->get_col( $query ); +} + +function get_author_user_ids() { + global $wpdb; + $level_key = $wpdb->prefix . 'user_level'; + + $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'"; + + return $wpdb->get_col( $query ); +} + +function get_nonauthor_user_ids() { + global $wpdb; + $level_key = $wpdb->prefix . 'user_level'; + + $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'"; + + return $wpdb->get_col( $query ); +} + +function wp_insert_category($catarr) { + global $wpdb; + + extract($catarr); + + $cat_ID = (int) $cat_ID; + + // Are we updating or creating? + if (!empty ($cat_ID)) + $update = true; + else + $update = false; + + $cat_name = wp_specialchars($cat_name); + + if (empty ($category_nicename)) + $category_nicename = sanitize_title($cat_name); + else + $category_nicename = sanitize_title($category_nicename); + + if (empty ($category_description)) + $category_description = ''; + + if (empty ($category_parent)) + $category_parent = 0; + + if (!$update) { + $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent')"); + $cat_ID = $wpdb->insert_id; + } else { + $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent' WHERE cat_ID = '$cat_ID'"); + } + + if ( $category_nicename == '' ) { + $category_nicename = sanitize_title($cat_name, $cat_ID ); + $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" ); + } + + wp_cache_delete($cat_ID, 'category'); + + if ($update) { + do_action('edit_category', $cat_ID); + } else { + wp_cache_delete('all_category_ids', 'category'); + do_action('create_category', $cat_ID); + do_action('add_category', $cat_ID); + } + + return $cat_ID; +} + +function wp_update_category($catarr) { + global $wpdb; + + $cat_ID = (int) $catarr['cat_ID']; + + // First, get all of the original fields + $category = get_category($cat_ID, ARRAY_A); + + // Escape data pulled from DB. + $category = add_magic_quotes($category); + + // Merge old and new fields with new fields overwriting old ones. + $catarr = array_merge($category, $catarr); + + return wp_insert_category($catarr); +} + +function wp_delete_category($cat_ID) { + global $wpdb; + + $cat_ID = (int) $cat_ID; + + // Don't delete the default cat. + if (1 == $cat_ID) + return 0; + + $category = get_category($cat_ID); + + $parent = $category->category_parent; + + // Delete the category. + $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'"); + + // Update children to point to new parent. + $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'"); + + // TODO: Only set categories to general if they're not in another category already + $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'"); + + wp_cache_delete($cat_ID, 'category'); + wp_cache_delete('all_category_ids', 'category'); + + do_action('delete_category', $cat_ID); + + return 1; +} + +function wp_create_category($cat_name) { + $cat_array = compact('cat_name'); + return wp_insert_category($cat_array); +} + +function wp_create_categories($categories, $post_id = '') { + $cat_ids = array (); + foreach ($categories as $category) { + if ($id = category_exists($category)) + $cat_ids[] = $id; + else + if ($id = wp_create_category($category)) + $cat_ids[] = $id; + } + + if ($post_id) + wp_set_post_cats('', $post_id, $cat_ids); + + return $cat_ids; +} + +function category_exists($cat_name) { + global $wpdb; + if (!$category_nicename = sanitize_title($cat_name)) + return 0; + + return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'"); +} + +function wp_delete_user($id, $reassign = 'novalue') { + global $wpdb; + + $id = (int) $id; + $user = get_userdata($id); + + if ($reassign == 'novalue') { + $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id"); + + if ($post_ids) { + foreach ($post_ids as $post_id) + wp_delete_post($post_id); + } + + // Clean links + $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id"); + } else { + $reassign = (int) $reassign; + $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}"); + $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}"); + } + + // FINALLY, delete user + $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id"); + $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'"); + + wp_cache_delete($id, 'users'); + wp_cache_delete($user->user_login, 'userlogins'); + + do_action('delete_user', $id); + + return true; +} + +function get_link($link_id, $output = OBJECT) { + global $wpdb; + + $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'"); + + if ( $output == OBJECT ) { + return $link; + } elseif ( $output == ARRAY_A ) { + return get_object_vars($link); + } elseif ( $output == ARRAY_N ) { + return array_values(get_object_vars($link)); + } else { + return $link; + } +} + +function wp_insert_link($linkdata) { + global $wpdb, $current_user; + + extract($linkdata); + + $update = false; + if ( !empty($link_id) ) + $update = true; + + if ( empty($link_rating) ) + $link_rating = 0; + + if ( empty($link_target) ) + $link_target = ''; + + if ( empty($link_visible) ) + $link_visible = 'Y'; + + if ( empty($link_owner) ) + $link_owner = $current_user->id; + + if ( empty($link_notes) ) + $link_notes = ''; + + if ( $update ) { + $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url', + link_name='$link_name', link_image='$link_image', + link_target='$link_target', link_category='$link_category', + link_visible='$link_visible', link_description='$link_description', + link_rating='$link_rating', link_rel='$link_rel', + link_notes='$link_notes', link_rss = '$link_rss' + WHERE link_id='$link_id'"); + } else { + $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')"); + $link_id = $wpdb->insert_id; + } + + if ( $update ) + do_action('edit_link', $link_id); + else + do_action('add_link', $link_id); + + return $link_id; +} + +function wp_update_link($linkdata) { + global $wpdb; + + $link_id = (int) $linkdata['link_id']; + + $link = get_link($link_id, ARRAY_A); + + // Escape data pulled from DB. + $link = add_magic_quotes($link); + + // Merge old and new fields with new fields overwriting old ones. + $linkdata = array_merge($link, $linkdata); + + return wp_insert_link($linkdata); +} + +function wp_delete_link($link_id) { + global $wpdb; + + do_action('delete_link', $link_id); + return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'"); +} + +function post_exists($title, $content = '', $post_date = '') { + global $wpdb; + + if (!empty ($post_date)) + $post_date = "AND post_date = '$post_date'"; + + if (!empty ($title)) + return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date"); + else + if (!empty ($content)) + return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date"); + + return 0; +} + +function comment_exists($comment_author, $comment_date) { + global $wpdb; + + return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments + WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'"); +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/admin-footer.php b/projects/tests/tests/real/wordpress/wp-admin/admin-footer.php new file mode 100644 index 00000000..acecf377 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/admin-footer.php @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/admin-functions.php b/projects/tests/tests/real/wordpress/wp-admin/admin-functions.php new file mode 100644 index 00000000..7f0ed5e0 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/admin-functions.php @@ -0,0 +1,1872 @@ + 31) ? 31 : $jj; + $hh = ($hh > 23) ? $hh -24 : $hh; + $mn = ($mn > 59) ? $mn -60 : $mn; + $ss = ($ss > 59) ? $ss -60 : $ss; + $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; + $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); + } + + // Create the post. + $post_ID = wp_insert_post($_POST); + add_meta($post_ID); + + // Reunite any orphaned attachments with their parent + if ( $_POST['temp_ID'] ) + relocate_children($_POST['temp_ID'], $post_ID); + + // Now that we have an ID we can fix any attachment anchor hrefs + fix_attachment_links($post_ID); + + return $post_ID; +} + +// Move child posts to a new parent +function relocate_children($old_ID, $new_ID) { + global $wpdb; + $old_ID = (int) $old_ID; + $new_ID = (int) $new_ID; + return $wpdb->query("UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID"); +} + +// Replace hrefs of attachment anchors with up-to-date permalinks. +function fix_attachment_links($post_ID) { + global $wp_rewrite; + + $post = & get_post($post_ID); + + $search = "#]+rel=('|\")[^'\"]*attachment[^>]*>#ie"; + + // See if we have any rel="attachment" links + if ( 0 == preg_match_all($search, $post->post_content, $anchor_matches, PREG_PATTERN_ORDER) ) + return; + + $i = 0; + $search = "# id=(\"|')p(\d+)\\1#i"; + foreach ( $anchor_matches[0] as $anchor ) { + if ( 0 == preg_match($search, $anchor, $id_matches) ) + continue; + + $id = $id_matches[2]; + + // While we have the attachment ID, let's adopt any orphans. + $attachment = & get_post($id); + if ( ! is_object(get_post($attachment->post_parent)) ) { + $attachment->post_parent = $post_ID; + wp_update_post($attachment); + } + + $post_search[$i] = $anchor; + $post_replace[$i] = preg_replace("#href=(\"|')[^'\"]*\\1#e", "stripslashes('href=\\1').get_attachment_link($id).stripslashes('\\1')", $anchor); + ++$i; + } + + $post->post_content = str_replace($post_search, $post_replace, $post->post_content); + + return wp_update_post($post); +} + +// Update an existing post with values provided in $_POST. +function edit_post() { + global $user_ID; + + $post_ID = (int) $_POST['post_ID']; + + if (!current_user_can('edit_post', $post_ID)) + die(__('You are not allowed to edit this post.')); + + // Rename. + $_POST['ID'] = (int) $_POST['post_ID']; + $_POST['post_content'] = $_POST['content']; + $_POST['post_excerpt'] = $_POST['excerpt']; + $_POST['post_parent'] = $_POST['parent_id']; + $_POST['to_ping'] = $_POST['trackback_url']; + + if (!empty ($_POST['post_author_override'])) { + $_POST['post_author'] = (int) $_POST['post_author_override']; + } else + if (!empty ($_POST['post_author'])) { + $_POST['post_author'] = (int) $_POST['post_author']; + } else { + $_POST['post_author'] = (int) $_POST['user_ID']; + } + + if (($_POST['post_author'] != $_POST['user_ID']) && !current_user_can('edit_others_posts')) + die(__('You cannot post as this user.')); + + // What to do based on which button they pressed + if ('' != $_POST['saveasdraft']) + $_POST['post_status'] = 'draft'; + if ('' != $_POST['saveasprivate']) + $_POST['post_status'] = 'private'; + if ('' != $_POST['publish']) + $_POST['post_status'] = 'publish'; + if ('' != $_POST['advanced']) + $_POST['post_status'] = 'draft'; + if ('' != $_POST['savepage']) + $_POST['post_status'] = 'static'; + + if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts')) + $_POST['post_status'] = 'draft'; + + if ('static' == $_POST['post_status'] && !current_user_can('edit_pages')) + die(__('This user cannot edit pages.')); + + if (!isset ($_POST['comment_status'])) + $_POST['comment_status'] = 'closed'; + + if (!isset ($_POST['ping_status'])) + $_POST['ping_status'] = 'closed'; + + if (!empty ($_POST['edit_date'])) { + $aa = $_POST['aa']; + $mm = $_POST['mm']; + $jj = $_POST['jj']; + $hh = $_POST['hh']; + $mn = $_POST['mn']; + $ss = $_POST['ss']; + $jj = ($jj > 31) ? 31 : $jj; + $hh = ($hh > 23) ? $hh -24 : $hh; + $mn = ($mn > 59) ? $mn -60 : $mn; + $ss = ($ss > 59) ? $ss -60 : $ss; + $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; + $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); + } + + // Meta Stuff + if ($_POST['meta']) { + foreach ($_POST['meta'] as $key => $value) + update_meta($key, $value['key'], $value['value']); + } + + if ($_POST['deletemeta']) { + foreach ($_POST['deletemeta'] as $key => $value) + delete_meta($key); + } + + add_meta($post_ID); + + wp_update_post($_POST); + + // Now that we have an ID we can fix any attachment anchor hrefs + fix_attachment_links($post_ID); + + return $post_ID; +} + +function edit_comment() { + global $user_ID; + + $comment_ID = (int) $_POST['comment_ID']; + $comment_post_ID = (int) $_POST['comment_post_ID']; + + if (!current_user_can('edit_post', $comment_post_ID)) + die(__('You are not allowed to edit comments on this post, so you cannot edit this comment.')); + + $_POST['comment_author'] = $_POST['newcomment_author']; + $_POST['comment_author_email'] = $_POST['newcomment_author_email']; + $_POST['comment_author_url'] = $_POST['newcomment_author_url']; + $_POST['comment_approved'] = $_POST['comment_status']; + $_POST['comment_content'] = $_POST['content']; + $_POST['comment_ID'] = (int) $_POST['comment_ID']; + + if (!empty ($_POST['edit_date'])) { + $aa = $_POST['aa']; + $mm = $_POST['mm']; + $jj = $_POST['jj']; + $hh = $_POST['hh']; + $mn = $_POST['mn']; + $ss = $_POST['ss']; + $jj = ($jj > 31) ? 31 : $jj; + $hh = ($hh > 23) ? $hh -24 : $hh; + $mn = ($mn > 59) ? $mn -60 : $mn; + $ss = ($ss > 59) ? $ss -60 : $ss; + $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; + } + + wp_update_comment($_POST); +} + +// Get an existing post and format it for editing. +function get_post_to_edit($id) { + global $richedit; + $richedit = ( 'true' == get_user_option('rich_editing') ) ? true : false; + + $post = get_post($id); + + $post->post_content = format_to_edit($post->post_content, $richedit); + $post->post_content = apply_filters('content_edit_pre', $post->post_content); + + $post->post_excerpt = format_to_edit($post->post_excerpt); + $post->post_excerpt = apply_filters('excerpt_edit_pre', $post->post_excerpt); + + $post->post_title = format_to_edit($post->post_title); + $post->post_title = apply_filters('title_edit_pre', $post->post_title); + + if ($post->post_status == 'static') + $post->page_template = get_post_meta($id, '_wp_page_template', true); + + return $post; +} + +// Default post information to use when populating the "Write Post" form. +function get_default_post_to_edit() { + if ( !empty($_REQUEST['post_title']) ) + $post_title = wp_specialchars(stripslashes($_REQUEST['post_title'])); + else if ( !empty($_REQUEST['popuptitle']) ) { + $post_title = wp_specialchars(stripslashes($_REQUEST['popuptitle'])); + $post_title = funky_javascript_fix($post_title); + } else { + $post_title = ''; + } + + if ( !empty($_REQUEST['content']) ) + $post_content = wp_specialchars(stripslashes($_REQUEST['content'])); + else if ( !empty($post_title) ) { + $text = wp_specialchars(stripslashes(urldecode($_REQUEST['text']))); + $text = funky_javascript_fix($text); + $popupurl = wp_specialchars($_REQUEST['popupurl']); + $post_content = ''.$post_title.''."\n$text"; + } + + if ( !empty($_REQUEST['excerpt']) ) + $post_excerpt = wp_specialchars(stripslashes($_REQUEST['excerpt'])); + else + $post_excerpt = ''; + + $post->post_status = 'draft'; + $post->comment_status = get_settings('default_comment_status'); + $post->ping_status = get_settings('default_ping_status'); + $post->post_pingback = get_settings('default_pingback_flag'); + $post->post_category = get_settings('default_category'); + $post->post_content = apply_filters('default_content', $post_content); + $post->post_title = apply_filters('default_title', $post_title); + $post->post_excerpt = apply_filters('default_excerpt', $post_excerpt); + $post->page_template = 'default'; + $post->post_parent = 0; + $post->menu_order = 0; + + return $post; +} + +function get_comment_to_edit($id) { + global $richedit; + $richedit = ( 'true' == get_user_option('rich_editing') ) ? true : false; + + $comment = get_comment($id); + + $comment->comment_content = format_to_edit($comment->comment_content, $richedit); + $comment->comment_content = apply_filters('comment_edit_pre', $comment->comment_content); + + $comment->comment_author = format_to_edit($comment->comment_author); + $comment->comment_author_email = format_to_edit($comment->comment_author_email); + $comment->comment_author_url = format_to_edit($comment->comment_author_url); + + return $comment; +} + +function get_category_to_edit($id) { + $category = get_category($id); + + return $category; +} + +// Creates a new user from the "Users" form using $_POST information. + +function add_user() { + return edit_user(); +} + +function edit_user($user_id = 0) { + global $current_user, $wp_roles; + + if ($user_id != 0) { + $update = true; + $user->ID = $user_id; + $userdata = get_userdata($user_id); + $user->user_login = $userdata->user_login; + } else { + $update = false; + $user = ''; + } + + if (isset ($_POST['user_login'])) + $user->user_login = wp_specialchars(trim($_POST['user_login'])); + + $pass1 = $pass2 = ''; + if (isset ($_POST['pass1'])) + $pass1 = $_POST['pass1']; + if (isset ($_POST['pass2'])) + $pass2 = $_POST['pass2']; + + if (isset ($_POST['role'])) { + if($user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap('edit_users')) + $user->role = $_POST['role']; + } + + if (isset ($_POST['email'])) + $user->user_email = wp_specialchars(trim($_POST['email'])); + if (isset ($_POST['url'])) { + $user->user_url = wp_specialchars(trim($_POST['url'])); + $user->user_url = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url; + } + if (isset ($_POST['first_name'])) + $user->first_name = wp_specialchars(trim($_POST['first_name'])); + if (isset ($_POST['last_name'])) + $user->last_name = wp_specialchars(trim($_POST['last_name'])); + if (isset ($_POST['nickname'])) + $user->nickname = wp_specialchars(trim($_POST['nickname'])); + if (isset ($_POST['display_name'])) + $user->display_name = wp_specialchars(trim($_POST['display_name'])); + if (isset ($_POST['description'])) + $user->description = wp_specialchars(trim($_POST['description'])); + if (isset ($_POST['jabber'])) + $user->jabber = wp_specialchars(trim($_POST['jabber'])); + if (isset ($_POST['aim'])) + $user->aim = wp_specialchars(trim($_POST['aim'])); + if (isset ($_POST['yim'])) + $user->yim = wp_specialchars(trim($_POST['yim'])); + + $errors = array (); + + /* checking that username has been typed */ + if ($user->user_login == '') + $errors['user_login'] = __('ERROR: Please enter a username.'); + + /* checking the password has been typed twice */ + do_action('check_passwords', array ($user->user_login, & $pass1, & $pass2)); + + if (!$update) { + if ($pass1 == '' || $pass2 == '') + $errors['pass'] = __('ERROR: Please enter your password twice.'); + } else { + if ((empty ($pass1) && !empty ($pass2)) || (empty ($pass2) && !empty ($pass1))) + $errors['pass'] = __("ERROR: you typed your new password only once."); + } + + /* Check for "\" in password */ + if( strpos( " ".$pass1, "\\" ) ) + $errors['pass'] = __('ERROR: Passwords may not contain the character "\\".'); + + /* checking the password has been typed twice the same */ + if ($pass1 != $pass2) + $errors['pass'] = __('ERROR: Please type the same password in the two password fields.'); + + if (!empty ($pass1)) + $user->user_pass = $pass1; + + if (!$update && username_exists($user->user_login)) + $errors['user_login'] = __('ERROR: This username is already registered, please choose another one.'); + + /* checking e-mail address */ + if (empty ($user->user_email)) { + $errors['user_email'] = __("ERROR: please type an e-mail address"); + } else + if (!is_email($user->user_email)) { + $errors['user_email'] = __("ERROR: the email address isn't correct"); + } + + if (count($errors) != 0) + return $errors; + + if ($update) { + $user_id = wp_update_user(get_object_vars($user)); + } else { + $user_id = wp_insert_user(get_object_vars($user)); + wp_new_user_notification($user_id); + } + + return $errors; +} + + +function get_link_to_edit($link_id) { + $link = get_link($link_id); + + $link->link_url = wp_specialchars($link->link_url, 1); + $link->link_name = wp_specialchars($link->link_name, 1); + $link->link_description = wp_specialchars($link->link_description); + $link->link_notes = wp_specialchars($link->link_notes); + $link->link_rss = wp_specialchars($link->link_rss); + + return $link; +} + +function get_default_link_to_edit() { + if ( isset($_GET['linkurl']) ) + $link->link_url = wp_specialchars($_GET['linkurl'], 1); + else + $link->link_url = ''; + + if ( isset($_GET['name']) ) + $link->link_name = wp_specialchars($_GET['name'], 1); + else + $link->link_name = ''; + + return $link; +} + +function add_link() { + return edit_link(); +} + +function edit_link($link_id = '') { + if (!current_user_can('manage_links')) + die(__("Cheatin' uh ?")); + + $_POST['link_url'] = wp_specialchars($_POST['link_url']); + $_POST['link_url'] = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $_POST['link_url']) ? $_POST['link_url'] : 'http://' . $_POST['link_url']; + $_POST['link_name'] = wp_specialchars($_POST['link_name']); + $_POST['link_image'] = wp_specialchars($_POST['link_image']); + $_POST['link_rss'] = wp_specialchars($_POST['link_rss']); + $auto_toggle = get_autotoggle($_POST['link_category']); + + // if we are in an auto toggle category and this one is visible then we + // need to make the others invisible before we add this new one. + // FIXME Add category toggle func. + //if (($auto_toggle == 'Y') && ($link_visible == 'Y')) { + // $wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category"); + //} + + if ( !empty($link_id) ) { + $_POST['link_id'] = $link_id; + return wp_update_link($_POST); + } else { + return wp_insert_link($_POST); + } +} + +function url_shorten($url) { + $short_url = str_replace('http://', '', stripslashes($url)); + $short_url = str_replace('www.', '', $short_url); + if ('/' == substr($short_url, -1)) + $short_url = substr($short_url, 0, -1); + if (strlen($short_url) > 35) + $short_url = substr($short_url, 0, 32).'...'; + return $short_url; +} + +function selected($selected, $current) { + if ($selected == $current) + echo ' selected="selected"'; +} + +function checked($checked, $current) { + if ($checked == $current) + echo ' checked="checked"'; +} + +function return_categories_list($parent = 0) { + global $wpdb; + return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY category_count DESC LIMIT 100"); +} + +function sort_cats($cat1, $cat2) { + return strcasecmp($cat1['cat_name'], $cat2['cat_name']); +} + +function get_nested_categories($default = 0, $parent = 0) { + global $post_ID, $mode, $wpdb; + + if ($post_ID) { + $checked_categories = $wpdb->get_col(" + SELECT category_id + FROM $wpdb->categories, $wpdb->post2cat + WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' + "); + + if (count($checked_categories) == 0) { + // No selected categories, strange + $checked_categories[] = $default; + } + + } else { + $checked_categories[] = $default; + } + + $cats = return_categories_list($parent); + $result = array (); + + if (is_array($cats)) { + foreach ($cats as $cat) { + $result[$cat]['children'] = get_nested_categories($default, $cat); + $result[$cat]['cat_ID'] = $cat; + $result[$cat]['checked'] = in_array($cat, $checked_categories); + $result[$cat]['cat_name'] = get_the_category_by_ID($cat); + } + } + + usort($result, 'sort_cats'); + + return $result; +} + +function write_nested_categories($categories) { + foreach ($categories as $category) { + echo '\n"; + + if (isset ($category['children'])) { + echo "\n\n"; + write_nested_categories($category['children']); + echo "\n"; + } + } +} + +function dropdown_categories($default = 0) { + write_nested_categories(get_nested_categories($default)); +} + +// Dandy new recursive multiple category stuff. +function cat_rows($parent = 0, $level = 0, $categories = 0) { + global $wpdb, $class; + + if (!$categories) + $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name"); + + if ($categories) { + foreach ($categories as $category) { + if ($category->category_parent == $parent) { + $category->cat_name = wp_specialchars($category->cat_name); + $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID"); + $pad = str_repeat('— ', $level); + if ( current_user_can('manage_categories') ) { + $edit = "".__('Edit').""; + $default_cat_id = get_option('default_category'); + + if ($category->cat_ID != $default_cat_id) + $edit .= "cat_ID, '".sprintf(__("You are about to delete the category "%s". All of its posts will go to the default category.\\n"OK" to delete, "Cancel" to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete').""; + else + $edit .= "".__("Default"); + } + else + $edit = ''; + + $class = ('alternate' == $class) ? '' : 'alternate'; + echo "$category->cat_ID$pad $category->cat_name + $category->category_description + $count + $edit + "; + cat_rows($category->cat_ID, $level +1, $categories); + } + } + } else { + return false; + } +} + +function page_rows($parent = 0, $level = 0, $pages = 0) { + global $wpdb, $class, $post; + if (!$pages) + $pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'static' ORDER BY menu_order"); + + if ($pages) { + foreach ($pages as $post) { + start_wp(); + if ($post->post_parent == $parent) { + $post->post_title = wp_specialchars($post->post_title); + $pad = str_repeat('— ', $level); + $id = $post->ID; + $class = ('alternate' == $class) ? '' : 'alternate'; +?> + + ID; ?> + + + + + post_modified); ?> + + " . __('Edit') . ""; } ?> + " . __('Delete') . ""; } ?> + + +get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name"); + } + if ($categories) { + foreach ($categories as $category) { + if ($currentcat != $category->cat_ID && $parent == $category->category_parent) { + $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID"); + $pad = str_repeat('– ', $level); + $category->cat_name = wp_specialchars($category->cat_name); + echo "\n\t"; + wp_dropdown_cats($currentcat, $currentparent, $category->cat_ID, $level +1, $categories); + } + } + } else { + return false; + } +} + +function link_category_dropdown($fieldname, $selected = 0) { + global $wpdb; + + $results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id"); + echo "\n\n"; +} + +function wp_create_thumbnail($file, $max_side, $effect = '') { + + // 1 = GIF, 2 = JPEG, 3 = PNG + + if (file_exists($file)) { + $type = getimagesize($file); + + // if the associated function doesn't exist - then it's not + // handle. duh. i hope. + + if (!function_exists('imagegif') && $type[2] == 1) { + $error = __('Filetype not supported. Thumbnail not created.'); + } + elseif (!function_exists('imagejpeg') && $type[2] == 2) { + $error = __('Filetype not supported. Thumbnail not created.'); + } + elseif (!function_exists('imagepng') && $type[2] == 3) { + $error = __('Filetype not supported. Thumbnail not created.'); + } else { + + // create the initial copy from the original file + if ($type[2] == 1) { + $image = imagecreatefromgif($file); + } + elseif ($type[2] == 2) { + $image = imagecreatefromjpeg($file); + } + elseif ($type[2] == 3) { + $image = imagecreatefrompng($file); + } + + if (function_exists('imageantialias')) + imageantialias($image, TRUE); + + $image_attr = getimagesize($file); + + // figure out the longest side + + if ($image_attr[0] > $image_attr[1]) { + $image_width = $image_attr[0]; + $image_height = $image_attr[1]; + $image_new_width = $max_side; + + $image_ratio = $image_width / $image_new_width; + $image_new_height = $image_height / $image_ratio; + //width is > height + } else { + $image_width = $image_attr[0]; + $image_height = $image_attr[1]; + $image_new_height = $max_side; + + $image_ratio = $image_height / $image_new_height; + $image_new_width = $image_width / $image_ratio; + //height > width + } + + $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height); + @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]); + + // If no filters change the filename, we'll do a default transformation. + if ( basename($file) == $thumb = apply_filters('thumbnail_filename', basename($file)) ) + $thumb = preg_replace('!(\.[^.]+)?$!', __('.thumbnail').'$1', basename($file), 1); + + $thumbpath = str_replace(basename($file), $thumb, $file); + + // move the thumbnail to it's final destination + if ($type[2] == 1) { + if (!imagegif($thumbnail, $thumbpath)) { + $error = __("Thumbnail path invalid"); + } + } + elseif ($type[2] == 2) { + if (!imagejpeg($thumbnail, $thumbpath)) { + $error = __("Thumbnail path invalid"); + } + } + elseif ($type[2] == 3) { + if (!imagepng($thumbnail, $thumbpath)) { + $error = __("Thumbnail path invalid"); + } + } + + } + } else { + $error = __('File not found'); + } + + if (!empty ($error)) { + return $error; + } else { + return $thumbpath; + } +} + +// Some postmeta stuff +function has_meta($postid) { + global $wpdb; + + return $wpdb->get_results(" + SELECT meta_key, meta_value, meta_id, post_id + FROM $wpdb->postmeta + WHERE post_id = '$postid' + ORDER BY meta_key,meta_id", ARRAY_A); + +} + +function list_meta($meta) { + global $post_ID; + // Exit if no meta + if (!$meta) + return; + $count = 0; +?> + + + + + + + + + + + + "; + } + echo " +

    +
    + "; +} + +// Get a list of previously defined keys +function get_meta_keys() { + global $wpdb; + + $keys = $wpdb->get_col(" + SELECT meta_key + FROM $wpdb->postmeta + GROUP BY meta_key + ORDER BY meta_key"); + + return $keys; +} + +function meta_form() { + global $wpdb; + $keys = $wpdb->get_col(" + SELECT meta_key + FROM $wpdb->postmeta + GROUP BY meta_key + ORDER BY meta_id DESC + LIMIT 10"); +?> +

    + + + + + + + + + + + +
    + + + +
    +

    +escape(stripslashes(trim($_POST['metakeyselect']))); + $metakeyinput = $wpdb->escape(stripslashes(trim($_POST['metakeyinput']))); + $metavalue = $wpdb->escape(stripslashes(trim($_POST['metavalue']))); + + if ( ('0' === $metavalue || !empty ($metavalue)) && ((('#NONE#' != $metakeyselect) && !empty ($metakeyselect)) || !empty ($metakeyinput)) ) { + // We have a key/value pair. If both the select and the + // input for the key have data, the input takes precedence: + + if ('#NONE#' != $metakeyselect) + $metakey = $metakeyselect; + + if ($metakeyinput) + $metakey = $metakeyinput; // default + + $result = $wpdb->query(" + INSERT INTO $wpdb->postmeta + (post_id,meta_key,meta_value) + VALUES ('$post_ID','$metakey','$metavalue') + "); + } +} // add_meta + +function delete_meta($mid) { + global $wpdb; + + $result = $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'"); +} + +function update_meta($mid, $mkey, $mvalue) { + global $wpdb; + + return $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'"); +} + +function touch_time($edit = 1, $for_post = 1) { + global $month, $post, $comment; + + if ( $for_post ) + $edit = ( ('draft' == $post->post_status) && (!$post->post_date || '0000-00-00 00:00:00' == $post->post_date) ) ? false : true; + + echo '
    '; + + $time_adj = time() + (get_settings('gmt_offset') * 3600); + $post_date = ($for_post) ? $post->post_date : $comment->comment_date; + $jj = ($edit) ? mysql2date('d', $post_date) : gmdate('d', $time_adj); + $mm = ($edit) ? mysql2date('m', $post_date) : gmdate('m', $time_adj); + $aa = ($edit) ? mysql2date('Y', $post_date) : gmdate('Y', $time_adj); + $hh = ($edit) ? mysql2date('H', $post_date) : gmdate('H', $time_adj); + $mn = ($edit) ? mysql2date('i', $post_date) : gmdate('i', $time_adj); + $ss = ($edit) ? mysql2date('s', $post_date) : gmdate('s', $time_adj); + + echo " + + @ + : + + + +
    + enable sending referrers for this feature to work.')); + do_action('check_admin_referer'); +} + +// insert_with_markers: Owen Winkler, fixed by Eric Anderson +// Inserts an array of strings into a file (.htaccess), placing it between +// BEGIN and END markers. Replaces existing marked info. Retains surrounding +// data. Creates file if none exists. +// Returns true on write success, false on failure. +function insert_with_markers($filename, $marker, $insertion) { + if (!file_exists($filename) || is_writeable($filename)) { + if (!file_exists($filename)) { + $markerdata = ''; + } else { + $markerdata = explode("\n", implode('', file($filename))); + } + + $f = fopen($filename, 'w'); + $foundit = false; + if ($markerdata) { + $state = true; + foreach ($markerdata as $markerline) { + if (strstr($markerline, "# BEGIN {$marker}")) + $state = false; + if ($state) + fwrite($f, "{$markerline}\n"); + if (strstr($markerline, "# END {$marker}")) { + fwrite($f, "# BEGIN {$marker}\n"); + if (is_array($insertion)) + foreach ($insertion as $insertline) + fwrite($f, "{$insertline}\n"); + fwrite($f, "# END {$marker}\n"); + $state = true; + $foundit = true; + } + } + } + if (!$foundit) { + fwrite($f, "# BEGIN {$marker}\n"); + foreach ($insertion as $insertline) + fwrite($f, "{$insertline}\n"); + fwrite($f, "# END {$marker}\n"); + } + fclose($f); + return true; + } else { + return false; + } +} + +// extract_from_markers: Owen Winkler +// Returns an array of strings from a file (.htaccess) from between BEGIN +// and END markers. +function extract_from_markers($filename, $marker) { + $result = array (); + + if (!file_exists($filename)) { + return $result; + } + + if ($markerdata = explode("\n", implode('', file($filename)))); + { + $state = false; + foreach ($markerdata as $markerline) { + if (strstr($markerline, "# END {$marker}")) + $state = false; + if ($state) + $result[] = $markerline; + if (strstr($markerline, "# BEGIN {$marker}")) + $state = true; + } + } + + return $result; +} + +function got_mod_rewrite() { + global $is_apache; + + // take 3 educated guesses as to whether or not mod_rewrite is available + if ( !$is_apache ) + return false; + + if ( function_exists('apache_get_modules') ) { + if ( !in_array('mod_rewrite', apache_get_modules()) ) + return false; + } + + return true; +} + +function save_mod_rewrite_rules() { + global $is_apache, $wp_rewrite; + $home_path = get_home_path(); + + if (!$wp_rewrite->using_mod_rewrite_permalinks()) + return; + + if (!((!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess'))) + return; + + if (! got_mod_rewrite()) + return; + + $rules = explode("\n", $wp_rewrite->mod_rewrite_rules()); + insert_with_markers($home_path.'.htaccess', 'WordPress', $rules); +} + +function the_quicktags() { + // Browser detection sucks, but until Safari supports the JS needed for this to work people just assume it's a bug in WP + if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Safari')) + echo ' +
    + + +
    +'; + else echo ' + +'; +} + +function validate_current_theme() { + $theme_loc = 'wp-content/themes'; + $theme_root = ABSPATH.$theme_loc; + + $template = get_settings('template'); + $stylesheet = get_settings('stylesheet'); + + if (($template != 'default') && (!file_exists("$theme_root/$template/index.php"))) { + update_option('template', 'default'); + update_option('stylesheet', 'default'); + do_action('switch_theme', 'Default'); + return false; + } + + if (($stylesheet != 'default') && (!file_exists("$theme_root/$stylesheet/style.css"))) { + update_option('template', 'default'); + update_option('stylesheet', 'default'); + do_action('switch_theme', 'Default'); + return false; + } + + return true; +} + +function get_broken_themes() { + global $wp_broken_themes; + + get_themes(); + return $wp_broken_themes; +} + +function get_page_templates() { + $themes = get_themes(); + $theme = get_current_theme(); + $templates = $themes[$theme]['Template Files']; + $page_templates = array (); + + if (is_array($templates)) { + foreach ($templates as $template) { + $template_data = implode('', file(ABSPATH.$template)); + preg_match("|Template Name:(.*)|i", $template_data, $name); + preg_match("|Description:(.*)|i", $template_data, $description); + + $name = $name[1]; + $description = $description[1]; + + if (!empty ($name)) { + $page_templates[trim($name)] = basename($template); + } + } + } + + return $page_templates; +} + +function page_template_dropdown($default = '') { + $templates = get_page_templates(); + foreach (array_keys($templates) as $template) + : if ($default == $templates[$template]) + $selected = " selected='selected'"; + else + $selected = ''; + echo "\n\t"; + endforeach; +} + +function parent_dropdown($default = 0, $parent = 0, $level = 0) { + global $wpdb, $post_ID; + $items = $wpdb->get_results("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_status = 'static' ORDER BY menu_order"); + + if ($items) { + foreach ($items as $item) { + // A page cannot be it's own parent. + if (!empty ($post_ID)) { + if ($item->ID == $post_ID) { + continue; + } + } + $pad = str_repeat(' ', $level * 3); + if ($item->ID == $default) + $current = ' selected="selected"'; + else + $current = ''; + + echo "\n\t"; + parent_dropdown($default, $item->ID, $level +1); + } + } else { + return false; + } +} + +function user_can_access_admin_page() { + global $pagenow; + global $menu; + global $submenu; + + $parent = get_admin_page_parent(); + + foreach ($menu as $menu_array) { + //echo "parent array: " . $menu_array[2]; + if ($menu_array[2] == $parent) { + if (!current_user_can($menu_array[1])) { + return false; + } else { + break; + } + } + } + + if (isset ($submenu[$parent])) { + foreach ($submenu[$parent] as $submenu_array) { + if ($submenu_array[2] == $pagenow) { + if (!current_user_can($submenu_array[1])) { + return false; + } else { + return true; + } + } + } + } + + return true; +} + +function get_admin_page_title() { + global $title; + global $menu; + global $submenu; + global $pagenow; + global $plugin_page; + + if (isset ($title) && !empty ($title)) { + return $title; + } + + $parent = get_admin_page_parent(); + if (empty ($parent)) { + foreach ($menu as $menu_array) { + if (isset ($menu_array[3])) { + if ($menu_array[2] == $pagenow) { + $title = $menu_array[3]; + return $menu_array[3]; + } else + if (isset ($plugin_page) && ($plugin_page == $menu_array[2])) { + $title = $menu_array[3]; + return $menu_array[3]; + } + } + } + } else { + foreach (array_keys($submenu) as $parent) { + foreach ($submenu[$parent] as $submenu_array) { + if (isset ($submenu_array[3])) { + if ($submenu_array[2] == $pagenow) { + $title = $submenu_array[3]; + return $submenu_array[3]; + } else + if (isset ($plugin_page) && ($plugin_page == $submenu_array[2])) { + $title = $submenu_array[3]; + return $submenu_array[3]; + } + } + } + } + } + + return ''; +} + +function get_admin_page_parent() { + global $parent_file; + global $menu; + global $submenu; + global $pagenow; + global $plugin_page; + + if (isset ($parent_file) && !empty ($parent_file)) { + return $parent_file; + } + + if ($pagenow == 'admin.php' && isset ($plugin_page)) { + foreach ($menu as $parent_menu) { + if ($parent_menu[2] == $plugin_page) { + $parent_file = $plugin_page; + return $plugin_page; + } + } + } + + foreach (array_keys($submenu) as $parent) { + foreach ($submenu[$parent] as $submenu_array) { + if ($submenu_array[2] == $pagenow) { + $parent_file = $parent; + return $parent; + } else + if (isset ($plugin_page) && ($plugin_page == $submenu_array[2])) { + $parent_file = $parent; + return $parent; + } + } + } + + $parent_file = ''; + return ''; +} + +function add_menu_page($page_title, $menu_title, $access_level, $file, $function = '') { + global $menu, $admin_page_hooks; + + $file = plugin_basename($file); + + $menu[] = array ($menu_title, $access_level, $file, $page_title); + + $admin_page_hooks[$file] = sanitize_title($menu_title); + + $hookname = get_plugin_page_hookname($file, ''); + if (!empty ($function) && !empty ($hookname)) + add_action($hookname, $function); + + return $hookname; +} + +function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file, $function = '') { + global $submenu; + global $menu; + + $parent = plugin_basename($parent); + $file = plugin_basename($file); + + // If the parent doesn't already have a submenu, add a link to the parent + // as the first item in the submenu. If the submenu file is the same as the + // parent file someone is trying to link back to the parent manually. In + // this case, don't automatically add a link back to avoid duplication. + if (!isset ($submenu[$parent]) && $file != $parent) { + foreach ($menu as $parent_menu) { + if ($parent_menu[2] == $parent) { + $submenu[$parent][] = $parent_menu; + } + } + } + + $submenu[$parent][] = array ($menu_title, $access_level, $file, $page_title); + + $hookname = get_plugin_page_hookname($file, $parent); + if (!empty ($function) && !empty ($hookname)) + add_action($hookname, $function); + + return $hookname; +} + +function add_options_page($page_title, $menu_title, $access_level, $file, $function = '') { + return add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file, $function); +} + +function add_management_page($page_title, $menu_title, $access_level, $file, $function = '') { + return add_submenu_page('edit.php', $page_title, $menu_title, $access_level, $file, $function); +} + +function add_theme_page($page_title, $menu_title, $access_level, $file, $function = '') { + return add_submenu_page('themes.php', $page_title, $menu_title, $access_level, $file, $function); +} + +function validate_file($file, $allowed_files = '') { + if (false !== strpos($file, './')) + return 1; + + if (':' == substr($file, 1, 1)) + return 2; + + if (!empty ($allowed_files) && (!in_array($file, $allowed_files))) + return 3; + + return 0; +} + +function validate_file_to_edit($file, $allowed_files = '') { + $file = stripslashes($file); + + $code = validate_file($file, $allowed_files); + + if (!$code) + return $file; + + switch ($code) { + case 1 : + die(__('Sorry, can’t edit files with ".." in the name. If you are trying to edit a file in your WordPress home directory, you can just type the name of the file in.')); + + case 2 : + die(__('Sorry, can’t call files with their real path.')); + + case 3 : + die(__('Sorry, that file cannot be edited.')); + } +} + +function get_home_path() { + $home = get_settings('home'); + if ($home != '' && $home != get_settings('siteurl')) { + $home_path = parse_url($home); + $home_path = $home_path['path']; + $root = str_replace($_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"]); + $home_path = trailingslashit($root.$home_path); + } else { + $home_path = ABSPATH; + } + + return $home_path; +} + +function get_real_file_to_edit($file) { + if ('index.php' == $file || '.htaccess' == $file) { + $real_file = get_home_path().$file; + } else { + $real_file = ABSPATH.$file; + } + + return $real_file; +} + +$wp_file_descriptions = array ('index.php' => __('Main Index Template'), 'style.css' => __('Stylesheet'), 'comments.php' => __('Comments'), 'comments-popup.php' => __('Popup Comments'), 'footer.php' => __('Footer'), 'header.php' => __('Header'), 'sidebar.php' => __('Sidebar'), 'archive.php' => __('Archives'), 'category.php' => __('Category Template'), 'page.php' => __('Page Template'), 'search.php' => __('Search Results'), 'single.php' => __('Single Post'), '404.php' => __('404 Template'), 'my-hacks.php' => __('my-hacks.php (legacy hacks support)'), '.htaccess' => __('.htaccess (for rewrite rules)'), + // Deprecated files + 'wp-layout.css' => __('Stylesheet'), 'wp-comments.php' => __('Comments Template'), 'wp-comments-popup.php' => __('Popup Comments Template')); + +function get_file_description($file) { + global $wp_file_descriptions; + + if (isset ($wp_file_descriptions[basename($file)])) { + return $wp_file_descriptions[basename($file)]; + } + elseif (file_exists(ABSPATH.$file)) { + $template_data = implode('', file(ABSPATH.$file)); + if (preg_match("|Template Name:(.*)|i", $template_data, $name)) + return $name[1]; + } + + return basename($file); +} + +function update_recently_edited($file) { + $oldfiles = (array) get_option('recently_edited'); + if ($oldfiles) { + $oldfiles = array_reverse($oldfiles); + $oldfiles[] = $file; + $oldfiles = array_reverse($oldfiles); + $oldfiles = array_unique($oldfiles); + if (5 < count($oldfiles)) + array_pop($oldfiles); + } else { + $oldfiles[] = $file; + } + update_option('recently_edited', $oldfiles); +} + +function get_plugin_data($plugin_file) { + $plugin_data = implode('', file($plugin_file)); + preg_match("|Plugin Name:(.*)|i", $plugin_data, $plugin_name); + preg_match("|Plugin URI:(.*)|i", $plugin_data, $plugin_uri); + preg_match("|Description:(.*)|i", $plugin_data, $description); + preg_match("|Author:(.*)|i", $plugin_data, $author_name); + preg_match("|Author URI:(.*)|i", $plugin_data, $author_uri); + if (preg_match("|Version:(.*)|i", $plugin_data, $version)) + $version = $version[1]; + else + $version = ''; + + $description = wptexturize($description[1]); + + $name = $plugin_name[1]; + $name = trim($name); + $plugin = $name; + if ('' != $plugin_uri[1] && '' != $name) { + $plugin = ''.$plugin.''; + } + + if ('' == $author_uri[1]) { + $author = $author_name[1]; + } else { + $author = ''.$author_name[1].''; + } + + return array ('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]); +} + +function get_plugins() { + global $wp_plugins; + + if (isset ($wp_plugins)) { + return $wp_plugins; + } + + $wp_plugins = array (); + $plugin_loc = 'wp-content/plugins'; + $plugin_root = ABSPATH.$plugin_loc; + + // Files in wp-content/plugins directory + $plugins_dir = @ dir($plugin_root); + if ($plugins_dir) { + while (($file = $plugins_dir->read()) !== false) { + if (preg_match('|^\.+$|', $file)) + continue; + if (is_dir($plugin_root.'/'.$file)) { + $plugins_subdir = @ dir($plugin_root.'/'.$file); + if ($plugins_subdir) { + while (($subfile = $plugins_subdir->read()) !== false) { + if (preg_match('|^\.+$|', $subfile)) + continue; + if (preg_match('|\.php$|', $subfile)) + $plugin_files[] = "$file/$subfile"; + } + } + } else { + if (preg_match('|\.php$|', $file)) + $plugin_files[] = $file; + } + } + } + + if (!$plugins_dir || !$plugin_files) { + return $wp_plugins; + } + + sort($plugin_files); + + foreach ($plugin_files as $plugin_file) { + if ( !is_readable("$plugin_root/$plugin_file")) + continue; + + $plugin_data = get_plugin_data("$plugin_root/$plugin_file"); + + if (empty ($plugin_data['Name'])) { + continue; + } + + $wp_plugins[plugin_basename($plugin_file)] = $plugin_data; + } + + return $wp_plugins; +} + +function get_plugin_page_hookname($plugin_page, $parent_page) { + global $admin_page_hooks; + + $parent = get_admin_page_parent(); + + if (empty ($parent_page) || 'admin.php' == $parent_page) { + if (isset ($admin_page_hooks[$plugin_page])) + $page_type = 'toplevel'; + else + if (isset ($admin_page_hooks[$parent])) + $page_type = $admin_page_hooks[$parent]; + } else + if (isset ($admin_page_hooks[$parent_page])) { + $page_type = $admin_page_hooks[$parent_page]; + } else { + $page_type = 'admin'; + } + + $plugin_name = preg_replace('!\.php!', '', $plugin_page); + + return $page_type.'_page_'.$plugin_name; +} + +function get_plugin_page_hook($plugin_page, $parent_page) { + global $wp_filter; + + $hook = get_plugin_page_hookname($plugin_page, $parent_page); + if (isset ($wp_filter[$hook])) + return $hook; + else + return ''; +} + +function browse_happy() { + $getit = __('WordPress recommends a better browser'); + echo ' +

    Browse Happy

    + '; +} +if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) + add_action('admin_footer', 'browse_happy'); + +function documentation_link($for) { + return; +} + +function register_importer($id, $name, $description, $callback) { + global $wp_importers; + + $wp_importers[$id] = array ($name, $description, $callback); +} + +function get_importers() { + global $wp_importers; + + return $wp_importers; +} + +function current_theme_info() { + $themes = get_themes(); + $current_theme = get_current_theme(); + $ct->title = $themes[$current_theme]['Title']; + $ct->version = $themes[$current_theme]['Version']; + $ct->parent_theme = $themes[$current_theme]['Parent Theme']; + $ct->template_dir = $themes[$current_theme]['Template Dir']; + $ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir']; + $ct->template = $themes[$current_theme]['Template']; + $ct->stylesheet = $themes[$current_theme]['Stylesheet']; + $ct->screenshot = $themes[$current_theme]['Screenshot']; + $ct->description = $themes[$current_theme]['Description']; + $ct->author = $themes[$current_theme]['Author']; + return $ct; +} + + +// array wp_handle_upload ( array &file [, array overrides] ) +// file: reference to a single element of $_FILES. Call the function once for each uploaded file. +// overrides: an associative array of names=>values to override default variables with extract($overrides, EXTR_OVERWRITE). +// On success, returns an associative array of file attributes. +// On failure, returns $overrides['upload_error_handler'](&$file, $message) or array('error'=>$message). +function wp_handle_upload(&$file, $overrides = false) { + // The default error handler. + if (! function_exists('wp_handle_upload_error') ) { + function wp_handle_upload_error(&$file, $message) { + return array('error'=>$message); + } + } + + // You may define your own function and pass the name in $overrides['upload_error_handler'] + $upload_error_handler = 'wp_handle_upload_error'; + + // $_POST['action'] must be set and its value must equal $overrides['action'] or this: + $action = 'wp_handle_upload'; + + // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error']. + $upload_error_strings = array(false, + __("The uploaded file exceeds the upload_max_filesize directive in php.ini."), + __("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."), + __("The uploaded file was only partially uploaded."), + __("No file was uploaded."), + __("Missing a temporary folder."), + __("Failed to write file to disk.")); + + // Accepted MIME types are set here as PCRE. Override with $override['mimes']. + $mimes = apply_filters('upload_mimes', array ( + 'jpg|jpeg|jpe' => 'image/jpeg', + 'gif' => 'image/gif', + 'png' => 'image/png', + 'bmp' => 'image/bmp', + 'tif|tiff' => 'image/tiff', + 'ico' => 'image/x-icon', + 'asf|asx|wax|wmv|wmx' => 'video/asf', + 'avi' => 'video/avi', + 'mov|qt' => 'video/quicktime', + 'mpeg|mpg|mpe' => 'video/mpeg', + 'txt|c|cc|h|php' => 'text/plain', + 'rtx' => 'text/richtext', + 'css' => 'text/css', + 'htm|html' => 'text/html', + 'mp3|mp4' => 'audio/mpeg', + 'ra|ram' => 'audio/x-realaudio', + 'wav' => 'audio/wav', + 'ogg' => 'audio/ogg', + 'mid|midi' => 'audio/midi', + 'wma' => 'audio/wma', + 'rtf' => 'application/rtf', + 'js' => 'application/javascript', + 'pdf' => 'application/pdf', + 'doc' => 'application/msword', + 'pot|pps|ppt' => 'application/vnd.ms-powerpoint', + 'wri' => 'application/vnd.ms-write', + 'xla|xls|xlt|xlw' => 'application/vnd.ms-excel', + 'mdb' => 'application/vnd.ms-access', + 'mpp' => 'application/vnd.ms-project', + 'swf' => 'application/x-shockwave-flash', + 'class' => 'application/java', + 'tar' => 'application/x-tar', + 'zip' => 'application/zip', + 'gz|gzip' => 'application/x-gzip', + 'exe' => 'application/x-msdownload' + )); + + // All tests are on by default. Most can be turned off by $override[{test_name}] = false; + $test_form = true; + $test_size = true; + + // If you override this, you must provide $ext and $type!!!! + $test_type = true; + + // Install user overrides. Did we mention that this voids your warranty? + if ( is_array($overrides) ) + extract($overrides, EXTR_OVERWRITE); + + // A correct form post will pass this test. + if ( $test_form && (!isset($_POST['action']) || ($_POST['action'] != $action)) ) + return $upload_error_handler($file, __('Invalid form submission.')); + + // A successful upload will pass this test. It makes no sense to override this one. + if ( $file['error'] > 0 ) + return $upload_error_handler($file, $upload_error_strings[$file['error']]); + + // A non-empty file will pass this test. + if ( $test_size && !($file['size'] > 0) ) + return $upload_error_handler($file, __('File is empty. Please upload something more substantial.')); + + // A properly uploaded file will pass this test. There should be no reason to override this one. + if (! is_uploaded_file($file['tmp_name']) ) + return $upload_error_handler($file, __('Specified file failed upload test.')); + + // A correct MIME type will pass this test. + if ( $test_type ) { + $type = false; + $ext = false; + foreach ($mimes as $ext_preg => $mime_match) { + $ext_preg = '![^.]\.(' . $ext_preg . ')$!i'; + if ( preg_match($ext_preg, $file['name'], $ext_matches) ) { + $type = $mime_match; + $ext = $ext_matches[1]; + } + } + + if ( !$type || !$ext ) + return $upload_error_handler($file, __('File type does not meet security guidelines. Try another.')); + } + + // A writable uploads dir will pass this test. Again, there's no point overriding this one. + if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) + return $upload_error_handler($file, $uploads['error']); + + // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied. + if ( isset($unique_filename_callback) && function_exists($unique_filename_callback) ) { + $filename = $unique_filename_callback($uploads['path'], $file['name']); + } else { + $number = ''; + $filename = $file['name']; + while ( file_exists($uploads['path'] . "/$filename") ) + $filename = str_replace("$number.$ext", ++$number . ".$ext", $filename); + } + + // Move the file to the uploads dir + $new_file = $uploads['path'] . "/$filename"; + if ( false === move_uploaded_file($file['tmp_name'], $new_file) ) + die(printf(__('The uploaded file could not be moved to %s.'), $file['path'])); + + // Set correct file permissions + $stat = stat(dirname($new_file)); + $perms = $stat['mode'] & 0000777; + @ chmod($new_file, $perms); + + // Compute the URL + $url = $uploads['url'] . "/$filename"; + + return array('file' => $new_file, 'url' => $url, 'type' => $type); +} + +function wp_shrink_dimensions($width, $height, $wmax = 128, $hmax = 96) { + if ( $height <= $hmax && $width <= $wmax ) + return array($width, $height); + elseif ( $width / $height > $wmax / $hmax ) + return array($wmax, (int) ($height / $width * $wmax)); + else + return array((int) ($width / $height * $hmax), $hmax); +} + +function wp_import_cleanup($id) { + wp_delete_attachment($id); +} + +function wp_import_upload_form($action) { +?> + +
    + + +
    + + +
    +
    + false, 'test_type' => false); + $file = wp_handle_upload($_FILES['import'], $overrides); + + if ( isset($file['error']) ) + return $file; + + $url = $file['url']; + $file = $file['file']; + $filename = basename($file); + + // Construct the object array + $object = array( + 'post_title' => $filename, + 'post_content' => $url, + 'post_mime_type' => 'import', + 'guid' => $url + ); + + // Save the data + $id = wp_insert_attachment($object, $file); + + return array('file' => $file, 'id' => $id); +} + +function user_can_richedit() { + if ( 'true' != get_user_option('rich_editing') ) + return false; + + if ( preg_match('!opera[ /][2-8]|konqueror|safari!i', $_SERVER['HTTP_USER_AGENT']) ) + return false; + + return true; // Best guess +} + +function the_attachment_links($id = false) { + $id = (int) $id; + $post = & get_post($id); + + if ( $post->post_status != 'attachment' ) + return false; + + $icon = get_attachment_icon($post->ID); + +?> +


    +

    +


    +

    + +


    +

    +


    +

    + + 4 / 3 ) + return array(128, (int) ($height / $width * 128)); + else + return array((int) ($width / $height * 96), 96); +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/admin-header.php b/projects/tests/tests/real/wordpress/wp-admin/admin-header.php new file mode 100644 index 00000000..d9f56617 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/admin-header.php @@ -0,0 +1,329 @@ + + + + + +<?php bloginfo('name') ?> › <?php echo $title; ?> — WordPress + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    %s.'), $user_identity) ?> [, ]

    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/admin.php b/projects/tests/tests/real/wordpress/wp-admin/admin.php new file mode 100644 index 00000000..202fd2c9 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/admin.php @@ -0,0 +1,104 @@ +upgrade."), get_option('siteurl') . '/wp-admin/upgrade.php')); + +require_once(ABSPATH . 'wp-admin/admin-functions.php'); +require_once(ABSPATH . 'wp-admin/admin-db.php'); +require_once(ABSPATH . WPINC . '/registration-functions.php'); + +auth_redirect(); + +nocache_headers(); + +update_category_cache(); + +get_currentuserinfo(); + +$posts_per_page = get_settings('posts_per_page'); +$what_to_show = get_settings('what_to_show'); +$date_format = get_settings('date_format'); +$time_format = get_settings('time_format'); + +$wpvarstoreset = array('profile','redirect','redirect_url','a','popuptitle','popupurl','text', 'trackback', 'pingback'); +for ($i=0; $i diff --git a/projects/tests/tests/real/wordpress/wp-admin/bookmarklet.php b/projects/tests/tests/real/wordpress/wp-admin/bookmarklet.php new file mode 100644 index 00000000..195e35cc --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/bookmarklet.php @@ -0,0 +1,104 @@ + + + + + + + +post_title = stripslashes($post_title); +else + $post->post_title = $popuptitle; + + +$content = wp_specialchars($_REQUEST['content']); +$popupurl = wp_specialchars($_REQUEST['popupurl']); + if ( !empty($content) ) { + $post->post_content = wp_specialchars( stripslashes($_REQUEST['content']) ); + } else { + $post->post_content = ''.$popuptitle.''."\n$text"; + } + + /* /big funky fixes */ + +?> + + +<?php bloginfo('name') ?> › Bookmarklet — WordPress + + + + + + + + + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/categories.php b/projects/tests/tests/real/wordpress/wp-admin/categories.php new file mode 100644 index 00000000..219c39ff --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/categories.php @@ -0,0 +1,168 @@ +%s category: this is the default one"), $cat_name)); + + wp_delete_category($cat_ID); + + header('Location: categories.php?message=2'); + +break; + +case 'edit': + + require_once ('admin-header.php'); + $cat_ID = (int) $_GET['cat_ID']; + $category = get_category_to_edit($cat_ID); + ?> + +
    +

    +
    + + + + + + + + + + + + + + + + + +
    +
    +
    +

    +
    +

    +
    + + + +

    + + +
    + +

    add new)'), '#addcat') ?>

    + +

    + + + + + + + + + + +
    + +
    + +
    + + +
    +

    Note:
    Deleting a category does not delete posts from that category, it will just set them back to the default category %s.'), get_catname(get_option('default_category'))) ?>

    +
    + +
    +

    +
    + +


    +

    +


    +

    +


    +

    +

    +
    +
    + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-comments.php b/projects/tests/tests/real/wordpress/wp-admin/edit-comments.php new file mode 100644 index 00000000..b13c7f15 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-comments.php @@ -0,0 +1,171 @@ + + + +
    +

    +
    +
    + + + + + +
    +
    +

    |

    +get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = $comment"); + $authordata = get_userdata( $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $post_id") ); + if ( current_user_can('edit_post', $post_id) ) : + $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID = $comment"); + ++$i; + endif; + endforeach; + echo "

    " . sprintf(__('%s comments deleted.'), $i) . "

    "; +endif; + +if (isset($_GET['s'])) { + $s = $wpdb->escape($_GET['s']); + $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE + (comment_author LIKE '%$s%' OR + comment_author_email LIKE '%$s%' OR + comment_author_url LIKE ('%$s%') OR + comment_author_IP LIKE ('%$s%') OR + comment_content LIKE ('%$s%') ) AND + comment_approved != 'spam' + ORDER BY comment_date DESC"); +} else { + if ( isset($_GET['offset']) ) + $offset = (int) $_GET['offset'] * 20; + else + $offset = 0; + + $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '0' OR comment_approved = '1' ORDER BY comment_date DESC LIMIT $offset,20"); +} +if ('view' == $mode) { + if ($comments) { + if ($offset) + $start = " start='$offset'"; + else + $start = ''; + + echo "
      "; + $i = 0; + foreach ($comments as $comment) { + ++$i; $class = ''; + $authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $comment->comment_post_ID")); + $comment_status = wp_get_comment_status($comment->comment_ID); + if ('unapproved' == $comment_status) + $class .= ' unapproved'; + if ($i % 2) + $class .= ' alternate'; + echo "
    1. "; +?> +

      comment_author_email) { ?>| comment_author_url && 'http://' != $comment->comment_author_url ) { ?> | |

      + + + +

      comment_post_ID) ) { + echo " | comment_ID."\">" . __('Edit Comment') . ""; + echo " | comment_post_ID."&comment=".$comment->comment_ID."\" onclick=\"return deleteSomething( 'comment', $comment->comment_ID, '" . sprintf(__("You are about to delete this comment by "%s".\\n"Cancel" to stop, "OK" to delete."), wp_specialchars( $comment->comment_author, 1 )) . "' );\">" . __('Delete Comment') . " — "; + } // end if any comments to show + // Get post title + if ( current_user_can('edit_post', $comment->comment_post_ID) ) { + $post_title = $wpdb->get_var("SELECT post_title FROM $wpdb->posts WHERE ID = $comment->comment_post_ID"); + $post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title; + ?> + + |

      +
    2. + + +
    + +
    + + +

    +

    + + + + + + + + + + + '; + foreach ($comments as $comment) { + $authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $comment->comment_post_ID")); + $class = ('alternate' == $class) ? '' : 'alternate'; +?> + + + + + + + + + + +
    *' . __('Name') . '' . __('E-mail') . '' . __('IP') . '' . __('Comment Excerpt') . '' . __('Actions') . '
    comment_post_ID) ) { ?>comment_post_ID) ) { + echo "" . __('Edit') . ""; } ?>comment_post_ID) ) { + echo "comment_post_ID."&comment=".$comment->comment_ID."\" onclick=\"return confirm('" . sprintf(__("You are about to delete this comment by \'%s\'\\n \'Cancel\' to stop, \'OK\' to delete."), $comment->comment_author) . "')\" class='delete'>" . __('Delete') . ""; } ?>
    +

    +

    ')" />

    + + +

    + +

    + + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-form-advanced.php b/projects/tests/tests/real/wordpress/wp-admin/edit-form-advanced.php new file mode 100644 index 00000000..fa75a364 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-form-advanced.php @@ -0,0 +1,269 @@ + + +

    + + +
    + + + + +
    +

    +

    +"; +} else { + $form_action = 'editpost'; + $form_extra = ""; +} + +$form_pingback = ''; + +$form_prevstatus = ''; + +$form_trackback = 'to_ping) .'" />'; + +if ('' != $post->pinged) { + $pings = '

    '. __('Already pinged:') . '

      '; + $already_pinged = explode("\n", trim($post->pinged)); + foreach ($already_pinged as $pinged_url) { + $pings .= "\n\t
    • $pinged_url
    • "; + } + $pings .= '
    '; +} + +$saveasdraft = ''; + +if (empty($post->post_status)) $post->post_status = 'draft'; + +?> + + + + + + + $_GET['message']) : ?> + + +
    + +
    +
    + +
    +

    +
    + + + +
    +
    + +
    +

    +
    +
    + +
    +

    +
    +
    + +
    +

    +
    +

    +
    +
    + +
    +

    +
    + + + +
    +
    + + +
    +

    :

    +
    +
    + + +id ) ) : // TODO: ROLE SYSTEM ?> +
    +

    :

    +
    + +
    +
    + + +
    +
    + +
    + +
    +
    + +
    + + + 100)) { + $rows = 12; + } +?> + + +
    +
    + + + + + + + +

    +post_status || 0 == $post_ID) { +?> + + + + +

    + + + +' . __('This feature requires iframe support.') . ''; +?> + +
    + +
    +

    +
    +
    + +
    +

    +
    : () + +
    +
    + +
    +

    +
    + + + +
    +
    + +
    + + +post_title) ) . "')\""; ?> /> + + +
    + +
    + +
    diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-form-ajax-cat.php b/projects/tests/tests/real/wordpress/wp-admin/edit-form-ajax-cat.php new file mode 100644 index 00000000..d023f946 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-form-ajax-cat.php @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-form-comment.php b/projects/tests/tests/real/wordpress/wp-admin/edit-form-comment.php new file mode 100644 index 00000000..fb72058f --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-form-comment.php @@ -0,0 +1,122 @@ +comment_ID); +$form_action = 'editedcomment'; +$form_extra = "' />\n\n' /> + + +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    + +
    + + + + 100)) { + $rows = 10; + } +?> +
    +
    + + + +

    + +

    + + + +
    +

    + + + + + + + + + + + + + + + + + + +
    :
    +
    +
    :
    :

    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-form.php b/projects/tests/tests/real/wordpress/wp-admin/edit-form.php new file mode 100644 index 00000000..dd4bc758 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-form.php @@ -0,0 +1,75 @@ + +
    +

    +
    + + + + + + + + + +
    +
    + +
    +
    + +
    + +
    post_category); ?>
    +
    + +
    +
    + + + 100)) { + $rows = 10; + } +?> +
    +
    + + + + + + +

    (Separate multiple URIs with spaces.)
    '), 'http://wordpress.org/docs/reference/post/#trackback') ?> +

    + +

    + + + + + + +'; + } ?> + +

    + + + +
    +
    + +
    diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-link-form.php b/projects/tests/tests/real/wordpress/wp-admin/edit-link-form.php new file mode 100644 index 00000000..d782813e --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-link-form.php @@ -0,0 +1,236 @@ +'; +} else { + $editing = false; + $heading = __('Add a link:'); + $submit_text = __('Add Link »'); + $form = '
    '; +} + +function xfn_check($class, $value = '', $type = 'check') { + global $link; + + $link_rel = $link->link_rel; + $rels = preg_split('/\s+/', $link_rel); + + if ('' != $value && in_array($value, $rels) ) { + echo ' checked="checked"'; + } + + if ('' == $value) { + if ('family' == $class && !strstr($link_rel, 'child') && !strstr($link_rel, 'parent') && !strstr($link_rel, 'sibling') && !strstr($link_rel, 'spouse') && !strstr($link_rel, 'kin')) echo ' checked="checked"'; + if ('friendship' == $class && !strstr($link_rel, 'friend') && !strstr($link_rel, 'acquaintance') && !strstr($link_rel, 'contact') ) echo ' checked="checked"'; + if ('geographical' == $class && !strstr($link_rel, 'co-resident') && !strstr($link_rel, 'neighbor') ) echo ' checked="checked"'; + if ('identity' == $class && in_array('me', $rels) ) echo ' checked="checked"'; + } +} + +?> + +
    + +

    +
    + + + + + + + + + + + + + + + + + + +
    link_category); ?>
    +
    +

    + +

    +
    + + + + + + + + + + +
    XFN Creator:') ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    + + + + +
    + +
    + + +
    + + + +
    + + + + + + +
    + + + + +
    +
    +
    +

    + +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +  

    +
    +
    +target attribute is illegal in XHTML 1.1 and 1.0 Strict.)') ?>

    +
    +

    + + + + + + + + + +
    \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-page-form.php b/projects/tests/tests/real/wordpress/wp-admin/edit-page-form.php new file mode 100644 index 00000000..66e029ee --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-page-form.php @@ -0,0 +1,219 @@ + +
    +

    +

    +"; +} else { + $form_action = 'editpost'; + $form_extra = ""; +} + +$sendto = $_SERVER['HTTP_REFERER']; + +if ( 0 != $post_ID && $sendto == get_permalink($post_ID) ) + $sendto = 'redo'; +$sendto = wp_specialchars( $sendto ); + +?> + +
    + +'; +} +?> + + + + + + +
    + +
    +
    +
    +

    +
    + + + +
    +
    + +
    +

    +
    +
    + +
    +

    +

    +
    +
    + + +
    +

    +

    +
    +
    + + +
    +

    +
    +
    + +id ) ) : // TODO: ROLE SYSTEM ?> +
    +

    :

    +
    + +
    +
    + + +
    +

    +

    +
    + +
    +
    + +
    + +
    +
    + + +
    + + 100)) { + $rows = 10; + } +?> + + +
    +
    + + + +

    + + + + + + + +

    + +' . __('This feature requires iframe support.') . ''; +?> + +
    + +
    +

    +
    + + + +
    +
    + +
    + + + escape($post->post_title) ) . "')\""; ?> /> + + + + + +
    + +
    diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit-pages.php b/projects/tests/tests/real/wordpress/wp-admin/edit-pages.php new file mode 100644 index 00000000..96083ec7 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit-pages.php @@ -0,0 +1,78 @@ + + +
    +

    +

    »

    + +
    +
    + + + +
    +
    + +get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'static'"); + +if ($posts) { +?> + + + + + + + + + + + + + + + + + + + + + +
    ID; ?> + + post_modified); ?>" . __('Edit') . ""; } ?>" . __('Delete') . ""; } ?>
    + +
    + + +

    + + +

    »

    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/edit.php b/projects/tests/tests/real/wordpress/wp-admin/edit.php new file mode 100644 index 00000000..1496a568 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/edit.php @@ -0,0 +1,298 @@ + +
    + +

    + post_title = stripslashes($draft->post_title); + if ($draft->post_title == '') + $draft->post_title = sprintf(__('Post #%s'), $draft->ID); + echo "$draft->post_title"; + ++$i; + } + ?> + .

    + + + +

    + post_title = stripslashes($draft->post_title); + if ($draft->post_title == '') + $draft->post_title = sprintf(__('Post #%s'), $draft->ID); + echo "$draft->post_title"; + ++$i; + } + ?> + .

    + + + +
    + + +
    +

    +post_title); + elseif ( ! is_paged() || get_query_var('paged') == 1 ) + _e('Last 15 Posts'); + else + _e('Previous Posts'); +} +?> +

    + +
    +
    + + + +
    +
    + +get_results("SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_date != '0000-00-00 00:00:00' ORDER BY post_date DESC"); + +if ( count($arc_result) ) { ?> + +
    +
    + + + +
    +
    + + + +
    + + 'display name' +$posts_columns = array( + 'id' => __('ID'), + 'date' => __('When'), + 'title' => __('Title'), + 'categories' => __('Categories'), + 'comments' => __('Comments'), + 'author' => __('Author') +); +$posts_columns = apply_filters('manage_posts_columns', $posts_columns); + +// you can not edit these at the moment +$posts_columns['control_view'] = ''; +$posts_columns['control_edit'] = ''; +$posts_columns['control_delete'] = ''; + +?> + + + + + + + + + + + + +$column_display_name) { + + switch($column_name) { + + case 'id': + ?> + + + + + + + + + + + + + + + + + + + + + + + + + + +
    g:i:s a'); ?> + post_status) _e(' - Private'); ?> + + ID) ) { echo "" . __('Edit') . ""; } ?>ID) ) { echo "" . __('Delete') . ""; } ?>
    + +
    + + + +get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved != 'spam' ORDER BY comment_date"); + if ($comments) { + ?> +

    +
      +comment_ID); +?> + +
    1. > + + @ + + ID) ) { + echo "[ comment_ID."\">" . __('Edit') . ""; + echo " - ID."&comment=".$comment->comment_ID."\" onclick=\"return confirm('" . sprintf(__("You are about to delete this comment by \'%s\'\\n \'OK\' to delete, \'Cancel\' to stop."), $comment->comment_author) . "')\">" . __('Delete') . " "; + if ( ('none' != $comment_status) && ( current_user_can('moderate_comments') ) ) { + if ('approved' == wp_get_comment_status($comment->comment_ID)) { + echo " - ID."&comment=".$comment->comment_ID."\">" . __('Unapprove') . " "; + } else { + echo " - ID."&comment=".$comment->comment_ID."\">" . __('Approve') . " "; + } + } + echo "]"; + } // end if any comments to show + ?> +
      + + + ( + + / + + ) (IP: + + ) + + +
    2. + +'; + }//end if comments + ?> + +
    + diff --git a/projects/tests/tests/real/wordpress/wp-admin/execute-pings.php b/projects/tests/tests/real/wordpress/wp-admin/execute-pings.php new file mode 100644 index 00000000..e68f76cb --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/execute-pings.php @@ -0,0 +1,30 @@ +get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) { + $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';"); + pingback($ping->post_content, $ping->ID); + echo "Pingback: $ping->post_title : $ping->ID
    "; + } + // Do Enclosures + while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) { + $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';"); + do_enclose($enclosure->post_content, $enclosure->ID); + echo "Enclosure: $enclosure->post_title : $enclosure->ID
    "; + } + // Do Trackbacks + while ($trackback = $wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE TRIM(to_ping) != '' AND post_status != 'draft' LIMIT 1")) { + echo "Trackback : $trackback->ID
    "; + do_trackbacks($trackback->ID); + } +} + +_e('Done.'); + +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/box-bg.gif b/projects/tests/tests/real/wordpress/wp-admin/images/box-bg.gif new file mode 100644 index 00000000..2eb7f582 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/box-bg.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/box-butt.gif b/projects/tests/tests/real/wordpress/wp-admin/images/box-butt.gif new file mode 100644 index 00000000..514a165e Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/box-butt.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/box-head.gif b/projects/tests/tests/real/wordpress/wp-admin/images/box-head.gif new file mode 100644 index 00000000..5c09a9ae Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/box-head.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/browse-happy.gif b/projects/tests/tests/real/wordpress/wp-admin/images/browse-happy.gif new file mode 100644 index 00000000..09f13bc3 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/browse-happy.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/fade-butt.png b/projects/tests/tests/real/wordpress/wp-admin/images/fade-butt.png new file mode 100644 index 00000000..42f08b79 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/fade-butt.png differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/notice.gif b/projects/tests/tests/real/wordpress/wp-admin/images/notice.gif new file mode 100644 index 00000000..ba6eab02 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/notice.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/toggle.gif b/projects/tests/tests/real/wordpress/wp-admin/images/toggle.gif new file mode 100644 index 00000000..72e8b449 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/toggle.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/images/wordpress-logo.png b/projects/tests/tests/real/wordpress/wp-admin/images/wordpress-logo.png new file mode 100644 index 00000000..3c852f40 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-admin/images/wordpress-logo.png differ diff --git a/projects/tests/tests/real/wordpress/wp-admin/import.php b/projects/tests/tests/real/wordpress/wp-admin/import.php new file mode 100644 index 00000000..2ed94ee8 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/import.php @@ -0,0 +1,62 @@ + + +
    +

    +

    + +read()) !== false) { + if (preg_match('|^\.+$|', $file)) + continue; + if (preg_match('|\.php$|', $file)) + require_once("$import_root/$file"); + } +} + +$importers = get_importers(); + +if (empty ($importers)) { + echo '

    '.__('No importers are available.').'

    '; // TODO: make more helpful +} else { +?> + + + $data) { + $style = ('class="alternate"' == $style || 'class="alternate active"' == $style) ? '' : 'alternate'; + $action = "{$data[0]}"; + + if ($style != '') + $style = 'class="'.$style.'"'; + echo " + + + + "; + } +?> + +
    $action{$data[1]}
    + + +
    + + + diff --git a/projects/tests/graphtest.png b/projects/tests/tests/real/wordpress/wp-admin/import/b2.php similarity index 100% rename from projects/tests/graphtest.png rename to projects/tests/tests/real/wordpress/wp-admin/import/b2.php diff --git a/projects/tests/tests/real/wordpress/wp-admin/import/blogger.php b/projects/tests/tests/real/wordpress/wp-admin/import/blogger.php new file mode 100644 index 00000000..08aa0a25 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/import/blogger.php @@ -0,0 +1,665 @@ +

    $title

    $welcome

    "; + if ( function_exists('curl_init') ) + echo "

    $reset

    "; + else + echo "

    $incompat

    "; + echo "\n"; + } + + // Deletes saved data and redirect. + function restart() { + delete_option('import-blogger'); + header("Location: admin.php?import=blogger"); + die(); + } + + // Generates a string that will make the page reload in a specified interval. + function refresher($msec) { + if ( $msec ) + return "\n\n\n"; + else + return "\n\n\n"; + } + + // Returns associative array of code, header, cookies, body. Based on code from php.net. + function parse_response($this_response) { + // Split response into header and body sections + list($response_headers, $response_body) = explode("\r\n\r\n", $this_response, 2); + $response_header_lines = explode("\r\n", $response_headers); + + // First line of headers is the HTTP response code + $http_response_line = array_shift($response_header_lines); + if(preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line, $matches)) { $response_code = $matches[1]; } + + // put the rest of the headers in an array + $response_header_array = array(); + foreach($response_header_lines as $header_line) { + list($header,$value) = explode(': ', $header_line, 2); + $response_header_array[$header] .= $value."\n"; + } + + $cookie_array = array(); + $cookies = explode("\n", $response_header_array["Set-Cookie"]); + foreach($cookies as $this_cookie) { array_push($cookie_array, "Cookie: ".$this_cookie); } + + return array("code" => $response_code, "header" => $response_header_array, "cookies" => $cookie_array, "body" => $response_body); + } + + // Prints a form for the user to enter Blogger creds. + function login_form($text='') { + echo '

    ' . __('Log in to Blogger') . "

    \n$text\n"; + echo '
    ' . __('Username') . ':
    ' . __('Password') . ':
    '; + die; + } + + // Sends creds to Blogger, returns the session cookies an array of headers. + function login_blogger($user, $pass) { + $_url = 'http://www.blogger.com/login.do'; + $params = "username=$user&password=$pass"; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_POST,1); + curl_setopt($ch, CURLOPT_POSTFIELDS,$params); + curl_setopt($ch, CURLOPT_URL,$_url); + curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter'); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); + curl_setopt($ch, CURLOPT_HEADER,1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); + $response = curl_exec ($ch); + + $response = $this->parse_response($response); + + sleep(1); + + return $response['cookies']; + } + + // Requests page from Blogger, returns the response array. + function get_blogger($url, $header = '', $user=false, $pass=false) { + $ch = curl_init(); + if ($user && $pass) curl_setopt($ch, CURLOPT_USERPWD,"{$user}:{$pass}"); + curl_setopt($ch, CURLOPT_URL,$url); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter'); + curl_setopt($ch, CURLOPT_HEADER,1); + if (is_array($header)) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); + $response = curl_exec ($ch); + + $response = $this->parse_response($response); + $response['url'] = $url; + + if (curl_errno($ch)) { + print curl_error($ch); + } else { + curl_close($ch); + } + + return $response; + } + + // Posts data to Blogger, returns response array. + function post_blogger($url, $header = false, $paramary = false, $parse=true) { + $params = ''; + if ( is_array($paramary) ) { + foreach($paramary as $key=>$value) + if($key && $value != '') + $params.=$key."=".urlencode(stripslashes($value))."&"; + } + if ($user && $pass) $params .= "username=$user&password=$pass"; + $params = trim($params,'&'); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_POST,1); + curl_setopt($ch, CURLOPT_POSTFIELDS,$params); + if ($user && $pass) curl_setopt($ch, CURLOPT_USERPWD,"{$user}:{$pass}"); + curl_setopt($ch, CURLOPT_URL,$url); + curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter'); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_HEADER,$parse); + curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); + if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); + $response = curl_exec ($ch); + + if ($parse) { + $response = $this->parse_response($response); + $response['url'] = $url; + return $response; + } + + return $response; + } + + // Prints the list of blogs for import. + function show_blogs() { + global $import; + echo '

    ' . __('Selecting a Blog') . "

    \n
      "; + foreach ( $this->import['blogs'] as $blog ) { + if (9 == $blog['nextstep']) $status = "100%"; + elseif (8 == $blog['nextstep']) $status = "90%"; + elseif (7 == $blog['nextstep']) $status = "82.5%"; + elseif (6 == $blog['nextstep']) $status = "75%"; + elseif (5 == $blog['nextstep']) $status = "57%"; + elseif (4 == $blog['nextstep']) $status = "28%"; + elseif (3 == $blog['nextstep']) $status = "14%"; + else $status = "0%"; + echo "\t
    • {$blog['title']} $status
    • \n"; + } + die("
    \n"); + } + + // Publishes. + function publish_blogger($i, $text) { + $head = $this->refresher(2000) . "

    $text

    \n"; + if ( ! strstr($this->import['blogs'][$_GET['blog']]['publish'][$i], 'http') ) { + // First call. Start the publish process with a fresh set of cookies. + $this->import['cookies'] = $this->login_blogger($this->import['user'], $this->import['pass']); + update_option('import-blogger', $this->import); + $paramary = array('blogID' => $_GET['blog'], 'all' => '1', 'republishAll' => 'Republish Entire Blog', 'publish' => '1', 'redirectUrl' => "/publish.do?blogID={$_GET['blog']}&inprogress=true"); + + $response = $this->post_blogger("http://www.blogger.com/publish.do?blogID={$_GET['blog']}", $this->import['cookies'], $paramary); + if ( $response['code'] == '302' ) { + $url = str_replace('publish.g', 'publish-body.g', $response['header']['Location']); + $this->import['blogs'][$_GET['blog']]['publish'][$i] = $url; + update_option('import-blogger', $this->import); + $response = $this->get_blogger($url, $this->import['cookies']); + preg_match('#

    .*

    #U', $response['body'], $matches); + $progress = $matches[0]; + die($head . $progress); + } else { + $this->import['blogs'][$_GET['blog']]['publish'][$i] = false; + update_option('import-blogger', $this->import); + die($head); + } + } else { + // Subsequent call. Keep checking status until Blogger reports publish complete. + $url = $this->import['blogs'][$_GET['blog']]['publish'][$i]; + $response = $this->get_blogger($url, $this->import['cookies']); + if ( preg_match('#

    .*

    #U', $response['body'], $matches) ) { + $progress = $matches[0]; + if ( strstr($progress, '100%') ) { + $this->set_next_step($i); + $progress .= '

    '.__('Moving on...').'

    '; + } + die($head . $progress); + } else { + $this->import['blogs'][$_GET['blog']]['publish'][$i] = false; + update_option('import-blogger', $this->import); + die("$head

    " . __('Trying again...') . '

    '); + } + } + } + + // Sets next step, saves options + function set_next_step($step) { + $this->import['blogs'][$_GET['blog']]['nextstep'] = $step; + update_option('import-blogger', $this->import); + } + + // Redirects to next step + function do_next_step() { + header("Location: admin.php?import=blogger&noheader=true&blog={$_GET['blog']}"); + die(); + } + + // Step 0: Do Blogger login, get blogid/title pairs. + function do_login() { + if ( ( ! $this->import['user'] && ! is_array($this->import['cookies']) ) ) { + // The user must provide a Blogger username and password. + if ( ! ( $_POST['user'] && $_POST['pass'] ) ) { + $this->login_form(__('The script will log into your Blogger account, change some settings so it can read your blog, and restore the original settings when it\'s done. Here\'s what you do:').'

    1. '.__('Back up your Blogger template.').'
    2. '.__('Back up any other Blogger settings you might need later.').'
    3. '.__('Log out of Blogger').'
    4. '.__('Log in here with your Blogger username and password.').'
    5. '.__('On the next screen, click one of your Blogger blogs.').'
    6. '.__('Do not close this window or navigate away until the process is complete.').'
    '); + } + + // Try logging in. If we get an array of cookies back, we at least connected. + $this->import['cookies'] = $this->login_blogger($_POST['user'], $_POST['pass']); + if ( !is_array( $this->import['cookies'] ) ) { + $this->login_form(__('Login failed. Please enter your credentials again.')); + } + + // Save the password so we can log the browser in when it's time to publish. + $this->import['pass'] = $_POST['pass']; + $this->import['user'] = $_POST['user']; + + // Get the Blogger welcome page and scrape the blog numbers and names from it + $response = $this->get_blogger('http://www.blogger.com/home', $this->import['cookies']); + if (! stristr($response['body'], 'signed in as') ) $this->login_form(__('Login failed. Please re-enter your username and password.')); + $blogsary = array(); + preg_match_all('#posts\.g\?blogID=(\d+)">([^<]+)#U', $response['body'], $blogsary); + if ( ! count( $blogsary[1] < 1 ) ) + die(__('No blogs found for this user.')); + $this->import['blogs'] = array(); + $template = '


    '.__('Are you looking for %title%? It is temporarily out of service. Please try again in a few minutes. Meanwhile, discover a better blogging tool.').'

    <$BlogArchiveName$>
    <$BlogItemDateTime$>|W|P|<$BlogItemAuthorNickname$>|W|P|<$BlogItemBody$>|W|P|<$BlogItemNumber$>|W|P|<$BlogItemTitle$>|W|P|<$BlogItemAuthorEmail$><$BlogCommentDateTime$>|W|P|<$BlogCommentAuthor$>|W|P|<$BlogCommentBody$>'; + foreach ( $blogsary[1] as $key => $id ) { + // Define the required Blogger options. + $blog_opts = array( + 'blog-options-basic' => false, + 'blog-options-archiving' => array('archiveFrequency' => 'm'), + 'blog-publishing' => array('publishMode'=>'0', 'blogID' => "$id", 'subdomain' => mt_rand().mt_rand(), 'pingWeblogs' => 'false'), + 'blog-formatting' => array('timeStampFormat' => '0', 'encoding'=>'UTF-8', 'convertLineBreaks'=>'false', 'floatAlignment'=>'false'), + 'blog-comments' => array('commentsTimeStampFormat' => '0'), + 'template-edit' => array( 'templateText' => str_replace('%title%', trim($blogsary[2][$key]), $template) ) + ); + + // Build the blog options array template + foreach ($blog_opts as $blog_opt => $modify) + $new_opts["$blog_opt"] = array('backup'=>false, 'modify' => $modify, 'error'=>false); + + $this->import['blogs']["$id"] = array( + 'id' => $id, + 'title' => trim($blogsary[2][$key]), + 'options' => $new_opts, + 'url' => false, + 'publish_cookies' => false, + 'published' => false, + 'archives' => false, + 'lump_authors' => false, + 'newusers' => array(), + 'nextstep' => 2 + ); + } + update_option('import-blogger', $this->import); + header("Location: admin.php?import=blogger&noheader=true&step=1"); + } + die(); + } + + // Step 1: Select one of the blogs belonging to the user logged in. + function select_blog() { + if ( is_array($this->import['blogs']) ) { + $this->show_blogs(); + die(); + } else { + $this->restart(); + } + } + + // Step 2: Backup the Blogger options pages, updating some of them. + function backup_settings() { + $output.= '

    '.__('Backing up Blogger options')."

    \n"; + $form = false; + foreach ($this->import['blogs'][$_GET['blog']]['options'] as $blog_opt => $optary) { + if ( $blog_opt == $_GET['form'] ) { + // Save the posted form data + $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['backup'] = $_POST; + update_option('import-blogger',$this->import); + + // Post the modified form data to Blogger + if ( $optary['modify'] ) { + $posturl = "http://www.blogger.com/{$blog_opt}.do"; + $headers = array_merge($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'], $this->import['cookies']); + if ( 'blog-publishing' == $blog_opt ) { + if ( $_POST['publishMode'] > 0 ) { + $response = $this->get_blogger("http://www.blogger.com/blog-publishing.g?blogID={$_GET['blog']}&publishMode=0", $headers); + if ( $response['code'] >= 400 ) + die('

    '.__('Failed attempt to change publish mode from FTP to BlogSpot.').'

    ' . addslashes(print_r($headers, 1)) . addslashes(print_r($response, 1)) . '
    '); + $this->import['blogs'][$_GET['blog']]['url'] = 'http://' . $optary['modify']['subdomain'] . '.blogspot.com/'; + sleep(2); + } else { + $this->import['blogs'][$_GET['blog']]['url'] = 'http://' . $_POST['subdomain'] . '.blogspot.com/'; + update_option('import-blogger', $this->import); + $output .= "

    $blog_opt

    \n"; + continue; + } + $paramary = $optary['modify']; + } else { + $paramary = array_merge($_POST, $optary['modify']); + } + $response = $this->post_blogger($posturl, $headers, $paramary); + if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') ) + die('

    '.__('Error on form submission. Retry or reset the importer.').'

    ' . addslashes(print_r($response, 1))); + } + $output .= "

    $blog_opt

    \n"; + } elseif ( is_array($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['backup']) ) { + // This option set has already been backed up. + $output .= "

    $blog_opt

    \n"; + } elseif ( ! $form ) { + // This option page needs to be downloaded and given to the browser for submission back to this script. + $response = $this->get_blogger("http://www.blogger.com/{$blog_opt}.g?blogID={$_GET['blog']}", $this->import['cookies']); + $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'] = $response['cookies']; + update_option('import-blogger',$this->import); + $body = $response['body']; + $body = preg_replace("|\]*>|ms","",$body); + $body = preg_replace("|/?{$blog_opt}.do|","admin.php?import=blogger&noheader=true&step=2&blog={$_GET['blog']}&form={$blog_opt}",$body); + $body = str_replace("name='submit'","name='supermit'",$body); + $body = str_replace('name="submit"','name="supermit"',$body); + $body = str_replace('','',str_replace('','',$body)); + $form = "
    "; + $form.= $body; + $form.= "
    "; + $output.= '

    '.sprintf('%s in progress, please wait...', $blog_opt)."

    \n"; + } else { + $output.= "

    $blog_opt

    \n"; + } + } + if ( $form ) + die($output . $form); + + $this->set_next_step(4); + $this->do_next_step(); + } + + // Step 3: Cancelled :-) + + // Step 4: Publish with the new template and settings. + function publish_blog() { + $this->publish_blogger(5, __('Publishing with new template and options')); + } + + // Step 5: Get the archive URLs from the new blog. + function get_archive_urls() { + $bloghtml = $this->get_blogger($this->import['blogs'][$_GET['blog']]['url']); + if (! strstr($bloghtml['body'], 'import['blogs'][$_GET['blog']]['archives'][$archive] = false; + } + $this->set_next_step(6); + $this->do_next_step(); + } + + // Step 6: Get each monthly archive, import it, mark it done. + function get_archive() { + global $wpdb; + $output = '

    '.__('Importing Blogger archives into WordPress').'

    '; + $did_one = false; + $post_array = $posts = array(); + foreach ( $this->import['blogs'][$_GET['blog']]['archives'] as $url => $status ) { + $archivename = substr(basename($url),0,7); + if ( $status || $did_one ) { + $foo = 'bar'; + // Do nothing. + } else { + // Import the selected month + $postcount = 0; + $skippedpostcount = 0; + $commentcount = 0; + $skippedcommentcount = 0; + $status = __('in progress...'); + $this->import['blogs'][$_GET['blog']]['archives']["$url"] = $status; + update_option('import-blogger', $import); + $archive = $this->get_blogger($url); + if ( $archive['code'] > 200 ) + continue; + $posts = explode('', $archive['body']); + for ($i = 1; $i < count($posts); $i = $i + 1) { + $postparts = explode('', $posts[$i]); + $postinfo = explode('|W|P|', $postparts[0]); + $post_date = $postinfo[0]; + $post_content = $postinfo[2]; + // Don't try to re-use the original numbers + // because the new, longer numbers are too + // big to handle as ints. + //$post_number = $postinfo[3]; + $post_title = ( $postinfo[4] != '' ) ? $postinfo[4] : $postinfo[3]; + $post_author_name = $wpdb->escape(trim($postinfo[1])); + $post_author_email = $postinfo[5] ? $postinfo[5] : 'user@wordpress.org'; + + if ( $this->lump_authors ) { + // Ignore Blogger authors. Use the current user_ID for all posts imported. + $post_author = $GLOBALS['user_ID']; + } else { + // Add a user for each new author encountered. + if (! username_exists($post_author_name) ) { + $user_login = $wpdb->escape($post_author_name); + $user_email = $wpdb->escape($post_author_email); + $user_password = substr(md5(uniqid(microtime())), 0, 6); + $result = wp_create_user( $user_login, $user_password, $user_email ); + $status.= sprintf('Registered user %s.', $user_login); + $this->import['blogs'][$_GET['blog']]['newusers'][] = $user_login; + } + $userdata = get_userdatabylogin( $post_author_name ); + $post_author = $userdata->ID; + } + $post_date = explode(' ', $post_date); + $post_date_Ymd = explode('/', $post_date[0]); + $postyear = $post_date_Ymd[2]; + $postmonth = zeroise($post_date_Ymd[0], 2); + $postday = zeroise($post_date_Ymd[1], 2); + $post_date_His = explode(':', $post_date[1]); + $posthour = zeroise($post_date_His[0], 2); + $postminute = zeroise($post_date_His[1], 2); + $postsecond = zeroise($post_date_His[2], 2); + + if (($post_date[2] == 'PM') && ($posthour != '12')) + $posthour = $posthour + 12; + else if (($post_date[2] == 'AM') && ($posthour == '12')) + $posthour = '00'; + + $post_date = "$postyear-$postmonth-$postday $posthour:$postminute:$postsecond"; + + $post_content = addslashes($post_content); + $post_content = str_replace(array('
    ','
    ','
    ','
    ','
    ','
    '), "\n", $post_content); // the XHTML touch... ;) + + $post_title = addslashes($post_title); + + $post_status = 'publish'; + + if ( $ID = post_exists($post_title, '', $post_date) ) { + $post_array[$i]['ID'] = $ID; + $skippedpostcount++; + } else { + $post_array[$i]['post'] = compact('post_author', 'post_content', 'post_title', 'post_category', 'post_author', 'post_date', 'post_status'); + $post_array[$i]['comments'] = false; + } + + // Import any comments attached to this post. + if ($postparts[1]) : + for ($j = 1; $j < count($postparts); $j = $j + 1) { + $commentinfo = explode('|W|P|', $postparts[$j]); + $comment_date = explode(' ', $commentinfo[0]); + $comment_date_Ymd = explode('/', $comment_date[0]); + $commentyear = $comment_date_Ymd[2]; + $commentmonth = zeroise($comment_date_Ymd[0], 2); + $commentday = zeroise($comment_date_Ymd[1], 2); + $comment_date_His = explode(':', $comment_date[1]); + $commenthour = zeroise($comment_date_His[0], 2); + $commentminute = zeroise($comment_date_His[1], 2); + $commentsecond = '00'; + if (($comment_date[2] == 'PM') && ($commenthour != '12')) + $commenthour = $commenthour + 12; + else if (($comment_date[2] == 'AM') && ($commenthour == '12')) + $commenthour = '00'; + $comment_date = "$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond"; + $comment_author = addslashes(strip_tags($commentinfo[1])); + if ( strpos($commentinfo[1], 'a href') ) { + $comment_author_parts = explode('"', htmlentities($commentinfo[1])); + $comment_author_url = $comment_author_parts[1]; + } else $comment_author_url = ''; + $comment_content = $commentinfo[2]; + $comment_content = str_replace(array('
    ','
    ','
    ','
    ','
    ','
    '), "\n", $comment_content); + $comment_approved = 1; + if ( comment_exists($comment_author, $comment_date) ) { + $skippedcommentcount++; + } else { + $comment = compact('comment_author', 'comment_author_url', 'comment_date', 'comment_content', 'comment_approved'); + $post_array[$i]['comments'][$j] = wp_filter_comment($comment); + } + $commentcount++; + } + endif; + $postcount++; + } + if ( count($post_array) ) { + krsort($post_array); + foreach($post_array as $post) { + if ( ! $comment_post_ID = $post['ID'] ) + $comment_post_ID = wp_insert_post($post['post']); + if ( $post['comments'] ) { + foreach ( $post['comments'] as $comment ) { + $comment['comment_post_ID'] = $comment_post_ID; + wp_insert_comment($comment); + } + } + } + } + $status = sprintf(__('%s post(s) parsed, %s skipped...'), $postcount, $skippedpostcount).' '. + sprintf(__('%s comment(s) parsed, %s skipped...'), $commentcoun, $skippedcommentcount).' '. + ' '.__('Done').''; + $import = $this->import; + $import['blogs'][$_GET['blog']]['archives']["$url"] = $status; + update_option('import-blogger', $import); + $did_one = true; + } + $output.= "

    $archivename $status

    \n"; + } + if ( ! $did_one ) + $this->set_next_step(7); + die( $this->refresher(1000) . $output ); + } + + // Step 7: Restore the backed-up settings to Blogger + function restore_settings() { + $output = '

    '.__('Restoring your Blogger options')."

    \n"; + $did_one = false; + // Restore options in reverse order. + if ( ! $this->import['reversed'] ) { + $this->import['blogs'][$_GET['blog']]['options'] = array_reverse($this->import['blogs'][$_GET['blog']]['options'], true); + $this->import['reversed'] = true; + update_option('import-blogger', $this->import); + } + foreach ( $this->import['blogs'][$_GET['blog']]['options'] as $blog_opt => $optary ) { + if ( $did_one ) { + $output .= "

    $blog_opt

    \n"; + } elseif ( $optary['restored'] || ! $optary['modify'] ) { + $output .= "

    $blog_opt

    \n"; + } else { + $posturl = "http://www.blogger.com/{$blog_opt}.do"; + $headers = array_merge($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'], $this->import['cookies']); + if ( 'blog-publishing' == $blog_opt) { + if ( $optary['backup']['publishMode'] > 0 ) { + $response = $this->get_blogger("http://www.blogger.com/blog-publishing.g?blogID={$_GET['blog']}&publishMode={$optary['backup']['publishMode']}", $headers); + sleep(2); + if ( $response['code'] >= 400 ) + die('

    Error restoring publishMode.

    Please tell the devs.

    ' . addslashes(print_r($response, 1)) ); + } + } + if ( $optary['backup'] != $optary['modify'] ) { + $response = $this->post_blogger($posturl, $headers, $optary['backup']); + if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') ) { + $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['error'] = true; + update_option('import-blogger', $this->import); + $output .= sprintf(__('%s failed. Trying again.'), "

    $blog_opt ").'

    '; + } else { + $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['restored'] = true; + update_option('import-blogger', $this->import); + $output .= sprintf(__('%s restored.'), "

    $blog_opt ").'

    '; + } + } + $did_one = true; + } + } + + if ( $did_one ) { + die( $this->refresher(1000) . $output ); + } elseif ( $this->import['blogs'][$_GET['blog']]['options']['blog-publishing']['backup']['publishMode'] > 0 ) { + $this->set_next_step(9); + } else { + $this->set_next_step(8); + } + + $this->do_next_step(); + } + + // Step 8: Republish, all back to normal + function republish_blog() { + $this->publish_blogger(9, __('Publishing with original template and options')); + } + + // Step 9: Congratulate the user + function congrats() { + echo '

    '.__('Congratulations!').'

    '.__('Now that you have imported your Blogger blog into WordPress, what are you going to do? Here are some suggestions:').'

    '; + } + + // Figures out what to do, then does it. + function start() { + if ( $_GET['restart'] == 'true' ) { + $this->restart(); + } + + if ( isset($_GET['noheader']) ) { + $this->import = get_settings('import-blogger'); + + if ( false === $this->import ) { + $step = 0; + } elseif ( isset($_GET['step']) ) { + $step = (int) $_GET['step']; + } elseif ( isset($_GET['blog']) && isset($this->import['blogs'][$_GET['blog']]['nextstep']) ) { + $step = $this->import['blogs'][$_GET['blog']]['nextstep']; + } elseif ( is_array($this->import['blogs']) ) { + $step = 1; + } else { + $step = 0; + } +//echo "Step $step."; +//die('
    '.print_r($this->import,1).'do_login();
    +					break;
    +				case 1 :
    +					$this->select_blog();
    +					break;
    +				case 2 :
    +					$this->backup_settings();
    +					break;
    +				case 3 :
    +					$this->wait_for_blogger();
    +					break;
    +				case 4 :
    +					$this->publish_blog();
    +					break;
    +				case 5 :
    +					$this->get_archive_urls();
    +					break;
    +				case 6 :
    +					$this->get_archive();
    +					break;
    +				case 7 :
    +					$this->restore_settings();
    +					break;
    +				case 8 :
    +					$this->republish_blog();
    +					break;
    +				case 9 :
    +					$this->congrats();
    +					break;
    +			}
    +			die;
    +			
    +		} else {
    +			$this->greet();
    +		}
    +	}
    +
    +	function Blogger_Import() {
    +		// This space intentionally left blank.
    +	}
    +}
    +
    +$blogger_import = new Blogger_Import();
    +
    +register_importer('blogger', 'Blogger', __('Import posts and comments from a Blogger account'), array ($blogger_import, 'start'));
    +
    +?>
    diff --git a/projects/tests/tests/real/wordpress/wp-admin/import/greymatter.php b/projects/tests/tests/real/wordpress/wp-admin/import/greymatter.php
    new file mode 100644
    index 00000000..e69de29b
    diff --git a/projects/tests/tests/real/wordpress/wp-admin/import/livejournal.php b/projects/tests/tests/real/wordpress/wp-admin/import/livejournal.php
    new file mode 100644
    index 00000000..e69de29b
    diff --git a/projects/tests/tests/real/wordpress/wp-admin/import/mt.php b/projects/tests/tests/real/wordpress/wp-admin/import/mt.php
    new file mode 100644
    index 00000000..c880544c
    --- /dev/null
    +++ b/projects/tests/tests/real/wordpress/wp-admin/import/mt.php
    @@ -0,0 +1,429 @@
    +';
    +		echo '

    '.__('Import Movable Type').'

    '; + } + + function footer() { + echo ''; + } + + function greet() { + $this->header(); +?> +

    + +

    out of memory error try splitting up the import file into pieces.'); ?>

    +footer(); + } + + function users_form($n) { + global $wpdb, $testing; + $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID"); +?> + mtnames))) { //a new mt author name is found + ++ $this->j; + $this->mtnames[$this->j] = $author; //add that new mt author name to an array + $user_id = username_exists($this->newauthornames[$j]); //check if the new author name defined by the user is a pre-existing wp user + if (!$user_id) { //banging my head against the desk now. + if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname + $user_id = wp_create_user($author, $pass); + $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank. + } else { + $user_id = wp_create_user($this->newauthornames[$this->j], $pass); + } + } else { + return $user_id; // return pre-existing wp username if it exists + } + } else { + $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array + $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames + } + + return $user_id; + } + + function get_entries() { + set_magic_quotes_runtime(0); + $importdata = file($this->file); // Read the file into an array + $importdata = implode('', $importdata); // squish it + $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata); + $importdata = preg_replace("/\n--------\n/", "--MT-ENTRY--\n", $importdata); + $this->posts = explode("--MT-ENTRY--", $importdata); + } + + function get_mt_authors() { + $temp = array (); + $i = -1; + foreach ($this->posts as $post) { + if ('' != trim($post)) { + ++ $i; + preg_match("|AUTHOR:(.*)|", $post, $thematch); + $thematch = trim($thematch[1]); + array_push($temp, "$thematch"); //store the extracted author names in a temporary array + } + } + + //we need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting. + $authors[0] = array_shift($temp); + $y = count($temp) + 1; + for ($x = 1; $x < $y; $x ++) { + $next = array_shift($temp); + if (!(in_array($next, $authors))) + array_push($authors, "$next"); + } + + return $authors; + } + + function get_authors_from_post() { + $formnames = array (); + $selectnames = array (); + + foreach ($_POST['user'] as $key => $line) { + $newname = trim(stripslashes($line)); + if ($newname == '') + $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form. + array_push($formnames, "$newname"); + } // $formnames is the array with the form entered names + + foreach ($_POST['userselect'] as $user => $key) { + $selected = trim(stripslashes($key)); + array_push($selectnames, "$selected"); + } + + $count = count($formnames); + for ($i = 0; $i < $count; $i ++) { + if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form + array_push($this->newauthornames, "$selectnames[$i]"); + } else { + array_push($this->newauthornames, "$formnames[$i]"); + } + } + } + + function mt_authors_form() { +?> +

    admins entries.'); ?>

    +

    italics. For each of these names, you can either pick an author in your WordPress installation from the menu, or enter a name for the author in the textbox.'); ?>

    +

    + get_mt_authors(); + echo '
      '; + echo '
      '; + $j = -1; + foreach ($authors as $author) { + ++ $j; + echo '
    1. '.$author.'
      '.''; + $this->users_form($j); + echo '
    2. '; + } + + echo ''.'
      '; + echo '
      '; + echo '
    '; + + flush(); + } + + function select_authors() { + $file = wp_import_handle_upload(); + if ( isset($file['error']) ) { + echo $file['error']; + return; + } + $this->file = $file['file']; + $this->id = $file['id']; + + $this->get_entries(); + $this->mt_authors_form(); + } + + function process_posts() { + global $wpdb; + $i = -1; + echo "
      "; + foreach ($this->posts as $post) { + if ('' != trim($post)) { + ++ $i; + unset ($post_categories); + + // Take the pings out first + preg_match("|(-----\n\nPING:.*)|s", $post, $pings); + $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post); + + // Then take the comments out + preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments); + $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post); + + // We ignore the keywords + $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post); + + // We want the excerpt + preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt); + $excerpt = $wpdb->escape(trim($excerpt[1])); + $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post); + + // We're going to put extended body into main body with a more tag + preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended); + $extended = trim($extended[1]); + if ('' != $extended) + $extended = "\n\n$extended"; + $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post); + + // Now for the main body + preg_match("|-----\nBODY:(.*)|s", $post, $body); + $body = trim($body[1]); + $post_content = $wpdb->escape($body.$extended); + $post = preg_replace("|(-----\nBODY:.*)|s", '', $post); + + // Grab the metadata from what's left + $metadata = explode("\n", $post); + foreach ($metadata as $line) { + preg_match("/^(.*?):(.*)/", $line, $token); + $key = trim($token[1]); + $value = trim($token[2]); + // Now we decide what it is and what to do with it + switch ($key) { + case '' : + break; + case 'AUTHOR' : + $post_author = $value; + break; + case 'TITLE' : + $post_title = $wpdb->escape($value); + break; + case 'STATUS' : + // "publish" and "draft" enumeration items match up; no change required + $post_status = $value; + if (empty ($post_status)) + $post_status = 'publish'; + break; + case 'ALLOW COMMENTS' : + $post_allow_comments = $value; + if ($post_allow_comments == 1) { + $comment_status = 'open'; + } else { + $comment_status = 'closed'; + } + break; + case 'CONVERT BREAKS' : + $post_convert_breaks = $value; + break; + case 'ALLOW PINGS' : + $post_allow_pings = trim($meta[2][0]); + if ($post_allow_pings == 1) { + $post_allow_pings = 'open'; + } else { + $post_allow_pings = 'closed'; + } + break; + case 'PRIMARY CATEGORY' : + if (! empty ($value) ) + $post_categories[] = $wpdb->escape($value); + break; + case 'CATEGORY' : + if (! empty ($value) ) + $post_categories[] = $wpdb->escape($value); + break; + case 'DATE' : + $post_modified = strtotime($value); + $post_modified = date('Y-m-d H:i:s', $post_modified); + $post_modified_gmt = get_gmt_from_date("$post_modified"); + $post_date = $post_modified; + $post_date_gmt = $post_modified_gmt; + break; + default : + // echo "\n$key: $value"; + break; + } // end switch + } // End foreach + + // Let's check to see if it's in already + if ($post_id = post_exists($post_title, '', $post_date)) { + echo '
    1. '; + printf(__('Post %s already exists.'), stripslashes($post_title)); + } else { + echo '
    2. '; + printf(__('Importing post %s...'), stripslashes($post_title)); + + $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor + + $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt'); + $post_id = wp_insert_post($postdata); + // Add categories. + if (0 != count($post_categories)) { + wp_create_categories($post_categories, $post_id); + } + } + + $comment_post_ID = $post_id; + + // Now for comments + $comments = explode("-----\nCOMMENT:", $comments[0]); + $num_comments = 0; + foreach ($comments as $comment) { + if ('' != trim($comment)) { + // Author + preg_match("|AUTHOR:(.*)|", $comment, $comment_author); + $comment_author = $wpdb->escape(trim($comment_author[1])); + $comment = preg_replace('|(\n?AUTHOR:.*)|', '', $comment); + preg_match("|EMAIL:(.*)|", $comment, $comment_author_email); + $comment_author_email = $wpdb->escape(trim($comment_author_email[1])); + $comment = preg_replace('|(\n?EMAIL:.*)|', '', $comment); + + preg_match("|IP:(.*)|", $comment, $comment_author_IP); + $comment_author_IP = trim($comment_author_IP[1]); + $comment = preg_replace('|(\n?IP:.*)|', '', $comment); + + preg_match("|URL:(.*)|", $comment, $comment_author_url); + $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); + $comment = preg_replace('|(\n?URL:.*)|', '', $comment); + + preg_match("|DATE:(.*)|", $comment, $comment_date); + $comment_date = trim($comment_date[1]); + $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); + $comment = preg_replace('|(\n?DATE:.*)|', '', $comment); + + $comment_content = $wpdb->escape(trim($comment)); + $comment_content = str_replace('-----', '', $comment_content); + // Check if it's already there + if (!comment_exists($comment_author, $comment_date)) { + $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content'); + $commentdata = wp_filter_comment($commentdata); + wp_insert_comment($commentdata); + $num_comments++; + } + } + } + if ( $num_comments ) + printf(__('(%s comments)'), $num_comments); + + // Finally the pings + // fix the double newline on the first one + $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]); + $pings = explode("-----\nPING:", $pings[0]); + $num_pings = 0; + foreach ($pings as $ping) { + if ('' != trim($ping)) { + // 'Author' + preg_match("|BLOG NAME:(.*)|", $ping, $comment_author); + $comment_author = $wpdb->escape(trim($comment_author[1])); + $ping = preg_replace('|(\n?BLOG NAME:.*)|', '', $ping); + + preg_match("|IP:(.*)|", $ping, $comment_author_IP); + $comment_author_IP = trim($comment_author_IP[1]); + $ping = preg_replace('|(\n?IP:.*)|', '', $ping); + + preg_match("|URL:(.*)|", $ping, $comment_author_url); + $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); + $ping = preg_replace('|(\n?URL:.*)|', '', $ping); + + preg_match("|DATE:(.*)|", $ping, $comment_date); + $comment_date = trim($comment_date[1]); + $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); + $ping = preg_replace('|(\n?DATE:.*)|', '', $ping); + + preg_match("|TITLE:(.*)|", $ping, $ping_title); + $ping_title = $wpdb->escape(trim($ping_title[1])); + $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping); + + $comment_content = $wpdb->escape(trim($ping)); + $comment_content = str_replace('-----', '', $comment_content); + + $comment_content = "$ping_title\n\n$comment_content"; + + $comment_type = 'trackback'; + + // Check if it's already there + if (!comment_exists($comment_author, $comment_date)) { + $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_type'); + $commentdata = wp_filter_comment($commentdata); + wp_insert_comment($commentdata); + $num_pings++; + } + } + } + if ( $num_pings ) + printf(__('(%s pings)'), $num_pings); + + echo "
    3. "; + } + flush(); + } + + echo '
    '; + + wp_import_cleanup($this->id); + + echo '

    '.sprintf(__('All done. Have fun!'), get_option('home')).'

    '; + } + + function import() { + $this->id = (int) $_GET['id']; + $this->file = get_attached_file($this->id); + $this->get_authors_from_post(); + $this->get_entries(); + $this->process_posts(); + } + + function dispatch() { + if (empty ($_GET['step'])) + $step = 0; + else + $step = (int) $_GET['step']; + + switch ($step) { + case 0 : + $this->greet(); + break; + case 1 : + $this->select_authors(); + break; + case 2: + $this->import(); + break; + } + } + + function MT_Import() { + // Nothing. + } +} + +$mt_import = new MT_Import(); + +register_importer('mt', 'Movable Type', __('Import posts and comments from your Movable Type blog'), array ($mt_import, 'dispatch')); +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/import/rss.php b/projects/tests/tests/real/wordpress/wp-admin/import/rss.php new file mode 100644 index 00000000..c79bf99e --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/import/rss.php @@ -0,0 +1,175 @@ +'; + echo '

    '.__('Import RSS').'

    '; + } + + function footer() { + echo ''; + } + + function unhtmlentities($string) { // From php.net for < 4.3 compat + $trans_tbl = get_html_translation_table(HTML_ENTITIES); + $trans_tbl = array_flip($trans_tbl); + return strtr($string, $trans_tbl); + } + + function greet() { + echo '

    '.__('Howdy! This importer allows you to extract posts from any RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. Pick an RSS file to upload and click Import.').'

    '; + wp_import_upload_form("admin.php?import=rss&step=1"); + } + + function get_posts() { + global $wpdb; + + set_magic_quotes_runtime(0); + $datalines = file($this->file); // Read the file into an array + $importdata = implode('', $datalines); // squish it + $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); + + preg_match_all('|(.*?)|is', $importdata, $this->posts); + $this->posts = $this->posts[1]; + $index = 0; + foreach ($this->posts as $post) { + preg_match('|(.*?)|is', $post, $post_title); + $post_title = $wpdb->escape(trim($post_title[1])); + + preg_match('|(.*?)|is', $post, $post_date); + + if ($post_date) { + $post_date = strtotime($post_date[1]); + } else { + // if we don't already have something from pubDate + preg_match('|(.*?)|is', $post, $post_date); + $post_date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date[1]); + $post_date = str_replace('T', ' ', $post_date); + $post_date = strtotime($post_date); + } + + $post_date = gmdate('Y-m-d H:i:s', $post_date); + + preg_match_all('|(.*?)|is', $post, $categories); + $categories = $categories[1]; + + if (!$categories) { + preg_match_all('|(.*?)|is', $post, $categories); + $categories = $categories[1]; + } + + $cat_index = 0; + foreach ($categories as $category) { + $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); + $cat_index++; + } + + preg_match('|(.*?)|is', $post, $guid); + if ($guid) + $guid = $wpdb->escape(trim($guid[1])); + else + $guid = ''; + + preg_match('|(.*?)|is', $post, $post_content); + $post_content = str_replace(array (''), '', $wpdb->escape(trim($post_content[1]))); + + if (!$post_content) { + // This is for feeds that put content in description + preg_match('|(.*?)|is', $post, $post_content); + $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1]))); + } + + // Clean up content + $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); + $post_content = str_replace('
    ', '
    ', $post_content); + $post_content = str_replace('
    ', '
    ', $post_content); + + $post_author = 1; + $post_status = 'publish'; + $this->posts[$index] = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status', 'guid', 'categories'); + $index++; + } + } + + function import_posts() { + echo '
      '; + + foreach ($this->posts as $post) { + echo "
    1. ".__('Importing post...'); + + extract($post); + + if ($post_id = post_exists($post_title, $post_content, $post_date)) { + _e('Post already imported'); + } else { + $post_id = wp_insert_post($post); + if (!$post_id) { + _e("Couldn't get post ID"); + return; + } + + if (0 != count($categories)) + wp_create_categories($categories, $post_id); + _e('Done !'); + } + echo '
    2. '; + } + + echo '
    '; + + } + + function import() { + $file = wp_import_handle_upload(); + if ( isset($file['error']) ) { + echo $file['error']; + return; + } + + $this->file = $file['file']; + $this->get_posts(); + $this->import_posts(); + wp_import_cleanup($file['id']); + + echo '

    '; + printf(__('All done. Have fun!'), get_option('home')); + echo '

    '; + } + + function dispatch() { + if (empty ($_GET['step'])) + $step = 0; + else + $step = (int) $_GET['step']; + + $this->header(); + + switch ($step) { + case 0 : + $this->greet(); + break; + case 1 : + $this->import(); + break; + } + + $this->footer(); + } + + function RSS_Import() { + // Nothing. + } +} + +$rss_import = new RSS_Import(); + +register_importer('rss', 'RSS', __('Import posts from an RSS feed'), array ($rss_import, 'dispatch')); +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/import/textpattern.php b/projects/tests/tests/real/wordpress/wp-admin/import/textpattern.php new file mode 100644 index 00000000..48202a93 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/import/textpattern.php @@ -0,0 +1,663 @@ +get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"'); + + return $name; + } +} + +if(!function_exists('get_comment_count')) +{ + function get_comment_count($post_ID) + { + global $wpdb; + return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID); + } +} + +if(!function_exists('link_exists')) +{ + function link_exists($linkname) + { + global $wpdb; + return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"'); + } +} + +/** + The Main Importer Class +**/ +class Textpattern_Import { + + function header() + { + echo '
    '; + echo '

    '.__('Import Textpattern').'

    '; + echo '

    '.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'

    '; + } + + function footer() + { + echo '
    '; + } + + function greet() + { + echo '

    '.__('Howdy! This importer allows you to extract posts from any Textpattern 4.0.2+ into your blog. This has not been tested on previous versions of Textpattern. Mileage may vary.').'

    '; + echo '

    '.__('Your Textpattern Configuration settings are as follows:').'

    '; + echo '
    '; + $this->db_form(); + echo ''; + echo '
    '; + } + + function get_txp_cats() + { + global $wpdb; + // General Housekeeping + $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); + set_magic_quotes_runtime(0); + $prefix = get_option('tpre'); + + // Get Categories + return $txpdb->get_results('SELECT + id, + name, + title + FROM '.$prefix.'txp_category + WHERE type = "article"', + ARRAY_A); + } + + function get_txp_users() + { + global $wpdb; + // General Housekeeping + $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); + set_magic_quotes_runtime(0); + $prefix = get_option('tpre'); + + // Get Users + + return $txpdb->get_results('SELECT + user_id, + name, + RealName, + email, + privs + FROM '.$prefix.'txp_users', ARRAY_A); + } + + function get_txp_posts() + { + // General Housekeeping + $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); + set_magic_quotes_runtime(0); + $prefix = get_option('tpre'); + + // Get Posts + return $txpdb->get_results('SELECT + ID, + Posted, + AuthorID, + LastMod, + Title, + Body, + Excerpt, + Category1, + Category2, + Status, + Keywords, + url_title, + comments_count + FROM '.$prefix.'textpattern + ', ARRAY_A); + } + + function get_txp_comments() + { + global $wpdb; + // General Housekeeping + $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); + set_magic_quotes_runtime(0); + $prefix = get_option('tpre'); + + // Get Comments + return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A); + } + + function get_txp_links() + { + //General Housekeeping + $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); + set_magic_quotes_runtime(0); + $prefix = get_option('tpre'); + + return $txpdb->get_results('SELECT + id, + date, + category, + url, + linkname, + description + FROM '.$prefix.'txp_link', + ARRAY_A); + } + + function cat2wp($categories='') + { + // General Housekeeping + global $wpdb; + $count = 0; + $txpcat2wpcat = array(); + // Do the Magic + if(is_array($categories)) + { + echo '

    '.__('Importing Categories...').'

    '; + foreach ($categories as $category) + { + $count++; + extract($category); + + + // Make Nice Variables + $name = $wpdb->escape($name); + $title = $wpdb->escape($title); + + if($cinfo = category_exists($name)) + { + $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title)); + } + else + { + $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title)); + } + $txpcat2wpcat[$id] = $ret_id; + } + + // Store category translation for future use + add_option('txpcat2wpcat',$txpcat2wpcat); + echo '

    '.sprintf(__('Done! %1$s categories imported.'), $count).'

    '; + return true; + } + echo __('No Categories to Import!'); + return false; + } + + function users2wp($users='') + { + // General Housekeeping + global $wpdb; + $count = 0; + $txpid2wpid = array(); + + // Midnight Mojo + if(is_array($users)) + { + echo '

    '.__('Importing Users...').'

    '; + foreach($users as $user) + { + $count++; + extract($user); + + // Make Nice Variables + $name = $wpdb->escape($name); + $RealName = $wpdb->escape($RealName); + + if($uinfo = get_userdatabylogin($name)) + { + + $ret_id = wp_insert_user(array( + 'ID' => $uinfo->ID, + 'user_login' => $name, + 'user_nicename' => $RealName, + 'user_email' => $email, + 'user_url' => 'http://', + 'display_name' => $name) + ); + } + else + { + $ret_id = wp_insert_user(array( + 'user_login' => $name, + 'user_nicename' => $RealName, + 'user_email' => $email, + 'user_url' => 'http://', + 'display_name' => $name) + ); + } + $txpid2wpid[$user_id] = $ret_id; + + // Set Textpattern-to-WordPress permissions translation + $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0'); + + // Update Usermeta Data + $user = new WP_User($ret_id); + if('10' == $transperms[$privs]) { $user->set_role('administrator'); } + if('9' == $transperms[$privs]) { $user->set_role('editor'); } + if('5' == $transperms[$privs]) { $user->set_role('editor'); } + if('4' == $transperms[$privs]) { $user->set_role('author'); } + if('3' == $transperms[$privs]) { $user->set_role('contributor'); } + if('2' == $transperms[$privs]) { $user->set_role('contributor'); } + if('0' == $transperms[$privs]) { $user->set_role('subscriber'); } + + update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] ); + update_usermeta( $ret_id, 'rich_editing', 'false'); + }// End foreach($users as $user) + + // Store id translation array for future use + add_option('txpid2wpid',$txpid2wpid); + + + echo '

    '.sprintf(__('Done! %1$s users imported.'), $count).'

    '; + return true; + }// End if(is_array($users) + + echo __('No Users to Import!'); + return false; + + }// End function user2wp() + + function posts2wp($posts='') + { + // General Housekeeping + global $wpdb; + $count = 0; + $txpposts2wpposts = array(); + $cats = array(); + + // Do the Magic + if(is_array($posts)) + { + echo '

    '.__('Importing Posts...').'

    '; + foreach($posts as $post) + { + $count++; + extract($post); + + // Set Textpattern-to-WordPress status translation + $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish'); + + //Can we do this more efficiently? + $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1; + $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ; + + $Title = $wpdb->escape($Title); + $Body = $wpdb->escape($Body); + $Excerpt = $wpdb->escape($Excerpt); + $post_status = $stattrans[$Status]; + + // Import Post data into WordPress + + if($pinfo = post_exists($Title,$Body)) + { + $ret_id = wp_insert_post(array( + 'ID' => $pinfo, + 'post_date' => $Posted, + 'post_date_gmt' => $post_date_gmt, + 'post_author' => $authorid, + 'post_modified' => $LastMod, + 'post_modified_gmt' => $post_modified_gmt, + 'post_title' => $Title, + 'post_content' => $Body, + 'post_excerpt' => $Excerpt, + 'post_status' => $post_status, + 'post_name' => $url_title, + 'comment_count' => $comments_count) + ); + } + else + { + $ret_id = wp_insert_post(array( + 'post_date' => $Posted, + 'post_date_gmt' => $post_date_gmt, + 'post_author' => $authorid, + 'post_modified' => $LastMod, + 'post_modified_gmt' => $post_modified_gmt, + 'post_title' => $Title, + 'post_content' => $Body, + 'post_excerpt' => $Excerpt, + 'post_status' => $post_status, + 'post_name' => $url_title, + 'comment_count' => $comments_count) + ); + } + $txpposts2wpposts[$ID] = $ret_id; + + // Make Post-to-Category associations + $cats = array(); + if($cat1 = get_catbynicename($Category1)) { $cats[1] = $cat1; } + if($cat2 = get_catbynicename($Category2)) { $cats[2] = $cat2; } + + if(!empty($cats)) { wp_set_post_cats('', $ret_id, $cats); } + } + } + // Store ID translation for later use + add_option('txpposts2wpposts',$txpposts2wpposts); + + echo '

    '.sprintf(__('Done! %1$s posts imported.'), $count).'

    '; + return true; + } + + function comments2wp($comments='') + { + // General Housekeeping + global $wpdb; + $count = 0; + $txpcm2wpcm = array(); + $postarr = get_option('txpposts2wpposts'); + + // Magic Mojo + if(is_array($comments)) + { + echo '

    '.__('Importing Comments...').'

    '; + foreach($comments as $comment) + { + $count++; + extract($comment); + + // WordPressify Data + $comment_ID = ltrim($discussid, '0'); + $comment_post_ID = $postarr[$parentid]; + $comment_approved = (1 == $visible) ? 1 : 0; + $name = $wpdb->escape($name); + $email = $wpdb->escape($email); + $web = $wpdb->escape($web); + $message = $wpdb->escape($message); + + if($cinfo = comment_exists($name, $posted)) + { + // Update comments + $ret_id = wp_update_comment(array( + 'comment_ID' => $cinfo, + 'comment_post_ID' => $comment_post_ID, + 'comment_author' => $name, + 'comment_author_email' => $email, + 'comment_author_url' => $web, + 'comment_date' => $posted, + 'comment_content' => $message, + 'comment_approved' => $comment_approved) + ); + } + else + { + // Insert comments + $ret_id = wp_insert_comment(array( + 'comment_post_ID' => $comment_post_ID, + 'comment_author' => $name, + 'comment_author_email' => $email, + 'comment_author_url' => $web, + 'comment_author_IP' => $ip, + 'comment_date' => $posted, + 'comment_content' => $message, + 'comment_approved' => $comment_approved) + ); + } + $txpcm2wpcm[$comment_ID] = $ret_id; + } + // Store Comment ID translation for future use + add_option('txpcm2wpcm', $txpcm2wpcm); + + // Associate newly formed categories with posts + get_comment_count($ret_id); + + + echo '

    '.sprintf(__('Done! %1$s comments imported.'), $count).'

    '; + return true; + } + echo __('No Comments to Import!'); + return false; + } + + function links2wp($links='') + { + // General Housekeeping + global $wpdb; + $count = 0; + + // Deal with the links + if(is_array($links)) + { + echo '

    '.__('Importing Links...').'

    '; + foreach($links as $link) + { + $count++; + extract($link); + + // Make nice vars + $category = $wpdb->escape($category); + $linkname = $wpdb->escape($linkname); + $description = $wpdb->escape($description); + + if($linfo = link_exists($linkname)) + { + $ret_id = wp_insert_link(array( + 'link_id' => $linfo, + 'link_url' => $url, + 'link_name' => $linkname, + 'link_category' => $category, + 'link_description' => $description, + 'link_updated' => $date) + ); + } + else + { + $ret_id = wp_insert_link(array( + 'link_url' => $url, + 'link_name' => $linkname, + 'link_category' => $category, + 'link_description' => $description, + 'link_updated' => $date) + ); + } + $txplinks2wplinks[$link_id] = $ret_id; + } + add_option('txplinks2wplinks',$txplinks2wplinks); + echo '

    '; + printf(__('Done! %s Links imported'), $count); + echo '

    '; + return true; + } + echo __('No Links to Import!'); + return false; + } + + function import_categories() + { + // Category Import + $cats = $this->get_txp_cats(); + $this->cat2wp($cats); + add_option('txp_cats', $cats); + + + + echo '
    '; + printf('', __('Import Users')); + echo '
    '; + + } + + function import_users() + { + // User Import + $users = $this->get_txp_users(); + $this->users2wp($users); + + echo '
    '; + printf('', __('Import Posts')); + echo '
    '; + } + + function import_posts() + { + // Post Import + $posts = $this->get_txp_posts(); + $this->posts2wp($posts); + + echo '
    '; + printf('', __('Import Comments')); + echo '
    '; + } + + function import_comments() + { + // Comment Import + $comments = $this->get_txp_comments(); + $this->comments2wp($comments); + + echo '
    '; + printf('', __('Import Links')); + echo '
    '; + } + + function import_links() + { + //Link Import + $links = $this->get_txp_links(); + $this->links2wp($links); + add_option('txp_links', $links); + + echo '
    '; + printf('', __('Finish')); + echo '
    '; + } + + function cleanup_txpimport() + { + delete_option('tpre'); + delete_option('txp_cats'); + delete_option('txpid2wpid'); + delete_option('txpcat2wpcat'); + delete_option('txpposts2wpposts'); + delete_option('txpcm2wpcm'); + delete_option('txplinks2wplinks'); + delete_option('txpuser'); + delete_option('txppass'); + delete_option('txpname'); + delete_option('txphost'); + $this->tips(); + } + + function tips() + { + echo '

    '.__('Welcome to WordPress. We hope (and expect!) that you will find this platform incredibly rewarding! As a new WordPress user coming from Textpattern, there are some things that we would like to point out. Hopefully, they will help your transition go as smoothly as possible.').'

    '; + echo '

    '.__('Users').'

    '; + echo '

    '.sprintf(__('You have already setup WordPress and have been assigned an administrative login and password. Forget it. You didn\'t have that login in Textpattern, why should you have it here? Instead we have taken care to import all of your users into our system. Unfortunately there is one downside. Because both WordPress and Textpattern uses a strong encryption hash with passwords, it is impossible to decrypt it and we are forced to assign temporary passwords to all your users. Every user has the same username, but their passwords are reset to password123. So Login and change it.'), '/wp-login.php').'

    '; + echo '

    '.__('Preserving Authors').'

    '; + echo '

    '.__('Secondly, we have attempted to preserve post authors. If you are the only author or contributor to your blog, then you are safe. In most cases, we are successful in this preservation endeavor. However, if we cannot ascertain the name of the writer due to discrepancies between database tables, we assign it to you, the administrative user.').'

    '; + echo '

    '.__('Textile').'

    '; + echo '

    '.__('Also, since you\'re coming from Textpattern, you probably have been using Textile to format your comments and posts. If this is the case, we recommend downloading and installing Textile for WordPress. Trust me... You\'ll want it.').'

    '; + echo '

    '.__('WordPress Resources').'

    '; + echo '

    '.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'

    '; + echo ''; + echo '

    '.sprintf(__('That\'s it! What are you waiting for? Go login!'), '/wp-login.php').'

    '; + } + + function db_form() + { + echo '
      '; + printf('
    • ', __('Textpattern Database User:')); + printf('
    • ', __('Textpattern Database Password:')); + printf('
    • ', __('Textpattern Database Name:')); + printf('
    • ', __('Textpattern Database Host:')); + printf('
    • ', __('Textpattern Table prefix (if any):')); + echo '
    '; + } + + function dispatch() + { + + if (empty ($_GET['step'])) + $step = 0; + else + $step = (int) $_GET['step']; + $this->header(); + + if ( $step > 0 ) + { + if($_POST['dbuser']) + { + if(get_option('txpuser')) + delete_option('txpuser'); + add_option('txpuser',$_POST['dbuser']); + } + if($_POST['dbpass']) + { + if(get_option('txppass')) + delete_option('txppass'); + add_option('txppass',$_POST['dbpass']); + } + + if($_POST['dbname']) + { + if(get_option('txpname')) + delete_option('txpname'); + add_option('txpname',$_POST['dbname']); + } + if($_POST['dbhost']) + { + if(get_option('txphost')) + delete_option('txphost'); + add_option('txphost',$_POST['dbhost']); + } + if($_POST['dbprefix']) + { + if(get_option('tpre')) + delete_option('tpre'); + add_option('tpre',$_POST['dbprefix']); + } + + + } + + switch ($step) + { + default: + case 0 : + $this->greet(); + break; + case 1 : + $this->import_categories(); + break; + case 2 : + $this->import_users(); + break; + case 3 : + $this->import_posts(); + break; + case 4 : + $this->import_comments(); + break; + case 5 : + $this->import_links(); + break; + case 6 : + $this->cleanup_txpimport(); + break; + } + + $this->footer(); + } + + function Textpattern_Import() + { + // Nothing. + } +} + +$txp_import = new Textpattern_Import(); +register_importer('textpattern', 'Textpattern', __('Import posts from a Textpattern Blog'), array ($txp_import, 'dispatch')); +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/index.php b/projects/tests/tests/real/wordpress/wp-admin/index.php new file mode 100644 index 00000000..d6d407ed --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/index.php @@ -0,0 +1,172 @@ + + +
    + +

    + +
    +

    + +items) && 0 != count($rss->items) ) { +?> + + + +get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 5"); +$numcomments = $wpdb->get_var("SELECT COUNT(*) FROM $tablecomments WHERE comment_approved = '0'"); + +if ( $comments || $numcomments ) : +?> +
    +

    »

    + + +

    »

    + +
    + + + + + +get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 5") ) : +?> +
    +

    »

    + +
    + + +get_results("SELECT ID, post_title, post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_date_gmt > '$today' ORDER BY post_date ASC") ) : +?> +
    +

    +
      +post_title == '') + $post->post_title = sprintf(__('Post #%s'), $post->ID); + echo "
    • " . sprintf(__('%1$s in %2$s'), "$post->post_title", human_time_diff( current_time('timestamp', 1), strtotime($post->post_date_gmt. ' GMT') )) . "
    • "; +} +?> +
    +
    + + +
    +

    +get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); +if (0 < $numposts) $numposts = number_format($numposts); + +$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'"); +if (0 < $numcomms) $numcomms = number_format($numcomms); + +$numcats = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories"); +if (0 < $numcats) $numcats = number_format($numcats); +?> +

    posts and %3$s comments, contained within %5$s categories.'), $numposts, 'edit.php', $numcomms, 'edit-comments.php', $numcats, 'categories.php'); ?>

    +
    + + +
    + +

    + +

    + +
      +
    • +
    • +
    • +
    • +
    + +

    great documentation or if that doesn't help visit the support forums."); ?>

    +items) && 0 != count($rss->items) ) { +?> +

    +items = array_slice($rss->items, 0, 3); +foreach ($rss->items as $item ) { +?> +

    '>

    +

    + + + +items) && 0 != count($rss->items) ) { +?> +
    +

    »

    +
      +items = array_slice($rss->items, 0, 20); +foreach ($rss->items as $item ) { +?> +
    • '>
    • + +
    +
    + +
      +
    +
    +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/inline-uploading.php b/projects/tests/tests/real/wordpress/wp-admin/inline-uploading.php new file mode 100644 index 00000000..c2706320 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/inline-uploading.php @@ -0,0 +1,700 @@ +".__('Go back').''); + +wp_delete_attachment($attachment); + +header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&start=$start"); +die; + +case 'save': + +$overrides = array('action'=>'save'); + +$file = wp_handle_upload($_FILES['image'], $overrides); + +if ( isset($file['error']) ) + die($file['error'] . '
    '.__('Back to Image Uploading').''); + +$url = $file['url']; +$type = $file['type']; +$file = $file['file']; +$filename = basename($file); + +// Construct the attachment array +$attachment = array( + 'post_title' => $imgtitle ? $imgtitle : $filename, + 'post_content' => $descr, + 'post_status' => 'attachment', + 'post_parent' => $post, + 'post_mime_type' => $type, + 'guid' => $url + ); + +// Save the data +$id = wp_insert_attachment($attachment, $file, $post); + +if ( preg_match('!^image/!', $attachment['post_mime_type']) ) { + // Generate the attachment's postmeta. + $imagesize = getimagesize($file); + $imagedata['width'] = $imagesize['0']; + $imagedata['height'] = $imagesize['1']; + list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']); + $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'"; + $imagedata['file'] = $file; + + add_post_meta($id, '_wp_attachment_metadata', $imagedata); + + if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) { + if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 ) + $thumb = wp_create_thumbnail($file, 128); + elseif ( $imagedata['height'] > 96 ) + $thumb = wp_create_thumbnail($file, 96); + + if ( @file_exists($thumb) ) { + $newdata = $imagedata; + $newdata['thumb'] = basename($thumb); + update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata); + } else { + $error = $thumb; + } + } +} else { + add_post_meta($id, '_wp_attachment_metadata', array()); +} + +header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&start=0"); +die(); + +case 'upload': + +$current_1 = ' class="current"'; +$back = $next = false; +break; + +case 'view': + +// How many images do we show? How many do we query? +$num = 5; +$double = $num * 2; + +if ( $post && (empty($all) || $all == 'false') ) { + $and_post = "AND post_parent = '$post'"; + $current_2 = ' class="current"'; +} else { + $current_3 = ' class="current"'; +} + +if (! current_user_can('edit_others_posts') ) + $and_user = "AND post_author = " . $user_ID; + +if ( $last ) + $start = $wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment' $and_user $and_post") - $num; +else + $start = (int) $start; + +if ( $start < 0 ) + $start = 0; + +if ( '' == $sort ) + $sort = "post_date_gmt DESC"; + +$attachments = $wpdb->get_results("SELECT ID, post_date, post_title, post_mime_type, guid FROM $wpdb->posts WHERE post_status = 'attachment' $and_type $and_post $and_user ORDER BY $sort LIMIT $start, $double", ARRAY_A); + +if ( count($attachments) == 0 ) { + header("Location: ".basename(__FILE__)."?post=$post&action=upload"); + die; +} elseif ( count($attachments) > $num ) { + $next = $start + count($attachments) - $num; +} else { + $next = false; +} + +if ( $start > 0 ) { + $back = $start - $num; + if ( $back < 1 ) + $back = '0'; +} else { + $back = false; +} + +$uwidth_sum = 0; +$html = ''; +$popups = ''; +$style = ''; +$script = ''; +if ( count($attachments) > 0 ) { + $attachments = array_slice( $attachments, 0, $num ); + $__delete = __('Delete'); + $__not_linked = __('Not Linked'); + $__linked_to_page = __('Linked to Page'); + $__linked_to_image = __('Linked to Image'); + $__linked_to_file = __('Linked to File'); + $__using_thumbnail = __('Using Thumbnail'); + $__using_original = __('Using Original'); + $__using_title = __('Using Title'); + $__using_filename = __('Using Filename'); + $__using_icon = __('Using Icon'); + $__no_thumbnail = ''.__('No Thumbnail').''; + $__send_to_editor = __('Send to editor'); + $__close = __('Close Options'); + $__confirmdelete = __('Delete this file from the server?'); + $__nothumb = __('There is no thumbnail associated with this photo.'); + $script .= "notlinked = '$__not_linked'; +linkedtoimage = '$__linked_to_image'; +linkedtopage = '$__linked_to_page'; +linkedtofile = '$__linked_to_file'; +usingthumbnail = '$__using_thumbnail'; +usingoriginal = '$__using_original'; +usingtitle = '$__using_title'; +usingfilename = '$__using_filename'; +usingicon = '$__using_icon'; +var aa = new Array(); +var ab = new Array(); +var imga = new Array(); +var imgb = new Array(); +var srca = new Array(); +var srcb = new Array(); +var title = new Array(); +var filename = new Array(); +var icon = new Array(); +"; + foreach ( $attachments as $key => $attachment ) { + $ID = $attachment['ID']; + $href = get_attachment_link($ID); + $meta = get_post_meta($ID, '_wp_attachment_metadata', true); + if (!is_array($meta)) { + $meta = get_post_meta($ID, 'imagedata', true); // Try 1.6 Alpha meta key + if (!is_array($meta)) { + $meta = array(); + } + add_post_meta($ID, '_wp_attachment_metadata', $meta); + } + $attachment = array_merge($attachment, $meta); + $noscript = " +"; + $send_delete_cancel = "$__send_to_editor +$__delete + $__close +"; + $uwidth_sum += 128; + if ( preg_match('!^image/!', $attachment['post_mime_type'] ) ) { + $image = & $attachment; + if ( ($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']).'/'.$image['thumb']) ) { + $src = str_replace(basename($image['guid']), $image['thumb'], $image['guid']); + $script .= "srca[{$ID}] = '$src'; +srcb[{$ID}] = '{$image['guid']}'; +"; + $thumb = 'true'; + $thumbtext = $__using_thumbnail; + } else { + $src = $image['guid']; + $thumb = 'false'; + $thumbtext = $__no_thumbnail; + } + list($image['uwidth'], $image['uheight']) = get_udims($image['width'], $image['height']); + $height_width = 'height="'.$image['uheight'].'" width="'.$image['uwidth'].'"'; + $xpadding = (128 - $image['uwidth']) / 2; + $ypadding = (96 - $image['uheight']) / 2; + $style .= "#target{$ID} img { padding: {$ypadding}px {$xpadding}px; }\n"; + $script .= "aa[{$ID}] = ''; +ab[{$ID}] = ''; +imga[{$ID}] = '\"{$image['post_title']}\"'; +imgb[{$ID}] = '\"{$image['post_title']}\"'; +"; + $html .= "
    +
    + \"{$image['post_title']}\" +
    + {$noscript} +
    +"; + $popups .= "
    +"; + } else { + $title = $attachment['post_title']; + $filename = basename($attachment['guid']); + $icon = get_attachment_icon($ID); + $toggle_icon = "$__using_title"; + $script .= "aa[{$ID}] = '{$attachment['post_title']}'; +ab[{$ID}] = '{$attachment['post_title']}'; +title[{$ID}] = '{$attachment['post_title']}'; +filename[{$ID}] = '{$filename}'; +icon[{$ID}] = '{$icon}'; +"; + $html .= "
    + + {$noscript} +
    +"; + $popups .= " +"; + } + } +} + +$images_width = $uwidth_sum + ( count($images) * 6 ) + 35; + +break; + +default: +die(__('This script was not meant to be called directly.')); +} + +?> + + + + + + + + + +
      +> +get_results("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post'") ) { ?> +> + +get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment'")) { ?> +> + +
    • + + +
    • +
    • «
    • + +
    • +
    • «
    • + + +
    • »
    • +
    • »|
    • + +
    • »
    • +
    • »|
    • + + +
    + +
    + +
    + + +
    +
    + +
    +
    + + + + + + + + + + + + + + + + + +
    + + + + +
    + + + + +
    +
    + +
    + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/install-helper.php b/projects/tests/tests/real/wordpress/wp-admin/install-helper.php new file mode 100644 index 00000000..86138db1 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/install-helper.php @@ -0,0 +1,152 @@ +get_col("SHOW TABLES",0) as $table ) { + if ($table == $table_name) { + return true; + } + } + //didn't find it try to create it. + $q = $wpdb->query($create_ddl); + // we cannot directly tell that whether this succeeded! + foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { + if ($table == $table_name) { + return true; + } + } + return false; +} + +/** + ** maybe_add_column() + ** Add column to db table if it doesn't exist. + ** Returns: true if already exists or on successful completion + ** false on error + */ +function maybe_add_column($table_name, $column_name, $create_ddl) { + global $wpdb, $debug; + foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { + if ($debug) echo("checking $column == $column_name
    "); + if ($column == $column_name) { + return true; + } + } + //didn't find it try to create it. + $q = $wpdb->query($create_ddl); + // we cannot directly tell that whether this succeeded! + foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { + if ($column == $column_name) { + return true; + } + } + return false; +} + + +/** + ** maybe_drop_column() + ** Drop column from db table if it exists. + ** Returns: true if it doesn't already exist or on successful drop + ** false on error + */ +function maybe_drop_column($table_name, $column_name, $drop_ddl) { + global $wpdb; + foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { + if ($column == $column_name) { + //found it try to drop it. + $q = $wpdb->query($drop_ddl); + // we cannot directly tell that whether this succeeded! + foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { + if ($column == $column_name) { + return false; + } + } + } + } + // else didn't find it + return true; +} + + +/** + ** check_column() + ** Check column matches passed in criteria. + ** Pass in null to skip checking that criteria + ** Returns: true if it matches + ** false otherwise + ** (case sensitive) Column names returned from DESC table are: + ** Field + ** Type + ** Null + ** Key + ** Default + ** Extra + */ +function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) { + global $wpdb, $debug; + $diffs = 0; + $results = $wpdb->get_results("DESC $table_name"); + + foreach ($results as $row ) { + if ($debug > 1) print_r($row); + if ($row->Field == $col_name) { + // got our column, check the params + if ($debug) echo ("checking $row->Type against $col_type\n"); + if (($col_type != null) && ($row->Type != $col_type)) { + ++$diffs; + } + if (($is_null != null) && ($row->Null != $is_null)) { + ++$diffs; + } + if (($key != null) && ($row->Key != $key)) { + ++$diffs; + } + if (($default != null) && ($row->Default != $default)) { + ++$diffs; + } + if (($extra != null) && ($row->Extra != $extra)) { + ++$diffs; + } + if ($diffs > 0) { + if ($debug) echo ("diffs = $diffs returning false\n"); + return false; + } + return true; + } // end if found our column + } + return false; +} + +/* +echo "

    testing

    "; +echo "
    ";
    +
    +//check_column('wp_links', 'link_description', 'mediumtext'); 
    +//if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
    +//    echo "ok\n";
    +$error_count = 0;
    +$tablename = $wpdb->links;
    +// check the column
    +if (!check_column($wpdb->links, 'link_description', 'varchar(255)'))
    +{
    +    $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
    +    $q = $wpdb->query($ddl);
    +}
    +if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
    +    $res .= $tablename . ' - ok 
    '; +} else { + $res .= 'There was a problem with ' . $tablename . '
    '; + ++$error_count; +} +echo "
    "; +*/ +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/install.php b/projects/tests/tests/real/wordpress/wp-admin/install.php new file mode 100644 index 00000000..3940b2b4 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/install.php @@ -0,0 +1,226 @@ +wp-config.php file. I need this before we can get started. Need more help? We got it. You can create a wp-config.php file through a web interface, but this doesn't work for all server setups. The safest way is to manually create the file."); + +require_once('../wp-config.php'); +require_once('./upgrade-functions.php'); + +$schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://'; +$guessurl = str_replace('/wp-admin/install.php?step=2', '', $schema . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) ); + +if (isset($_GET['step'])) + $step = $_GET['step']; +else + $step = 0; +header( 'Content-Type: text/html; charset=utf-8' ); +?> + + + + <?php _e('WordPress › Installation'); ?> + + + + +

    WordPress

    +hide_errors(); +$installed = $wpdb->get_results("SELECT * FROM $wpdb->users"); +if ($installed) die('

    '.__('Already Installed').'

    '.__('You appear to have already installed WordPress. To reinstall please clear your old database tables first.').'

    '); +$wpdb->show_errors(); + +switch($step) { + + case 0: +?> +

    ReadMe documentation at your leisure.'), '../readme.html'); ?>

    +

    + +

    +

    + +
    + + + + + + + + + +
    +

    +

    + +

    +
    + +ERROR: please type your e-mail address")); +} else if (!is_email($admin_email)) { + die (__("ERROR: the e-mail address isn't correct")); +} + +?> +

    +

    + + +query("INSERT INTO $wpdb->linkcategories (cat_id, cat_name) VALUES (1, '".$wpdb->escape(__('Blogroll'))."')"); +$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://blogs.linux.ie/xeer/', 'Donncha', 1, 'http://blogs.linux.ie/xeer/feed/', '');"); +$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zengun.org/weblog/', 'Michel', 1, 'http://zengun.org/weblog/feed/', '');"); +$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://boren.nu/', 'Ryan', 1, 'http://boren.nu/feed/', '');"); +$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://photomatt.net/', 'Matt', 1, 'http://xml.photomatt.net/feed/', '');"); +$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zed1.com/journalized/', 'Mike', 1, 'http://zed1.com/journalized/feed/', '');"); +$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://www.alexking.org/', 'Alex', 1, 'http://www.alexking.org/blog/wp-rss2.php', '');"); +$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://dougal.gunters.org/', 'Dougal', 1, 'http://dougal.gunters.org/feed/', '');"); + +// Default category +$wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_count, category_description) VALUES ('0', '".$wpdb->escape(__('Uncategorized'))."', '".sanitize_title(__('Uncategorized'))."', '1', '')"); + +// First post +$now = date('Y-m-d H:i:s'); +$now_gmt = gmdate('Y-m-d H:i:s'); +$wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, comment_count, to_ping, pinged, post_content_filtered) VALUES ('1', '$now', '$now_gmt', '".$wpdb->escape(__('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'))."', '', '".$wpdb->escape(__('Hello world!'))."', '0', '".$wpdb->escape(__('hello-world'))."', '$now', '$now_gmt', '1', '', '', '')"); + +$wpdb->query( "INSERT INTO $wpdb->post2cat (`rel_id`, `post_id`, `category_id`) VALUES (1, 1, 1)" ); + +// Default comment +$wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_date_gmt, comment_content) VALUES ('1', '".$wpdb->escape(__('Mr WordPress'))."', '', 'http://wordpress.org/', '$now', '$now_gmt', '".$wpdb->escape(__('Hi, this is a comment.
    To delete a comment, just log in, and view the posts\' comments, there you will have the option to edit or delete them.'))."')"); + +// First Page + +$wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, post_status, to_ping, pinged, post_content_filtered) VALUES ('1', '$now', '$now_gmt', '".$wpdb->escape(__('This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.'))."', '', '".$wpdb->escape(__('About'))."', '0', '".$wpdb->escape(__('about'))."', '$now', '$now_gmt', 'static', '', '', '')"); +generate_page_rewrite_rules(); + +// Set up admin user +$random_password = substr(md5(uniqid(microtime())), 0, 6); +$display_name_array = explode('@', $admin_email); +$display_name = $display_name_array[0]; +$wpdb->query("INSERT INTO $wpdb->users (ID, user_login, user_pass, user_email, user_registered, display_name, user_nicename) VALUES ( '1', 'admin', MD5('$random_password'), '$admin_email', NOW(), '$display_name', 'admin')"); +$wpdb->query("INSERT INTO $wpdb->usermeta (user_id, meta_key, meta_value) VALUES ({$wpdb->insert_id}, '{$table_prefix}user_level', '10');"); +$admin_caps = serialize(array('administrator' => true)); +$wpdb->query("INSERT INTO $wpdb->usermeta (user_id, meta_key, meta_value) VALUES ({$wpdb->insert_id}, '{$table_prefix}capabilities', '{$admin_caps}');"); + +$message_headers = 'From: ' . $weblog_title . ' '; +$message = sprintf(__("Your new WordPress blog has been successfully set up at: + +%1\$s + +You can log in to the administrator account with the following information: + +Username: admin +Password: %2\$s + +We hope you enjoy your new weblog. Thanks! + +--The WordPress Team +http://wordpress.org/ +"), $guessurl, $random_password); + +@wp_mail($admin_email, __('New WordPress Blog'), $message, $message_headers); + +wp_cache_flush(); +?> + +

    + +

    log in with the username "admin" and password "%2$s".'), '../wp-login.php', $random_password); ?>

    +

    Note that password carefully! It is a random password that was generated just for you. If you lose it, you will have to delete the tables from the database yourself, and re-install WordPress. So to review:'); ?> +

    +
    +
    +
    admin
    +
    +
    +
    +
    wp-login.php
    +
    +

    + + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/link-add.php b/projects/tests/tests/real/wordpress/wp-admin/link-add.php new file mode 100644 index 00000000..d5b609cc --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/link-add.php @@ -0,0 +1,47 @@ + + + +

    + + + + +
    +You can drag Link This to your toolbar and when you click it a window will pop up that will allow you to add whatever site you’re on to your links! Right now this only works on Mozilla or Netscape, but we’re working on it.

    '), "javascript:void(linkmanpopup=window.open('" . get_settings('siteurl') . "/wp-admin/link-add.php?action=popup&linkurl='+escape(location.href)+'&name='+escape(document.title),'LinkManager','scrollbars=yes,width=750,height=550,left=15,top=15,status=yes,resizable=yes'));linkmanpopup.focus();window.focus();linkmanpopup.focus();") ?> +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/link-categories.php b/projects/tests/tests/real/wordpress/wp-admin/link-categories.php new file mode 100644 index 00000000..bbbe65e1 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/link-categories.php @@ -0,0 +1,454 @@ +query("INSERT INTO $wpdb->linkcategories (cat_id, cat_name, auto_toggle, show_images, show_description, \n" . + " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, text_after_all, list_limit) \n" . + " VALUES ('0', '$cat_name', '$auto_toggle', '$show_images', '$show_description', \n" . + " '$show_rating', '$show_updated', '$sort_order', '$sort_desc', '$text_before_link', '$text_after_link', \n" . + " '$text_after_all', $list_limit)"); + + header('Location: link-categories.php'); + break; + } // end addcat + case 'Delete': + { + $cat_id = (int) $_GET['cat_id']; + $cat_name=get_linkcatname($cat_id); + + if ($cat_id=="1") + die(sprintf(__("Can't delete the %s link category: this is the default one"), $cat_name)); + + if ( !current_user_can('manage_links') ) + die (__("Cheatin' uh ?")); + + $wpdb->query("DELETE FROM $wpdb->linkcategories WHERE cat_id='$cat_id'"); + $wpdb->query("UPDATE $wpdb->links SET link_category=1 WHERE link_category='$cat_id'"); + + header('Location: link-categories.php'); + break; + } // end delete + case 'Edit': + { + include_once ('admin-header.php'); + $cat_id = (int) $_GET['cat_id']; + $row = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, " + . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, " + . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$cat_id"); + if ($row) { + if ($row->list_limit == -1) { + $row->list_limit = ''; + } +?> + +
    +

    cat_name)); ?>

    + +
    + + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + + title regardless)') ?>
    +
    + +title regardless)') ?>
    + + +
    + + +
    +
    +
    + + + + + + + + + + + + + + +
    +
    +

    +
    + +
    +query("UPDATE $wpdb->linkcategories set + cat_name='$cat_name', + auto_toggle='$auto_toggle', + show_images='$show_images', + show_description='$show_description', + show_rating='$show_rating', + show_updated='$show_updated', + sort_order='$sort_order', + sort_desc='$sort_desc', + text_before_link='$text_before_link', + text_after_link='$text_after_link', + text_after_all='$text_after_all', + list_limit=$list_limit + WHERE cat_id=$cat_id + "); + } // end if save + + + header("Location: link-categories.php"); + break; + } // end editcat + default: + { + include_once ("admin-header.php"); + if ( !current_user_can('manage_links') ) + die(__("You have do not have sufficient permissions to edit the link categories for this blog. :)")); +?> + +
    +

    + + + + + + + + + + + + + + + + + + + + + +get_results("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, " + . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, " + . " text_after_all, list_limit FROM $wpdb->linkcategories ORDER BY cat_id"); +$i = 1; +foreach ($results as $row) { + if ($row->list_limit == -1) { + $row->list_limit = __('none'); + } + $style = ($i % 2) ? ' class="alternate"' : ''; + /* + Manually internationalize every sort order option. + */ + switch ($row->sort_order) { + case 'name': + $row->sort_order = __('name'); + break; + case 'id': + $row->sort_order = __('id'); + break; + case 'url': + $row->sort_order = __('url'); + break; + case 'rating': + $row->sort_order = __('rating'); + break; + case 'updated': + $row->sort_order = __('updated'); + break; + case 'rand': + $row->sort_order = __('rand'); + break; + case 'length': + $row->sort_order = __('length'); + break; + } +?> + style="border-bottom: 1px dotted #9C9A9C;"> + + + + + + + + + + + + + + + + + +
     
    +

    + +
    + +
    + +
    +
    + +

    +
    + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + + title regardless)') ?>
    +
    + +title regardless)') ?>
    + + +
    + +
    +
    +
    + + + + + + + + + + + + + + +
    +
    +

    +
    +
    +
    +

    +

    It will just set them back to the default category %s.'), get_linkcatname(1)) ?>

    +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/link-import.php b/projects/tests/tests/real/wordpress/wp-admin/link-import.php new file mode 100644 index 00000000..a5f22089 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/link-import.php @@ -0,0 +1,131 @@ + + + +
    +

    +
    + +

    +

    + + +
    +

    + +
    + +
    +

    + +
    + + +
    + +


    +

    + +

    +
    + +
    + +
    + +

    + false, 'test_type' => false); + $file = wp_handle_upload($_FILES['import'], $overrides); + + if ( isset($file['error']) ) + die($file['error']); + + $url = $file['url']; + $opml_url = $file['file']; + $blogrolling = false; + } + + if (isset($opml_url) && $opml_url != '') { + $opml = wp_remote_fopen($opml_url); + include_once('link-parse-opml.php'); + + $link_count = count($names); + for ($i = 0; $i < $link_count; $i++) { + if ('Last' == substr($titles[$i], 0, 4)) + $titles[$i] = ''; + if ('http' == substr($titles[$i], 0, 4)) + $titles[$i] = ''; + // FIXME: Use wp_insert_link(). + $query = "INSERT INTO $wpdb->links (link_url, link_name, link_target, link_category, link_description, link_owner, link_rss) + VALUES('{$urls[$i]}', '".$wpdb->escape($names[$i])."', '', $cat_id, '".$wpdb->escape($descriptions[$i])."', $user_ID, '{$feeds[$i]}')\n"; + $result = $wpdb->query($query); + echo sprintf('

    '.__('Inserted %s').'

    ', $names[$i]); + } +?> +

    manage those links.'), $link_count, $cat_id, 'link-manager.php') ?>

    +" . __("You need to supply your OPML url. Press back on your browser and try again") . "

    \n"; + } // end else + + if ( ! $blogrolling ) + @unlink($opml_url); +?> +
    + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/link-manager.php b/projects/tests/tests/real/wordpress/wp-admin/link-manager.php new file mode 100644 index 00000000..4c1a2f87 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/link-manager.php @@ -0,0 +1,444 @@ += the + //userlevel of the owner of the link then we can proceed. + + if (count($linkcheck) == 0) { + header('Location: ' . $this_file); + exit; + } + $all_links = join(',', $linkcheck); + $results = $wpdb->get_results("SELECT link_id, link_owner FROM $wpdb->links LEFT JOIN $wpdb->users ON link_owner = ID WHERE link_id in ($all_links)"); + foreach ($results as $row) { + $ids_to_change[] = $row->link_id; + } + + // should now have an array of links we can change + $all_links = join(',', $ids_to_change); + $q = $wpdb->query("update $wpdb->links SET link_owner='$newowner' WHERE link_id IN ($all_links)"); + + header('Location: ' . $this_file); + break; + } + case 'visibility': + { + check_admin_referer(); + + // check the current user's level first. + if ( !current_user_can('manage_links') ) + die (__("Cheatin' uh ?")); + + //for each link id (in $linkcheck[]): toggle the visibility + if (count($linkcheck) == 0) { + header('Location: ' . $this_file); + exit; + } + $all_links = join(',', $linkcheck); + $results = $wpdb->get_results("SELECT link_id, link_visible FROM $wpdb->links WHERE link_id in ($all_links)"); + foreach ($results as $row) { + if ($row->link_visible == 'Y') { // ok to proceed + $ids_to_turnoff[] = $row->link_id; + } else { + $ids_to_turnon[] = $row->link_id; + } + } + + // should now have two arrays of links to change + if (count($ids_to_turnoff)) { + $all_linksoff = join(',', $ids_to_turnoff); + $q = $wpdb->query("update $wpdb->links SET link_visible='N' WHERE link_id IN ($all_linksoff)"); + } + + if (count($ids_to_turnon)) { + $all_linkson = join(',', $ids_to_turnon); + $q = $wpdb->query("update $wpdb->links SET link_visible='Y' WHERE link_id IN ($all_linkson)"); + } + + header('Location: ' . $this_file); + break; + } + case 'move': + { + check_admin_referer(); + + // check the current user's level first. + if ( !current_user_can('manage_links') ) + die (__("Cheatin' uh ?")); + + //for each link id (in $linkcheck[]) change category to selected value + if (count($linkcheck) == 0) { + header('Location: ' . $this_file); + exit; + } + $all_links = join(',', $linkcheck); + // should now have an array of links we can change + $q = $wpdb->query("update $wpdb->links SET link_category='$category' WHERE link_id IN ($all_links)"); + + header('Location: ' . $this_file); + break; + } + + case 'Add': + { + check_admin_referer(); + + add_link(); + + header('Location: ' . $_SERVER['HTTP_REFERER'] . '?added=true'); + break; + } // end Add + + case 'editlink': + { + + check_admin_referer(); + + if (isset($links_show_cat_id) && ($links_show_cat_id != '')) + $cat_id = $links_show_cat_id; + + if (!isset($cat_id) || ($cat_id == '')) { + if (!isset($links_show_cat_id) || ($links_show_cat_id == '')) + $cat_id = 'All'; + } + $links_show_cat_id = $cat_id; + + $link_id = (int) $_POST['link_id']; + edit_link($link_id); + + setcookie('links_show_cat_id_' . COOKIEHASH, $links_show_cat_id, time()+600); + wp_redirect($this_file); + break; + } // end Save + + case 'Delete': + { + check_admin_referer(); + + if ( !current_user_can('manage_links') ) + die (__("Cheatin' uh ?")); + + $link_id = (int) $_GET['link_id']; + + wp_delete_link($link_id); + + if (isset($links_show_cat_id) && ($links_show_cat_id != '')) + $cat_id = $links_show_cat_id; + + if (!isset($cat_id) || ($cat_id == '')) { + if (!isset($links_show_cat_id) || ($links_show_cat_id == '')) + $cat_id = 'All'; + } + $links_show_cat_id = $cat_id; + setcookie('links_show_cat_id_' . COOKIEHASH, $links_show_cat_id, time()+600); + wp_redirect($this_file); + break; + } // end Delete + + case 'linkedit': { + $xfn = true; + include_once ('admin-header.php'); + if ( !current_user_can('manage_links') ) + die(__('You do not have sufficient permissions to edit the links for this blog.')); + + $link_id = (int) $_GET['link_id']; + + if ( !$link = get_link_to_edit($link_id) ) + die( __('Link not found.') ); + + include('edit-link-form.php'); + break; + } // end linkedit + case __("Show"): + { + if (!isset($cat_id) || ($cat_id == '')) { + if (!isset($links_show_cat_id) || ($links_show_cat_id == '')) + $cat_id = 'All'; + } + $links_show_cat_id = $cat_id; + if (!isset($order_by) || ($order_by == '')) { + if (!isset($links_show_order) || ($links_show_order == '')) + $order_by = 'order_name'; + } + $links_show_order = $order_by; + //break; fall through + } // end Show + case "popup": + { + $link_url = stripslashes($_GET["linkurl"]); + $link_name = stripslashes($_GET["name"]); + //break; fall through + } + default: + { + if (isset($links_show_cat_id) && ($links_show_cat_id != '')) + $cat_id = $links_show_cat_id; + + if (!isset($cat_id) || ($cat_id == '')) { + if (!isset($links_show_cat_id) || ($links_show_cat_id == '')) + $cat_id = 'All'; + } + $links_show_cat_id = $cat_id; + if (isset($links_show_order) && ($links_show_order != '')) + $order_by = $links_show_order; + + if (!isset($order_by) || ($order_by == '')) + $order_by = 'order_name'; + $links_show_order = $order_by; + + setcookie('links_show_cat_id_' . COOKIEHASH, $links_show_cat_id, time()+600); + setcookie('links_show_order_' . COOKIEHASH, $links_show_order, time()+600); + include_once ("./admin-header.php"); + if ( !current_user_can('manage_links') ) + die(__("You do not have sufficient permissions to edit the links for this blog.")); + + switch ($order_by) + { + case 'order_id': $sqlorderby = 'id'; break; + case 'order_url': $sqlorderby = 'url'; break; + case 'order_desc': $sqlorderby = 'description'; break; + case 'order_owner': $sqlorderby = 'owner'; break; + case 'order_rating': $sqlorderby = 'rating'; break; + case 'order_name': + default: $sqlorderby = 'name'; break; + } + + if ($action != "popup") { +?> + + +
    +
    + + + + + + + + + + + +
    + Show links in category:'); ?>
    +
    + Order by:');?> +  
    +get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id"); + echo " \n"; +?> + + + + +
    +
    + +
    + + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/link-parse-opml.php b/projects/tests/tests/real/wordpress/wp-admin/link-parse-opml.php new file mode 100644 index 00000000..196f3cb0 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/link-parse-opml.php @@ -0,0 +1,65 @@ + 'link_url', + 'HTMLURL' => 'link_url', + 'TEXT' => 'link_name', + 'TITLE' => 'link_name', + 'TARGET' => 'link_target', + 'DESCRIPTION' => 'link_description', + 'XMLURL' => 'link_rss' +); + +$map = $opml_map; + +/** + ** startElement() + ** Callback function. Called at the start of a new xml tag. + **/ +function startElement($parser, $tagName, $attrs) { + global $updated_timestamp, $all_links, $map; + global $names, $urls, $targets, $descriptions, $feeds; + + if ($tagName == 'OUTLINE') { + foreach (array_keys($map) as $key) { + if (isset($attrs[$key])) { + $$map[$key] = $attrs[$key]; + } + } + + //echo("got data: link_url = [$link_url], link_name = [$link_name], link_target = [$link_target], link_description = [$link_description]
    \n"); + + // save the data away. + $names[] = $link_name; + $urls[] = $link_url; + $targets[] = $link_target; + $feeds[] = $link_rss; + $descriptions[] = $link_description; + } // end if outline +} + +/** + ** endElement() + ** Callback function. Called at the end of an xml tag. + **/ +function endElement($parser, $tagName) { + // nothing to do. +} + +// Create an XML parser +$xml_parser = xml_parser_create(); + +// Set the functions to handle opening and closing tags +xml_set_element_handler($xml_parser, "startElement", "endElement"); + +if (!xml_parse($xml_parser, $opml, true)) { + echo(sprintf(__('XML error: %1$s at line %2$s'), + xml_error_string(xml_get_error_code($xml_parser)), + xml_get_current_line_number($xml_parser))); +} + +// Free up memory used by the XML parser +xml_parser_free($xml_parser); +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/list-manipulation.js b/projects/tests/tests/real/wordpress/wp-admin/list-manipulation.js new file mode 100644 index 00000000..38cc6024 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/list-manipulation.js @@ -0,0 +1,96 @@ +var listItems; +var reg_color = '#FFFFFF'; +var alt_color = '#F1F1F1'; + +addLoadEvent(getListItems); + +function deleteSomething( what, id, message ) { + what.replace('-', ' '); + if (!message) message = 'Are you sure you want to delete this ' + what + '?'; + if ( confirm(message) ) { + return ajaxDelete( what.replace(' ', '-'), id ); + } else { + return false; + } +} + +function getResponseElement() { + var p = document.getElementById('ajax-response-p'); + if (!p) { + p = document.createElement('p'); + p.id = 'ajax-response-p'; + document.getElementById('ajax-response').appendChild(p); + return p; + } +} + +function ajaxDelete(what, id) { + ajaxDel = new sack('list-manipulation.php'); + if ( ajaxDel.failed ) return true; + ajaxDel.myResponseElement = getResponseElement(); + ajaxDel.method = 'POST'; + ajaxDel.onLoading = function() { ajaxDel.myResponseElement.innerHTML = 'Sending Data...'; }; + ajaxDel.onLoaded = function() { ajaxDel.myResponseElement.innerHTML = 'Data Sent...'; }; + ajaxDel.onInteractive = function() { ajaxDel.myResponseElement.innerHTML = 'Processing Data...'; }; + ajaxDel.onCompletion = function() { removeThisItem( what + '-' + id ); }; + ajaxDel.runAJAX('action=delete-' + what + '&id=' + id); + return false; +} + +function removeThisItem(id) { + var response = ajaxDel.response; + if ( isNaN(response) ) { alert(response); } + response = parseInt(response, 10); + if ( -1 == response ) { ajaxDel.myResponseElement.innerHTML = "You don't have permission to do that."; } + else if ( 0 == response ) { ajaxDel.myResponseElement.interHTML = "Something odd happened. Try refreshing the page? Either that or what you tried to delete never existed in the first place."; } + else if ( 1 == response ) { + theItem = document.getElementById(id); + Fat.fade_element(id,null,700,'#FF3333'); + setTimeout('theItem.parentNode.removeChild(theItem)', 705); + var pos = getListPos(id); + listItems.splice(pos,1); + recolorList(pos); + ajaxDel.myResponseElement.parentNode.removeChild(ajaxDel.myResponseElement); + + } +} + +function getListPos(id) { + for (var i = 0; i < listItems.length; i++) { + if (id == listItems[i]) { + var pos = i; + break; + } + } + return pos; +} + +function getListItems() { + if (list) return; + listItems = new Array(); + var extra = false; + var list = document.getElementById('the-list'); + if (!list) { var list = document.getElementById('the-list-x'); extra = true; } + if (list) { + var items = list.getElementsByTagName('tr'); + if (!items[0]) { items = list.getElementsByTagName('li'); } + for (var i=0; iquery("DELETE FROM $wpdb->links WHERE link_id = '$id'") ) + die('1'); + else die('0'); + break; +case 'delete-post' : +case 'delete-page' : + $id = (int) $_POST['id']; + if ( !current_user_can('edit_post', $post_id) ) + die('-1'); + + if ( wp_delete_post($id) ) + die('1'); + else die('0'); + break; +case 'delete-cat' : + if ( !current_user_can('manage_categories') ) + die ('-1'); + + $id = (int) $_POST['id']; + $cat_name = get_catname($cat_ID); + + if ( wp_delete_category($id) ) + die('1'); + else die('0'); + break; +case 'delete-comment' : + $id = (int) $_POST['id']; + + if ( !$comment = get_comment($id) ) + die('0'); + if ( !current_user_can('edit_post', $comment->comment_post_ID) ) + die('-1'); + + if ( wp_delete_comment($comment->comment_ID) ) { + die('1'); + } else { + die('0'); + } + break; +case 'delete-link-category' : + $id = (int) $_POST['id']; + if ( 1 == $id ) + die('0'); + if ( !current_user_can('manage_links') ) + die('-1'); + + if ( $wpdb->query("DELETE FROM $wpdb->linkcategories WHERE cat_id='$id'") ) { + $wpdb->query("UPDATE $wpdb->links SET link_category=1 WHERE link_category='$id'"); + die('1'); + } else { + die('0'); + } + break; +endswitch; +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/menu-header.php b/projects/tests/tests/real/wordpress/wp-admin/menu-header.php new file mode 100644 index 00000000..379ee4ca --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/menu-header.php @@ -0,0 +1,55 @@ + + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/menu.php b/projects/tests/tests/real/wordpress/wp-admin/menu.php new file mode 100644 index 00000000..ed58186c --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/menu.php @@ -0,0 +1,64 @@ +get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'"); +$submenu['edit.php'][25] = array(sprintf(__("Awaiting Moderation (%s)"), $awaiting_mod), 'edit_posts', 'moderation.php'); +$submenu['edit.php'][30] = array(__('Files'), 'edit_files', 'templates.php'); + +$submenu['link-manager.php'][5] = array(__('Manage Links'), 'manage_links', 'link-manager.php'); +$submenu['link-manager.php'][10] = array(__('Add Link'), 'manage_links', 'link-add.php'); +$submenu['link-manager.php'][15] = array(__('Link Categories'), 'manage_links', 'link-categories.php'); +$submenu['link-manager.php'][20] = array(__('Import Links'), 'manage_links', 'link-import.php'); + +$submenu['profile.php'][5] = array(__('Your Profile'), 'read', 'profile.php'); +$submenu['profile.php'][10] = array(__('Authors & Users'), 'edit_users', 'users.php'); + +$submenu['options-general.php'][10] = array(__('General'), 'manage_options', 'options-general.php'); +$submenu['options-general.php'][15] = array(__('Writing'), 'manage_options', 'options-writing.php'); +$submenu['options-general.php'][20] = array(__('Reading'), 'manage_options', 'options-reading.php'); +$submenu['options-general.php'][25] = array(__('Discussion'), 'manage_options', 'options-discussion.php'); +$submenu['options-general.php'][30] = array(__('Permalinks'), 'manage_options', 'options-permalink.php'); +$submenu['options-general.php'][35] = array(__('Miscellaneous'), 'manage_options', 'options-misc.php'); + +$submenu['plugins.php'][5] = array(__('Plugins'), 'activate_plugins', 'plugins.php'); +$submenu['plugins.php'][10] = array(__('Plugin Editor'), 'edit_plugins', 'plugin-editor.php'); + +$submenu['themes.php'][5] = array(__('Themes'), 'switch_themes', 'themes.php'); +$submenu['themes.php'][10] = array(__('Theme Editor'), 'edit_themes', 'theme-editor.php'); + +// Create list of page plugin hook names. +foreach ($menu as $menu_page) { + $admin_page_hooks[$menu_page[2]] = sanitize_title($menu_page[0]); +} + +do_action('admin_menu', ''); +ksort($menu); // make it all pretty + +if (! user_can_access_admin_page()) { + die( __('You do not have sufficient permissions to access this page.') ); +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/moderation.php b/projects/tests/tests/real/wordpress/wp-admin/moderation.php new file mode 100644 index 00000000..a3c3d7f4 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/moderation.php @@ -0,0 +1,221 @@ + $v) { + $comment[intval($k)] = $v; + } +} + +switch($action) { + +case 'update': + + if ( ! current_user_can('moderate_comments') ) + die('

    '.__('Your level is not high enough to moderate comments.').'

    '); + + $item_ignored = 0; + $item_deleted = 0; + $item_approved = 0; + $item_spam = 0; + + foreach($comment as $key => $value) { + if ($feelinglucky && 'later' == $value) + $value = 'delete'; + switch($value) { + case 'later': + // do nothing with that comment + // wp_set_comment_status($key, "hold"); + ++$item_ignored; + break; + case 'delete': + wp_set_comment_status($key, 'delete'); + ++$item_deleted; + break; + case 'spam': + wp_set_comment_status($key, 'spam'); + ++$item_spam; + break; + case 'approve': + wp_set_comment_status($key, 'approve'); + if ( get_settings('comments_notify') == true ) { + wp_notify_postauthor($key); + } + ++$item_approved; + break; + } + } + + $file = basename(__FILE__); + header("Location: $file?ignored=$item_ignored&deleted=$item_deleted&approved=$item_approved&spam=$item_spam"); + exit(); + +break; + +default: + +require_once('admin-header.php'); + +if ( isset($_GET['deleted']) || isset($_GET['approved']) || isset($_GET['ignored']) ) { + echo "
    \n

    "; + $approved = (int) $_GET['approved']; + $deleted = (int) $_GET['deleted']; + $ignored = (int) $_GET['ignored']; + $spam = (int) $_GET['spam']; + if ($approved) { + if ('1' == $approved) { + echo __("1 comment approved") . "
    \n"; + } else { + echo sprintf(__("%s comments approved
    "), $approved) . "\n"; + } + } + if ($deleted) { + if ('1' == $deleted) { + echo __("1 comment deleted") . "
    \n"; + } else { + echo sprintf(__("%s comments deleted"), $deleted) . "
    \n"; + } + } + if ($spam) { + if ('1' == $spam) { + echo __("1 comment marked as spam") . "
    \n"; + } else { + echo sprintf(__("%s comments marked as spam"), $spam) . "
    \n"; + } + } + if ($ignored) { + if ('1' == $ignored) { + echo __("1 comment unchanged") . "
    \n"; + } else { + echo sprintf(__("%s comments unchanged"), $ignored) . "
    \n"; + } + } + echo "

    \n"; +} + +?> + +
    + +get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '0'"); +else + $comments = ''; + +if ($comments) { + // list all comments that are waiting for approval + $file = basename(__FILE__); +?> +

    +
    + +
      +comment_date); + $post_title = $wpdb->get_var("SELECT post_title FROM $wpdb->posts WHERE ID='$comment->comment_post_ID'"); + if ($i % 2) $class = 'class="alternate"'; + else $class = ''; + echo "\n\t
    1. "; + ?> +

      comment_author_email) { ?>| comment_author_url && 'http://' != $comment->comment_author_url) { ?> | | |

      + +

      comment_ID.'">' . __('Edit') . ' | ';?> + | +comment_post_ID."&comment=".$comment->comment_ID."\" onclick=\"return deleteSomething( 'comment', $comment->comment_ID, '" . sprintf(__("You are about to delete this comment by "%s".\\n"Cancel" to stop, "OK" to delete."), wp_specialchars($comment->comment_author, 1)) . "' );\">" . __('Delete just this comment') . " | "; ?> + + + + +

      + +
    2. + +
    + +
    + +

    + + + +
    +'.__("Currently there are no comments for you to moderate.") . "

    \n"; +} +?> + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/options-discussion.php b/projects/tests/tests/real/wordpress/wp-admin/options-discussion.php new file mode 100644 index 00000000..af2db9a6 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options-discussion.php @@ -0,0 +1,100 @@ +find_spam(); + echo $retrospaminator->display_edit_form( $result ); + include('./admin-footer.php'); + exit; +} +?> + +
    +

    +
    +
    +(These settings may be overridden for individual articles.)') ?> +
      +
    • + +
    • +
    • + +
    • +
    • + +
    • +
    +
    +
    + +
      +
    • + +
    • +
    • + +
    • +
    +
    +
    + +
      +
    • + +
    • +
    • +
    • +
    +
    +
    + +

    ' ) ?>

    + +

    Common spam words.') ?>

    +

    + +

    +

    + +

    +
    +
    + +

    +

    + +

    +

    +
    +

    + + + +

    +
    +
    + diff --git a/projects/tests/tests/real/wordpress/wp-admin/options-general.php b/projects/tests/tests/real/wordpress/wp-admin/options-general.php new file mode 100644 index 00000000..5620eccf --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options-general.php @@ -0,0 +1,110 @@ + + +
    +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    +

    to be different than the directory you installed WordPress in, enter that address here.'); ?>
    +
    +

    + +
    +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    UTC time is:') ?>
    +

    +

    +
     Documentation on date formatting. Save option to update sample output.') ?>
    + +
    +

    + +

    +
    +
    + diff --git a/projects/tests/tests/real/wordpress/wp-admin/options-head.php b/projects/tests/tests/real/wordpress/wp-admin/options-head.php new file mode 100644 index 00000000..579da0a3 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options-head.php @@ -0,0 +1,24 @@ + + +
    + + +

    + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/options-misc.php b/projects/tests/tests/real/wordpress/wp-admin/options-misc.php new file mode 100644 index 00000000..994b5ebc --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options-misc.php @@ -0,0 +1,27 @@ + + +
    +

    +
    +

    /> +

    +

    + +

    +

    + + + +

    +
    +
    + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/options-permalink.php b/projects/tests/tests/real/wordpress/wp-admin/options-permalink.php new file mode 100644 index 00000000..9c6c9cac --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options-permalink.php @@ -0,0 +1,177 @@ + + +set_permalink_structure($permalink_structure); + } + + if ( isset($_POST['category_base']) ) { + $category_base = $_POST['category_base']; + if (! empty($category_base) ) + $category_base = preg_replace('#/+#', '/', '/' . $_POST['category_base']); + $wp_rewrite->set_category_base($category_base); + } +} + +$permalink_structure = get_settings('permalink_structure'); +$category_base = get_settings('category_base'); + +generate_page_rewrite_rules(); + +if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') ) + $writable = true; +else + $writable = false; + +if ($wp_rewrite->using_index_permalinks()) + $usingpi = true; +else + $usingpi = false; + +save_mod_rewrite_rules(); +?> + + +

    + + +
    +

    +

    number of tags are available, and here are some examples to get you started.'); ?>

    + + +
    +

    +

    + +

    +

    + +

    +

    + +

    +

    + +
    +

    +

    :

    + +

    + +

    /taxonomy/tags would make your category links like http://example.org/taxonomy/tags/uncategorized/. If you leave this blank the default will be used.') ?>

    + +

    /index.php/taxonomy/tags would make your category links like http://example.org/index.php/taxonomy/tags/uncategorized/. If you leave this blank the default will be used.') ?>

    + +

    + : +

    +

    + +

    +
    + +

    .htaccess file were writable, we could do this automatically, but it isn’t so these are the mod_rewrite rules you should have in your .htaccess file. Click in the field and press CTRL + a to select all.') ?>

    +
    +

    + +

    +
    + + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/options-reading.php b/projects/tests/tests/real/wordpress/wp-admin/options-reading.php new file mode 100644 index 00000000..30f06d27 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options-reading.php @@ -0,0 +1,63 @@ + + +
    +

    +
    +
    + + + + + + +
    + + +
    +
    + +
    + + + + + + + + + + +
    +
    + +
    +
    + + + + + +

    +recommended)') ?>
    +

    + +

    +

    + + + +

    +
    +
    + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/options-writing.php b/projects/tests/tests/real/wordpress/wp-admin/options-writing.php new file mode 100644 index 00000000..d3d2fbae --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options-writing.php @@ -0,0 +1,99 @@ + + +
    +

    +
    + + + + + + + + + + + + + +
    +
    +
    +
    + +
    + +
    + +

    %s, %s, %s.'), substr(md5(uniqid(microtime())),0,5), substr(md5(uniqid(microtime())),0,5), substr(md5(uniqid(microtime())),0,5)) ?>

    + + + + + + + + + + + + + + + + + + +
    + + +
    + +
    +
    + +
    + +

    Update Services on the Codex. Separate multiple service URIs with line breaks.') ?>

    + + +
    + +

    + + + +

    +
    +
    + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/options.php b/projects/tests/tests/real/wordpress/wp-admin/options.php new file mode 100644 index 00000000..c67ccf5b --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/options.php @@ -0,0 +1,119 @@ + $value) { + $options[] = $key; + } + } else { + $options = explode(',', stripslashes($_POST['page_options'])); + } + + // Save for later. + $old_siteurl = get_settings('siteurl'); + $old_home = get_settings('home'); + + // HACK + // Options that if not there have 0 value but need to be something like "closed" + $nonbools = array('default_ping_status', 'default_comment_status'); + if ($options) { + foreach ($options as $option) { + $option = trim($option); + $value = trim(stripslashes($_POST[$option])); + if( in_array($option, $nonbools) && ( $value == '0' || $value == '') ) + $value = 'closed'; + + if( $option == 'blogdescription' || $option == 'blogname' ) + if (current_user_can('unfiltered_html') == false) + $value = wp_filter_post_kses( $value ); + + if ( update_option($option, $value) ) + $any_changed++; + } + } + + if ($any_changed) { + // If siteurl or home changed, reset cookies. + if ( get_settings('siteurl') != $old_siteurl || get_settings('home') != $old_home ) { + // If home changed, write rewrite rules to new location. + save_mod_rewrite_rules(); + // Get currently logged in user and password. + get_currentuserinfo(); + // Clear cookies for old paths. + wp_clearcookie(); + // Set cookies for new paths. + wp_setcookie($user_login, $user_pass_md5, true, get_settings('home'), get_settings('siteurl')); + } + + //$message = sprintf(__('%d setting(s) saved... '), $any_changed); + } + + $referred = remove_query_arg('updated' , $_SERVER['HTTP_REFERER']); + $goback = add_query_arg('updated', 'true', $_SERVER['HTTP_REFERER']); + $goback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $goback); + wp_redirect($goback); + break; + +default: + include('admin-header.php'); ?> + +
    +

    +
    + + +get_results("SELECT * FROM $wpdb->options ORDER BY option_name"); + +foreach ($options as $option) : + $value = wp_specialchars($option->option_value); + echo " + + + + +"; +endforeach; +?> +
    $option->option_description
    +

    +
    +
    + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/page-new.php b/projects/tests/tests/real/wordpress/wp-admin/page-new.php new file mode 100644 index 00000000..4defde16 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/page-new.php @@ -0,0 +1,25 @@ + + + + + + +post_status = 'static'; + + include('edit-page-form.php'); +} +?> + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/plugin-editor.php b/projects/tests/tests/real/wordpress/wp-admin/plugin-editor.php new file mode 100644 index 00000000..37aed6b0 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/plugin-editor.php @@ -0,0 +1,123 @@ +'.__('You have do not have sufficient permissions to edit templates for this blog.').'

    '); + + $newcontent = stripslashes($_POST['newcontent']); + if (is_writeable($real_file)) { + $f = fopen($real_file, 'w+'); + fwrite($f, $newcontent); + fclose($f); + header("Location: plugin-editor.php?file=$file&a=te"); + } else { + header("Location: plugin-editor.php?file=$file"); + } + + exit(); + +break; + +default: + + require_once('admin-header.php'); + if ( !current_user_can('edit_plugins') ) + die('

    '.__('You have do not have sufficient permissions to edit plugins for this blog.').'

    '); + + update_recently_edited("wp-content/plugins/$file"); + + if (!is_file($real_file)) + $error = 1; + + if (!$error) { + $f = fopen($real_file, 'r'); + $content = fread($f, filesize($real_file)); + $content = htmlspecialchars($content); + } + + ?> + +

    + +
    + ' . sprintf(__('Editing %s'), $file) . ''; + } else { + echo '

    ' . sprintf(__('Browsing %s'), $file) . '

    '; + } + ?> +
    +

    + + +
      + +
    • ">
    • + +
    + +
    + +
    +
    + + +
    + +

    +"; +?> +

    + +

    + +
    +

    ' . __('Oops, no such file exists! Double check the name and try again, merci.') . '

    '; + } + ?> +
     
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/plugins.php b/projects/tests/tests/real/wordpress/wp-admin/plugins.php new file mode 100644 index 00000000..d06a22c1 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/plugins.php @@ -0,0 +1,125 @@ + + + +

    activated.') ?>

    +
    + + +

    deactivated.') ?>

    +
    + + +
    +

    +

    +'; + _e("Couldn't open plugins directory or there are no plugins available."); // TODO: make more helpful + echo '

    '; +} else { +?> + + + + + + + + $plugin_data) { + $style = ('class="alternate"' == $style|| 'class="alternate active"' == $style) ? '' : 'alternate'; + + if (!empty($current_plugins) && in_array($plugin_file, $current_plugins)) { + $action = "".__('Deactivate').""; + $plugin_data['Title'] = "{$plugin_data['Title']}"; + $style .= $style == 'alternate' ? ' active' : 'active'; + } else { + $action = "".__('Activate').""; + } + $plugin_data['Description'] = wp_kses($plugin_data['Description'], array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array()) ); ; + if ($style != '') $style = 'class="' . $style . '"'; + echo " + + + + + + "; + } +?> + +
    {$plugin_data['Title']}{$plugin_data['Version']}{$plugin_data['Description']} ".sprintf(__('By %s'), $plugin_data['Author']).".$action
    + + +

    wp-content/plugins directory and it will be automatically deactivated.'); ?>

    + +

    +

    WordPress plugin directory. To install a plugin you generally just need to upload the plugin file into your wp-content/plugins directory. Once a plugin is uploaded, you may activate it here.'); ?>

    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/post.php b/projects/tests/tests/real/wordpress/wp-admin/post.php new file mode 100644 index 00000000..3ff15a3a --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/post.php @@ -0,0 +1,426 @@ +post_status == 'static') + include('edit-page-form.php'); + elseif ($post->post_status == 'attachment') + include('edit-attachment-form.php'); + else + include('edit-form-advanced.php'); + + ?> +
    +

    + +
    + post_status == 'attachment' ) { + if ( ! wp_delete_attachment($post_id) ) + die( __('Error in deleting...') ); + } else { + if ( !wp_delete_post($post_id) ) + die( __('Error in deleting...') ); + } + + $sendback = $_SERVER['HTTP_REFERER']; + if (strstr($sendback, 'post.php')) $sendback = get_settings('siteurl') .'/wp-admin/post.php'; + elseif (strstr($sendback, 'attachments.php')) $sendback = get_settings('siteurl') .'/wp-admin/attachments.php'; + $sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback); + header ('Location: ' . $sendback); + break; + +case 'editcomment': + $title = __('Edit Comment'); + $parent_file = 'edit.php'; + require_once ('admin-header.php'); + + get_currentuserinfo(); + + $comment = (int) $_GET['comment']; + + if ( ! $comment = get_comment($comment) ) + die(sprintf(__('Oops, no comment with this ID. Go back!'), 'javascript:history.go(-1)')); + + if ( !current_user_can('edit_post', $comment->comment_post_ID) ) + die( __('You are not allowed to edit comments on this post.') ); + + $comment = get_comment_to_edit($comment); + + include('edit-form-comment.php'); + + break; + +case 'confirmdeletecomment': + + require_once('./admin-header.php'); + + $comment = (int) $_GET['comment']; + $p = (int) $_GET['p']; + + if ( ! $comment = get_comment($comment) ) + die(sprintf(__('Oops, no comment with this ID. Go back!'), 'edit.php')); + + if ( !current_user_can('edit_post', $comment->comment_post_ID) ) + die( __('You are not allowed to delete comments on this post.') ); + + echo "
    \n"; + echo "

    " . __('Caution: You are about to delete the following comment:') . "

    \n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "
    " . __('Author:') . "$comment->comment_author
    " . __('E-mail:') . "$comment->comment_author_email
    ". __('URL:') . "$comment->comment_author_url
    ". __('Comment:') . "$comment->comment_content
    \n"; + echo "

    " . __('Are you sure you want to do that?') . "

    \n"; + + echo "
    \n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo ""; + echo "  "; + echo "\n"; + echo "
    \n"; + echo "
    \n"; + + break; + +case 'deletecomment': + + check_admin_referer(); + + $comment = (int) $_GET['comment']; + $p = (int) $_GET['p']; + if (isset($_GET['noredir'])) { + $noredir = true; + } else { + $noredir = false; + } + + $postdata = get_post($p) or die(sprintf(__('Oops, no post with this ID. Go back!'), 'edit.php')); + + if ( ! $comment = get_comment($comment) ) + die(sprintf(__('Oops, no comment with this ID. Go back!'), 'post.php')); + + if ( !current_user_can('edit_post', $comment->comment_post_ID) ) + die( __('You are not allowed to edit comments on this post.') ); + + wp_set_comment_status($comment->comment_ID, "delete"); + do_action('delete_comment', $comment->comment_ID); + + if (($_SERVER['HTTP_REFERER'] != "") && (false == $noredir)) { + header('Location: ' . $_SERVER['HTTP_REFERER']); + } else { + header('Location: '. get_settings('siteurl') .'/wp-admin/edit.php?p='.$p.'&c=1#comments'); + } + + break; + +case 'unapprovecomment': + + check_admin_referer(); + + $comment = (int) $_GET['comment']; + $p = (int) $_GET['p']; + if (isset($_GET['noredir'])) { + $noredir = true; + } else { + $noredir = false; + } + + if ( ! $comment = get_comment($comment) ) + die(sprintf(__('Oops, no comment with this ID. Go back!'), 'edit.php')); + + if ( !current_user_can('edit_post', $comment->comment_post_ID) ) + die( __('You are not allowed to edit comments on this post, so you cannot disapprove this comment.') ); + + wp_set_comment_status($comment->comment_ID, "hold"); + + if (($_SERVER['HTTP_REFERER'] != "") && (false == $noredir)) { + header('Location: ' . $_SERVER['HTTP_REFERER']); + } else { + header('Location: '. get_settings('siteurl') .'/wp-admin/edit.php?p='.$p.'&c=1#comments'); + } + + break; + +case 'mailapprovecomment': + + $comment = (int) $_GET['comment']; + + if ( ! $comment = get_comment($comment) ) + die(sprintf(__('Oops, no comment with this ID. Go back!'), 'edit.php')); + + if ( !current_user_can('edit_post', $comment->comment_post_ID) ) + die( __('You are not allowed to edit comments on this post, so you cannot approve this comment.') ); + + if ('1' != $comment->comment_approved) { + wp_set_comment_status($comment->comment_ID, 'approve'); + if (true == get_option('comments_notify')) + wp_notify_postauthor($comment->comment_ID); + } + + header('Location: ' . get_option('siteurl') . '/wp-admin/moderation.php?approved=1'); + + break; + +case 'approvecomment': + + $comment = (int) $_GET['comment']; + $p = (int) $_GET['p']; + if (isset($_GET['noredir'])) { + $noredir = true; + } else { + $noredir = false; + } + + if ( ! $comment = get_comment($comment) ) + die(sprintf(__('Oops, no comment with this ID. Go back!'), 'edit.php')); + + if ( !current_user_can('edit_post', $comment->comment_post_ID) ) + die( __('You are not allowed to edit comments on this post, so you cannot approve this comment.') ); + + wp_set_comment_status($comment->comment_ID, "approve"); + if (get_settings("comments_notify") == true) { + wp_notify_postauthor($comment->comment_ID); + } + + + if (($_SERVER['HTTP_REFERER'] != "") && (false == $noredir)) { + header('Location: ' . $_SERVER['HTTP_REFERER']); + } else { + header('Location: '. get_settings('siteurl') .'/wp-admin/edit.php?p='.$p.'&c=1#comments'); + } + + break; + +case 'editedcomment': + + edit_comment(); + + $referredby = $_POST['referredby']; + if (!empty($referredby)) { + header('Location: ' . $referredby); + } else { + header ("Location: edit.php?p=$comment_post_ID&c=1#comments"); + } + + break; + +default: + $title = __('Create New Post'); + require_once ('./admin-header.php'); +?> + +

    View site »'), get_bloginfo('home') . '/'); ?>

    + + +
    +

    + 15 ) $num_drafts = 15; + for ( $i = 0; $i < $num_drafts; $i++ ) { + $draft = $drafts[$i]; + if ( 0 != $i ) + echo ', '; + $draft->post_title = stripslashes($draft->post_title); + if ( empty($draft->post_title) ) + $draft->post_title = sprintf(__('Post # %s'), $draft->ID); + echo "$draft->post_title"; + } + ?> + + , » + + .

    +
    + +
    +'.__('WordPress bookmarklet').' +

    '.__('Right click on the following link and choose "Add to favorites" to create a posting shortcut.').'

    '; ?> +

    + + + + + + +
    +
    +
    + + + + + + +

    +
    + +
    +

    +You can also e-mail the admin to ask for a promotion.
    +When you’re promoted, just reload this page and you’ll be able to blog. :)'), get_settings('admin_email')); ?> +

    +
    + */ +include('admin-footer.php'); +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/profile-update.php b/projects/tests/tests/real/wordpress/wp-admin/profile-update.php new file mode 100644 index 00000000..b3cb166f --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/profile-update.php @@ -0,0 +1,33 @@ + $error) { + echo $error . '
    '; + } + exit; +} + +if ( !isset( $_POST['rich_editing'] ) ) + $_POST['rich_editing'] = 'false'; +update_user_option( $current_user->id, 'rich_editing', $_POST['rich_editing'], true ); + +do_action('personal_options_update'); + +if ( 'profile' == $_POST['from'] ) + $to = 'profile.php?updated=true'; +else + $to = 'profile.php?updated=true'; + +wp_redirect( $to ); +exit; + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/profile.php b/projects/tests/tests/real/wordpress/wp-admin/profile.php new file mode 100644 index 00000000..4d71e7c9 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/profile.php @@ -0,0 +1,144 @@ + + + +
    +

    +
    + + +
    +

    +
    +

    + + +

    + +
    + +

    + +

    + +

    + +

    + +

    +
    + +
    + + +

    + +

    + +

    + +

    + +

    +

    +
    +
    +
    + +

    +

    +
    + + +
    + +

    +

    +

    +
    + + + + +
    + +

    + +

    + + + + + caps) > count($profileuser->roles)): + ?> + + + + + +
    caps as $cap => $value) { + if(!$wp_roles->is_role($cap)) { + if($output != '') $output .= ', '; + $output .= $value ? $cap : "Denied: {$cap}"; + } + } + echo $output; + ?>
    +

    + +

    +
    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/setup-config.php b/projects/tests/tests/real/wordpress/wp-admin/setup-config.php new file mode 100644 index 00000000..38efd2b7 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/setup-config.php @@ -0,0 +1,156 @@ +installing now."); + +if (!file_exists('../wp-config-sample.php')) + die('Sorry, I need a wp-config-sample.php file to work from. Please re-upload this file from your WordPress installation.'); +$configFile = file('../wp-config-sample.php'); + +if (!is_writable('../')) die("Sorry, I can't write to the directory. You'll have to either change the permissions on your WordPress directory or create your wp-config.php manually."); + +$step = 0; +if(isset($_GET['step'])) $step = $_GET['step']; +header( 'Content-Type: text/html; charset=utf-8' ); +?> + + + +WordPress › Setup Configuration File + + + + +

    WordPress

    + +

    Welcome to WordPress. Before getting started, we need some information on the database. You will need to know the following items before proceeding.

    +
      +
    1. Database name
    2. +
    3. Database username
    4. +
    5. Database password
    6. +
    7. Database host
    8. +
    9. Table prefix (if you want to run more than one WordPress in a single database)
    10. +
    +

    If for any reason this automatic file creation doesn't work, don't worry. All this does is fill in the database information to a configuration file. You may also simply open wp-config-sample.php in a text editor, fill in your information, and save it as wp-config.php.

    +

    In all likelihood, these items were supplied to you by your ISP. If you do not have this information, then you will need to contact them before you can continue. If you’re all ready, let’s go!

    + +

    +
    +

    Below you should enter your database connection details. If you're not sure about these, contact your host.

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Database NameThe name of the database you want to run WP in.
    User NameYour MySQL username
    Password...and MySQL password.
    Database Host99% chance you won't need to change this value.
    Table PrefixIf you want to run multiple WordPress installations in a single database, change this.
    + +
    + $line) { + switch (substr($line,0,16)) { + case "define('DB_NAME'": + fwrite($handle, str_replace("wordpress", $dbname, $line)); + break; + case "define('DB_USER'": + fwrite($handle, str_replace("'username'", "'$uname'", $line)); + break; + case "define('DB_PASSW": + fwrite($handle, str_replace("'password'", "'$passwrd'", $line)); + break; + case "define('DB_HOST'": + fwrite($handle, str_replace("localhost", $dbhost, $line)); + break; + case '$table_prefix =': + fwrite($handle, str_replace('wp_', $prefix, $line)); + break; + default: + fwrite($handle, $line); + } + } + fclose($handle); + chmod('../wp-config.php', 0666); +?> +

    All right sparky! You've made it through this part of the installation. WordPress can now communicate with your database. If you are ready, time now to run the install!

    + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/sidebar.php b/projects/tests/tests/real/wordpress/wp-admin/sidebar.php new file mode 100644 index 00000000..17fd3cb4 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/sidebar.php @@ -0,0 +1,80 @@ + + + +WordPress › Posted + + + + +

    Posted !

    +

    Click here to post again.

    + + + + +WordPress › Sidebar + + + + + +

    WordPress

    +
    +
    + + +

    Title: + +

    +

    Categories: + + + +

    +

    +Post: + +

    +

    + + + + +

    +
    +
    + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-admin/templates.php b/projects/tests/tests/real/wordpress/wp-admin/templates.php new file mode 100644 index 00000000..cc0d03ee --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/templates.php @@ -0,0 +1,167 @@ +'.__('You have do not have sufficient permissions to edit templates for this blog.').'

    '); + + $newcontent = stripslashes($_POST['newcontent']); + if (is_writeable($real_file)) { + $f = @ fopen($real_file, 'w+'); + if ( $f ) { + fwrite($f, $newcontent); + fclose($f); + header("Location: templates.php?file=$file&a=te"); + } else { + header("Location: templates.php?file=$file&a=err"); + } + } else { + header("Location: templates.php?file=$file&a=err"); + } + + exit(); + +break; + +default: + + require_once('./admin-header.php'); + + if ( ! current_user_can('edit_files') ) + die('

    '.__('You have do not have sufficient permissions to edit templates for this blog.').'

    '); + + if ( strstr( $file, 'wp-config.php' ) ) + die('

    '.__('The config file cannot be edited or viewed through the web interface. Sorry!').'

    '); + + update_recently_edited($file); + + if (!is_file($real_file)) + $error = true; + + if (!$error) { + $f = @ fopen($real_file, 'r'); + if ( $f ) { + $content = fread($f, filesize($real_file)); + $content = htmlspecialchars($content); + } else { + $error = true; + } + } + + ?> + + +

    + +

    + + +
    +' . sprintf(__('Editing %s'), wp_specialchars($file) ) . ''; +} else { + echo '

    ' . sprintf(__('Browsing %s'), wp_specialchars($file) ) . '

    '; +} +?> +
    + +

    +'; +foreach ($recents as $recent) : + echo "
  • " . get_file_description(basename($recent)) . "
  • "; +endforeach; +echo ''; +endif; +?> +

    + +
      + +
    • + +
    +
    + +
    +
    + + +
    + +

    +"; +?> +

    + +

    + +
    +

    ' . __('Oops, no such file exists! Double check the name and try again, merci.') . '

    '; + } + ?> +
     
    + +
    +

    + +

    writable by the server, e.g. CHMOD 666.') ?>

    +
    + + +
    + +

    +
    + diff --git a/projects/tests/tests/real/wordpress/wp-admin/theme-editor.php b/projects/tests/tests/real/wordpress/wp-admin/theme-editor.php new file mode 100644 index 00000000..8ed51079 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/theme-editor.php @@ -0,0 +1,158 @@ +'.__('You have do not have sufficient permissions to edit templates for this blog.').'

    '); + + $newcontent = stripslashes($_POST['newcontent']); + $theme = urlencode($theme); + if (is_writeable($real_file)) { + $f = fopen($real_file, 'w+'); + fwrite($f, $newcontent); + fclose($f); + header("Location: theme-editor.php?file=$file&theme=$theme&a=te"); + } else { + header("Location: theme-editor.php?file=$file&theme=$theme"); + } + + exit(); + +break; + +default: + + require_once('admin-header.php'); + if ( !current_user_can('edit_themes') ) + die('

    '.__('You have do not have sufficient permissions to edit themes for this blog.').'

    '); + + update_recently_edited($file); + + if (!is_file($real_file)) + $error = 1; + + if (!$error && filesize($real_file) > 0) { + $f = fopen($real_file, 'r'); + $content = fread($f, filesize($real_file)); + $content = htmlspecialchars($content); + } + + ?> + +

    + +
    +
    + + + +
    +
    + +
    + ' . sprintf(__('Editing %s'), $file_show) . ''; + } else { + echo '

    ' . sprintf(__('Browsing %s'), $file_show) . '

    '; + } + ?> +
    +

    '%s' theme files"), $theme) ?>

    + + + + +
    + +
    +
    + + + +
    + +

    +"; +?> +

    + +

    + +
    +

    ' . __('Oops, no such file exists! Double check the name and try again, merci.') . '

    '; + } + ?> +
     
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/themes.php b/projects/tests/tests/real/wordpress/wp-admin/themes.php new file mode 100644 index 00000000..8a2327f2 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/themes.php @@ -0,0 +1,131 @@ + + + +

    + +

    View site »'), get_bloginfo('home') . '/'); ?>

    + + + + +
    +

    +
    +screenshot ) : ?> +<?php _e('Current theme preview'); ?> + +

    title, $ct->version, $ct->author) ; ?>

    +

    description; ?>

    +parent_theme) { ?> +

    %2$s. The stylesheet files are located in %3$s. %4$s uses templates from %5$s. Changes made to the templates will affect both themes.'), $ct->title, $ct->template_dir, $ct->stylesheet_dir, $ct->title, $ct->parent_theme); ?>

    + +

    %2$s.'), $ct->title, $ct->template_dir, $ct->stylesheet_dir); ?>

    + +
    + +

    + + + +
    +

    + + + + + + + +

    +
    + + + + + + +

    +

    + + + + + + + + + + "; + } +?> +
    $title$description
    + + +

    +

    WordPress theme directory. To install a theme you generally just need to upload the theme folder into your wp-content/themes directory. Once a theme is uploaded, you should see it on this page.'); ?>

    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/update-links.php b/projects/tests/tests/real/wordpress/wp-admin/update-links.php new file mode 100644 index 00000000..46a7f5a8 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/update-links.php @@ -0,0 +1,44 @@ +get_col("SELECT link_url FROM $wpdb->links"); + +if ( !$link_uris ) + die('No links'); + +$link_uris = urlencode( join( $link_uris, "\n" ) ); + +$query_string = "uris=$link_uris"; + +$http_request = "POST /updated-batch/ HTTP/1.0\r\n"; +$http_request .= "Host: api.pingomatic.com\r\n"; +$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_settings('blog_charset')."\r\n"; +$http_request .= 'Content-Length: ' . strlen($query_string) . "\r\n"; +$http_request .= 'User-Agent: WordPress/' . $wp_version . "\r\n"; +$http_request .= "\r\n"; +$http_request .= $query_string; + +$response = ''; +if( false !== ( $fs = fsockopen('api.pingomatic.com', 80, $errno, $errstr, 5) ) ) { + fwrite($fs, $http_request); + while ( !feof($fs) ) + $response .= fgets($fs, 1160); // One TCP-IP packet + fclose($fs); + + $response = explode("\r\n\r\n", $response, 2); + $body = trim( $response[1] ); + $body = str_replace(array("\r\n", "\r"), "\n", $body); + + $returns = explode("\n", $body); + + foreach ($returns as $return) : + $time = $wpdb->escape( substr($return, 0, 19) ); + $uri = $wpdb->escape( preg_replace('/(.*?) | (.*?)/', '$2', $return) ); + $wpdb->query("UPDATE $wpdb->links SET link_updated = '$time' WHERE link_url = '$uri'"); + endforeach; +} +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/upgrade-functions.php b/projects/tests/tests/real/wordpress/wp-admin/upgrade-functions.php new file mode 100644 index 00000000..2f67831e --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/upgrade-functions.php @@ -0,0 +1,847 @@ +get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''"); + if ($posts) { + foreach($posts as $post) { + if ('' == $post->post_name) { + $newtitle = sanitize_title($post->post_title); + $wpdb->query("UPDATE $wpdb->posts SET post_name = '$newtitle' WHERE ID = '$post->ID'"); + } + } + } + + $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories"); + foreach ($categories as $category) { + if ('' == $category->category_nicename) { + $newtitle = sanitize_title($category->cat_name); + $wpdb->query("UPDATE $wpdb->categories SET category_nicename = '$newtitle' WHERE cat_ID = '$category->cat_ID'"); + } + } + + + $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/') + WHERE option_name LIKE 'links_rating_image%' + AND option_value LIKE 'wp-links/links-images/%'"); + + $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat"); + if ($done_ids) : + foreach ($done_ids as $done_id) : + $done_posts[] = $done_id->post_id; + endforeach; + $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')'; + else: + $catwhere = ''; + endif; + + $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere"); + if ($allposts) : + foreach ($allposts as $post) { + // Check to see if it's already been imported + $cat = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post->ID AND category_id = $post->post_category"); + if (!$cat && 0 != $post->post_category) { // If there's no result + $wpdb->query(" + INSERT INTO $wpdb->post2cat + (post_id, category_id) + VALUES + ('$post->ID', '$post->post_category') + "); + } + } + endif; +} + +function upgrade_101() { + global $wpdb; + + // Clean up indices, add a few + add_clean_index($wpdb->posts, 'post_name'); + add_clean_index($wpdb->posts, 'post_status'); + add_clean_index($wpdb->categories, 'category_nicename'); + add_clean_index($wpdb->comments, 'comment_approved'); + add_clean_index($wpdb->comments, 'comment_post_ID'); + add_clean_index($wpdb->links , 'link_category'); + add_clean_index($wpdb->links , 'link_visible'); +} + + +function upgrade_110() { + global $wpdb; + + // Set user_nicename. + $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users"); + foreach ($users as $user) { + if ('' == $user->user_nicename) { + $newname = sanitize_title($user->user_nickname); + $wpdb->query("UPDATE $wpdb->users SET user_nicename = '$newname' WHERE ID = '$user->ID'"); + } + } + + $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users"); + foreach ($users as $row) { + if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) { + $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\''); + } + } + + + // Get the GMT offset, we'll use that later on + $all_options = get_alloptions_110(); + + $time_difference = $all_options->time_difference; + + $server_time = time()+date('Z'); + $weblogger_time = $server_time + $time_difference*3600; + $gmt_time = time(); + + $diff_gmt_server = ($gmt_time - $server_time) / 3600; + $diff_weblogger_server = ($weblogger_time - $server_time) / 3600; + $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server; + $gmt_offset = -$diff_gmt_weblogger; + + // Add a gmt_offset option, with value $gmt_offset + add_option('gmt_offset', $gmt_offset); + + // Check if we already set the GMT fields (if we did, then + // MAX(post_date_gmt) can't be '0000-00-00 00:00:00' + // I just slapped myself silly for not thinking about it earlier + $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true; + + if (!$got_gmt_fields) { + + // Add or substract time to all dates, to get GMT dates + $add_hours = intval($diff_gmt_weblogger); + $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours)); + $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); + $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date"); + $wpdb->query("UPDATE $wpdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'"); + $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); + $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); + } + +} + +function upgrade_130() { + global $wpdb, $table_prefix; + + // Remove extraneous backslashes. + $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts"); + if ($posts) { + foreach($posts as $post) { + $post_content = addslashes(deslash($post->post_content)); + $post_title = addslashes(deslash($post->post_title)); + $post_excerpt = addslashes(deslash($post->post_excerpt)); + if ( empty($post->guid) ) + $guid = get_permalink($post->ID); + else + $guid = $post->guid; + + $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'"); + } + } + + // Remove extraneous backslashes. + $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments"); + if ($comments) { + foreach($comments as $comment) { + $comment_content = addslashes(deslash($comment->comment_content)); + $comment_author = addslashes(deslash($comment->comment_author)); + $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'"); + } + } + + // Remove extraneous backslashes. + $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links"); + if ($links) { + foreach($links as $link) { + $link_name = addslashes(deslash($link->link_name)); + $link_description = addslashes(deslash($link->link_description)); + $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'"); + } + } + + // The "paged" option for what_to_show is no more. + if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') { + $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'"); + } + + $active_plugins = __get_option('active_plugins'); + + // If plugins are not stored in an array, they're stored in the old + // newline separated format. Convert to new format. + if ( !is_array( $active_plugins ) ) { + $active_plugins = explode("\n", trim($active_plugins)); + update_option('active_plugins', $active_plugins); + } + + // Obsolete tables + $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optionvalues'); + $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optiontypes'); + $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optiongroups'); + $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optiongroup_options'); + + // Update comments table to use comment_type + $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '', '') WHERE comment_content LIKE '%'"); + $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '', '') WHERE comment_content LIKE '%'"); + + // Some versions have multiple duplicate option_name rows with the same values + $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name"); + foreach ( $options as $option ) { + if ( 1 != $option->dupes ) { // Could this be done in the query? + $limit = $option->dupes - 1; + $dupe_ids = $wpdb->get_col("SELECT option_id FROM $wpdb->options WHERE option_name = '$option->option_name' LIMIT $limit"); + $dupe_ids = join($dupe_ids, ','); + $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)"); + } + } + + make_site_theme(); +} + +function upgrade_160() { + global $wpdb, $table_prefix, $wp_current_db_version; + + populate_roles_160(); + + $users = $wpdb->get_results("SELECT * FROM $wpdb->users"); + foreach ( $users as $user ) : + if ( !empty( $user->user_firstname ) ) + update_usermeta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) ); + if ( !empty( $user->user_lastname ) ) + update_usermeta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) ); + if ( !empty( $user->user_nickname ) ) + update_usermeta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) ); + if ( !empty( $user->user_level ) ) + update_usermeta( $user->ID, $table_prefix . 'user_level', $user->user_level ); + if ( !empty( $user->user_icq ) ) + update_usermeta( $user->ID, 'icq', $wpdb->escape($user->user_icq) ); + if ( !empty( $user->user_aim ) ) + update_usermeta( $user->ID, 'aim', $wpdb->escape($user->user_aim) ); + if ( !empty( $user->user_msn ) ) + update_usermeta( $user->ID, 'msn', $wpdb->escape($user->user_msn) ); + if ( !empty( $user->user_yim ) ) + update_usermeta( $user->ID, 'yim', $wpdb->escape($user->user_icq) ); + if ( !empty( $user->user_description ) ) + update_usermeta( $user->ID, 'description', $wpdb->escape($user->user_description) ); + + if ( isset( $user->user_idmode ) ): + $idmode = $user->user_idmode; + if ($idmode == 'nickname') $id = $user->user_nickname; + if ($idmode == 'login') $id = $user->user_login; + if ($idmode == 'firstname') $id = $user->user_firstname; + if ($idmode == 'lastname') $id = $user->user_lastname; + if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname; + if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname; + if (!$idmode) $id = $user->user_nickname; + $id = $wpdb->escape( $id ); + $wpdb->query("UPDATE $wpdb->users SET display_name = '$id' WHERE ID = '$user->ID'"); + endif; + + // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set. + $caps = get_usermeta( $user->ID, $table_prefix . 'capabilities'); + if ( empty($caps) || defined('RESET_CAPS') ) { + $level = get_usermeta($user->ID, $table_prefix . 'user_level'); + $role = translate_level_to_role($level); + update_usermeta( $user->ID, $table_prefix . 'capabilities', array($role => true) ); + } + + endforeach; + $old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' ); + $wpdb->hide_errors(); + foreach ( $old_user_fields as $old ) + $wpdb->query("ALTER TABLE $wpdb->users DROP $old"); + $wpdb->show_errors(); + + if ( 0 == $wpdb->get_var("SELECT SUM(category_count) FROM $wpdb->categories") ) { // Create counts + $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories"); + foreach ( $categories as $cat_id ) { + $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'"); + $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); + } + } + + // populate comment_count field of posts table + $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" ); + if( is_array( $comments ) ) { + foreach ($comments as $comment) { + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $comment->c WHERE ID = '$comment->comment_post_ID'" ); + } + } + + // Some alpha versions used a post status of object instead of attachment and put + // the mime type in post_type instead of post_mime_type. + if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) { + $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'"); + foreach ($objects as $object) { + $wpdb->query("UPDATE $wpdb->posts SET post_status = 'attachment', + post_mime_type = '$object->post_type', + post_type = '' + WHERE ID = $object->ID"); + + $meta = get_post_meta($object->ID, 'imagedata', true); + if ( ! empty($meta['file']) ) + add_post_meta($object->ID, '_wp_attached_file', $meta['file']); + } + } +} + +// The functions we use to actually do stuff + +// General +function maybe_create_table($table_name, $create_ddl) { + global $wpdb; + foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { + if ($table == $table_name) { + return true; + } + } + //didn't find it try to create it. + $q = $wpdb->query($create_ddl); + // we cannot directly tell that whether this succeeded! + foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { + if ($table == $table_name) { + return true; + } + } + return false; +} + +function drop_index($table, $index) { + global $wpdb; + $wpdb->hide_errors(); + $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`"); + // Now we need to take out all the extra ones we may have created + for ($i = 0; $i < 25; $i++) { + $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`"); + } + $wpdb->show_errors(); + return true; +} + +function add_clean_index($table, $index) { + global $wpdb; + drop_index($table, $index); + $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )"); + return true; +} + +/** + ** maybe_add_column() + ** Add column to db table if it doesn't exist. + ** Returns: true if already exists or on successful completion + ** false on error + */ +function maybe_add_column($table_name, $column_name, $create_ddl) { + global $wpdb, $debug; + foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { + if ($debug) echo("checking $column == $column_name
    "); + if ($column == $column_name) { + return true; + } + } + //didn't find it try to create it. + $q = $wpdb->query($create_ddl); + // we cannot directly tell that whether this succeeded! + foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { + if ($column == $column_name) { + return true; + } + } + return false; +} + + +// get_alloptions as it was for 1.2. +function get_alloptions_110() { + global $wpdb; + if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) { + foreach ($options as $option) { + // "When trying to design a foolproof system, + // never underestimate the ingenuity of the fools :)" -- Dougal + if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); + if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); + if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); + $all_options->{$option->option_name} = stripslashes($option->option_value); + } + } + return $all_options; +} + +// Version of get_option that is private to install/upgrade. +function __get_option($setting) { + global $wpdb; + + $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'"); + + if ( 'home' == $setting && '' == $option ) + return __get_option('siteurl'); + + if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting ) + $option = preg_replace('|/+$|', '', $option); + + @ $kellogs = unserialize($option); + if ($kellogs !== FALSE) + return $kellogs; + else + return $option; +} + +function deslash($content) { + // Note: \\\ inside a regex denotes a single backslash. + + // Replace one or more backslashes followed by a single quote with + // a single quote. + $content = preg_replace("/\\\+'/", "'", $content); + + // Replace one or more backslashes followed by a double quote with + // a double quote. + $content = preg_replace('/\\\+"/', '"', $content); + + // Replace one or more backslashes with one backslash. + $content = preg_replace("/\\\+/", "\\", $content); + + return $content; +} + +function dbDelta($queries, $execute = true) { + global $wpdb; + + // Seperate individual queries into an array + if( !is_array($queries) ) { + $queries = explode( ';', $queries ); + if('' == $queries[count($queries) - 1]) array_pop($queries); + } + + $cqueries = array(); // Creation Queries + $iqueries = array(); // Insertion Queries + $for_update = array(); + + // Create a tablename index for an array ($cqueries) of queries + foreach($queries as $qry) { + if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) { + $cqueries[strtolower($matches[1])] = $qry; + $for_update[$matches[1]] = 'Created table '.$matches[1]; + } + else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { + array_unshift($cqueries, $qry); + } + else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { + $iqueries[] = $qry; + } + else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { + $iqueries[] = $qry; + } + else { + // Unrecognized query type + } + } + + // Check to see which tables and fields exist + if($tables = $wpdb->get_col('SHOW TABLES;')) { + // For every table in the database + foreach($tables as $table) { + // If a table query exists for the database table... + if( array_key_exists(strtolower($table), $cqueries) ) { + // Clear the field and index arrays + unset($cfields); + unset($indices); + // Get all of the field names in the query from between the parens + preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2); + $qryline = trim($match2[1]); + + // Separate field lines into an array + $flds = explode("\n", $qryline); + + //echo "
    \n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."

    "; + + // For every field line specified in the query + foreach($flds as $fld) { + // Extract the field name + preg_match("|^([^ ]*)|", trim($fld), $fvals); + $fieldname = $fvals[1]; + + // Verify the found field name + $validfield = true; + switch(strtolower($fieldname)) + { + case '': + case 'primary': + case 'index': + case 'fulltext': + case 'unique': + case 'key': + $validfield = false; + $indices[] = trim(trim($fld), ", \n"); + break; + } + $fld = trim($fld); + + // If it's a valid field, add it to the field array + if($validfield) { + $cfields[strtolower($fieldname)] = trim($fld, ", \n"); + } + } + + // Fetch the table column structure from the database + $tablefields = $wpdb->get_results("DESCRIBE {$table};"); + + // For every field in the table + foreach($tablefields as $tablefield) { + // If the table field exists in the field array... + if(array_key_exists(strtolower($tablefield->Field), $cfields)) { + // Get the field type from the query + preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); + $fieldtype = $matches[1]; + + // Is actual field type different from the field type in query? + if($tablefield->Type != $fieldtype) { + // Add a query to change the column type + $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; + $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; + } + + // Get the default value from the array + //echo "{$cfields[strtolower($tablefield->Field)]}
    "; + if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { + $default_value = $matches[1]; + if($tablefield->Default != $default_value) + { + // Add a query to change the column's default value + $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; + $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; + } + } + + // Remove the field from the array (so it's not added) + unset($cfields[strtolower($tablefield->Field)]); + } + else { + // This field exists in the table, but not in the creation queries? + } + } + + // For every remaining field specified for the table + foreach($cfields as $fieldname => $fielddef) { + // Push a query line into $cqueries that adds the field to that table + $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; + $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; + } + + // Index stuff goes here + // Fetch the table index structure from the database + $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); + + if($tableindices) { + // Clear the index array + unset($index_ary); + + // For every index in the table + foreach($tableindices as $tableindex) { + // Add the index to the index data array + $keyname = $tableindex->Key_name; + $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); + $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; + } + + // For each actual index in the index array + foreach($index_ary as $index_name => $index_data) { + // Build a create string to compare to the query + $index_string = ''; + if($index_name == 'PRIMARY') { + $index_string .= 'PRIMARY '; + } + else if($index_data['unique']) { + $index_string .= 'UNIQUE '; + } + $index_string .= 'KEY '; + if($index_name != 'PRIMARY') { + $index_string .= $index_name; + } + $index_columns = ''; + // For each column in the index + foreach($index_data['columns'] as $column_data) { + if($index_columns != '') $index_columns .= ','; + // Add the field to the column list string + $index_columns .= $column_data['fieldname']; + if($column_data['subpart'] != '') { + $index_columns .= '('.$column_data['subpart'].')'; + } + } + // Add the column list to the index create string + $index_string .= ' ('.$index_columns.')'; + + if(!(($aindex = array_search($index_string, $indices)) === false)) { + unset($indices[$aindex]); + //echo "
    {$table}:
    Found index:".$index_string."
    \n"; + } + //else echo "
    {$table}:
    Did not find index:".$index_string."
    ".print_r($indices, true)."
    \n"; + } + } + + // For every remaining index specified for the table + foreach($indices as $index) { + // Push a query line into $cqueries that adds the index to that table + $cqueries[] = "ALTER TABLE {$table} ADD $index"; + $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; + } + + // Remove the original table creation query from processing + unset($cqueries[strtolower($table)]); + unset($for_update[strtolower($table)]); + } else { + // This table exists in the database, but not in the creation queries? + } + } + } + + $allqueries = array_merge($cqueries, $iqueries); + if($execute) { + foreach($allqueries as $query) { + //echo "
    ".print_r($query, true)."
    \n"; + $wpdb->query($query); + } + } + + return $for_update; +} + +function make_db_current() { + global $wp_queries; + + $alterations = dbDelta($wp_queries); + echo "
      \n"; + foreach($alterations as $alteration) echo "
    1. $alteration
    2. \n"; + echo "
    \n"; +} + +function make_db_current_silent() { + global $wp_queries; + + $alterations = dbDelta($wp_queries); +} + +function make_site_theme_from_oldschool($theme_name, $template) { + $home_path = get_home_path(); + $site_dir = ABSPATH . "wp-content/themes/$template"; + + if (! file_exists("$home_path/index.php")) + return false; + + // Copy files from the old locations to the site theme. + // TODO: This does not copy arbitarary include dependencies. Only the + // standard WP files are copied. + $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php'); + + foreach ($files as $oldfile => $newfile) { + if ($oldfile == 'index.php') + $oldpath = $home_path; + else + $oldpath = ABSPATH; + + if ($oldfile == 'index.php') { // Check to make sure it's not a new index + $index = implode('', file("$oldpath/$oldfile")); + if ( strstr( $index, 'WP_USE_THEMES' ) ) { + if (! @copy(ABSPATH . 'wp-content/themes/default/index.php', "$site_dir/$newfile")) + return false; + continue; // Don't copy anything + } + } + + if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile")) + return false; + + chmod("$site_dir/$newfile", 0777); + + // Update the blog header include in each file. + $lines = explode("\n", implode('', file("$site_dir/$newfile"))); + if ($lines) { + $f = fopen("$site_dir/$newfile", 'w'); + + foreach ($lines as $line) { + if (preg_match('/require.*wp-blog-header/', $line)) + $line = '//' . $line; + + // Update stylesheet references. + $line = str_replace("/wp-layout.css", "", $line); + + // Update comments template inclusion. + $line = str_replace("", "", $line); + + fwrite($f, "{$line}\n"); + } + fclose($f); + } + } + + // Add a theme header. + $header = "/*\nTheme Name: $theme_name\nTheme URI: " . __get_option('siteurl') . "\nDescription: A theme automatically created by the upgrade.\nVersion: 1.0\nAuthor: Moi\n*/\n"; + + $stylelines = file_get_contents("$site_dir/style.css"); + if ($stylelines) { + $f = fopen("$site_dir/style.css", 'w'); + + fwrite($f, $header); + fwrite($f, $stylelines); + fclose($f); + } + + return true; +} + +function make_site_theme_from_default($theme_name, $template) { + $site_dir = ABSPATH . "wp-content/themes/$template"; + $default_dir = ABSPATH . 'wp-content/themes/default'; + + // Copy files from the default theme to the site theme. + //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css'); + + $theme_dir = @ dir("$default_dir"); + if ($theme_dir) { + while(($theme_file = $theme_dir->read()) !== false) { + if (is_dir("$default_dir/$theme_file")) + continue; + if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file")) + return; + chmod("$site_dir/$theme_file", 0777); + } + } + + // Rewrite the theme header. + $stylelines = explode("\n", implode('', file("$site_dir/style.css"))); + if ($stylelines) { + $f = fopen("$site_dir/style.css", 'w'); + + foreach ($stylelines as $line) { + if (strstr($line, "Theme Name:")) $line = "Theme Name: $theme_name"; + elseif (strstr($line, "Theme URI:")) $line = "Theme URI: " . __get_option('siteurl'); + elseif (strstr($line, "Description:")) $line = "Description: Your theme"; + elseif (strstr($line, "Version:")) $line = "Version: 1"; + elseif (strstr($line, "Author:")) $line = "Author: You"; + fwrite($f, "{$line}\n"); + } + fclose($f); + } + + // Copy the images. + umask(0); + if (! mkdir("$site_dir/images", 0777)) { + return false; + } + + $images_dir = @ dir("$default_dir/images"); + if ($images_dir) { + while(($image = $images_dir->read()) !== false) { + if (is_dir("$default_dir/images/$image")) + continue; + if (! @copy("$default_dir/images/$image", "$site_dir/images/$image")) + return; + chmod("$site_dir/images/$image", 0777); + } + } +} + +// Create a site theme from the default theme. +function make_site_theme() { + // Name the theme after the blog. + $theme_name = __get_option('blogname'); + $template = sanitize_title($theme_name); + $site_dir = ABSPATH . "wp-content/themes/$template"; + + // If the theme already exists, nothing to do. + if ( is_dir($site_dir)) { + return false; + } + + // We must be able to write to the themes dir. + if (! is_writable(ABSPATH . "wp-content/themes")) { + return false; + } + + umask(0); + if (! mkdir($site_dir, 0777)) { + return false; + } + + if (file_exists(ABSPATH . 'wp-layout.css')) { + if (! make_site_theme_from_oldschool($theme_name, $template)) { + // TODO: rm -rf the site theme directory. + return false; + } + } else { + if (! make_site_theme_from_default($theme_name, $template)) + // TODO: rm -rf the site theme directory. + return false; + } + + // Make the new site theme active. + $current_template = __get_option('template'); + if ($current_template == 'default') { + update_option('template', $template); + update_option('stylesheet', $template); + } + return $template; +} + +function translate_level_to_role($level) { + switch ($level) { + case 10: + case 9: + case 8: + return 'administrator'; + case 7: + case 6: + case 5: + return 'editor'; + case 4: + case 3: + case 2: + return 'author'; + case 1: + return 'contributor'; + case 0: + return 'subscriber'; + } +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/upgrade-schema.php b/projects/tests/tests/real/wordpress/wp-admin/upgrade-schema.php new file mode 100644 index 00000000..570030e1 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/upgrade-schema.php @@ -0,0 +1,330 @@ +categories ( + cat_ID bigint(20) NOT NULL auto_increment, + cat_name varchar(55) NOT NULL default '', + category_nicename varchar(200) NOT NULL default '', + category_description longtext NOT NULL, + category_parent bigint(20) NOT NULL default '0', + category_count bigint(20) NOT NULL default '0', + PRIMARY KEY (cat_ID), + KEY category_nicename (category_nicename) +); +CREATE TABLE $wpdb->comments ( + comment_ID bigint(20) unsigned NOT NULL auto_increment, + comment_post_ID int(11) NOT NULL default '0', + comment_author tinytext NOT NULL, + comment_author_email varchar(100) NOT NULL default '', + comment_author_url varchar(200) NOT NULL default '', + comment_author_IP varchar(100) NOT NULL default '', + comment_date datetime NOT NULL default '0000-00-00 00:00:00', + comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', + comment_content text NOT NULL, + comment_karma int(11) NOT NULL default '0', + comment_approved enum('0','1','spam') NOT NULL default '1', + comment_agent varchar(255) NOT NULL default '', + comment_type varchar(20) NOT NULL default '', + comment_parent bigint(20) NOT NULL default '0', + user_id bigint(20) NOT NULL default '0', + PRIMARY KEY (comment_ID), + KEY comment_approved (comment_approved), + KEY comment_post_ID (comment_post_ID) +); +CREATE TABLE $wpdb->linkcategories ( + cat_id bigint(20) NOT NULL auto_increment, + cat_name tinytext NOT NULL, + auto_toggle enum('Y','N') NOT NULL default 'N', + show_images enum('Y','N') NOT NULL default 'Y', + show_description enum('Y','N') NOT NULL default 'N', + show_rating enum('Y','N') NOT NULL default 'Y', + show_updated enum('Y','N') NOT NULL default 'Y', + sort_order varchar(64) NOT NULL default 'rand', + sort_desc enum('Y','N') NOT NULL default 'N', + text_before_link varchar(128) NOT NULL default '
  • ', + text_after_link varchar(128) NOT NULL default '
    ', + text_after_all varchar(128) NOT NULL default '
  • ', + list_limit int(11) NOT NULL default '-1', + PRIMARY KEY (cat_id) +); +CREATE TABLE $wpdb->links ( + link_id bigint(20) NOT NULL auto_increment, + link_url varchar(255) NOT NULL default '', + link_name varchar(255) NOT NULL default '', + link_image varchar(255) NOT NULL default '', + link_target varchar(25) NOT NULL default '', + link_category bigint(20) NOT NULL default '0', + link_description varchar(255) NOT NULL default '', + link_visible enum('Y','N') NOT NULL default 'Y', + link_owner int(11) NOT NULL default '1', + link_rating int(11) NOT NULL default '0', + link_updated datetime NOT NULL default '0000-00-00 00:00:00', + link_rel varchar(255) NOT NULL default '', + link_notes mediumtext NOT NULL, + link_rss varchar(255) NOT NULL default '', + PRIMARY KEY (link_id), + KEY link_category (link_category), + KEY link_visible (link_visible) +); +CREATE TABLE $wpdb->options ( + option_id bigint(20) NOT NULL auto_increment, + blog_id int(11) NOT NULL default '0', + option_name varchar(64) NOT NULL default '', + option_can_override enum('Y','N') NOT NULL default 'Y', + option_type int(11) NOT NULL default '1', + option_value longtext NOT NULL, + option_width int(11) NOT NULL default '20', + option_height int(11) NOT NULL default '8', + option_description tinytext NOT NULL, + option_admin_level int(11) NOT NULL default '1', + autoload enum('yes','no') NOT NULL default 'yes', + PRIMARY KEY (option_id,blog_id,option_name), + KEY option_name (option_name) +); +CREATE TABLE $wpdb->post2cat ( + rel_id bigint(20) NOT NULL auto_increment, + post_id bigint(20) NOT NULL default '0', + category_id bigint(20) NOT NULL default '0', + PRIMARY KEY (rel_id), + KEY post_id (post_id,category_id) +); +CREATE TABLE $wpdb->postmeta ( + meta_id bigint(20) NOT NULL auto_increment, + post_id bigint(20) NOT NULL default '0', + meta_key varchar(255) default NULL, + meta_value longtext, + PRIMARY KEY (meta_id), + KEY post_id (post_id), + KEY meta_key (meta_key) +); +CREATE TABLE $wpdb->posts ( + ID bigint(20) unsigned NOT NULL auto_increment, + post_author bigint(20) NOT NULL default '0', + post_date datetime NOT NULL default '0000-00-00 00:00:00', + post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', + post_content longtext NOT NULL, + post_title text NOT NULL, + post_category int(4) NOT NULL default '0', + post_excerpt text NOT NULL, + post_status enum('publish','draft','private','static','object','attachment') NOT NULL default 'publish', + comment_status enum('open','closed','registered_only') NOT NULL default 'open', + ping_status enum('open','closed') NOT NULL default 'open', + post_password varchar(20) NOT NULL default '', + post_name varchar(200) NOT NULL default '', + to_ping text NOT NULL, + pinged text NOT NULL, + post_modified datetime NOT NULL default '0000-00-00 00:00:00', + post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00', + post_content_filtered text NOT NULL, + post_parent bigint(20) NOT NULL default '0', + guid varchar(255) NOT NULL default '', + menu_order int(11) NOT NULL default '0', + post_type varchar(100) NOT NULL default '', + post_mime_type varchar(100) NOT NULL default '', + comment_count bigint(20) NOT NULL default '0', + PRIMARY KEY (ID), + KEY post_name (post_name) +); +CREATE TABLE $wpdb->users ( + ID bigint(20) unsigned NOT NULL auto_increment, + user_login varchar(60) NOT NULL default '', + user_pass varchar(64) NOT NULL default '', + user_nicename varchar(50) NOT NULL default '', + user_email varchar(100) NOT NULL default '', + user_url varchar(100) NOT NULL default '', + user_registered datetime NOT NULL default '0000-00-00 00:00:00', + user_activation_key varchar(60) NOT NULL default '', + user_status int(11) NOT NULL default '0', + display_name varchar(250) NOT NULL default '', + PRIMARY KEY (ID), + KEY user_login_key (user_login) +); +CREATE TABLE $wpdb->usermeta ( + umeta_id bigint(20) NOT NULL auto_increment, + user_id bigint(20) NOT NULL default '0', + meta_key varchar(255) default NULL, + meta_value longtext, + PRIMARY KEY (umeta_id), + KEY user_id (user_id), + KEY meta_key (meta_key) +);"; + +function populate_options() { + global $wpdb, $wp_db_version; + + $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://'; + $guessurl = preg_replace('|/wp-admin/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); + add_option('siteurl', $guessurl, __('WordPress web address')); + add_option('blogname', __('My Weblog'), __('Blog title')); + add_option('blogdescription', __('Just another WordPress weblog'), __('Short tagline')); + add_option('new_users_can_blog', 0); + add_option('users_can_register', 0); + add_option('admin_email', 'you@example.com'); + add_option('start_of_week', 1); + add_option('use_balanceTags', 0); + add_option('use_smilies', 1); + add_option('require_name_email', 1); + add_option('comments_notify', 1); + add_option('posts_per_rss', 10); + add_option('rss_excerpt_length', 50); + add_option('rss_use_excerpt', 0); + add_option('mailserver_url', 'mail.example.com'); + add_option('mailserver_login', 'login@example.com'); + add_option('mailserver_pass', 'password'); + add_option('mailserver_port', 110); + add_option('default_category', 1); + add_option('default_comment_status', 'open'); + add_option('default_ping_status', 'open'); + add_option('default_pingback_flag', 1); + add_option('default_post_edit_rows', 10); + add_option('posts_per_page', 10); + add_option('what_to_show', 'posts'); + add_option('date_format', __('F j, Y')); + add_option('time_format', __('g:i a')); + add_option('links_updated_date_format', __('F j, Y g:i a')); + add_option('links_recently_updated_prepend', ''); + add_option('links_recently_updated_append', ''); + add_option('links_recently_updated_time', 120); + add_option('comment_moderation', 0); + add_option('moderation_notify', 1); + add_option('permalink_structure'); + add_option('gzipcompression', 0); + add_option('hack_file', 0); + add_option('blog_charset', 'UTF-8'); + add_option('moderation_keys'); + add_option('active_plugins'); + add_option('home', $guessurl); + // in case it is set, but blank, update "home" + if ( !__get_option('home') ) update_option('home', $guessurl); + add_option('category_base'); + add_option('ping_sites', 'http://rpc.pingomatic.com/'); + add_option('advanced_edit', 0); + add_option('comment_max_links', 2); + add_option('gmt_offset', date('Z') / 3600); + // 1.5 + add_option('default_email_category', 1, __('Posts by email go to this category')); + add_option('recently_edited'); + add_option('use_linksupdate', 0); + add_option('template', 'default'); + add_option('stylesheet', 'default'); + add_option('comment_whitelist', 1); + add_option('page_uris'); + add_option('blacklist_keys'); + add_option('comment_registration', 0); + add_option('open_proxy_check', 1); + add_option('rss_language', 'en'); + add_option('html_type', 'text/html'); + // 1.5.1 + add_option('use_trackback', 0); + // 1.6 + add_option('default_role', 'subscriber'); + add_option('rich_editing', 'true'); + add_option('db_version', $wp_db_version); + + // Delete unused options + $unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog'); + foreach ($unusedoptions as $option) : + delete_option($option); + endforeach; + + // Set up a few options not to load by default + $fatoptions = array( 'moderation_keys', 'recently_edited', 'blacklist_keys' ); + foreach ($fatoptions as $fatoption) : + $wpdb->query("UPDATE $wpdb->options SET `autoload` = 'no' WHERE option_name = '$fatoption'"); + endforeach; +} + +function populate_roles() { + populate_roles_160(); +} + +function populate_roles_160() { + global $wp_roles; + + // Add roles + $wp_roles->add_role('administrator', __('Administrator')); + $wp_roles->add_role('editor', __('Editor')); + $wp_roles->add_role('author', __('Author')); + $wp_roles->add_role('contributor', __('Contributor')); + $wp_roles->add_role('subscriber', __('Subscriber')); + + // Add caps for Administrator role + $role = $wp_roles->get_role('administrator'); + $role->add_cap('switch_themes'); + $role->add_cap('edit_themes'); + $role->add_cap('activate_plugins'); + $role->add_cap('edit_plugins'); + $role->add_cap('edit_users'); + $role->add_cap('edit_files'); + $role->add_cap('manage_options'); + $role->add_cap('moderate_comments'); + $role->add_cap('manage_categories'); + $role->add_cap('manage_links'); + $role->add_cap('upload_files'); + $role->add_cap('import'); + $role->add_cap('unfiltered_html'); + $role->add_cap('edit_posts'); + $role->add_cap('edit_others_posts'); + $role->add_cap('edit_published_posts'); + $role->add_cap('publish_posts'); + $role->add_cap('edit_pages'); + $role->add_cap('read'); + $role->add_cap('level_10'); + $role->add_cap('level_9'); + $role->add_cap('level_8'); + $role->add_cap('level_7'); + $role->add_cap('level_6'); + $role->add_cap('level_5'); + $role->add_cap('level_4'); + $role->add_cap('level_3'); + $role->add_cap('level_2'); + $role->add_cap('level_1'); + $role->add_cap('level_0'); + + // Add caps for Editor role + $role = $wp_roles->get_role('editor'); + $role->add_cap('moderate_comments'); + $role->add_cap('manage_categories'); + $role->add_cap('manage_links'); + $role->add_cap('upload_files'); + $role->add_cap('unfiltered_html'); + $role->add_cap('edit_posts'); + $role->add_cap('edit_others_posts'); + $role->add_cap('edit_published_posts'); + $role->add_cap('publish_posts'); + $role->add_cap('edit_pages'); + $role->add_cap('read'); + $role->add_cap('level_7'); + $role->add_cap('level_6'); + $role->add_cap('level_5'); + $role->add_cap('level_4'); + $role->add_cap('level_3'); + $role->add_cap('level_2'); + $role->add_cap('level_1'); + $role->add_cap('level_0'); + + // Add caps for Author role + $role = $wp_roles->get_role('author'); + $role->add_cap('upload_files'); + $role->add_cap('edit_posts'); + $role->add_cap('edit_published_posts'); + $role->add_cap('publish_posts'); + $role->add_cap('read'); + $role->add_cap('level_2'); + $role->add_cap('level_1'); + $role->add_cap('level_0'); + + // Add caps for Contributor role + $role = $wp_roles->get_role('contributor'); + $role->add_cap('edit_posts'); + $role->add_cap('read'); + $role->add_cap('level_1'); + $role->add_cap('level_0'); + + // Add caps for Subscriber role + $role = $wp_roles->get_role('subscriber'); + $role->add_cap('read'); + $role->add_cap('level_0'); +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-admin/upgrade.php b/projects/tests/tests/real/wordpress/wp-admin/upgrade.php new file mode 100644 index 00000000..263dd901 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/upgrade.php @@ -0,0 +1,98 @@ + + + + + WordPress › Upgrade + + + + +

    WordPress

    + +

    +

    + +

    +

    Have fun!"), __get_option('home') . '/'); ?>

    + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-admin/user-edit.php b/projects/tests/tests/real/wordpress/wp-admin/user-edit.php new file mode 100644 index 00000000..cbae0496 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/user-edit.php @@ -0,0 +1,204 @@ + + + +
    +

    +
    + + +
    +
      + $error"; + ?> +
    +
    + + +
    +

    + +
    +

    + + +

    + +
    + +

    + +

    + +

    + +

    + +

    + +

    +
    + +
    + + +

    + +

    + +

    + +

    + +

    +

    +
    +
    +
    + +

    +

    +
    + + +
    + +

    +

    +

    +
    + + + + +
    + + caps) > count($profileuser->roles)): + ?> + + + + + +
    caps as $cap => $value) { + if(!$wp_roles->is_role($cap)) { + if($output != '') $output .= ', '; + $output .= $value ? $cap : "Denied: {$cap}"; + } + } + echo $output; + ?>
    +

    + + + +

    +
    +
    + diff --git a/projects/tests/tests/real/wordpress/wp-admin/users.php b/projects/tests/tests/real/wordpress/wp-admin/users.php new file mode 100644 index 00000000..bca3120c --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/users.php @@ -0,0 +1,328 @@ +id && !$wp_roles->role_objects[$_POST['new_role']]->has_cap('edit_users')) { + $update = 'err_admin_role'; + continue; + } + + $user = new WP_User($id); + $user->set_role($_POST['new_role']); + } + + header('Location: users.php?update=' . $update); + +break; + +case 'dodelete': + + check_admin_referer(); + + if ( empty($_POST['users']) ) { + header('Location: users.php'); + } + + if ( !current_user_can('edit_users') ) + die(__('You can’t delete users.')); + + $userids = $_POST['users']; + + $update = 'del'; + foreach ($userids as $id) { + if($id == $current_user->id) { + $update = 'err_admin_del'; + continue; + } + switch($_POST['delete_option']) { + case 'delete': + wp_delete_user($id); + break; + case 'reassign': + wp_delete_user($id, $_POST['reassign_user']); + break; + } + } + + header('Location: users.php?update=' . $update); + +break; + +case 'delete': + + check_admin_referer(); + + if (empty($_POST['users'])) { + header('Location: users.php'); + } + + if ( !current_user_can('edit_users') ) + $error['edit_users'] = __('You can’t delete users.'); + + $userids = $_POST['users']; + + include ('admin-header.php'); +?> +
    +
    +

    +

    +
      +id) { + echo "
    • " . sprintf(__('ID #%1s: %2s The current user will not be deleted.'), $id, $user->user_login) . "
    • \n"; + } else { + echo "
    • " . sprintf(__('ID #%1s: %2s'), $id, $user->user_login) . "
    • \n"; + $go_delete = true; + } + } + $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY user_login"); + $user_dropdown = ''; + ?> +
    + +

    +
      +
    • +
    • + '.__('Attribute all posts and links to:')." $user_dropdown"; ?>
    • +
    + +

    + +

    + +
    +
    +get_col("SELECT ID FROM $wpdb->users;"); + + foreach($userids as $userid) { + $tmp_user = new WP_User($userid); + $roles = $tmp_user->roles; + $role = $roles[0]; + $roleclasses[$role][$tmp_user->user_login] = $tmp_user; + } + + ?> + + +

    + +

    + +

    + +

    +

    + +

    +

    + +
    +
      + $error"; + ?> +
    +
    + + +
    +
    +

    + + $roleclass) { + ksort($roleclass); + ?> + + + + + + + + + + + + + + user_email; + $url = $user_object->user_url; + $short_url = str_replace('http://', '', $url); + $short_url = str_replace('www.', '', $short_url); + if ('/' == substr($short_url, -1)) + $short_url = substr($short_url, 0, -1); + if (strlen($short_url) > 35) + $short_url = substr($short_url, 0, 32).'...'; + $style = ('class="alternate"' == $style) ? '' : 'class="alternate"'; + $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$user_object->ID' and post_status = 'publish'"); + if (0 < $numposts) $numposts = "$numposts"; + echo " + + + + + + "; + echo ""; + echo ''; + echo ''; + } + + ?> + + + +
    +

    role_names[$role]; ?>

    +
     
    $email$short_url$numposts'; + if (current_user_can('edit_users')) + echo "".__('Edit').""; + echo '
    + + +

    +'; +foreach($wp_roles->role_names as $role => $name) { + $role_select .= ""; +} +$role_select .= ''; +?> +
      +
    • +
    • '.__('Set the Role of checked users to:')." $role_select"; ?>
    • +
    +

    +
    +
    + +
    +

    +'.sprintf(__('Users can register themselves or you can manually create users here.'), get_settings('siteurl').'/wp-register.php').'

    '; ?> +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    +
    +
    +

    + +

    +
    +
    + diff --git a/projects/tests/tests/real/wordpress/wp-admin/wp-admin.css b/projects/tests/tests/real/wordpress/wp-admin/wp-admin.css new file mode 100644 index 00000000..bf4e35de --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-admin/wp-admin.css @@ -0,0 +1,904 @@ +* html #poststuff { + height: 100%; /* kill peekaboo bug in IE */ +} + +/* This is the Holly Hack \*/ +* html .wrap { height: 1% } +/* For Win IE's eyes only */ + +body { + border: none; +} +a { + border-bottom: 1px solid #69c; + color: #00019b; + text-decoration: none; +} + +a.delete:hover { + background: #c00; + color: #fff; +} + +#planetnews ul { + list-style: none; + margin: 0; + padding: 0; +} + +#planetnews li { + width: 17%; + margin: 1%; + float: left; +} + +#planetnews li a { + display: block; + padding: .5em; + background: #ddd; + height: 6em; + overflow: hidden; +} + +a.edit, a.delete, a.edit:hover, a.delete:hover { + border-bottom: none; + display: block; + padding: 5px 0; + text-align: center; +} + +a.edit:hover { + background: #ccc; + color: #036; +} + +a:visited { + color: #006; +} + +a:hover { +/* border-bottom: 1px solid #3a75ae;*/ + color: #069; +} + +body { + background: #f9fcfe; + color: #000; + margin: 0; + padding: 0; +} + +body, td { + font: 13px "Lucida Grande", "Lucida Sans Unicode", Tahoma, Verdana; +} + +fieldset { + border: none; + padding: 3px; +} + +fieldset label.selectit { + display: block; + font-size: 11px; + padding: 0 2px; +} + +fieldset label.selectit:hover { + background: #e9e9e9; +} + +fieldset legend { + padding: .1em .3em; +} + +fieldset span.cat-nest { + display: block; + margin-left: 10px; +} + +fieldset.options { + padding: 1em; +} + +fieldset.options legend { + font-size: 16px; +} + +form, label input { + margin: 0; + padding: 0; +} + +h2 { + border-bottom: .5em solid #f0f8ff; + color: #333; + font: normal 30px/5px serif; + margin: 5px 10px; +} + +h2 small.quickjump { + display: block; + text-align: right; +} + +h2 small.quickjump a { + text-decoration: none; + border-bottom: 0; + font-size: 15px; + background: #f0f8ff; + padding: 5px 10px; +} + +img, #footer a { + border: 0; +} + +input:focus, textarea:focus, label:focus { + background: #fff; + border: 1px solid #686868; +} + +label { + cursor: pointer; +} + +li, dd { + margin-bottom: 6px; +} + +p, li, dl, dd, dt { + line-height: 130%; +} + +textarea, input, select { + background: #f4f4f4; + border: 1px solid #b2b2b2; + color: #000; + font: 13px Verdana, Arial, Helvetica, sans-serif; + margin: 1px; + padding: 3px; +} + +#uploading { + border-style: none; + padding: 0px; + margin-bottom: 16px; + height: 15em; + width: 100%; +/* overflow-y: hidden;*/ +} + +form#upload th { + text-align: right; +} + +form#upload #post_content, form#upload #post_title { + width: 250px; +} + +form#upload #post_content { + height: 50px; +} + +.attpreview { + width: 1px; /* hug */ + text-align: center; +} + +.alignleft { + float: left +} + +.alignright { + float: right; +} + +.alternate { + background: #f1f1f1; +} + +.anchors { + margin: 10px 20px 10px 20px; +} + +.available-theme { + width: 30%; + margin: 0 1em; + float: left; + text-align: center; + height: 28em; + overflow: hidden; +} + +.available-theme a.screenshot { + width: 250px; + height: 200px; + display: block; + margin: auto; + background: #f1f1f1; + border: 1px solid #ccc; + margin-bottom: 10px; + overflow: hidden; +} + +.available-theme a.screenshot:hover { +/* border: 1px solid #666;*/ +} + +.available-theme img { + width: 100%; +} + +.checkbox { + background: #fff; + border: none; + margin: 0; + padding: 0; +} + +.code { + font-family: "Courier New", Courier, monospace; +} + +.commentlist li { + border-bottom: 1px solid #369; + padding: .3em 1em; +} + +.clear { + clear: both; + height: 2px; +} + +.hidden { + display: none; +} + +.navigation { + display: block; + text-align: center; + margin-top: 10px; + margin-bottom: 30px; +} + +.post-categories { + display: inline; + margin: 0; + padding: 0; +} + +.post-categories li, #ed_toolbar { + display: inline; +} + +.quicktags, .search { + background: #ccc; + color: #000; + font: 12px Georgia, "Times New Roman", Times, serif; +} + +.submit input, .submit input:focus, .button { + background: url( images/fade-butt.png ); + border: 3px double #999; + border-left-color: #ccc; + border-top-color: #ccc; + color: #333; + padding: 0.25em; +} + +.submit input:active, .button:active { + background: #f4f4f4; + border: 3px double #ccc; + border-left-color: #999; + border-top-color: #999; +} + +.submit, .editform th, #postcustomsubmit { + text-align: right; +} + +.unapproved { + color: #888; +} + +.unapproved a:link { + color: #b9bcff; +} + +.unapproved a:visited { + color: #696dff; +} + +.unapproved a:hover { + color: #009ef0; +} + +.updated { + background: #CFEBF7 url(images/notice.gif) no-repeat 1em ; + border: 1px solid #2580B2; + margin: 1em 5% 10px; + padding: 0 1em 0 3em; +} + +.error { + background: #FFEFF7; + border: 1px solid #c69; + margin: 1em 5% 10px; + padding: 0 1em 0 1em; +} + +.wrap { + background: #fff; + border: 1px solid #ccc; + clear: both; + margin: 15px 5%; + padding: .5em 1em; +} + +.wrap h2 { + margin: .8em 0 .5em; + clear: both; +} + +table .vers, table .name { + text-align: center; +} + +#adminmenu { + background: #6da6d1; + border-top: 3px solid #448abd; + margin: 0; + padding: .2em .2em .2em 2em; +} + +#adminmenu .current, #submenu .current { + font-weight: bold; +} + +#adminmenu a { + color: #000; + font-size: 14px; + font-weight: normal; + margin: 0; + padding: 3px 5px; + text-decoration: none; +} + +#adminmenu a:hover, .current { + background: #ddeaf4; + color: #333; +} + +#adminmenu li, #submenu li { + display: inline; + line-height: 200%; + list-style: none; + text-align: center; +} + +#submenu { + background: #0d324f; + border-bottom: none; + margin: 0; + padding: 3px 2em 0 3em; +} + +#submenu .current { + background: #f9fcfe; + border-top: 1px solid #045290; + border-right: 2px solid #045290; + color: #000; +} + +#submenu a { + border: none; + color: #fff; + font-size: 12px; + padding: .3em .4em .33em; +} + +#submenu a:hover { + background: #ddeaf4; + color: #393939; +} + +#submenu li { + line-height: 170%; +} + + +#categorydiv input, #poststatusdiv input, #commentstatusdiv input, #pingstatusdiv input { + border: none; +} + +#titlediv, #guiddiv { + margin: 0 8px 0 0; + padding: 0px; +} + +#postdiv { + margin: 0 8px 0 0; + padding: 0px; +} + +#postdivrich { + margin: 0px; + padding: 0px; +} + +#content { + margin: 0 0 0 0; + width: 100%; +} + +#titlediv input, #guiddiv input { + margin: 0px; + width: 100%; +} + +#quicktags { + margin-left: -1px; +} + +#currenttheme img { + float: left; + border: 1px solid #666; + margin-right: 1em; + margin-bottom: 1.5em; + width: 300px; +} + +#deletepost:hover { + background: #ce0000; + color: #fff; +} + +#quicktags #ed_strong { + font-weight: bold; +} + +#quicktags #ed_link { + color: blue; + text-decoration: underline; +} + +#quicktags #ed_del { + text-decoration: line-through; +} + +#quicktags #ed_em { + font-style: italic; +} + +#quicktags #ed_code { + font-family: "Courier New", Courier, mono; +} + +#title { + font-size: 1.5em; +} + +#postexcerpt div, #attachmentlinks div { + margin-right: 8px; +} + +#attachmentlinks textarea { + width: 100%; + height: 2.5em; + margin-bottom: 6px; +} + +* html #postexcerpt .dbx-toggle-open, * html #postexcerpt .dbx-toggle-open { + padding-right: 8px; +} + +#excerpt, .attachmentlinks { + margin: 0px; + height: 4em; + width: 100%; +} + +#footer { + clear: both; + text-align: center; +} + +#login { + background: #fff; + border: 1px solid #a2a2a2; + margin: 5em auto; + padding: 1.5em; + width: 25em; +} + +#login #login_error { + background: #c00; + border: 1px solid #a40000; + color: #fff; + font-size: 16px; + font-weight: bold; + padding: .5em; + text-align: center; +} + +#login h1 { + background: url(images/wordpress-logo.png) no-repeat top left; + margin-top: 0; +} + +#login h1 a { + display: block; + text-indent: -1000px; + height: 66px; + border-bottom: none; +} + +#login input { + padding: 3px; +} + +#login ul { + list-style: none; + margin: 0; + padding: 0; +} + +#login ul li { + display: inline; + margin-left: 1.4em; + text-align: center; +} + +#login #log, #pwd { + font-size: 1.7em; + width: 80%; +} + +#login #submit { + font-size: 1.7em; +} + +#postcustom .updatemeta, #postcustom .deletemeta { + margin: auto; +} + +#postcustom table { + border: 1px solid #ccc; + margin: 0px; + width: 100%; +} + +#postcustom table input, #postcustom table textarea { + width: 95%; +} + +#poststuff { + margin-right: 16em; +} + +#save { + width: 15em; +} + +#template div { + margin-right: 190px; +} + +* html #template div { + margin-right: 0px; +} + +#template, #template div, #editcat, #addcat { + zoom: 1; +} + +#template textarea { + font: small 'Courier New', Courier, monospace; + width: 97%; +} + +#templateside { + float: right; + width: 170px; +} + +#templateside h3, #postcustom p { + margin: 0; +} + +#templateside ol, #templateside ul { + list-style: none; + margin: .5em; + padding: 0; +} + +#user_info { + position: absolute; + right: 1em; + top: 0; + color: #fff; + font-size: .9em; +} + +#user_info a { + color: #fff; +} + +#wphead { + background: #14568a; + padding: .8em 19em .8em 2em; + color: #c3def1; +} + +#wphead a { + color: #fff; +} + +#wphead h1 { + font-size: 2.5em; + font-weight: normal; + letter-spacing: -.05em; + margin: 0; + font-family: Georgia, "Times New Roman", Times, serif +} + +#wphead h1 span { + font-size: .4em; + letter-spacing: 0; +} + +#zeitgeist { + background: #eee; + border: 1px solid #69c; + float: right; + font-size: 90%; + margin-bottom: .5em; + margin-left: 1em; + margin-top: .5em; + padding: 1em; + width: 40%; +} + +#zeitgeist h2, fieldset legend a { + border-bottom: none; +} + +#zeitgeist h2 { + margin-top: .4em; +} + +#zeitgeist h3 { + border-bottom: 1px solid #ccc; + font-size: 16px; + margin: 1em 0 0; +} + +#zeitgeist h3 cite { + font-size: 12px; + font-style: normal; +} + +#zeitgeist li, #zeitgeist p { + margin: .2em 0; +} + +#zeitgeist ul { + margin: 0 0 .3em .6em; + padding: 0 0 0 .6em; +} + +.active td { + background: #BEB; +} +.active .name { + background: #9C9; +} +.alternate.active td { + background: #ADA; +} +.alternate.active .name { + background: #8B8; +} + +/* A handy div class for hiding controls. + Some browsers will disable them when you + set display:none; */ +.zerosize { + height: 0px; + width: 0px; + margin: 0px; + border: 0px; + padding: 0px; + overflow: hidden; + position: absolute; +} + +/* Box stuff */ +.dbx-clone { + position:absolute; + visibility:hidden; +} +.dbx-clone, .dbx-clone .dbx-handle-cursor { + cursor:move !important; +} +.dbx-dummy { + display:block; + width:0; + height:0; + overflow:hidden; +} +.dbx-group, .dbx-box, .dbx-handle { + position:relative; + display:block; +} + +* html #themeselect { + padding: 0px 3px; + height: 22px; +} + +/**************************************************************** + avoid padding, margins or borders on dbx-box, + to reduce visual discrepancies between it and the clone. + overall, dbx-box is best left as visually unstyled as possible +*****************************************************************/ +.dbx-box { + margin:0; + padding:0; + border:none; +} + +/* Can change this */ +#moremeta fieldset, #advancedstuff fieldset { + margin-bottom: 1em; +} +#moremeta fieldset div { + margin: 2px 0 0 0px; + padding: 7px; +} +#moremeta { + line-height: 130%; + margin-right: 15px; + position: absolute; + right: 5%; + width: 14.5em; +} + +#slugdiv input, #passworddiv input, #authordiv select, #thumbdiv input, #parentdiv input { + margin-top: .5em; + width: 90%; +} + +#moremeta h3, #advancedstuff h3 { + padding: 3px; + font-weight: normal; + font-size: 13px; +} + +#advancedstuff div { + margin-top: .5em; +} + +#categorydiv div div { + height: 12em; + overflow: auto; +} + +#ajaxcat input { + border: 1px solid #ccc; +} + +#your-profile fieldset { + border: 1px solid #ccc; + float: left; + width: 40%; + padding: .5em 2em; + margin: 1em; +} + +#your-profile fieldset input { + width: 100%; + font-size: 20px; + padding: 2px; +} + +#your-profile fieldset textarea { + width: 100%; + padding: 2px; +} + +#your-profile legend { + font-family: Georgia, "Times New Roman", Times, serif; + font-size: 22px; +} + +/* default box styles */ + +/* toggle state of inner content area */ +.dbx-box-open .dbx-content { + display: block; +} +.dbx-box-closed .dbx-content { + display: none; +} + +#moremeta .dbx-content { + background: url(images/box-butt.gif) no-repeat bottom right; + padding-bottom: 15px; + padding-right: 2px; +} + +/* handles */ + +.dbx-handle { + background: #2685af; + padding: 6px 1em 2px; + font-size: 12px; + margin: 0; + color: #E3EFF5; +} + +#moremeta .dbx-handle { + padding: 6px 1em 2px; + font-size: 12px; + background: #2685af url(images/box-head.gif) no-repeat right; +} + +#moremeta .dbx-box { + background: url(images/box-bg.gif) repeat-y right; +} + +/* handle cursors */ +.dbx-handle-cursor { + cursor: move; +} + +/* toggle images */ +a.dbx-toggle, a.dbx-toggle:visited { + display:block; + overflow: hidden; + background-image: url( images/toggle.gif ); + position: absolute; + top: 0px; + right: 0px; + background-repeat: no-repeat; + border: 0px; + margin: 0px; + padding: 0px; +} + +#moremeta a.dbx-toggle, #moremeta a.dbx-toggle-open:visited { + height: 25px; + width: 27px; + background-position: 0 0px; +} + +#moremeta a.dbx-toggle-open, #moremeta a.dbx-toggle-open:visited { + height: 25px; + width: 27px; + background-position: 0 -25px; +} + +#advancedstuff a.dbx-toggle, #advancedstuff a.dbx-toggle-open:visited { + height: 22px; + width: 22px; + background-position: 0 -3px; +} + +#advancedstuff a.dbx-toggle-open, #advancedstuff a.dbx-toggle-open:visited { + height: 22px; + width: 22px; + background-position: 0 -28px; +} + +#categorychecklist { + margin-right: 6px; +} + +/* additional clone styles */ +.dbx-clone { + opacity: 0.8; + -moz-opacity: 0.8; + -khtml-opacity: 0.8; + filter: alpha(opacity=80); +} diff --git a/projects/tests/tests/real/wordpress/wp-atom.php b/projects/tests/tests/real/wordpress/wp-atom.php new file mode 100644 index 00000000..feb2845b --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-atom.php @@ -0,0 +1,45 @@ + +'; ?> + + > + <?php bloginfo_rss('name') ?> + + + + Copyright + WordPress + + + + + + + <![CDATA[<?php the_title_rss() ?>]]> + + + + + + ]]> + + ]]> + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-blog-header.php b/projects/tests/tests/real/wordpress/wp-blog-header.php new file mode 100644 index 00000000..c087324f --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-blog-header.php @@ -0,0 +1,21 @@ +wp-config.php file. I need this before we can get started. Need more help? We got it. You can create a wp-config.php file through a web interface, but this doesn't work for all server setups. The safest way is to manually create the file."); +} + +$wp_did_header = true; + +require_once( dirname(__FILE__) . '/wp-config.php'); + +wp(); +gzip_compression(); + +require_once(ABSPATH . WPINC . '/template-loader.php'); + +endif; + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-comments-post.php b/projects/tests/tests/real/wordpress/wp-comments-post.php new file mode 100644 index 00000000..e0d95a40 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-comments-post.php @@ -0,0 +1,63 @@ +get_row("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = '$comment_post_ID'"); + +if ( empty($status->comment_status) ) { + do_action('comment_id_not_found', $comment_post_ID); + exit; +} elseif ( 'closed' == $status->comment_status ) { + do_action('comment_closed', $comment_post_ID); + die( __('Sorry, comments are closed for this item.') ); +} elseif ( 'draft' == $status->post_status ) { + do_action('comment_on_draft', $comment_post_ID); + exit; +} + +$comment_author = trim($_POST['author']); +$comment_author_email = trim($_POST['email']); +$comment_author_url = trim($_POST['url']); +$comment_content = trim($_POST['comment']); + +// If the user is logged in +get_currentuserinfo(); +if ( $user_ID ) : + $comment_author = $wpdb->escape($user_identity); + $comment_author_email = $wpdb->escape($user_email); + $comment_author_url = $wpdb->escape($user_url); +else : + if ( get_option('comment_registration') ) + die( __('Sorry, you must be logged in to post a comment.') ); +endif; + +$comment_type = ''; + +if ( get_settings('require_name_email') && !$user_ID ) { + if ( 6 > strlen($comment_author_email) || '' == $comment_author ) + die( __('Error: please fill the required fields (name, email).') ); + elseif ( !is_email($comment_author_email)) + die( __('Error: please enter a valid email address.') ); +} + +if ( '' == $comment_content ) + die( __('Error: please type a comment.') ); + +$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID'); + +wp_new_comment( $commentdata ); + +if ( !$user_ID ) : + setcookie('comment_author_' . COOKIEHASH, stripslashes($comment_author), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); + setcookie('comment_author_email_' . COOKIEHASH, stripslashes($comment_author_email), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); + setcookie('comment_author_url_' . COOKIEHASH, stripslashes($comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); +endif; + +$location = ( empty( $_POST['redirect_to'] ) ) ? get_permalink( $comment_post_ID ) : $_POST['redirect_to']; + +wp_redirect( $location ); + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-commentsrss2.php b/projects/tests/tests/real/wordpress/wp-commentsrss2.php new file mode 100644 index 00000000..023914d9 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-commentsrss2.php @@ -0,0 +1,89 @@ +'; +?> + + + + + <?php if (is_single() || is_page() ) { printf(__('Comments on: %s'), get_the_title_rss()); } else { printf(__('Comments for %s'), get_bloginfo_rss("name")); } ?> + + + + http://wordpress.org/?v= + +get_results($_GET["p"]); + + if (is_single() || is_page()) { + $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_author_email, + comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, + $wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments + LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE comment_post_ID = '$id' + AND $wpdb->comments.comment_approved = '1' AND $wpdb->posts.post_status IN ('publish', 'static', 'object') + AND post_date_gmt < '" . gmdate("Y-m-d H:i:59") . "' + ORDER BY comment_date_gmt DESC LIMIT " . get_settings('posts_per_rss') ); + } else { // if no post id passed in, we'll just ue the last 10 comments. + $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_author_email, + comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, + $wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments + LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE $wpdb->posts.post_status IN ('publish', 'static', 'object') + AND $wpdb->comments.comment_approved = '1' AND post_date_gmt < '" . gmdate("Y-m-d H:i:s") . "' + ORDER BY comment_date_gmt DESC LIMIT " . get_settings('posts_per_rss') ); + } + // this line is WordPress' motor, do not delete it. + if ($comments) { + foreach ($comments as $comment) { + // Some plugins may need to know the metadata + // associated with this comment's post: + get_post_custom($comment->comment_post_ID); +?> + + <?php if ( ! (is_single() || is_page()) ) { + $title = get_the_title($comment->comment_post_ID); + $title = apply_filters('the_title', $title); + $title = apply_filters('the_title_rss', $title); + printf(__('Comment on %1$s by %2$s'), $title, get_comment_author_rss()); + } else { + printf(__('by: %s'), get_comment_author_rss()); + } ?> + + + + post_password) && $_COOKIE['wp-postpass'] != $comment->post_password) { + ?> + + ]]> + + + ]]> + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-config.php b/projects/tests/tests/real/wordpress/wp-config.php new file mode 100644 index 00000000..f8a6d507 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-config.php @@ -0,0 +1,22 @@ + diff --git a/projects/tests/tests/real/wordpress/wp-content/index.php b/projects/tests/tests/real/wordpress/wp-content/index.php new file mode 100644 index 00000000..3d5acf05 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/index.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-content/plugins/akismet/akismet.php b/projects/tests/tests/real/wordpress/wp-content/plugins/akismet/akismet.php new file mode 100644 index 00000000..708f0b77 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/plugins/akismet/akismet.php @@ -0,0 +1,294 @@ +WordPress.com API key to use this service. You can review the spam it catches under "Manage" and it automatically deletes old spam after 15 days. Hat tip: Michael Hampton and Chris J. Davis for help with the plugin. +Author: Matt Mullenweg +Version: 1.12 +Author URI: http://photomatt.net/ +*/ + +add_action('admin_menu', 'ksd_config_page'); + +function ksd_config_page() { + global $wpdb; + if ( function_exists('add_submenu_page') ) + add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 1, __FILE__, 'akismet_conf'); +} + +function akismet_conf() { + if ( isset($_POST['submit']) ) { + check_admin_referer(); + $key = preg_replace('/[^a-h0-9]/i', '', $_POST['key']); + if ( akismet_verify_key( $key ) ) + update_option('wordpress_api_key', $key); + else + $invalid_key = true; + } + if ( !akismet_verify_key( get_option('wordpress_api_key') ) ) + $invalid_key = true; +?> + +
    +

    +

    Akismet will greatly reduce or even completely eliminate the comment and trackback spam you get on your site. If one does happen to get through, simply mark it as "spam" on the moderation screen and Akismet will learn from the mistakes. If you don\'t have a WordPress.com account yet, you can get one at WordPress.com.'), 'http://akismet.com/', 'http://wordpress.com/api-keys/'); ?>

    + +
    +

    + +

    + +

    (What is this?'); ?>)

    +

    +
    +
    +

    ".__('Akismet is not active.')." ".sprintf(__('You must enter your WordPress.com API key for it to work.'), "plugins.php?page=$path")."

    + + "; + } + add_action('admin_footer', 'akismet_warning'); + return; +} + +$ksd_api_host = get_option('wordpress_api_key') . '.rest.akismet.com'; +$ksd_api_port = 80; +$ksd_user_agent = "WordPress/$wp_version | Akismet/1.12"; + +// Returns array with headers in $response[0] and entity in $response[1] +function ksd_http_post($request, $host, $path, $port = 80) { + global $ksd_user_agent; + + $http_request = "POST $path HTTP/1.0\r\n"; + $http_request .= "Host: $host\r\n"; + $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_settings('blog_charset') . "\r\n"; + $http_request .= "Content-Length: " . strlen($request) . "\r\n"; + $http_request .= "User-Agent: $ksd_user_agent\r\n"; + $http_request .= "\r\n"; + $http_request .= $request; + + $response = ''; + if( false !== ( $fs = @fsockopen($host, $port, $errno, $errstr, 3) ) ) { + fwrite($fs, $http_request); + + while ( !feof($fs) ) + $response .= fgets($fs, 1160); // One TCP-IP packet + fclose($fs); + $response = explode("\r\n\r\n", $response, 2); + } + return $response; +} + +function ksd_auto_check_comment( $comment ) { + global $auto_comment_approved, $ksd_api_host, $ksd_api_port; + $comment['user_ip'] = $_SERVER['REMOTE_ADDR']; + $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT']; + $comment['referrer'] = $_SERVER['HTTP_REFERER']; + $comment['blog'] = get_option('home'); + + $ignore = array( 'HTTP_COOKIE' ); + + foreach ( $_SERVER as $key => $value ) + if ( !in_array( $key, $ignore ) ) + $comment["$key"] = $value; + + $query_string = ''; + foreach ( $comment as $key => $data ) + $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; + + $response = ksd_http_post($query_string, $ksd_api_host, '/1.1/comment-check', $ksd_api_port); + if ( 'true' == $response[1] ) { + $auto_comment_approved = 'spam'; + update_option( 'akismet_spam_count', get_option('akismet_spam_count') + 1 ); + } + akismet_delete_old(); + return $comment; +} + +function akismet_delete_old() { + global $wpdb; + $now_gmt = current_time('mysql', 1); + $wpdb->query("DELETE FROM $wpdb->comments WHERE DATE_SUB('$now_gmt', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'"); + $n = mt_rand(1, 5); + if ( $n % 5 ) + $wpdb->query("OPTIMIZE TABLE $wpdb->comments"); +} + +function ksd_auto_approved( $approved ) { + global $auto_comment_approved; + if ( 'spam' == $auto_comment_approved ) + $approved = $auto_comment_approved; + return $approved; +} + +function ksd_submit_nonspam_comment ( $comment_id ) { + global $wpdb, $ksd_api_host, $ksd_api_port; + + $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'"); + if ( !$comment ) // it was deleted + return; + $comment->blog = get_option('home'); + $query_string = ''; + foreach ( $comment as $key => $data ) + $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; + $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-ham", $ksd_api_port); +} + +function ksd_submit_spam_comment ( $comment_id ) { + global $wpdb, $ksd_api_host, $ksd_api_port; + + $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'"); + if ( !$comment ) // it was deleted + return; + if ( 'spam' != $comment->comment_approved ) + return; + $comment->blog = get_option('home'); + $query_string = ''; + foreach ( $comment as $key => $data ) + $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; + + $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-spam", $ksd_api_port); +} + +add_action('wp_set_comment_status', 'ksd_submit_spam_comment'); +add_action('edit_comment', 'ksd_submit_spam_comment'); +add_action('preprocess_comment', 'ksd_auto_check_comment', 1); +add_filter('pre_comment_approved', 'ksd_auto_approved'); + + +function ksd_spam_count() { + global $wpdb, $comments; + $count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'"); + return $count; +} + +function ksd_manage_page() { + global $wpdb; + $count = sprintf(__('Akismet Spam (%s)'), ksd_spam_count()); + if ( function_exists('add_management_page') ) + add_management_page(__('Akismet Spam'), $count, 1, __FILE__, 'ksd_caught'); +} + +function ksd_caught() { + global $wpdb, $comment; + if (isset($_POST['submit']) && 'recover' == $_POST['action'] && ! empty($_POST['not_spam'])) { + $i = 0; + foreach ($_POST['not_spam'] as $comment): + $comment = (int) $comment; + if ( function_exists('wp_set_comment_status') ) + wp_set_comment_status($comment, 'approve'); + else + $wpdb->query("UPDATE $wpdb->comments SET comment_approved = '1' WHERE comment_ID = '$comment'"); + ksd_submit_nonspam_comment($comment); + ++$i; + endforeach; + echo '

    ' . sprintf(__('%1$s comments recovered.'), $i) . "

    "; + } + if ('delete' == $_POST['action']) { + $delete_time = addslashes( $_POST['display_time'] ); + $nuked = $wpdb->query( "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND '$delete_time' > comment_date_gmt" ); + if (isset($nuked)) { + echo '

    '; + if ($nuked) { + _e('All spam deleted.'); + } + echo "

    "; + } + } +?> +
    +

    + +

    %1$s for you since you installed it.'), $count); ?>

    +'.__('You have no spam currently in the queue. Must be your lucky day. :)').'

    '; + echo '
    '; +} else { + echo '

    '.__('You can delete all of the spam from your database with a single click. This operation cannot be undone, so you may wish to check to ensure that no legitimate comments got through first. Spam is automattically deleted after 15 days, so don’t sweat it.').'

    '; +?> +
    + +    + +
    + +
    +

    +'.__('These are the latest comments identified as spam by Akismet. If you see any mistakes, simple mark the comment as "not spam" and Akismet will learn from the submission. If you wish to recover a comment from spam, simply select the comment, and click Not Spam. After 15 days we clean out the junk for you.').'

    '; ?> +get_results("SELECT *, COUNT(*) AS ccount FROM $wpdb->comments WHERE comment_approved = 'spam' GROUP BY comment_author_IP ORDER BY comment_date DESC LIMIT 150"); + +if ($comments) { +?> +
    + +
      +comment_date); + $post = get_post($comment->comment_post_ID); + $post_title = $post->post_title; + if ($i % 2) $class = 'class="alternate"'; + else $class = ''; + echo "\n\t
    1. "; + ?> +

      comment_author_email) { ?>| comment_author_url && 'http://' != $comment->comment_author_url) { ?> | | |

      + + + +
    +

    + +

    +
    +
    +'.__('Spam').''; + echo '

    '.sprintf(__('Akismet has protected your site from %3$s spam comments.'), 'http://akismet.com/', "edit.php?page=$path", $count).'

    '; +} + +add_action('activity_box_end', 'akismet_stats'); + +?> diff --git a/projects/tests/tests/real/wordpress/wp-content/plugins/hello.php b/projects/tests/tests/real/wordpress/wp-content/plugins/hello.php new file mode 100644 index 00000000..e03dbcc3 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/plugins/hello.php @@ -0,0 +1,73 @@ +Hello, Dolly in the upper right of your admin screen on every page. +Author: Matt Mullenweg +Version: 1.5 +Author URI: http://photomatt.net/ +*/ + +// These are the lyrics to Hello Dolly +$lyrics = "Hello, Dolly +Well, hello, Dolly +It's so nice to have you back where you belong +You're lookin' swell, Dolly +I can tell, Dolly +You're still glowin', you're still crowin' +You're still goin' strong +We feel the room swayin' +While the band's playin' +One of your old favourite songs from way back when +So, take her wrap, fellas +Find her an empty lap, fellas +Dolly'll never go away again +Hello, Dolly +Well, hello, Dolly +It's so nice to have you back where you belong +You're lookin' swell, Dolly +I can tell, Dolly +You're still glowin', you're still crowin' +You're still goin' strong +We feel the room swayin' +While the band's playin' +One of your old favourite songs from way back when +Golly, gee, fellas +Find her a vacant knee, fellas +Dolly'll never go away +Dolly'll never go away +Dolly'll never go away again"; + +// Here we split it into lines +$lyrics = explode("\n", $lyrics); +// And then randomly choose a line +$chosen = wptexturize( $lyrics[ mt_rand(0, count($lyrics) ) ] ); + +// This just echoes the chosen line, we'll position it later +function hello_dolly() { + global $chosen; + echo "

    $chosen

    "; +} + +// Now we set that function up to execute when the admin_footer action is called +add_action('admin_footer', 'hello_dolly'); + +// We need some CSS to position the paragraph +function dolly_css() { + echo " + + "; +} + +add_action('admin_head', 'dolly_css'); + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-content/plugins/wp-db-backup.php b/projects/tests/tests/real/wordpress/wp-content/plugins/wp-db-backup.php new file mode 100644 index 00000000..21b677f2 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/plugins/wp-db-backup.php @@ -0,0 +1,889 @@ +backup_dir = trailingslashit($this->backup_dir); + $this->basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); + + if (isset($_POST['do_backup'])) { + switch($_POST['do_backup']) { + case 'backup': + $this->perform_backup(); + break; + case 'fragments': + add_action('admin_menu', array(&$this, 'fragment_menu')); + break; + } + } elseif (isset($_GET['fragment'] )) { + add_action('init', array(&$this, 'init')); + } elseif (isset($_GET['backup'] )) { + add_action('init', array(&$this, 'init')); + } else { + add_action('admin_menu', array(&$this, 'admin_menu')); + } + } + + function init() { + global $user_level; + get_currentuserinfo(); + + if ($user_level < 9) die(__('Need higher user level.')); + + if (isset($_GET['backup'])) { + $via = isset($_GET['via']) ? $_GET['via'] : 'http'; + + $this->backup_file = $_GET['backup']; + + switch($via) { + case 'smtp': + case 'email': + $this->deliver_backup ($this->backup_file, 'smtp', $_GET['recipient']); + echo ' + + + '; + break; + default: + $this->deliver_backup ($this->backup_file, $via); + } + die(); + } + if (isset($_GET['fragment'] )) { + list($table, $segment, $filename) = explode(':', $_GET['fragment']); + $this->backup_fragment($table, $segment, $filename); + } + + die(); + } + + function build_backup_script() { + global $table_prefix, $wpdb; + + $datum = date("Ymd_B"); + $backup_filename = DB_NAME . "_$table_prefix$datum.sql"; + if ($this->gzip()) $backup_filename .= '.gz'; + + echo "
    "; + //echo "
    " . print_r($_POST, 1) . "
    "; + echo '

    ' . __('Backup') . '

    +
    ' . __('Progress') . ' +

    ' . + __('DO NOT DO THE FOLLOWING AS IT WILL CAUSE YOUR BACKUP TO FAIL:'). + '

    +
      +
    1. '.__('Close this browser').'
    2. +
    3. '.__('Reload this page').'
    4. +
    5. '.__('Click the Stop or Back buttons in your browser').'
    6. +
    +

    ' . __('Progress:') . '

    +
     
    +
    +
    +
    + + +
    + '; + } + + function backup_fragment($table, $segment, $filename) { + global $table_prefix, $wpdb; + + echo "$table:$segment:$filename"; + + if($table == '') { + $msg = __('Creating backup file...'); + } else { + if($segment == -1) { + $msg = sprintf(__('Finished backing up table \\"%s\\".'), $table); + } else { + $msg = sprintf(__('Backing up table \\"%s\\"...'), $table); + } + } + + echo ' + '; + } + else { + echo ' + window.parent.nextStep(); + //--> + '; + } + + die(); + } + + function perform_backup() { + // are we backing up any other tables? + $also_backup = array(); + if (isset($_POST['other_tables'])) { + $also_backup = $_POST['other_tables']; + } + + $core_tables = $_POST['core_tables']; + $this->backup_file = $this->db_backup($core_tables, $also_backup); + if (FALSE !== $backup_file) { + if ('smtp' == $_POST['deliver']) { + $this->deliver_backup ($this->backup_file, $_POST['deliver'], $_POST['backup_recipient']); + } elseif ('http' == $_POST['deliver']) { + $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); + header('Refresh: 3; ' . get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}&backup={$this->backup_file}"); + } + // we do this to say we're done. + $this->backup_complete = true; + } + } + + /////////////////////////////// + function admin_menu() { + add_management_page(__('Backup'), __('Backup'), 9, basename(__FILE__), array(&$this, 'backup_menu')); + } + + function fragment_menu() { + add_management_page(__('Backup'), __('Backup'), 9, basename(__FILE__), array(&$this, 'build_backup_script')); + } + + ///////////////////////////////////////////////////////// + function sql_addslashes($a_string = '', $is_like = FALSE) + { + /* + Better addslashes for SQL queries. + Taken from phpMyAdmin. + */ + if ($is_like) { + $a_string = str_replace('\\', '\\\\\\\\', $a_string); + } else { + $a_string = str_replace('\\', '\\\\', $a_string); + } + $a_string = str_replace('\'', '\\\'', $a_string); + + return $a_string; + } // function sql_addslashes($a_string = '', $is_like = FALSE) + + /////////////////////////////////////////////////////////// + function backquote($a_name) + { + /* + Add backqouotes to tables and db-names in + SQL queries. Taken from phpMyAdmin. + */ + if (!empty($a_name) && $a_name != '*') { + if (is_array($a_name)) { + $result = array(); + reset($a_name); + while(list($key, $val) = each($a_name)) { + $result[$key] = '`' . $val . '`'; + } + return $result; + } else { + return '`' . $a_name . '`'; + } + } else { + return $a_name; + } + } // function backquote($a_name, $do_it = TRUE) + + ///////////// + function open($filename = '', $mode = 'w') { + if ('' == $filename) return false; + if ($this->gzip()) { + $fp = @gzopen($filename, $mode); + } else { + $fp = @fopen($filename, $mode); + } + return $fp; + } + + ////////////// + function close($fp) { + if ($this->gzip()) { + gzclose($fp); + } else { + fclose($fp); + } + } + + ////////////// + function stow($query_line) { + if ($this->gzip()) { + if(@gzwrite($this->fp, $query_line) === FALSE) { + backup_error(__('There was an error writing a line to the backup script:')); + backup_error('  ' . $query_line); + } + } else { + if(@fwrite($this->fp, $query_line) === FALSE) { + backup_error(__('There was an error writing a line to the backup script:')); + backup_error('  ' . $query_line); + } + } + } + + function backup_error($err) { + if(count($this->backup_errors) < 20) { + $this->backup_errors[] = $err; + } elseif(count($this->backup_errors) == 20) { + $this->backup_errors[] = __('Subsequent errors have been omitted from this log.'); + } + } + + ///////////////////////////// + function backup_table($table, $segment = 'none') { + global $wpdb; + + /* + Taken partially from phpMyAdmin and partially from + Alain Wolf, Zurich - Switzerland + Website: http://restkultur.ch/personal/wolf/scripts/db_backup/ + + Modified by Scott Merril (http://www.skippy.net/) + to use the WordPress $wpdb object + */ + + $table_structure = $wpdb->get_results("DESCRIBE $table"); + if (! $table_structure) { + backup_errors(__('Error getting table details') . ": $table"); + return FALSE; + } + + if(($segment == 'none') || ($segment == 0)) { + // + // Add SQL statement to drop existing table + $this->stow("\n\n"); + $this->stow("#\n"); + $this->stow("# Delete any existing table " . $this->backquote($table) . "\n"); + $this->stow("#\n"); + $this->stow("\n"); + $this->stow("DROP TABLE IF EXISTS " . $this->backquote($table) . ";\n"); + + // + //Table structure + // Comment in SQL-file + $this->stow("\n\n"); + $this->stow("#\n"); + $this->stow("# Table structure of table " . $this->backquote($table) . "\n"); + $this->stow("#\n"); + $this->stow("\n"); + + $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N); + if (FALSE === $create_table) { + $this->backup_error(sprintf(__("Error with SHOW CREATE TABLE for %s."), $table)); + $this->stow("#\n# Error with SHOW CREATE TABLE for $table!\n#\n"); + } + $this->stow($create_table[0][1] . ' ;'); + + if (FALSE === $table_structure) { + $this->backup_error(sprintf(__("Error getting table structure of %s"), $table)); + $this->stow("#\n# Error getting table structure of $table!\n#\n"); + } + + // + // Comment in SQL-file + $this->stow("\n\n"); + $this->stow("#\n"); + $this->stow('# Data contents of table ' . $this->backquote($table) . "\n"); + $this->stow("#\n"); + } + + if(($segment == 'none') || ($segment >= 0)) { + $ints = array(); + foreach ($table_structure as $struct) { + if ( (0 === strpos($struct->Type, 'tinyint')) || + (0 === strpos(strtolower($struct->Type), 'smallint')) || + (0 === strpos(strtolower($struct->Type), 'mediumint')) || + (0 === strpos(strtolower($struct->Type), 'int')) || + (0 === strpos(strtolower($struct->Type), 'bigint')) || + (0 === strpos(strtolower($struct->Type), 'timestamp')) ) { + $ints[strtolower($struct->Field)] = "1"; + } + } + + + // Batch by $row_inc + + if($segment == 'none') { + $row_start = 0; + $row_inc = ROWS_PER_SEGMENT; + } else { + $row_start = $segment * ROWS_PER_SEGMENT; + $row_inc = ROWS_PER_SEGMENT; + } + + do { + if ( !ini_get('safe_mode')) @set_time_limit(15*60); + $table_data = $wpdb->get_results("SELECT * FROM $table LIMIT {$row_start}, {$row_inc}", ARRAY_A); + + /* + if (FALSE === $table_data) { + $wp_backup_error .= "Error getting table contents from $table\r\n"; + fwrite($fp, "#\n# Error getting table contents fom $table!\n#\n"); + } + */ + + $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES ('; + // \x08\\x09, not required + $search = array("\x00", "\x0a", "\x0d", "\x1a"); + $replace = array('\0', '\n', '\r', '\Z'); + if($table_data) { + foreach ($table_data as $row) { + $values = array(); + foreach ($row as $key => $value) { + if ($ints[strtolower($key)]) { + $values[] = $value; + } else { + $values[] = "'" . str_replace($search, $replace, $this->sql_addslashes($value)) . "'"; + } + } + $this->stow(" \n" . $entries . implode(', ', $values) . ') ;'); + } + $row_start += $row_inc; + } + } while((count($table_data) > 0) and ($segment=='none')); + } + + + if(($segment == 'none') || ($segment < 0)) { + // Create footer/closing comment in SQL-file + $this->stow("\n"); + $this->stow("#\n"); + $this->stow("# End of data contents of table " . $this->backquote($table) . "\n"); + $this->stow("# --------------------------------------------------------\n"); + $this->stow("\n"); + } + + } // end backup_table() + + function return_bytes($val) { + $val = trim($val); + $last = strtolower($val{strlen($val)-1}); + switch($last) { + // The 'G' modifier is available since PHP 5.1.0 + case 'g': + $val *= 1024; + case 'm': + $val *= 1024; + case 'k': + $val *= 1024; + } + + return $val; + } + + //////////////////////////// + function db_backup($core_tables, $other_tables) { + global $table_prefix, $wpdb; + + $datum = date("Ymd_B"); + $wp_backup_filename = DB_NAME . "_$table_prefix$datum.sql"; + if ($this->gzip()) { + $wp_backup_filename .= '.gz'; + } + + if (is_writable(ABSPATH . $this->backup_dir)) { + $this->fp = $this->open(ABSPATH . $this->backup_dir . $wp_backup_filename); + if(!$this->fp) { + $this->backup_error(__('Could not open the backup file for writing!')); + return false; + } + } else { + $this->backup_error(__('The backup directory is not writeable!')); + return false; + } + + //Begin new backup of MySql + $this->stow("# WordPress MySQL database backup\n"); + $this->stow("#\n"); + $this->stow("# Generated: " . date("l j. F Y H:i T") . "\n"); + $this->stow("# Hostname: " . DB_HOST . "\n"); + $this->stow("# Database: " . $this->backquote(DB_NAME) . "\n"); + $this->stow("# --------------------------------------------------------\n"); + + if ( (is_array($other_tables)) && (count($other_tables) > 0) ) + $tables = array_merge($core_tables, $other_tables); + else + $tables = $core_tables; + + foreach ($tables as $table) { + // Increase script execution time-limit to 15 min for every table. + if ( !ini_get('safe_mode')) @set_time_limit(15*60); + // Create the SQL statements + $this->stow("# --------------------------------------------------------\n"); + $this->stow("# Table: " . $this->backquote($table) . "\n"); + $this->stow("# --------------------------------------------------------\n"); + $this->backup_table($table); + } + + $this->close($this->fp); + + if (count($this->backup_errors)) { + return false; + } else { + return $wp_backup_filename; + } + + } //wp_db_backup + + /////////////////////////// + function deliver_backup ($filename = '', $delivery = 'http', $recipient = '') { + if ('' == $filename) { return FALSE; } + + $diskfile = ABSPATH . $this->backup_dir . $filename; + if ('http' == $delivery) { + if (! file_exists($diskfile)) { + $msg = sprintf(__('File not found:%s'), "
    $filename
    "); + $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); + $msg .= '
    ' . __('Return to Backup'); + die($msg); + } + header('Content-Description: File Transfer'); + header('Content-Type: application/octet-stream'); + header('Content-Length: ' . filesize($diskfile)); + header("Content-Disposition: attachment; filename=$filename"); + readfile($diskfile); + unlink($diskfile); + } elseif ('smtp' == $delivery) { + if (! file_exists($diskfile)) return false; + + if (! is_email ($recipient)) { + $recipient = get_settings('admin_email'); + } + $randomish = md5(time()); + $boundary = "==WPBACKUP-BY-SKIPPY-$randomish"; + $fp = fopen($diskfile,"rb"); + $file = fread($fp,filesize($diskfile)); + $this->close($fp); + $data = chunk_split(base64_encode($file)); + $headers = "MIME-Version: 1.0\n"; + $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; + $headers .= 'From: ' . get_settings('admin_email') . "\n"; + + $message = sprintf(__("Attached to this email is\n %1s\n Size:%2s kilobytes\n"), $filename, round(filesize($diskfile)/1024)); + // Add a multipart boundary above the plain message + $message = "This is a multi-part message in MIME format.\n\n" . + "--{$boundary}\n" . + "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . + "Content-Transfer-Encoding: 7bit\n\n" . + $message . "\n\n"; + + // Add file attachment to the message + $message .= "--{$boundary}\n" . + "Content-Type: application/octet-stream;\n" . + " name=\"{$filename}\"\n" . + "Content-Disposition: attachment;\n" . + " filename=\"{$filename}\"\n" . + "Content-Transfer-Encoding: base64\n\n" . + $data . "\n\n" . + "--{$boundary}--\n"; + + if (function_exists('wp_mail')) { + wp_mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers); + } else { + mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers); + } + + unlink($diskfile); + } + return; + } + + //////////////////////////// + function backup_menu() { + global $table_prefix, $wpdb; + $feedback = ''; + $WHOOPS = FALSE; + + // did we just do a backup? If so, let's report the status + if ( $this->backup_complete ) { + $feedback = '

    ' . __('Backup Successful') . '!'; + $file = $this->backup_file; + switch($_POST['deliver']) { + case 'http': + $feedback .= '
    ' . sprintf(__('Your backup file:
    %2s should begin downloading shortly.'), get_settings('siteurl') . "/{$this->backup_dir}{$this->backup_file}", $this->backup_file); + break; + case 'smtp': + if (! is_email($_POST['backup_recipient'])) { + $feedback .= get_settings('admin_email'); + } else { + $feedback .= $_POST['backup_recipient']; + } + $feedback = '
    ' . sprintf(__('Your backup has been emailed to %s'), $feedback); + break; + case 'none': + $feedback .= '
    ' . __('Your backup file has been saved on the server. If you would like to download it now, right click and select "Save As"'); + $feedback .= ':
    backup_dir}$file\">$file : " . sprintf(__('%s bytes'), filesize(ABSPATH . $this->backup_dir . $file)); + } + $feedback .= '

    '; + } + + if (count($this->backup_errors)) { + $feedback .= '
    ' . __('The following errors were reported:') . "
    ";
    +			foreach($this->backup_errors as $error) {
    +				$feedback .= "{$error}\n";  //Errors are already localized
    +			}
    +			$feedback .= "
    "; + } + + // did we just save options for wp-cron? + if ( (function_exists('wp_cron_init')) && isset($_POST['wp_cron_backup_options']) ) { + update_option('wp_cron_backup_schedule', intval($_POST['cron_schedule']), FALSE); + update_option('wp_cron_backup_tables', $_POST['wp_cron_backup_tables']); + if (is_email($_POST['cron_backup_recipient'])) { + update_option('wp_cron_backup_recipient', $_POST['cron_backup_recipient'], FALSE); + } + $feedback .= '

    ' . __('Scheduled Backup Options Saved!') . '

    '; + } + + // Simple table name storage + $wp_table_names = explode(',','categories,comments,linkcategories,links,options,post2cat,postmeta,posts,users,usermeta'); + // Apply WP DB prefix to table names + $wp_table_names = array_map(create_function('$a', 'global $table_prefix;return "{$table_prefix}{$a}";'), $wp_table_names); + + $other_tables = array(); + $also_backup = array(); + + // Get complete db table list + $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N); + $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables); + // Get list of WP tables that actually exist in this DB (for 1.6 compat!) + $wp_backup_default_tables = array_intersect($all_tables, $wp_table_names); + // Get list of non-WP tables + $other_tables = array_diff($all_tables, $wp_backup_default_tables); + + if ('' != $feedback) { + echo $feedback; + } + + // Give the new dirs the same perms as wp-content. + $stat = stat( ABSPATH . 'wp-content' ); + $dir_perms = $stat['mode'] & 0000777; // Get the permission bits. + + if ( !file_exists( ABSPATH . $this->backup_dir) ) { + if ( @ mkdir( ABSPATH . $this->backup_dir) ) { + @ chmod( ABSPATH . $this->backup_dir, $dir_perms); + } else { + echo '

    ' . __('WARNING: Your wp-content directory is NOT writable! We can not create the backup directory.') . '
    ' . ABSPATH . $this->backup_dir . "

    "; + $WHOOPS = TRUE; + } + } + + if ( !is_writable( ABSPATH . $this->backup_dir) ) { + echo '

    ' . __('WARNING: Your backup directory is NOT writable! We can not create the backup directory.') . '
    ' . ABSPATH . "

    "; + } + + if ( !file_exists( ABSPATH . $this->backup_dir . 'index.php') ) { + @ touch( ABSPATH . $this->backup_dir . "index.php"); + } + + echo "
    "; + echo '

    ' . __('Backup') . '

    '; + echo '
    ' . __('Tables') . ''; + echo '
    '; + echo '
    '; + echo __('These core WordPress tables will always be backed up:') . '
      '; + foreach ($wp_backup_default_tables as $table) { + echo "
    • $table
    • "; + } + echo '
    '; + if (count($other_tables) > 0) { + echo __('You may choose to include any of the following tables:') . '
    '; + foreach ($other_tables as $table) { + echo ""; + } + } + echo '
    '; + echo '
    ' . __('Backup Options') . ''; + echo __('What to do with the backup file:') . "
    "; + echo '"; + echo ''; + echo '
    '; + echo ''; + + // Check DB dize. + $table_status = $wpdb->get_results("SHOW TABLE STATUS FROM " . $this->backquote(DB_NAME)); + $core_size = $db_size = 0; + foreach($table_status as $table) { + $table_size = $table->Data_length - $table->Data_free; + if(in_array($table->Name, $wp_backup_default_tables)) { + $core_size += $table_size; + } + $db_size += $table_size; + } + $mem_limit = ini_get('memory_limit'); + $mem_limit = $this->return_bytes($mem_limit); + $mem_limit = ($mem_limit == 0) ? 8*1024*1024 : $mem_limit - 2000000; + + if (! $WHOOPS) { + echo '
    '; + echo '

    '; + } else { + echo '

    ' . __('WARNING: Your backup directory is NOT writable!') . '

    '; + } + echo '
    '; + echo ''; + + // this stuff only displays if wp_cron is installed + if (function_exists('wp_cron_init')) { + echo '
    ' . __('Scheduled Backup') . ''; + $datetime = get_settings('date_format') . ' @ ' . get_settings('time_format'); + echo '

    ' . __('Last WP-Cron Daily Execution') . ': ' . date($datetime, get_option('wp_cron_daily_lastrun')) . '
    '; + echo __('Next WP-Cron Daily Execution') . ': ' . date($datetime, (get_option('wp_cron_daily_lastrun') + 86400)) . '

    '; + echo '
    '; + echo ''; + echo ''; + $cron_tables = get_option('wp_cron_backup_tables'); + if (! is_array($cron_tables)) { + $cron_tables = array(); + } + if (count($other_tables) > 0) { + echo ''; + } + echo '
    '; + echo __('Schedule: '); + $wp_cron_backup_schedule = get_option('wp_cron_backup_schedule'); + $schedule = array(0 => __('None'), 1 => __('Daily')); + foreach ($schedule as $value => $name) { + echo ' ' . __($name); + } + echo ''; + $cron_recipient = get_option('wp_cron_backup_recipient'); + if (! is_email($cron_recipient)) { + $cron_recipient = get_settings('admin_email'); + } + echo __('Email backup to:') . ' '; + echo '
    ' . __('Tables to include:') . '
    '; + foreach ($other_tables as $table) { + echo ' {$table}
    "; + } + echo '
    '; + echo '
    '; + } + // end of wp_cron section + + echo '
    '; + + }// end wp_backup_menu() + + ///////////////////////////// + function wp_cron_daily() { + + $schedule = intval(get_option('wp_cron_backup_schedule')); + if (0 == $schedule) { + // Scheduled backup is disabled + return; + } + + global $table_prefix, $wpdb; + + $wp_table_names = explode(',','categories,comments,linkcategories,links,options,post2cat,postmeta,posts,users,usermeta'); + $wp_table_names = array_map(create_function('$a', 'global $table_prefix;return "{$table_prefix}{$a}";'), $wp_table_names); + $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N); + $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables); + $core_tables = array_intersect($all_tables, $wp_table_names); + $other_tables = get_option('wp_cron_backup_tables'); + + $recipient = get_option('wp_cron_backup_recipient'); + + $backup_file = $this->db_backup($core_tables, $other_tables); + if (FALSE !== $backup_file) { + $this->deliver_backup ($backup_file, 'smtp', $recipient); + } + + return; + } // wp_cron_db_backup +} + +$mywpdbbackup = new wpdbBackup(); + +?> diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/comments-popup.php b/projects/tests/tests/real/wordpress/wp-content/themes/classic/comments-popup.php new file mode 100644 index 00000000..f42d38dc --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/classic/comments-popup.php @@ -0,0 +1,113 @@ + + + + + <?php echo get_settings('blogname'); ?> - <?php echo sprintf(__("Comments on %s"), the_title('','',false)); ?> + + + + + + + +

    + +

    + +

    RSS feed for comments on this post."); ?>

    + +ping_status) { ?> +

    URI to TrackBack this entry is:"); ?>

    + + +post_password) && $_COOKIE['wp-postpass_'. COOKIEHASH] != $commentstatus->post_password) { // and it doesn't match the cookie + echo(get_the_password_form()); +} else { ?> + + +
      + +
    1. + +

      @

      +
    2. + + +
    + +

    + + +comment_status) { ?> +

    +

    HTML allowed:"); ?>

    + +
    +

    + + + + " /> +

    + +

    + + +

    + +

    + + +

    + +

    + +
    + +

    + +

    + " /> +

    + ID); ?> +
    + +

    + + +
    + + + + + +

    Powered by Wordpress"),__("Powered by WordPress, state-of-the-art semantic personal publishing platform.")); ?>

    + + + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/comments.php b/projects/tests/tests/real/wordpress/wp-content/themes/classic/comments.php new file mode 100644 index 00000000..6222f1af --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/classic/comments.php @@ -0,0 +1,75 @@ +post_password) && $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) : ?> +

    + + +

    + + ">» + +

    + + +
      + + +
    1. + +

      @

      +
    2. + + + +
    + + +

    + + +

    RSS feed for comments on this post.')); ?> + + URI'); ?> + +

    + + +

    + + +

    You must be logged in to post a comment.

    + + +
    + + + +

    Logged in as . Logout »

    + + + +

    +

    + +

    +

    + +

    +

    + + + + + +

    + +

    + +

    +ID); ?> + +
    + + + + +

    + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/footer.php b/projects/tests/tests/real/wordpress/wp-content/themes/classic/footer.php new file mode 100644 index 00000000..0894f78e --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/classic/footer.php @@ -0,0 +1,12 @@ + + + + + +

    WordPress"), __("Powered by WordPress, state-of-the-art semantic personal publishing platform.")); ?>

    + + + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/header.php b/projects/tests/tests/real/wordpress/wp-content/themes/classic/header.php new file mode 100644 index 00000000..e505e298 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/classic/header.php @@ -0,0 +1,30 @@ + + + + + + + <?php bloginfo('name'); ?><?php wp_title(); ?> + + + + + + + + + + + + + + + + +
    +

    + +
    + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/index.php b/projects/tests/tests/real/wordpress/wp-content/themes/classic/index.php new file mode 100644 index 00000000..bbee16e7 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/classic/index.php @@ -0,0 +1,32 @@ + + + + +',''); ?> + +
    +

    +
    @
    + +
    + +
    + + + +
    + + + + +

    + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/screenshot.png b/projects/tests/tests/real/wordpress/wp-content/themes/classic/screenshot.png new file mode 100644 index 00000000..66921306 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/classic/screenshot.png differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/sidebar.php b/projects/tests/tests/real/wordpress/wp-content/themes/classic/sidebar.php new file mode 100644 index 00000000..46dee784 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/classic/sidebar.php @@ -0,0 +1,43 @@ + + + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/classic/style.css b/projects/tests/tests/real/wordpress/wp-content/themes/classic/style.css new file mode 100644 index 00000000..e09122dc --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/classic/style.css @@ -0,0 +1,313 @@ +/* +Theme Name: WordPress Classic +Theme URI: http://wordpress.org/ +Description: The original WordPress theme that graced versions 1.2.x and prior. +Version: 1.5 +Author: Dave Shea + +Default WordPress by Dave Shea || http://mezzoblue.com +Modifications by Matthew Mullenweg || http://photomatt.net +This is just a basic layout, with only the bare minimum defined. +Please tweak this and make it your own. :) +*/ + +a { + color: #675; +} + +a img { + border: none; +} + +a:visited { + color: #342; +} + +a:hover { + color: #9a8; +} + +acronym, abbr { + border-bottom: 1px dashed #333; +} + +acronym, abbr, span.caps { + font-size: 90%; + letter-spacing: .07em; +} + +acronym, abbr { + cursor: help; +} + +blockquote { + border-left: 5px solid #ccc; + margin-left: 1.5em; + padding-left: 5px; +} + +body { + background: #fff; + border: solid 2px #565; + border-bottom: solid 1px #565; + border-top: solid 3px #565; + color: #000; + font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, sans-serif; + margin: 0; + padding: 0; +} + +cite { + font-size: 90%; + font-style: normal; +} + +h2 { + border-bottom: 1px dotted #ccc; + font: 95% "Times New Roman", Times, serif; + letter-spacing: 0.2em; + margin: 15px 0 2px 0; + padding-bottom: 2px; +} + +h3 { + border-bottom: dotted 1px #eee; + font-family: "Times New Roman", Times, serif; + margin-top: 0; +} + +ol#comments li p { + font-size: 100%; +} + +p, li, .feedback { + font: 90%/175% 'Lucida Grande', 'Lucida Sans Unicode', Verdana, sans-serif; + letter-spacing: -1px; +} + +/* classes used by the_meta() */ +ul.post-meta { + list-style: none; +} + +ul.post-meta span.post-meta-key { + font-weight: bold; +} + +.credit { + background: #90a090; + border-top: double 3px #aba; + color: #fff; + font-size: 11px; + margin: 10px 0 0 0; + padding: 3px; + text-align: center; +} + +.credit a:link, .credit a:hover { + color: #fff; +} + +.feedback { + color: #ccc; + text-align: right; + clear: both; +} + +.meta { + font-size: .75em; +} + +.meta li, ul.post-meta li { + display: inline; +} + +.meta ul { + display: inline; + list-style: none; + margin: 0; + padding: 0; +} + +.meta, .meta a { + color: #808080; + font-weight: normal; + letter-spacing: 0; +} + +.storytitle { + margin: 0; +} + +.storytitle a { + text-decoration: none; +} + +#commentform #author, #commentform #email, #commentform #url, #commentform textarea { + background: #fff; + border: 1px solid #333; + padding: .2em; +} + +#commentform textarea { + width: 100%; +} + +#commentlist li ul { + border-left: 1px solid #ddd; + font-size: 110%; + list-style-type: none; +} + +#content { + margin: 30px 13em 0 3em; + padding-right: 60px; +} + +#header { + background: #90a090; + border-bottom: double 3px #aba; + border-left: solid 1px #9a9; + border-right: solid 1px #565; + border-top: solid 1px #9a9; + font: italic normal 230% 'Times New Roman', Times, serif; + letter-spacing: 0.2em; + margin: 0; + padding: 15px 10px 15px 60px; +} + +#header a { + color: #fff; + text-decoration: none; +} + +#header a:hover { + text-decoration: underline; +} + +#menu { + background: #fff; + border-left: 1px dotted #ccc; + border-top: solid 3px #e0e6e0; + padding: 20px 0 10px 30px; + position: absolute; + right: 2px; + top: 0; + width: 11em; +} + +#menu form { + margin: 0 0 0 13px; +} + +#menu input#s { + width: 80%; + background: #eee; + border: 1px solid #999; + color: #000; +} + +#menu ul { + color: #ccc; + font-weight: bold; + list-style-type: none; + margin: 0; + padding-left: 3px; + text-transform: lowercase; +} + +#menu ul li { + font: italic normal 110% 'Times New Roman', Times, serif; + letter-spacing: 0.1em; + margin-top: 10px; + padding-bottom: 2px; /*border-bottom: dotted 1px #ccc;*/ +} + +#menu ul ul { + font-variant: normal; + font-weight: normal; + line-height: 100%; + list-style-type: none; + margin: 0; + padding: 0; + text-align: left; +} + +#menu ul ul li { + border: 0; + font: normal normal 12px/115% 'Lucida Grande', 'Lucida Sans Unicode', Verdana, sans-serif; + letter-spacing: 0; + margin-top: 0; + padding: 0; + padding-left: 12px; +} + +#menu ul ul li a { + color: #000; + text-decoration: none; +} + +#menu ul ul li a:hover { + border-bottom: 1px solid #809080; +} + +#menu ul ul ul.children { + font-size: 142%; + padding-left: 4px; +} + +#wp-calendar { + border: 1px solid #ddd; + empty-cells: show; + font-size: 14px; + margin: 0; + width: 90%; +} + +#wp-calendar #next a { + padding-right: 10px; + text-align: right; +} + +#wp-calendar #prev a { + padding-left: 10px; + text-align: left; +} + +#wp-calendar a { + display: block; + text-decoration: none; +} + +#wp-calendar a:hover { + background: #e0e6e0; + color: #333; +} + +#wp-calendar caption { + color: #999; + font-size: 16px; + text-align: left; +} + +#wp-calendar td { + color: #ccc; + font: normal 12px 'Lucida Grande', 'Lucida Sans Unicode', Verdana, sans-serif; + letter-spacing: normal; + padding: 2px 0; + text-align: center; +} + +#wp-calendar td.pad:hover { + background: #fff; +} + +#wp-calendar td:hover, #wp-calendar #today { + background: #eee; + color: #bbb; +} + +#wp-calendar th { + font-style: normal; + text-transform: capitalize; +} diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/404.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/404.php new file mode 100644 index 00000000..01a8e8a6 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/404.php @@ -0,0 +1,11 @@ + + +
    + +

    Error 404 - Not Found

    + +
    + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/archive.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/archive.php new file mode 100644 index 00000000..104c8b27 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/archive.php @@ -0,0 +1,68 @@ + + +
    + + + + + +

    Archive for the '' Category

    + + +

    Archive for

    + + +

    Archive for

    + + +

    Archive for

    + + +

    Search Results

    + + +

    Author Archive

    + + +

    Blog Archives

    + + + + + + + +
    +

    + + +
    + +
    + + + +
    + + + + + + + +

    Not Found

    + + + + +
    + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/archives.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/archives.php new file mode 100644 index 00000000..ae9d8dd2 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/archives.php @@ -0,0 +1,25 @@ + + + + +
    + + + +

    Archives by Month:

    +
      + +
    + +

    Archives by Subject:

    +
      + +
    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/attachment.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/attachment.php new file mode 100644 index 00000000..b8f33478 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/attachment.php @@ -0,0 +1,67 @@ + + +
    + + + + +ID, true, array(450, 800)); // This also populates the iconsize for the next line ?> +ID); $classname = ($_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment'; // This lets us style narrow icons specially ?> +
    +

    post_parent); ?> »

    +
    +


    guid); ?>

    + + Read the rest of this entry »

    '); ?> + + Pages: ', '

    ', 'number'); ?> + + + +
    +
    + + + + + +

    Sorry, no attachments matched your criteria.

    + + + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/comments-popup.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/comments-popup.php new file mode 100644 index 00000000..d5dd1763 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/comments-popup.php @@ -0,0 +1,113 @@ + + + + + <?php echo get_settings('blogname'); ?> - Comments on <?php the_title(); ?> + + + + + + + +

    + +

    Comments

    + +

    RSS feed for comments on this post.

    + +ping_status) { ?> +

    The URI to TrackBack this entry is:

    + + +post_password) && $_COOKIE['wp-postpass_'. COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie + echo(get_the_password_form()); +} else { ?> + + +
      + +
    1. + +

      by @

      +
    2. + + +
    + +

    No comments yet.

    + + +comment_status) { ?> +

    Leave a comment

    +

    Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed:

    + +
    +

    + + + + " /> +

    + +

    + + +

    + +

    + + +

    + +

    + +
    + +

    + +

    + +

    + ID); ?> +
    + +

    Sorry, the comment form is closed at this time.

    + + + + + + + + +

    Powered by Wordpress

    + + + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/comments.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/comments.php new file mode 100644 index 00000000..dff40d82 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/comments.php @@ -0,0 +1,104 @@ +post_password)) { // if there's a password + if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie + ?> + +

    This post is password protected. Enter the password to view comments.

    + + + + + + +

    to “

    + +
      + + + +
    1. + Says: + comment_approved == '0') : ?> + Your comment is awaiting moderation. + +
      + + + + + +
    2. + + + + + +
    + + + + comment_status) : ?> + + + + +

    Comments are closed.

    + + + + + +comment_status) : ?> + +

    Leave a Reply

    + + +

    You must be logged in to post a comment.

    + + +
    + + + +

    Logged in as . Logout »

    + + + +

    +

    + +

    +

    + +

    +

    + + + + + +

    + +

    + +

    +ID); ?> + +
    + + + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/footer.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/footer.php new file mode 100644 index 00000000..a6cfd870 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/footer.php @@ -0,0 +1,19 @@ + +
    + +
    + + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/functions.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/functions.php new file mode 100644 index 00000000..2af0b16d --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/functions.php @@ -0,0 +1,393 @@ +\n\n"; + if ( '' != $output ) + echo $head . $output . $foot; +} + +add_action('wp_head', 'kubrick_head'); + +function kubrick_header_image() { + return apply_filters('kubrick_header_image', get_settings('kubrick_header_image')); +} + +function kubrick_upper_color() { + if ( strstr( kubrick_header_image_url(), 'header-img.php?' ) ) + return substr( kubrick_header_image(), 21, 6 ); + else + return '69aee7'; +} + +function kubrick_lower_color() { + if ( strstr( kubrick_header_image_url(), 'header-img.php?' ) ) + return substr( kubrick_header_image(), 34, 6 ); + else + return '4180b6'; +} + +function kubrick_header_image_url() { + if ( $image = kubrick_header_image() ) + $url = get_template_directory_uri() . '/images/' . $image; + else + $url = get_template_directory_uri() . '/images/kubrickheader.jpg'; + + return $url; +} + +function kubrick_header_color() { + return apply_filters('kubrick_header_color', get_settings('kubrick_header_color')); +} + +function kubrick_header_color_string() { + $color = kubrick_header_color(); + if ( false === $color ) + return 'white'; + + return $color; +} + +function kubrick_header_display() { + return apply_filters('kubrick_header_display', get_settings('kubrick_header_display')); +} + +function kubrick_header_display_string() { + $display = kubrick_header_display(); + return $display ? $display : 'inline'; +} + +add_action('admin_menu', 'kubrick_add_theme_page'); + +function kubrick_add_theme_page() { + if ( $_GET['page'] == basename(__FILE__) ) { + if ( 'save' == $_REQUEST['action'] ) { + if ( isset($_REQUEST['njform']) ) { + if ( isset($_REQUEST['defaults']) ) { + delete_option('kubrick_header_image'); + delete_option('kubrick_header_color'); + delete_option('kubrick_header_display'); + } else { + if ( '' == $_REQUEST['njfontcolor'] ) + delete_option('kubrick_header_color'); + else + update_option('kubrick_header_color', $_REQUEST['njfontcolor']); + + if ( preg_match('/[0-9A-F]{6}|[0-9A-F]{3}/i', $_REQUEST['njuppercolor'], $uc) && preg_match('/[0-9A-F]{6}|[0-9A-F]{3}/i', $_REQUEST['njlowercolor'], $lc) ) { + $uc = ( strlen($uc[0]) == 3 ) ? $uc[0]{0}.$uc[0]{0}.$uc[0]{1}.$uc[0]{1}.$uc[0]{2}.$uc[0]{2} : $uc[0]; + $lc = ( strlen($lc[0]) == 3 ) ? $lc[0]{0}.$lc[0]{0}.$lc[0]{1}.$lc[0]{1}.$lc[0]{2}.$lc[0]{2} : $lc[0]; + update_option('kubrick_header_image', "header-img.php?upper=$uc&lower=$lc"); + } + + if ( isset($_REQUEST['toggledisplay']) ) { + if ( false === get_settings('kubrick_header_display') ) + update_option('kubrick_header_display', 'none'); + else + delete_option('kubrick_header_display'); + } + } + } else { + + if ( isset($_REQUEST['headerimage']) ) { + if ( '' == $_REQUEST['headerimage'] ) + delete_option('kubrick_header_image'); + else + update_option('kubrick_header_image', $_REQUEST['headerimage']); + } + + if ( isset($_REQUEST['fontcolor']) ) { + if ( '' == $_REQUEST['fontcolor'] ) + delete_option('kubrick_header_color'); + else + update_option('kubrick_header_color', $_REQUEST['fontcolor']); + } + + if ( isset($_REQUEST['fontdisplay']) ) { + if ( '' == $_REQUEST['fontdisplay'] || 'inline' == $_REQUEST['fontdisplay'] ) + delete_option('kubrick_header_display'); + else + update_option('kubrick_header_display', 'none'); + } + } + //print_r($_REQUEST); + header("Location: themes.php?page=functions.php&saved=true"); + die; + } + add_action('admin_head', 'kubrick_theme_page_head'); + } + add_theme_page("Kubrick Theme Options", "Current Theme Options", 'edit_themes', basename(__FILE__), 'kubrick_theme_page'); +} + +function kubrick_theme_page_head() { +?> + + + +

    Options saved.

    '; +?> +
    +
    +

    Header Image and Color

    +
    + +
    +
    +
    +
    +
    + Any CSS color (red or #FF0000 or rgb(255, 0, 0))
    + HEX only (#FF0000 or #F00)
    + HEX only (#FF0000 or #F00)
    + + + + + + +
    +
    +
    +
    + + + + + + + + + + + + +
    + +
    + +
    +
    +
    +
    + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/header.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/header.php new file mode 100644 index 00000000..65ea78b3 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/header.php @@ -0,0 +1,56 @@ + + + + + + +<?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> » Blog Archive <?php } ?> <?php wp_title(); ?> + + + + + + + + + + + + +
    + + + +
    diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/images/audio.jpg b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/audio.jpg new file mode 100644 index 00000000..b02e1c86 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/audio.jpg differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/images/header-img.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/header-img.php new file mode 100644 index 00000000..afae590b --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/header-img.php @@ -0,0 +1,74 @@ +array(0=>'r1', 2=>'g1', 4=>'b1'), 'lower'=>array(0=>'r2', 2=>'g2', 4=>'b2')); +foreach ( $vars as $var => $subvars ) { + if ( isset($_GET[$var]) ) { + foreach ( $subvars as $index => $subvar ) { + $$subvar = hexdec( substr($_GET[$var], $index, 2) ); + if ( $$subvar < 0 || $$subvar > 255 ) + $default = true; + } + } else { + $default = true; + } +} + +if ( $default ) + list ( $r1, $g1, $b1, $r2, $g2, $b2 ) = array ( 105, 174, 231, 65, 128, 182 ); + +// Create the image +$im = imagecreatefromjpeg($img); + +// Get the background color, define the rectangle height +$white = imagecolorat( $im, 15, 15 ); +$h = 182; + +// Define the boundaries of the rounded edges ( y => array ( x1, x2 ) ) +$corners = array( + 0 => array ( 25, 734 ), + 1 => array ( 23, 736 ), + 2 => array ( 22, 737 ), + 3 => array ( 21, 738 ), + 4 => array ( 21, 738 ), + 177 => array ( 21, 738 ), + 178 => array ( 21, 738 ), + 179 => array ( 22, 737 ), + 180 => array ( 23, 736 ), + 181 => array ( 25, 734 ), + ); + +// Blank out the blue thing +for ( $i = 0; $i < $h; $i++ ) { + $x1 = 19; + $x2 = 740; + imageline( $im, $x1, 18 + $i, $x2, 18 + $i, $white ); +} + +// Draw a new color thing +for ( $i = 0; $i < $h; $i++ ) { + $x1 = 20; + $x2 = 739; + $r = ( $r2 - $r1 != 0 ) ? $r1 + ( $r2 - $r1 ) * ( $i / $h ) : $r1; + $g = ( $g2 - $g1 != 0 ) ? $g1 + ( $g2 - $g1 ) * ( $i / $h ) : $g1; + $b = ( $b2 - $b1 != 0 ) ? $b1 + ( $b2 - $b1 ) * ( $i / $h ) : $b1; + $color = imagecolorallocate( $im, $r, $g, $b ); + if ( array_key_exists($i, $corners) ) { + imageline( $im, $x1, 18 + $i, $x2, 18 + $i, $white ); + list ( $x1, $x2 ) = $corners[$i]; + } + imageline( $im, $x1, 18 + $i, $x2, 18 + $i, $color ); +} + +//die; +header("Content-Type: image/jpeg"); +imagejpeg($im, '', 92); +imagedestroy($im); +?> diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbg.jpg b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbg.jpg new file mode 100644 index 00000000..dc74fb8d Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbg.jpg differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbgcolor.jpg b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbgcolor.jpg new file mode 100644 index 00000000..4653b68e Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbgcolor.jpg differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbgwide.jpg b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbgwide.jpg new file mode 100644 index 00000000..77096f24 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickbgwide.jpg differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickfooter.jpg b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickfooter.jpg new file mode 100644 index 00000000..d7a4c659 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickfooter.jpg differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickheader.jpg b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickheader.jpg new file mode 100644 index 00000000..69441ee3 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/default/images/kubrickheader.jpg differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/index.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/index.php new file mode 100644 index 00000000..32a432ee --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/index.php @@ -0,0 +1,39 @@ + + +
    + + + + + +
    +

    + + +
    + +
    + + +
    + + + + + + + +

    Not Found

    +

    Sorry, but you are looking for something that isn't here.

    + + + + +
    + + + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/links.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/links.php new file mode 100644 index 00000000..2970897a --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/links.php @@ -0,0 +1,18 @@ + + + + +
    + +

    Links:

    +
      + +
    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/page.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/page.php new file mode 100644 index 00000000..5f914c12 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/page.php @@ -0,0 +1,21 @@ + + +
    + + +
    +

    +
    + Read the rest of this page »

    '); ?> + + Pages: ', '

    ', 'number'); ?> + +
    +
    + + ', '

    '); ?> +
    + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/screenshot.png b/projects/tests/tests/real/wordpress/wp-content/themes/default/screenshot.png new file mode 100644 index 00000000..e0b1d488 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-content/themes/default/screenshot.png differ diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/search.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/search.php new file mode 100644 index 00000000..4c6875fa --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/search.php @@ -0,0 +1,42 @@ + + +
    + + + +

    Search Results

    + + + + + + +
    +

    + + + +
    + + + + + + + +

    No posts found. Try a different search?

    + + + + +
    + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/searchform.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/searchform.php new file mode 100644 index 00000000..c7c73f17 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/searchform.php @@ -0,0 +1,5 @@ +
    +
    + +
    +
    diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/sidebar.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/sidebar.php new file mode 100644 index 00000000..03364b89 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/sidebar.php @@ -0,0 +1,72 @@ + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/single.php b/projects/tests/tests/real/wordpress/wp-content/themes/default/single.php new file mode 100644 index 00000000..1130d2f5 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/single.php @@ -0,0 +1,65 @@ + + +
    + + + + + +
    +

    + +
    + Read the rest of this entry »

    '); ?> + + Pages: ', '

    ', 'number'); ?> + + + +
    +
    + + + + + +

    Sorry, no posts matched your criteria.

    + + + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-content/themes/default/style.css b/projects/tests/tests/real/wordpress/wp-content/themes/default/style.css new file mode 100644 index 00000000..8d5a7304 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-content/themes/default/style.css @@ -0,0 +1,626 @@ +/* +Theme Name: WordPress Default +Theme URI: http://wordpress.org/ +Description: The default WordPress theme based on the famous Kubrick. +Version: 1.5 +Author: Michael Heilemann +Author URI: http://binarybonsai.com/ + + Kubrick v1.5 + http://binarybonsai.com/kubrick/ + + This theme was designed and built by Michael Heilemann, + whose blog you will find at http://binarybonsai.com/ + + The CSS, XHTML and design is released under GPL: + http://www.opensource.org/licenses/gpl-license.php + + + *** REGARDING IMAGES *** + All CSS that involves the use of images, can be found in the 'index.php' file. + This is to ease installation inside subdirectories of a server. + + Have fun, and don't be afraid to contact me if you have questions. +*/ + + + +/* Begin Typography & Colors */ +body { + font-size: 62.5%; /* Resets 1em to 10px */ + font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; + background-color: #d5d6d7; + color: #333; + text-align: center; + } + +#page { + background-color: white; + border: 1px solid #959596; + text-align: left; + } + +#header { + background-color: #73a0c5; + } + +#content { + font-size: 1.2em + } + +.widecolumn .entry p { + font-size: 1.05em; + } + +.narrowcolumn .entry, .widecolumn .entry { + line-height: 1.4em; + } + +.widecolumn { + line-height: 1.6em; + } + +.narrowcolumn .postmetadata { + text-align: center; + } + +.alt { + background-color: #f8f8f8; + border-top: 1px solid #ddd; + border-bottom: 1px solid #ddd; + } + +#footer { + background-color: #eee; + } + +small { + font-family: Arial, Helvetica, Sans-Serif; + font-size: 0.9em; + line-height: 1.5em; + } + +h1, h2, h3 { + font-family: 'Trebuchet MS', 'Lucida Grande', Verdana, Arial, Sans-Serif; + font-weight: bold; + } + +h1 { + font-size: 4em; + text-align: center; + } + +.description { + font-size: 1.2em; + text-align: center; + } + +h2 { + font-size: 1.6em; + } + +h2.pagetitle { + font-size: 1.6em; + } + +#sidebar h2 { + font-family: 'Lucida Grande', Verdana, Sans-Serif; + font-size: 1.2em; + } + +h3 { + font-size: 1.3em; + } + +h1, h1 a, h1 a:hover, h1 a:visited, .description { + text-decoration: none; + color: white; + } + +h2, h2 a, h2 a:visited, h3, h3 a, h3 a:visited { + color: #333; + } + +h2, h2 a, h2 a:hover, h2 a:visited, h3, h3 a, h3 a:hover, h3 a:visited, #sidebar h2, #wp-calendar caption, cite { + text-decoration: none; + } + +.entry p a:visited { + color: #b85b5a; + } + +.commentlist li, #commentform input, #commentform textarea { + font: 0.9em 'Lucida Grande', Verdana, Arial, Sans-Serif; + } + +.commentlist li { + font-weight: bold; + } + +.commentlist cite, .commentlist cite a { + font-weight: bold; + font-style: normal; + font-size: 1.1em; + } + +.commentlist p { + font-weight: normal; + line-height: 1.5em; + text-transform: none; + } + +#commentform p { + font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; + } + +.commentmetadata { + font-weight: normal; + } + +#sidebar { + font: 1em 'Lucida Grande', Verdana, Arial, Sans-Serif; + } + +small, #sidebar ul ul li, #sidebar ul ol li, .nocomments, .postmetadata, blockquote, strike { + color: #777; + } + +code { + font: 1.1em 'Courier New', Courier, Fixed; + } + +acronym, abbr, span.caps +{ + font-size: 0.9em; + letter-spacing: .07em; + } + +a, h2 a:hover, h3 a:hover { + color: #06c; + text-decoration: none; + } + +a:hover { + color: #147; + text-decoration: underline; + } + +#wp-calendar #prev a { + font-size: 9pt; + } + +#wp-calendar a { + text-decoration: none; + } + +#wp-calendar caption { + font: bold 1.3em 'Lucida Grande', Verdana, Arial, Sans-Serif; + text-align: center; + } + +#wp-calendar th { + font-style: normal; + text-transform: capitalize; + } +/* End Typography & Colors */ + + + +/* Begin Structure */ +body { + margin: 0; + padding: 0; + } + +#page { + background-color: white; + margin: 20px auto; + padding: 0; + width: 760px; + border: 1px solid #959596; + } + +#header { + padding: 0; + margin: 0 auto; + height: 200px; + width: 100%; + background-color: #73a0c5; + } + +#headerimg { + margin: 0; + height: 200px; + width: 100%; + } + +.narrowcolumn { + float: left; + padding: 0 0 20px 45px; + margin: 0px 0 0; + width: 450px; + } + +.widecolumn { + padding: 10px 0 20px 0; + margin: 5px 0 0 150px; + width: 450px; + } + +.post { + margin: 0 0 40px; + text-align: justify; + } + +.widecolumn .post { + margin: 0; + } + +.narrowcolumn .postmetadata { + padding-top: 5px; + } + +.widecolumn .postmetadata { + margin: 30px 0; + } + +.widecolumn .smallattachment { + text-align: center; + float: left; + width: 128px; + margin: 5px 5px 5px 0px; +} + +.widecolumn .attachment { + text-align: center; + margin: 5px 0px; +} + +.postmetadata { + clear: left; +} + +#footer { + padding: 0 0 0 1px; + margin: 0 auto; + width: 760px; + clear: both; + } + +#footer p { + margin: 0; + padding: 20px 0; + text-align: center; + } +/* End Structure */ + + + +/* Begin Headers */ +h1 { + padding-top: 70px; + margin: 0; + } + +.description { + text-align: center; + } + +h2 { + margin: 30px 0 0; + } + +h2.pagetitle { + margin-top: 30px; + text-align: center; +} + +#sidebar h2 { + margin: 5px 0 0; + padding: 0; + } + +h3 { + padding: 0; + margin: 30px 0 0; + } + +h3.comments { + padding: 0; + margin: 40px auto 20px ; + } +/* End Headers */ + + + +/* Begin Images */ +p img { + padding: 0; + max-width: 100%; + } + +/* Using 'class="alignright"' on an image will (who would've + thought?!) align the image to the right. And using 'class="centered', + will of course center the image. This is much better than using + align="center", being much more futureproof (and valid) */ + +img.centered { + display: block; + margin-left: auto; + margin-right: auto; + } + +img.alignright { + padding: 4px; + margin: 0 0 2px 7px; + display: inline; + } + +img.alignleft { + padding: 4px; + margin: 0 7px 2px 0; + display: inline; + } + +.alignright { + float: right; + } + +.alignleft { + float: left + } +/* End Images */ + + + +/* Begin Lists + + Special stylized non-IE bullets + Do not work in Internet Explorer, which merely default to normal bullets. */ + +html>body .entry ul { + margin-left: 0px; + padding: 0 0 0 30px; + list-style: none; + padding-left: 10px; + text-indent: -10px; + } + +html>body .entry li { + margin: 7px 0 8px 10px; + } + +.entry ul li:before, #sidebar ul ul li:before { + content: "\00BB \0020"; + } + +.entry ol { + padding: 0 0 0 35px; + margin: 0; + } + +.entry ol li { + margin: 0; + padding: 0; + } + +.postmetadata ul, .postmetadata li { + display: inline; + list-style-type: none; + list-style-image: none; + } + +#sidebar ul, #sidebar ul ol { + margin: 0; + padding: 0; + } + +#sidebar ul li { + list-style-type: none; + list-style-image: none; + margin-bottom: 15px; + } + +#sidebar ul p, #sidebar ul select { + margin: 5px 0 8px; + } + +#sidebar ul ul, #sidebar ul ol { + margin: 5px 0 0 10px; + } + +#sidebar ul ul ul, #sidebar ul ol { + margin: 0 0 0 10px; + } + +ol li, #sidebar ul ol li { + list-style: decimal outside; + } + +#sidebar ul ul li, #sidebar ul ol li { + margin: 3px 0 0; + padding: 0; + } +/* End Entry Lists */ + + + +/* Begin Form Elements */ +#searchform { + margin: 10px auto; + padding: 5px 3px; + text-align: center; + } + +#sidebar #searchform #s { + width: 115px; + padding: 2px; + } + +#sidebar #searchsubmit { + padding: 1px; + } + +.entry form { /* This is mainly for password protected posts, makes them look better. */ + text-align:center; + } + +select { + width: 130px; + } + +#commentform input { + width: 170px; + padding: 2px; + margin: 5px 5px 1px 0; + } + +#commentform textarea { + width: 100%; + padding: 2px; + } + +#commentform #submit { + margin: 0; + float: right; + } +/* End Form Elements */ + + + +/* Begin Comments*/ +.alt { + margin: 0; + padding: 10px; + } + +.commentlist { + padding: 0; + text-align: justify; + } + +.commentlist li { + margin: 15px 0 3px; + padding: 5px 10px 3px; + list-style: none; + } + +.commentlist p { + margin: 10px 5px 10px 0; + } + +#commentform p { + margin: 5px 0; + } + +.nocomments { + text-align: center; + margin: 0; + padding: 0; + } + +.commentmetadata { + margin: 0; + display: block; + } +/* End Comments */ + + + +/* Begin Sidebar */ +#sidebar +{ + padding: 20px 0 10px 0; + margin-left: 545px; + width: 190px; + } + +#sidebar form { + margin: 0; + } +/* End Sidebar */ + + + +/* Begin Calendar */ +#wp-calendar { + empty-cells: show; + margin: 10px auto 0; + width: 155px; + } + +#wp-calendar #next a { + padding-right: 10px; + text-align: right; + } + +#wp-calendar #prev a { + padding-left: 10px; + text-align: left; + } + +#wp-calendar a { + display: block; + } + +#wp-calendar caption { + text-align: center; + width: 100%; + } + +#wp-calendar td { + padding: 3px 0; + text-align: center; + } + +#wp-calendar td.pad:hover { /* Doesn't work in IE */ + background-color: #fff; } +/* End Calendar */ + + + +/* Begin Various Tags & Classes */ +acronym, abbr, span.caps { + cursor: help; + } + +acronym, abbr { + border-bottom: 1px dashed #999; + } + +blockquote { + margin: 15px 30px 0 10px; + padding-left: 20px; + border-left: 5px solid #ddd; + } + +blockquote cite { + margin: 5px 0 0; + display: block; + } + +.center { + text-align: center; + } + +hr { + display: none; + } + +a img { + border: none; + } + +.navigation { + display: block; + text-align: center; + margin-top: 10px; + margin-bottom: 60px; + } +/* End Various Tags & Classes*/ + + + +/* "Daisy, Daisy, give me your answer do. I'm half crazy all for the love of you. + It won't be a stylish marriage, I can't afford a carriage. + But you'll look sweet upon the seat of a bicycle built for two." */ diff --git a/projects/tests/tests/real/wordpress/wp-feed.php b/projects/tests/tests/real/wordpress/wp-feed.php new file mode 100644 index 00000000..93050771 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-feed.php @@ -0,0 +1,37 @@ + diff --git a/projects/tests/tests/real/wordpress/wp-includes/cache.php b/projects/tests/tests/real/wordpress/wp-includes/cache.php new file mode 100644 index 00000000..ecad5c6a --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/cache.php @@ -0,0 +1,373 @@ +add($key, $data, $flag, $expire); +} + +function wp_cache_close() { + global $wp_object_cache; + + return $wp_object_cache->save(); +} + +function wp_cache_delete($id, $flag = '') { + global $wp_object_cache; + + return $wp_object_cache->delete($id, $flag); +} + +function wp_cache_flush() { + global $wp_object_cache; + + return $wp_object_cache->flush(); +} + +function wp_cache_get($id, $flag = '') { + global $wp_object_cache; + + return $wp_object_cache->get($id, $flag); +} + +function wp_cache_init() { + global $wp_object_cache; + + $wp_object_cache = new WP_Object_Cache(); +} + +function wp_cache_replace($key, $data, $flag = '', $expire = 0) { + global $wp_object_cache; + + return $wp_object_cache->replace($key, $data, $flag, $expire); +} + +function wp_cache_set($key, $data, $flag = '', $expire = 0) { + global $wp_object_cache; + + return $wp_object_cache->set($key, $data, $flag, $expire); +} + +define('CACHE_SERIAL_HEADER', ""); + +class WP_Object_Cache { + var $cache_dir; + var $cache_enabled = false; + var $expiration_time = 86400; + var $flock_filename = 'wp_object_cache.lock'; + var $cache = array (); + var $dirty_objects = array (); + var $non_existant_objects = array (); + var $global_groups = array ('users', 'userlogins', 'usermeta'); + var $blog_id; + var $cold_cache_hits = 0; + var $warm_cache_hits = 0; + var $cache_misses = 0; + + function add($id, $data, $group = 'default', $expire = '') { + if (empty ($group)) + $group = 'default'; + + if (false !== $this->get($id, $group, false)) + return false; + + return $this->set($id, $data, $group, $expire); + } + + function delete($id, $group = 'default', $force = false) { + if (empty ($group)) + $group = 'default'; + + if (!$force && false === $this->get($id, $group, false)) + return false; + + unset ($this->cache[$group][$id]); + $this->non_existant_objects[$group][$id] = true; + $this->dirty_objects[$group][] = $id; + return true; + } + + function flush() { + if ( !$this->cache_enabled ) + return; + + $this->rm($this->cache_dir.'*'); + $this->cache = array (); + $this->dirty_objects = array (); + $this->non_existant_objects = array (); + return true; + } + + function get($id, $group = 'default', $count_hits = true) { + if (empty ($group)) + $group = 'default'; + + if (isset ($this->cache[$group][$id])) { + if ($count_hits) + $this->warm_cache_hits += 1; + return $this->cache[$group][$id]; + } + + if (isset ($this->non_existant_objects[$group][$id])) + return false; + + // If caching is not enabled, we have to fall back to pulling from the DB. + if (!$this->cache_enabled) { + if (!isset ($this->cache[$group])) + $this->load_group_from_db($group); + + if (isset ($this->cache[$group][$id])) { + $this->cold_cache_hits += 1; + return $this->cache[$group][$id]; + } + + $this->non_existant_objects[$group][$id] = true; + $this->cache_misses += 1; + return false; + } + + $cache_file = $this->cache_dir.$this->get_group_dir($group)."/".md5($id.DB_PASSWORD).'.php'; + if (!file_exists($cache_file)) { + $this->non_existant_objects[$group][$id] = true; + $this->cache_misses += 1; + return false; + } + + // If the object has expired, remove it from the cache and return false to force + // a refresh. + $now = time(); + if ((filemtime($cache_file) + $this->expiration_time) <= $now) { + $this->cache_misses += 1; + $this->delete($id, $group, true); + return false; + } + + $this->cache[$group][$id] = unserialize(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER))); + if (false === $this->cache[$group][$id]) + $this->cache[$group][$id] = ''; + + $this->cold_cache_hits += 1; + return $this->cache[$group][$id]; + } + + function get_group_dir($group) { + if (false !== array_search($group, $this->global_groups)) + return $group; + + return "{$this->blog_id}/$group"; + } + + function load_group_from_db($group) { + global $wpdb; + + if ('category' == $group) { + $this->cache['category'] = array (); + if ($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")) { + foreach ($dogs as $catt) + $this->cache['category'][$catt->cat_ID] = $catt; + + foreach ($this->cache['category'] as $catt) { + $curcat = $catt->cat_ID; + $fullpath = '/'.$this->cache['category'][$catt->cat_ID]->category_nicename; + while ($this->cache['category'][$curcat]->category_parent != 0) { + $curcat = $this->cache['category'][$curcat]->category_parent; + $fullpath = '/'.$this->cache['category'][$curcat]->category_nicename.$fullpath; + } + $this->cache['category'][$catt->cat_ID]->fullpath = $fullpath; + } + } + } else + if ('options' == $group) { + $wpdb->hide_errors(); + if (!$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'")) { + $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options"); + } + $wpdb->show_errors(); + + if ( ! $options ) + return; + + foreach ($options as $option) { + $this->cache['options'][$option->option_name] = $option->option_value; + } + } + } + + function make_group_dir($group, $perms) { + $group_dir = $this->get_group_dir($group); + $make_dir = ''; + foreach (split('/', $group_dir) as $subdir) { + $make_dir .= "$subdir/"; + if (!file_exists($this->cache_dir.$make_dir)) { + if (! @ mkdir($this->cache_dir.$make_dir)) + break; + @ chmod($this->cache_dir.$make_dir, $perms); + } + + if (!file_exists($this->cache_dir.$make_dir."index.php")) { + @ touch($this->cache_dir.$make_dir."index.php"); + } + } + + return $this->cache_dir."$group_dir/"; + } + + function rm($fileglob) { + if (is_file($fileglob)) { + return @ unlink($fileglob); + } else + if (is_dir($fileglob)) { + $ok = WP_Object_Cache::rm("$fileglob/*"); + if (!$ok) + return false; + return @ rmdir($fileglob); + } else { + $matching = glob($fileglob); + if ($matching === false) + return true; + $rcs = array_map(array ('WP_Object_Cache', 'rm'), $matching); + if (in_array(false, $rcs)) { + return false; + } + } + return true; + } + + function replace($id, $data, $group = 'default', $expire = '') { + if (empty ($group)) + $group = 'default'; + + if (false === $this->get($id, $group, false)) + return false; + + return $this->set($id, $data, $group, $expire); + } + + function set($id, $data, $group = 'default', $expire = '') { + if (empty ($group)) + $group = 'default'; + + if (NULL == $data) + $data = ''; + + $this->cache[$group][$id] = $data; + unset ($this->non_existant_objects[$group][$id]); + $this->dirty_objects[$group][] = $id; + + return true; + } + + function save() { + //$this->stats(); + + if (!$this->cache_enabled) + return; + + if (empty ($this->dirty_objects)) + return; + + // Give the new dirs the same perms as wp-content. + $stat = stat(ABSPATH.'wp-content'); + $dir_perms = $stat['mode'] & 0000777; // Get the permission bits. + + // Make the base cache dir. + if (!file_exists($this->cache_dir)) { + if (! @ mkdir($this->cache_dir)) + return; + @ chmod($this->cache_dir, $dir_perms); + } + + if (!file_exists($this->cache_dir."index.php")) { + @ touch($this->cache_dir."index.php"); + } + + // Acquire a write lock. + $mutex = @fopen($this->cache_dir.$this->flock_filename, 'w'); + if ( false == $mutex) + return; + flock($mutex, LOCK_EX); + + // Loop over dirty objects and save them. + foreach ($this->dirty_objects as $group => $ids) { + $group_dir = $this->make_group_dir($group, $dir_perms); + + $ids = array_unique($ids); + foreach ($ids as $id) { + $cache_file = $group_dir.md5($id.DB_PASSWORD).'.php'; + + // Remove the cache file if the key is not set. + if (!isset ($this->cache[$group][$id])) { + if (file_exists($cache_file)) + unlink($cache_file); + continue; + } + + $temp_file = tempnam($group_dir, 'tmp'); + $serial = CACHE_SERIAL_HEADER.serialize($this->cache[$group][$id]).CACHE_SERIAL_FOOTER; + $fd = @fopen($temp_file, 'w'); + if ( false === $fd ) + continue; + fputs($fd, $serial); + fclose($fd); + if (!@ rename($temp_file, $cache_file)) { + if (@ copy($temp_file, $cache_file)) { + @ unlink($temp_file); + } + } + } + } + + // Release write lock. + flock($mutex, LOCK_UN); + fclose($mutex); + } + + function stats() { + echo "

    "; + echo "Cold Cache Hits: {$this->cold_cache_hits}
    "; + echo "Warm Cache Hits: {$this->warm_cache_hits}
    "; + echo "Cache Misses: {$this->cache_misses}
    "; + echo "

    "; + + foreach ($this->cache as $group => $cache) { + echo "

    "; + echo "Group: $group
    "; + echo "Cache:"; + echo "

    ";
    +			print_r($cache);
    +			echo "
    "; + if (isset ($this->dirty_objects[$group])) { + echo "Dirty Objects:"; + echo "
    ";
    +				print_r(array_unique($this->dirty_objects[$group]));
    +				echo "
    "; + echo "

    "; + } + } + } + + function WP_Object_Cache() { + global $blog_id; + + if (defined('DISABLE_CACHE')) + return; + + if (defined('CACHE_PATH')) + $this->cache_dir = CACHE_PATH; + else + $this->cache_dir = ABSPATH.'wp-content/cache/'; + + if (is_writable($this->cache_dir) && is_dir($this->cache_dir)) { + $this->cache_enabled = true; + } else + if (is_writable(ABSPATH.'wp-content')) { + $this->cache_enabled = true; + } + + if (defined('CACHE_EXPIRATION_TIME')) + $this->expiration_time = CACHE_EXPIRATION_TIME; + + $this->blog_id = md5($blog_id); + } +} +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/capabilities.php b/projects/tests/tests/real/wordpress/wp-includes/capabilities.php new file mode 100644 index 00000000..646c143d --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/capabilities.php @@ -0,0 +1,404 @@ +role_key = $table_prefix . 'user_roles'; + + $this->roles = get_option($this->role_key); + + if ( empty($this->roles) ) + return; + + foreach ($this->roles as $role => $data) { + $this->role_objects[$role] = new WP_Role($role, $this->roles[$role]['capabilities']); + $this->role_names[$role] = $this->roles[$role]['name']; + } + } + + function add_role($role, $display_name, $capabilities = '') { + if ( isset($this->roles[$role]) ) + return; + + $this->roles[$role] = array( + 'name' => $display_name, + 'capabilities' => $capabilities); + update_option($this->role_key, $this->roles); + $this->role_objects[$role] = new WP_Role($role, $capabilities); + $this->role_names[$role] = $display_name; + return $this->role_objects[$role]; + } + + function remove_role($role) { + if ( ! isset($this->role_objects[$role]) ) + return; + + unset($this->role_objects[$role]); + unset($this->role_names[$role]); + unset($this->roles[$role]); + + update_option($this->role_key, $this->roles); + } + + function add_cap($role, $cap, $grant = true) { + $this->roles[$role]['capabilities'][$cap] = $grant; + update_option($this->role_key, $this->roles); + } + + function remove_cap($role, $cap) { + unset($this->roles[$role]['capabilities'][$cap]); + update_option($this->role_key, $this->roles); + } + + function &get_role($role) { + if ( isset($this->role_objects[$role]) ) + return $this->role_objects[$role]; + else + return null; + } + + function get_names() { + return $this->role_names; + } + + function is_role($role) + { + return isset($this->role_names[$role]); + } +} + +class WP_Role { + var $name; + var $capabilities; + + function WP_Role($role, $capabilities) { + $this->name = $role; + $this->capabilities = $capabilities; + } + + function add_cap($cap, $grant = true) { + global $wp_roles; + + $this->capabilities[$cap] = $grant; + $wp_roles->add_cap($this->name, $cap, $grant); + } + + function remove_cap($cap) { + global $wp_roles; + + unset($this->capabilities[$cap]); + $wp_roles->remove_cap($this->name, $cap); + } + + function has_cap($cap) { + $capabilities = apply_filters('role_has_cap', $this->capabilities, $cap, $this->name); + if ( !empty($capabilities[$cap]) ) + return $capabilities[$cap]; + else + return false; + } + +} + +class WP_User { + var $data; + var $id = 0; + var $caps = array(); + var $cap_key; + var $roles = array(); + var $allcaps = array(); + + function WP_User($id, $name = '') { + global $wp_roles, $table_prefix; + + if ( empty($id) && empty($name) ) + return; + + if ( ! is_numeric($id) ) { + $name = $id; + $id = 0; + } + + if ( ! empty($id) ) + $this->data = get_userdata($id); + else + $this->data = get_userdatabylogin($name); + + if ( empty($this->data->ID) ) + return; + + foreach (get_object_vars($this->data) as $key => $value) { + $this->{$key} = $value; + } + + $this->id = $this->ID; + $this->cap_key = $table_prefix . 'capabilities'; + $this->caps = &$this->{$this->cap_key}; + if ( ! is_array($this->caps) ) + $this->caps = array(); + $this->get_role_caps(); + } + + function get_role_caps() { + global $wp_roles; + //Filter out caps that are not role names and assign to $this->roles + if(is_array($this->caps)) + $this->roles = array_filter(array_keys($this->caps), array(&$wp_roles, 'is_role')); + + //Build $allcaps from role caps, overlay user's $caps + $this->allcaps = array(); + foreach($this->roles as $role) { + $role = $wp_roles->get_role($role); + $this->allcaps = array_merge($this->allcaps, $role->capabilities); + } + $this->allcaps = array_merge($this->allcaps, $this->caps); + } + + function add_role($role) { + $this->caps[$role] = true; + update_usermeta($this->id, $this->cap_key, $this->caps); + $this->get_role_caps(); + $this->update_user_level_from_caps(); + } + + function remove_role($role) { + if ( empty($this->roles[$role]) || (count($this->roles) <= 1) ) + return; + unset($this->caps[$role]); + update_usermeta($this->id, $this->cap_key, $this->caps); + $this->get_role_caps(); + } + + function set_role($role) { + foreach($this->roles as $oldrole) + unset($this->caps[$oldrole]); + $this->caps[$role] = true; + $this->roles = array($role => true); + update_usermeta($this->id, $this->cap_key, $this->caps); + $this->get_role_caps(); + $this->update_user_level_from_caps(); + } + + function level_reduction($max, $item) { + if(preg_match('/^level_(10|[0-9])$/i', $item, $matches)) { + $level = intval($matches[1]); + return max($max, $level); + } else { + return $max; + } + } + + function update_user_level_from_caps() { + global $table_prefix; + $this->user_level = array_reduce(array_keys($this->allcaps), array(&$this, 'level_reduction'), 0); + update_usermeta($this->id, $table_prefix.'user_level', $this->user_level); + } + + function add_cap($cap, $grant = true) { + $this->caps[$cap] = $grant; + update_usermeta($this->id, $this->cap_key, $this->caps); + } + + function remove_cap($cap) { + if ( empty($this->caps[$cap]) ) return; + unset($this->caps[$cap]); + update_usermeta($this->id, $this->cap_key, $this->caps); + } + + //has_cap(capability_or_role_name) or + //has_cap('edit_post', post_id) + function has_cap($cap) { + global $wp_roles; + + if ( is_numeric($cap) ) + $cap = $this->translate_level_to_cap($cap); + + $args = array_slice(func_get_args(), 1); + $args = array_merge(array($cap, $this->id), $args); + $caps = call_user_func_array('map_meta_cap', $args); + // Must have ALL requested caps + $capabilities = apply_filters('user_has_cap', $this->allcaps, $caps, $args); + foreach ($caps as $cap) { + //echo "Checking cap $cap
    "; + if(empty($capabilities[$cap]) || !$capabilities[$cap]) + return false; + } + + return true; + } + + function translate_level_to_cap($level) { + return 'level_' . $level; + } + +} + +// Map meta capabilities to primitive capabilities. +function map_meta_cap($cap, $user_id) { + $args = array_slice(func_get_args(), 2); + $caps = array(); + + switch ($cap) { + // edit_post breaks down to edit_posts, edit_published_posts, or + // edit_others_posts + case 'edit_post': + $author_data = get_userdata($user_id); + //echo "post ID: {$args[0]}
    "; + $post = get_post($args[0]); + $post_author_data = get_userdata($post->post_author); + //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "
    "; + // If the user is the author... + if ($user_id == $post_author_data->ID) { + // If the post is published... + if ($post->post_status == 'publish') + $caps[] = 'edit_published_posts'; + else if ($post->post_status == 'static') + $caps[] = 'edit_pages'; + else + // If the post is draft... + $caps[] = 'edit_posts'; + } else { + if ($post->post_status == 'static') { + $caps[] = 'edit_pages'; + break; + } + + // The user is trying to edit someone else's post. + $caps[] = 'edit_others_posts'; + // The post is published, extra cap required. + if ($post->post_status == 'publish') + $caps[] = 'edit_published_posts'; + } + break; + case 'read_post': + $post = get_post($args[0]); + + if ( 'private' != $post->post_status ) { + $caps[] = 'read'; + break; + } + + $author_data = get_userdata($user_id); + $post_author_data = get_userdata($post->post_author); + if ($user_id == $post_author_data->ID) + $caps[] = 'read'; + else + $caps[] = 'read_private_posts'; + break; + default: + // If no meta caps match, return the original cap. + $caps[] = $cap; + } + + return $caps; +} + +// Capability checking wrapper around the global $current_user object. +function current_user_can($capability) { + global $current_user; + + $args = array_slice(func_get_args(), 1); + $args = array_merge(array($capability), $args); + + if ( empty($current_user) ) + return false; + + return call_user_func_array(array(&$current_user, 'has_cap'), $args); +} + +// Convenience wrappers around $wp_roles. +function get_role($role) { + global $wp_roles; + + return $wp_roles->get_role($role); +} + +function add_role($role, $display_name, $capabilities = '') { + global $wp_roles; + + return $wp_roles->add_role($role, $display_name, $capabilities = ''); +} + +function remove_role($role) { + global $wp_roles; + + return $wp_roles->remove_role($role); +} + +// +// These are deprecated. Use current_user_can(). +// + +/* returns true if $user_id can create a new post */ +function user_can_create_post($user_id, $blog_id = 1, $category_id = 'None') { + $author_data = get_userdata($user_id); + return ($author_data->user_level > 1); +} + +/* returns true if $user_id can create a new post */ +function user_can_create_draft($user_id, $blog_id = 1, $category_id = 'None') { + $author_data = get_userdata($user_id); + return ($author_data->user_level >= 1); +} + +/* returns true if $user_id can edit $post_id */ +function user_can_edit_post($user_id, $post_id, $blog_id = 1) { + $author_data = get_userdata($user_id); + $post = get_post($post_id); + $post_author_data = get_userdata($post->post_author); + + if ( (($user_id == $post_author_data->ID) && !($post->post_status == 'publish' && $author_data->user_level < 2)) + || ($author_data->user_level > $post_author_data->user_level) + || ($author_data->user_level >= 10) ) { + return true; + } else { + return false; + } +} + +/* returns true if $user_id can delete $post_id */ +function user_can_delete_post($user_id, $post_id, $blog_id = 1) { + // right now if one can edit, one can delete + return user_can_edit_post($user_id, $post_id, $blog_id); +} + +/* returns true if $user_id can set new posts' dates on $blog_id */ +function user_can_set_post_date($user_id, $blog_id = 1, $category_id = 'None') { + $author_data = get_userdata($user_id); + return (($author_data->user_level > 4) && user_can_create_post($user_id, $blog_id, $category_id)); +} + +/* returns true if $user_id can edit $post_id's date */ +function user_can_edit_post_date($user_id, $post_id, $blog_id = 1) { + $author_data = get_userdata($user_id); + return (($author_data->user_level > 4) && user_can_edit_post($user_id, $post_id, $blog_id)); +} + +/* returns true if $user_id can edit $post_id's comments */ +function user_can_edit_post_comments($user_id, $post_id, $blog_id = 1) { + // right now if one can edit a post, one can edit comments made on it + return user_can_edit_post($user_id, $post_id, $blog_id); +} + +/* returns true if $user_id can delete $post_id's comments */ +function user_can_delete_post_comments($user_id, $post_id, $blog_id = 1) { + // right now if one can edit comments, one can delete comments + return user_can_edit_post_comments($user_id, $post_id, $blog_id); +} + +function user_can_edit_user($user_id, $other_user) { + $user = get_userdata($user_id); + $other = get_userdata($other_user); + if ( $user->user_level > $other->user_level || $user->user_level > 8 || $user->ID == $other->ID ) + return true; + else + return false; +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/class-IXR.php b/projects/tests/tests/real/wordpress/wp-includes/class-IXR.php new file mode 100644 index 00000000..7851b68a --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/class-IXR.php @@ -0,0 +1,815 @@ +data = $data; + if (!$type) { + $type = $this->calculateType(); + } + $this->type = $type; + if ($type == 'struct') { + /* Turn all the values in the array in to new IXR_Value objects */ + foreach ($this->data as $key => $value) { + $this->data[$key] = new IXR_Value($value); + } + } + if ($type == 'array') { + for ($i = 0, $j = count($this->data); $i < $j; $i++) { + $this->data[$i] = new IXR_Value($this->data[$i]); + } + } + } + function calculateType() { + if ($this->data === true || $this->data === false) { + return 'boolean'; + } + if (is_integer($this->data)) { + return 'int'; + } + if (is_double($this->data)) { + return 'double'; + } + // Deal with IXR object types base64 and date + if (is_object($this->data) && is_a($this->data, 'IXR_Date')) { + return 'date'; + } + if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) { + return 'base64'; + } + // If it is a normal PHP object convert it in to a struct + if (is_object($this->data)) { + + $this->data = get_object_vars($this->data); + return 'struct'; + } + if (!is_array($this->data)) { + return 'string'; + } + /* We have an array - is it an array or a struct ? */ + if ($this->isStruct($this->data)) { + return 'struct'; + } else { + return 'array'; + } + } + function getXml() { + /* Return XML for this value */ + switch ($this->type) { + case 'boolean': + return ''.(($this->data) ? '1' : '0').''; + break; + case 'int': + return ''.$this->data.''; + break; + case 'double': + return ''.$this->data.''; + break; + case 'string': + return ''.htmlspecialchars($this->data).''; + break; + case 'array': + $return = ''."\n"; + foreach ($this->data as $item) { + $return .= ' '.$item->getXml()."\n"; + } + $return .= ''; + return $return; + break; + case 'struct': + $return = ''."\n"; + foreach ($this->data as $name => $value) { + $name = htmlspecialchars($name); + $return .= " $name"; + $return .= $value->getXml()."\n"; + } + $return .= ''; + return $return; + break; + case 'date': + case 'base64': + return $this->data->getXml(); + break; + } + return false; + } + function isStruct($array) { + /* Nasty function to check if an array is a struct or not */ + $expected = 0; + foreach ($array as $key => $value) { + if ((string)$key != (string)$expected) { + return true; + } + $expected++; + } + return false; + } +} + + +class IXR_Message { + var $message; + var $messageType; // methodCall / methodResponse / fault + var $faultCode; + var $faultString; + var $methodName; + var $params; + // Current variable stacks + var $_arraystructs = array(); // The stack used to keep track of the current array/struct + var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array + var $_currentStructName = array(); // A stack as well + var $_param; + var $_value; + var $_currentTag; + var $_currentTagContents; + // The XML parser + var $_parser; + function IXR_Message ($message) { + $this->message = $message; + } + function parse() { + // first remove the XML declaration + $this->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message); + if (trim($this->message) == '') { + return false; + } + $this->_parser = xml_parser_create(); + // Set XML parser to take the case of tags in to account + xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false); + // Set XML parser callback functions + xml_set_object($this->_parser, $this); + xml_set_element_handler($this->_parser, 'tag_open', 'tag_close'); + xml_set_character_data_handler($this->_parser, 'cdata'); + if (!xml_parse($this->_parser, $this->message)) { + /* die(sprintf('XML error: %s at line %d', + xml_error_string(xml_get_error_code($this->_parser)), + xml_get_current_line_number($this->_parser))); */ + return false; + } + xml_parser_free($this->_parser); + // Grab the error messages, if any + if ($this->messageType == 'fault') { + $this->faultCode = $this->params[0]['faultCode']; + $this->faultString = $this->params[0]['faultString']; + } + return true; + } + function tag_open($parser, $tag, $attr) { + $this->_currentTagContents = ''; + $this->currentTag = $tag; + switch($tag) { + case 'methodCall': + case 'methodResponse': + case 'fault': + $this->messageType = $tag; + break; + /* Deal with stacks of arrays and structs */ + case 'data': // data is to all intents and puposes more interesting than array + $this->_arraystructstypes[] = 'array'; + $this->_arraystructs[] = array(); + break; + case 'struct': + $this->_arraystructstypes[] = 'struct'; + $this->_arraystructs[] = array(); + break; + } + } + function cdata($parser, $cdata) { + $this->_currentTagContents .= $cdata; + } + function tag_close($parser, $tag) { + $valueFlag = false; + switch($tag) { + case 'int': + case 'i4': + $value = (int) trim($this->_currentTagContents); + $valueFlag = true; + break; + case 'double': + $value = (double) trim($this->_currentTagContents); + $valueFlag = true; + break; + case 'string': + $value = $this->_currentTagContents; + $valueFlag = true; + break; + case 'dateTime.iso8601': + $value = new IXR_Date(trim($this->_currentTagContents)); + // $value = $iso->getTimestamp(); + $valueFlag = true; + break; + case 'value': + // "If no type is indicated, the type is string." + if (trim($this->_currentTagContents) != '') { + $value = (string)$this->_currentTagContents; + $valueFlag = true; + } + break; + case 'boolean': + $value = (boolean) trim($this->_currentTagContents); + $valueFlag = true; + break; + case 'base64': + $value = base64_decode( trim( $this->_currentTagContents ) ); + $valueFlag = true; + break; + /* Deal with stacks of arrays and structs */ + case 'data': + case 'struct': + $value = array_pop($this->_arraystructs); + array_pop($this->_arraystructstypes); + $valueFlag = true; + break; + case 'member': + array_pop($this->_currentStructName); + break; + case 'name': + $this->_currentStructName[] = trim($this->_currentTagContents); + break; + case 'methodName': + $this->methodName = trim($this->_currentTagContents); + break; + } + if ($valueFlag) { + if (count($this->_arraystructs) > 0) { + // Add value to struct or array + if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') { + // Add to struct + $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value; + } else { + // Add to array + $this->_arraystructs[count($this->_arraystructs)-1][] = $value; + } + } else { + // Just add as a paramater + $this->params[] = $value; + } + } + $this->_currentTagContents = ''; + } +} + + +class IXR_Server { + var $data; + var $callbacks = array(); + var $message; + var $capabilities; + function IXR_Server($callbacks = false, $data = false) { + $this->setCapabilities(); + if ($callbacks) { + $this->callbacks = $callbacks; + } + $this->setCallbacks(); + $this->serve($data); + } + function serve($data = false) { + if (!$data) { + global $HTTP_RAW_POST_DATA; + if (!$HTTP_RAW_POST_DATA) { + die('XML-RPC server accepts POST requests only.'); + } + $data = $HTTP_RAW_POST_DATA; + } + $this->message = new IXR_Message($data); + if (!$this->message->parse()) { + $this->error(-32700, 'parse error. not well formed'); + } + if ($this->message->messageType != 'methodCall') { + $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall'); + } + $result = $this->call($this->message->methodName, $this->message->params); + // Is the result an error? + if (is_a($result, 'IXR_Error')) { + $this->error($result); + } + // Encode the result + $r = new IXR_Value($result); + $resultxml = $r->getXml(); + // Create the XML + $xml = << + + + + $resultxml + + + + + +EOD; + // Send it + $this->output($xml); + } + function call($methodname, $args) { + if (!$this->hasMethod($methodname)) { + return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.'); + } + $method = $this->callbacks[$methodname]; + // Perform the callback and send the response + if (count($args) == 1) { + // If only one paramater just send that instead of the whole array + $args = $args[0]; + } + // Are we dealing with a function or a method? + if (substr($method, 0, 5) == 'this:') { + // It's a class method - check it exists + $method = substr($method, 5); + if (!method_exists($this, $method)) { + return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.'); + } + // Call the method + $result = $this->$method($args); + } else { + // It's a function - does it exist? + if (is_array($method)) { + if (!method_exists($method[0], $method[1])) { + return new IXR_Error(-32601, 'server error. requested object method "'.$method[1].'" does not exist.'); + } + } else if (!function_exists($method)) { + return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.'); + } + // Call the function + $result = call_user_func($method, $args); + } + return $result; + } + + function error($error, $message = false) { + // Accepts either an error object or an error code and message + if ($message && !is_object($error)) { + $error = new IXR_Error($error, $message); + } + $this->output($error->getXml()); + } + function output($xml) { + $xml = ''."\n".$xml; + $length = strlen($xml); + header('Connection: close'); + header('Content-Length: '.$length); + header('Content-Type: text/xml'); + header('Date: '.date('r')); + echo $xml; + exit; + } + function hasMethod($method) { + return in_array($method, array_keys($this->callbacks)); + } + function setCapabilities() { + // Initialises capabilities array + $this->capabilities = array( + 'xmlrpc' => array( + 'specUrl' => 'http://www.xmlrpc.com/spec', + 'specVersion' => 1 + ), + 'faults_interop' => array( + 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php', + 'specVersion' => 20010516 + ), + 'system.multicall' => array( + 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208', + 'specVersion' => 1 + ), + ); + } + function getCapabilities($args) { + return $this->capabilities; + } + function setCallbacks() { + $this->callbacks['system.getCapabilities'] = 'this:getCapabilities'; + $this->callbacks['system.listMethods'] = 'this:listMethods'; + $this->callbacks['system.multicall'] = 'this:multiCall'; + } + function listMethods($args) { + // Returns a list of methods - uses array_reverse to ensure user defined + // methods are listed before server defined methods + return array_reverse(array_keys($this->callbacks)); + } + function multiCall($methodcalls) { + // See http://www.xmlrpc.com/discuss/msgReader$1208 + $return = array(); + foreach ($methodcalls as $call) { + $method = $call['methodName']; + $params = $call['params']; + if ($method == 'system.multicall') { + $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden'); + } else { + $result = $this->call($method, $params); + } + if (is_a($result, 'IXR_Error')) { + $return[] = array( + 'faultCode' => $result->code, + 'faultString' => $result->message + ); + } else { + $return[] = array($result); + } + } + return $return; + } +} + +class IXR_Request { + var $method; + var $args; + var $xml; + function IXR_Request($method, $args) { + $this->method = $method; + $this->args = $args; + $this->xml = << + +{$this->method} + + +EOD; + foreach ($this->args as $arg) { + $this->xml .= ''; + $v = new IXR_Value($arg); + $this->xml .= $v->getXml(); + $this->xml .= "\n"; + } + $this->xml .= ''; + } + function getLength() { + return strlen($this->xml); + } + function getXml() { + return $this->xml; + } +} + + +class IXR_Client { + var $server; + var $port; + var $path; + var $useragent; + var $response; + var $message = false; + var $debug = false; + var $timeout; + // Storage place for an error message + var $error = false; + function IXR_Client($server, $path = false, $port = 80, $timeout = false) { + if (!$path) { + // Assume we have been given a URL instead + $bits = parse_url($server); + $this->server = $bits['host']; + $this->port = isset($bits['port']) ? $bits['port'] : 80; + $this->path = isset($bits['path']) ? $bits['path'] : '/'; + // Make absolutely sure we have a path + if (!$this->path) { + $this->path = '/'; + } + } else { + $this->server = $server; + $this->path = $path; + $this->port = $port; + } + $this->useragent = 'Incutio XML-RPC'; + $this->timeout = $timeout; + } + function query() { + $args = func_get_args(); + $method = array_shift($args); + $request = new IXR_Request($method, $args); + $length = $request->getLength(); + $xml = $request->getXml(); + $r = "\r\n"; + $request = "POST {$this->path} HTTP/1.0$r"; + $request .= "Host: {$this->server}$r"; + $request .= "Content-Type: text/xml$r"; + $request .= "User-Agent: {$this->useragent}$r"; + $request .= "Content-length: {$length}$r$r"; + $request .= $xml; + // Now send the request + if ($this->debug) { + echo '
    '.htmlspecialchars($request)."\n
    \n\n"; + } + if ($this->timeout) { + $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout); + } else { + $fp = @fsockopen($this->server, $this->port, $errno, $errstr); + } + if (!$fp) { + $this->error = new IXR_Error(-32300, "transport error - could not open socket: $errno $errstr"); + return false; + } + fputs($fp, $request); + $contents = ''; + $gotFirstLine = false; + $gettingHeaders = true; + while (!feof($fp)) { + $line = fgets($fp, 4096); + if (!$gotFirstLine) { + // Check line for '200' + if (strstr($line, '200') === false) { + $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); + return false; + } + $gotFirstLine = true; + } + if (trim($line) == '') { + $gettingHeaders = false; + } + if (!$gettingHeaders) { + $contents .= trim($line)."\n"; + } + } + if ($this->debug) { + echo '
    '.htmlspecialchars($contents)."\n
    \n\n"; + } + // Now parse what we've got back + $this->message = new IXR_Message($contents); + if (!$this->message->parse()) { + // XML error + $this->error = new IXR_Error(-32700, 'parse error. not well formed'); + return false; + } + // Is the message a fault? + if ($this->message->messageType == 'fault') { + $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); + return false; + } + // Message must be OK + return true; + } + function getResponse() { + // methodResponses can only have one param - return that + return $this->message->params[0]; + } + function isError() { + return (is_object($this->error)); + } + function getErrorCode() { + return $this->error->code; + } + function getErrorMessage() { + return $this->error->message; + } +} + + +class IXR_Error { + var $code; + var $message; + function IXR_Error($code, $message) { + $this->code = $code; + $this->message = $message; + } + function getXml() { + $xml = << + + + + + faultCode + {$this->code} + + + faultString + {$this->message} + + + + + + +EOD; + return $xml; + } +} + + +class IXR_Date { + var $year; + var $month; + var $day; + var $hour; + var $minute; + var $second; + function IXR_Date($time) { + // $time can be a PHP timestamp or an ISO one + if (is_numeric($time)) { + $this->parseTimestamp($time); + } else { + $this->parseIso($time); + } + } + function parseTimestamp($timestamp) { + $this->year = date('Y', $timestamp); + $this->month = date('m', $timestamp); + $this->day = date('d', $timestamp); + $this->hour = date('H', $timestamp); + $this->minute = date('i', $timestamp); + $this->second = date('s', $timestamp); + } + function parseIso($iso) { + $this->year = substr($iso, 0, 4); + $this->month = substr($iso, 4, 2); + $this->day = substr($iso, 6, 2); + $this->hour = substr($iso, 9, 2); + $this->minute = substr($iso, 12, 2); + $this->second = substr($iso, 15, 2); + $this->timezone = substr($iso, 17); + } + function getIso() { + return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second.$this->timezone; + } + function getXml() { + return ''.$this->getIso().''; + } + function getTimestamp() { + return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year); + } +} + + +class IXR_Base64 { + var $data; + function IXR_Base64($data) { + $this->data = $data; + } + function getXml() { + return ''.base64_encode($this->data).''; + } +} + + +class IXR_IntrospectionServer extends IXR_Server { + var $signatures; + var $help; + function IXR_IntrospectionServer() { + $this->setCallbacks(); + $this->setCapabilities(); + $this->capabilities['introspection'] = array( + 'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html', + 'specVersion' => 1 + ); + $this->addCallback( + 'system.methodSignature', + 'this:methodSignature', + array('array', 'string'), + 'Returns an array describing the return type and required parameters of a method' + ); + $this->addCallback( + 'system.getCapabilities', + 'this:getCapabilities', + array('struct'), + 'Returns a struct describing the XML-RPC specifications supported by this server' + ); + $this->addCallback( + 'system.listMethods', + 'this:listMethods', + array('array'), + 'Returns an array of available methods on this server' + ); + $this->addCallback( + 'system.methodHelp', + 'this:methodHelp', + array('string', 'string'), + 'Returns a documentation string for the specified method' + ); + } + function addCallback($method, $callback, $args, $help) { + $this->callbacks[$method] = $callback; + $this->signatures[$method] = $args; + $this->help[$method] = $help; + } + function call($methodname, $args) { + // Make sure it's in an array + if ($args && !is_array($args)) { + $args = array($args); + } + // Over-rides default call method, adds signature check + if (!$this->hasMethod($methodname)) { + return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.'); + } + $method = $this->callbacks[$methodname]; + $signature = $this->signatures[$methodname]; + $returnType = array_shift($signature); + // Check the number of arguments + if (count($args) != count($signature)) { + return new IXR_Error(-32602, 'server error. wrong number of method parameters'); + } + // Check the argument types + $ok = true; + $argsbackup = $args; + for ($i = 0, $j = count($args); $i < $j; $i++) { + $arg = array_shift($args); + $type = array_shift($signature); + switch ($type) { + case 'int': + case 'i4': + if (is_array($arg) || !is_int($arg)) { + $ok = false; + } + break; + case 'base64': + case 'string': + if (!is_string($arg)) { + $ok = false; + } + break; + case 'boolean': + if ($arg !== false && $arg !== true) { + $ok = false; + } + break; + case 'float': + case 'double': + if (!is_float($arg)) { + $ok = false; + } + break; + case 'date': + case 'dateTime.iso8601': + if (!is_a($arg, 'IXR_Date')) { + $ok = false; + } + break; + } + if (!$ok) { + return new IXR_Error(-32602, 'server error. invalid method parameters'); + } + } + // It passed the test - run the "real" method call + return parent::call($methodname, $argsbackup); + } + function methodSignature($method) { + if (!$this->hasMethod($method)) { + return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.'); + } + // We should be returning an array of types + $types = $this->signatures[$method]; + $return = array(); + foreach ($types as $type) { + switch ($type) { + case 'string': + $return[] = 'string'; + break; + case 'int': + case 'i4': + $return[] = 42; + break; + case 'double': + $return[] = 3.1415; + break; + case 'dateTime.iso8601': + $return[] = new IXR_Date(time()); + break; + case 'boolean': + $return[] = true; + break; + case 'base64': + $return[] = new IXR_Base64('base64'); + break; + case 'array': + $return[] = array('array'); + break; + case 'struct': + $return[] = array('struct' => 'struct'); + break; + } + } + return $return; + } + function methodHelp($method) { + return $this->help[$method]; + } +} + + +class IXR_ClientMulticall extends IXR_Client { + var $calls = array(); + function IXR_ClientMulticall($server, $path = false, $port = 80) { + parent::IXR_Client($server, $path, $port); + $this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)'; + } + function addCall() { + $args = func_get_args(); + $methodName = array_shift($args); + $struct = array( + 'methodName' => $methodName, + 'params' => $args + ); + $this->calls[] = $struct; + } + function query() { + // Prepare multicall, then call the parent::query() method + return parent::query('system.multicall', $this->calls); + } +} + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-includes/class-pop3.php b/projects/tests/tests/real/wordpress/wp-includes/class-pop3.php new file mode 100644 index 00000000..468ca8bd --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/class-pop3.php @@ -0,0 +1,680 @@ +BUFFER,"integer"); + if( !empty($server) ) { + // Do not allow programs to alter MAILSERVER + // if it is already specified. They can get around + // this if they -really- want to, so don't count on it. + if(empty($this->MAILSERVER)) + $this->MAILSERVER = $server; + } + if(!empty($timeout)) { + settype($timeout,"integer"); + $this->TIMEOUT = $timeout; + set_time_limit($timeout); + } + return true; + } + + function update_timer () { + set_time_limit($this->TIMEOUT); + return true; + } + + function connect ($server, $port = 110) { + // Opens a socket to the specified server. Unless overridden, + // port defaults to 110. Returns true on success, false on fail + + // If MAILSERVER is set, override $server with it's value + + if(!empty($this->MAILSERVER)) + $server = $this->MAILSERVER; + + if(empty($server)){ + $this->ERROR = _("POP3 connect:") . ' ' . _("No server specified"); + unset($this->FP); + return false; + } + + $fp = fsockopen("$server", $port, $errno, $errstr); + + if(!$fp) { + $this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]"; + unset($this->FP); + return false; + } + + socket_set_blocking($fp,-1); + $this->update_timer(); + $reply = fgets($fp,$this->BUFFER); + $reply = $this->strip_clf($reply); + if($this->DEBUG) + error_log("POP3 SEND [connect: $server] GOT [$reply]",0); + if(!$this->is_ok($reply)) { + $this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$reply]"; + unset($this->FP); + return false; + } + $this->FP = $fp; + $this->BANNER = $this->parse_banner($reply); + $this->RFC1939 = $this->noop(); + if($this->RFC1939) { + $this->ERROR = _("POP3: premature NOOP OK, NOT an RFC 1939 Compliant server"); + $this->quit(); + return false; + } else + return true; + } + + function noop () { + + if(!isset($this->FP)) { + $this->ERROR = _("POP3 noop:") . ' ' . _("No connection to server"); + return false; + } else { + $cmd = "NOOP"; + $reply = $this->send_cmd( $cmd ); + return( $this->is_ok( $reply ) ); + } + } + + function user ($user = "") { + // Sends the USER command, returns true or false + + if( empty($user) ) { + $this->ERROR = _("POP3 user:") . ' ' . _("no login ID submitted"); + return false; + } elseif(!isset($this->FP)) { + $this->ERROR = _("POP3 user:") . ' ' . _("connection not established"); + return false; + } else { + $reply = $this->send_cmd("USER $user"); + if(!$this->is_ok($reply)) { + $this->ERROR = _("POP3 user:") . ' ' . _("Error ") . "[$reply]"; + return false; + } else + return true; + } + } + + function pass ($pass = "") { + // Sends the PASS command, returns # of msgs in mailbox, + // returns false (undef) on Auth failure + + if(empty($pass)) { + $this->ERROR = _("POP3 pass:") . ' ' . _("No password submitted"); + return false; + } elseif(!isset($this->FP)) { + $this->ERROR = _("POP3 pass:") . ' ' . _("connection not established"); + return false; + } else { + $reply = $this->send_cmd("PASS $pass"); + if(!$this->is_ok($reply)) { + $this->ERROR = _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]"; + $this->quit(); + return false; + } else { + // Auth successful. + $count = $this->last("count"); + $this->COUNT = $count; + $this->RFC1939 = $this->noop(); + if(!$this->RFC1939) { + $this->ERROR = _("POP3 pass:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant"); + $this->quit(); + return false; + } else + return $count; + } + } + } + + function apop ($login,$pass) { + // Attempts an APOP login. If this fails, it'll + // try a standard login. YOUR SERVER MUST SUPPORT + // THE USE OF THE APOP COMMAND! + // (apop is optional per rfc1939) + + if(!isset($this->FP)) { + $this->ERROR = _("POP3 apop:") . ' ' . _("No connection to server"); + return false; + } elseif(!$this->ALLOWAPOP) { + $retVal = $this->login($login,$pass); + return $retVal; + } elseif(empty($login)) { + $this->ERROR = _("POP3 apop:") . ' ' . _("No login ID submitted"); + return false; + } elseif(empty($pass)) { + $this->ERROR = _("POP3 apop:") . ' ' . _("No password submitted"); + return false; + } else { + $banner = $this->BANNER; + if( (!$banner) or (empty($banner)) ) { + $this->ERROR = _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort"); + $retVal = $this->login($login,$pass); + return $retVal; + } else { + $AuthString = $banner; + $AuthString .= $pass; + $APOPString = md5($AuthString); + $cmd = "APOP $login $APOPString"; + $reply = $this->send_cmd($cmd); + if(!$this->is_ok($reply)) { + $this->ERROR = _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort"); + $retVal = $this->login($login,$pass); + return $retVal; + } else { + // Auth successful. + $count = $this->last("count"); + $this->COUNT = $count; + $this->RFC1939 = $this->noop(); + if(!$this->RFC1939) { + $this->ERROR = _("POP3 apop:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant"); + $this->quit(); + return false; + } else + return $count; + } + } + } + } + + function login ($login = "", $pass = "") { + // Sends both user and pass. Returns # of msgs in mailbox or + // false on failure (or -1, if the error occurs while getting + // the number of messages.) + + if( !isset($this->FP) ) { + $this->ERROR = _("POP3 login:") . ' ' . _("No connection to server"); + return false; + } else { + $fp = $this->FP; + if( !$this->user( $login ) ) { + // Preserve the error generated by user() + return false; + } else { + $count = $this->pass($pass); + if( (!$count) || ($count == -1) ) { + // Preserve the error generated by last() and pass() + return false; + } else + return $count; + } + } + } + + function top ($msgNum, $numLines = "0") { + // Gets the header and first $numLines of the msg body + // returns data in an array with each returned line being + // an array element. If $numLines is empty, returns + // only the header information, and none of the body. + + if(!isset($this->FP)) { + $this->ERROR = _("POP3 top:") . ' ' . _("No connection to server"); + return false; + } + $this->update_timer(); + + $fp = $this->FP; + $buffer = $this->BUFFER; + $cmd = "TOP $msgNum $numLines"; + fwrite($fp, "TOP $msgNum $numLines\r\n"); + $reply = fgets($fp, $buffer); + $reply = $this->strip_clf($reply); + if($this->DEBUG) { + @error_log("POP3 SEND [$cmd] GOT [$reply]",0); + } + if(!$this->is_ok($reply)) + { + $this->ERROR = _("POP3 top:") . ' ' . _("Error ") . "[$reply]"; + return false; + } + + $count = 0; + $MsgArray = array(); + + $line = fgets($fp,$buffer); + while ( !ereg("^\.\r\n",$line)) + { + $MsgArray[$count] = $line; + $count++; + $line = fgets($fp,$buffer); + if(empty($line)) { break; } + } + + return $MsgArray; + } + + function pop_list ($msgNum = "") { + // If called with an argument, returns that msgs' size in octets + // No argument returns an associative array of undeleted + // msg numbers and their sizes in octets + + if(!isset($this->FP)) + { + $this->ERROR = _("POP3 pop_list:") . ' ' . _("No connection to server"); + return false; + } + $fp = $this->FP; + $Total = $this->COUNT; + if( (!$Total) or ($Total == -1) ) + { + return false; + } + if($Total == 0) + { + return array("0","0"); + // return -1; // mailbox empty + } + + $this->update_timer(); + + if(!empty($msgNum)) + { + $cmd = "LIST $msgNum"; + fwrite($fp,"$cmd\r\n"); + $reply = fgets($fp,$this->BUFFER); + $reply = $this->strip_clf($reply); + if($this->DEBUG) { + @error_log("POP3 SEND [$cmd] GOT [$reply]",0); + } + if(!$this->is_ok($reply)) + { + $this->ERROR = _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]"; + return false; + } + list($junk,$num,$size) = explode(" ",$reply); + return $size; + } + $cmd = "LIST"; + $reply = $this->send_cmd($cmd); + if(!$this->is_ok($reply)) + { + $reply = $this->strip_clf($reply); + $this->ERROR = _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]"; + return false; + } + $MsgArray = array(); + $MsgArray[0] = $Total; + for($msgC=1;$msgC <= $Total; $msgC++) + { + if($msgC > $Total) { break; } + $line = fgets($fp,$this->BUFFER); + $line = $this->strip_clf($line); + if(ereg("^\.",$line)) + { + $this->ERROR = _("POP3 pop_list:") . ' ' . _("Premature end of list"); + return false; + } + list($thisMsg,$msgSize) = explode(" ",$line); + settype($thisMsg,"integer"); + if($thisMsg != $msgC) + { + $MsgArray[$msgC] = "deleted"; + } + else + { + $MsgArray[$msgC] = $msgSize; + } + } + return $MsgArray; + } + + function get ($msgNum) { + // Retrieve the specified msg number. Returns an array + // where each line of the msg is an array element. + + if(!isset($this->FP)) + { + $this->ERROR = _("POP3 get:") . ' ' . _("No connection to server"); + return false; + } + + $this->update_timer(); + + $fp = $this->FP; + $buffer = $this->BUFFER; + $cmd = "RETR $msgNum"; + $reply = $this->send_cmd($cmd); + + if(!$this->is_ok($reply)) + { + $this->ERROR = _("POP3 get:") . ' ' . _("Error ") . "[$reply]"; + return false; + } + + $count = 0; + $MsgArray = array(); + + $line = fgets($fp,$buffer); + while ( !ereg("^\.\r\n",$line)) + { + $MsgArray[$count] = $line; + $count++; + $line = fgets($fp,$buffer); + if(empty($line)) { break; } + } + return $MsgArray; + } + + function last ( $type = "count" ) { + // Returns the highest msg number in the mailbox. + // returns -1 on error, 0+ on success, if type != count + // results in a popstat() call (2 element array returned) + + $last = -1; + if(!isset($this->FP)) + { + $this->ERROR = _("POP3 last:") . ' ' . _("No connection to server"); + return $last; + } + + $reply = $this->send_cmd("STAT"); + if(!$this->is_ok($reply)) + { + $this->ERROR = _("POP3 last:") . ' ' . _("Error ") . "[$reply]"; + return $last; + } + + $Vars = explode(" ",$reply); + $count = $Vars[1]; + $size = $Vars[2]; + settype($count,"integer"); + settype($size,"integer"); + if($type != "count") + { + return array($count,$size); + } + return $count; + } + + function reset () { + // Resets the status of the remote server. This includes + // resetting the status of ALL msgs to not be deleted. + // This method automatically closes the connection to the server. + + if(!isset($this->FP)) + { + $this->ERROR = _("POP3 reset:") . ' ' . _("No connection to server"); + return false; + } + $reply = $this->send_cmd("RSET"); + if(!$this->is_ok($reply)) + { + // The POP3 RSET command -never- gives a -ERR + // response - if it ever does, something truely + // wild is going on. + + $this->ERROR = _("POP3 reset:") . ' ' . _("Error ") . "[$reply]"; + @error_log("POP3 reset: ERROR [$reply]",0); + } + $this->quit(); + return true; + } + + function send_cmd ( $cmd = "" ) + { + // Sends a user defined command string to the + // POP server and returns the results. Useful for + // non-compliant or custom POP servers. + // Do NOT includ the \r\n as part of your command + // string - it will be appended automatically. + + // The return value is a standard fgets() call, which + // will read up to $this->BUFFER bytes of data, until it + // encounters a new line, or EOF, whichever happens first. + + // This method works best if $cmd responds with only + // one line of data. + + if(!isset($this->FP)) + { + $this->ERROR = _("POP3 send_cmd:") . ' ' . _("No connection to server"); + return false; + } + + if(empty($cmd)) + { + $this->ERROR = _("POP3 send_cmd:") . ' ' . _("Empty command string"); + return ""; + } + + $fp = $this->FP; + $buffer = $this->BUFFER; + $this->update_timer(); + fwrite($fp,"$cmd\r\n"); + $reply = fgets($fp,$buffer); + $reply = $this->strip_clf($reply); + if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } + return $reply; + } + + function quit() { + // Closes the connection to the POP3 server, deleting + // any msgs marked as deleted. + + if(!isset($this->FP)) + { + $this->ERROR = _("POP3 quit:") . ' ' . _("connection does not exist"); + return false; + } + $fp = $this->FP; + $cmd = "QUIT"; + fwrite($fp,"$cmd\r\n"); + $reply = fgets($fp,$this->BUFFER); + $reply = $this->strip_clf($reply); + if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } + fclose($fp); + unset($this->FP); + return true; + } + + function popstat () { + // Returns an array of 2 elements. The number of undeleted + // msgs in the mailbox, and the size of the mbox in octets. + + $PopArray = $this->last("array"); + + if($PopArray == -1) { return false; } + + if( (!$PopArray) or (empty($PopArray)) ) + { + return false; + } + return $PopArray; + } + + function uidl ($msgNum = "") + { + // Returns the UIDL of the msg specified. If called with + // no arguments, returns an associative array where each + // undeleted msg num is a key, and the msg's uidl is the element + // Array element 0 will contain the total number of msgs + + if(!isset($this->FP)) { + $this->ERROR = _("POP3 uidl:") . ' ' . _("No connection to server"); + return false; + } + + $fp = $this->FP; + $buffer = $this->BUFFER; + + if(!empty($msgNum)) { + $cmd = "UIDL $msgNum"; + $reply = $this->send_cmd($cmd); + if(!$this->is_ok($reply)) + { + $this->ERROR = _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]"; + return false; + } + list ($ok,$num,$myUidl) = explode(" ",$reply); + return $myUidl; + } else { + $this->update_timer(); + + $UIDLArray = array(); + $Total = $this->COUNT; + $UIDLArray[0] = $Total; + + if ($Total < 1) + { + return $UIDLArray; + } + $cmd = "UIDL"; + fwrite($fp, "UIDL\r\n"); + $reply = fgets($fp, $buffer); + $reply = $this->strip_clf($reply); + if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } + if(!$this->is_ok($reply)) + { + $this->ERROR = _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]"; + return false; + } + + $line = ""; + $count = 1; + $line = fgets($fp,$buffer); + while ( !ereg("^\.\r\n",$line)) { + if(ereg("^\.\r\n",$line)) { + break; + } + list ($msg,$msgUidl) = explode(" ",$line); + $msgUidl = $this->strip_clf($msgUidl); + if($count == $msg) { + $UIDLArray[$msg] = $msgUidl; + } + else + { + $UIDLArray[$count] = 'deleted'; + } + $count++; + $line = fgets($fp,$buffer); + } + } + return $UIDLArray; + } + + function delete ($msgNum = "") { + // Flags a specified msg as deleted. The msg will not + // be deleted until a quit() method is called. + + if(!isset($this->FP)) + { + $this->ERROR = _("POP3 delete:") . ' ' . _("No connection to server"); + return false; + } + if(empty($msgNum)) + { + $this->ERROR = _("POP3 delete:") . ' ' . _("No msg number submitted"); + return false; + } + $reply = $this->send_cmd("DELE $msgNum"); + if(!$this->is_ok($reply)) + { + $this->ERROR = _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]"; + return false; + } + return true; + } + + // ********************************************************* + + // The following methods are internal to the class. + + function is_ok ($cmd = "") { + // Return true or false on +OK or -ERR + + if( empty($cmd) ) + return false; + else + return( ereg ("^\+OK", $cmd ) ); + } + + function strip_clf ($text = "") { + // Strips \r\n from server responses + + if(empty($text)) + return $text; + else { + $stripped = str_replace("\r",'',$text); + $stripped = str_replace("\n",'',$stripped); + return $stripped; + } + } + + function parse_banner ( $server_text ) { + $outside = true; + $banner = ""; + $length = strlen($server_text); + for($count =0; $count < $length; $count++) + { + $digit = substr($server_text, $count, 1); + if ( false !== $digit ) { + if( (!$outside) && ($digit != '<') && ($digit != '>') ) + { + $banner .= $digit; + } + if ($digit == '<') + { + $outside = false; + } + if($digit == '>') + { + $outside = true; + } + } + } + $banner = $this->strip_clf($banner); // Just in case + return "<$banner>"; + } + +} // End class + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/class-snoopy.php b/projects/tests/tests/real/wordpress/wp-includes/class-snoopy.php new file mode 100644 index 00000000..9711a28d --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/class-snoopy.php @@ -0,0 +1,1259 @@ + +Copyright (c): 1999-2000 ispi, all rights reserved +Version: 1.01 + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +You may contact the author of Snoopy by e-mail at: +monte@ispi.net + +Or, write to: +Monte Ohrt +CTO, ispi +237 S. 70th suite 220 +Lincoln, NE 68510 + +The latest version of Snoopy can be obtained from: +http://snoopy.sourceforge.net/ + +*************************************************/ + +if ( !in_array('Snoopy', get_declared_classes() ) ) : +class Snoopy +{ + /**** Public variables ****/ + + /* user definable vars */ + + var $host = "www.php.net"; // host name we are connecting to + var $port = 80; // port we are connecting to + var $proxy_host = ""; // proxy host to use + var $proxy_port = ""; // proxy port to use + var $proxy_user = ""; // proxy user to use + var $proxy_pass = ""; // proxy password to use + + var $agent = "Snoopy v1.2.3"; // agent we masquerade as + var $referer = ""; // referer info to pass + var $cookies = array(); // array of cookies to pass + // $cookies["username"]="joe"; + var $rawheaders = array(); // array of raw headers to send + // $rawheaders["Content-type"]="text/html"; + + var $maxredirs = 5; // http redirection depth maximum. 0 = disallow + var $lastredirectaddr = ""; // contains address of last redirected address + var $offsiteok = true; // allows redirection off-site + var $maxframes = 0; // frame content depth maximum. 0 = disallow + var $expandlinks = true; // expand links to fully qualified URLs. + // this only applies to fetchlinks() + // submitlinks(), and submittext() + var $passcookies = true; // pass set cookies back through redirects + // NOTE: this currently does not respect + // dates, domains or paths. + + var $user = ""; // user for http authentication + var $pass = ""; // password for http authentication + + // http accept types + var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; + + var $results = ""; // where the content is put + + var $error = ""; // error messages sent here + var $response_code = ""; // response code returned from server + var $headers = array(); // headers returned from server sent here + var $maxlength = 500000; // max return data length (body) + var $read_timeout = 0; // timeout on read operations, in seconds + // supported only since PHP 4 Beta 4 + // set to 0 to disallow timeouts + var $timed_out = false; // if a read operation timed out + var $status = 0; // http request status + + var $temp_dir = "/tmp"; // temporary directory that the webserver + // has permission to write to. + // under Windows, this should be C:\temp + + var $curl_path = "/usr/local/bin/curl"; + // Snoopy will use cURL for fetching + // SSL content if a full system path to + // the cURL binary is supplied here. + // set to false if you do not have + // cURL installed. See http://curl.haxx.se + // for details on installing cURL. + // Snoopy does *not* use the cURL + // library functions built into php, + // as these functions are not stable + // as of this Snoopy release. + + /**** Private variables ****/ + + var $_maxlinelen = 4096; // max line length (headers) + + var $_httpmethod = "GET"; // default http request method + var $_httpversion = "HTTP/1.0"; // default http request version + var $_submit_method = "POST"; // default submit method + var $_submit_type = "application/x-www-form-urlencoded"; // default submit type + var $_mime_boundary = ""; // MIME boundary for multipart/form-data submit type + var $_redirectaddr = false; // will be set if page fetched is a redirect + var $_redirectdepth = 0; // increments on an http redirect + var $_frameurls = array(); // frame src urls + var $_framedepth = 0; // increments on frame depth + + var $_isproxy = false; // set if using a proxy server + var $_fp_timeout = 30; // timeout for socket connection + +/*======================================================================*\ + Function: fetch + Purpose: fetch the contents of a web page + (and possibly other protocols in the + future like ftp, nntp, gopher, etc.) + Input: $URI the location of the page to fetch + Output: $this->results the output text from the fetch +\*======================================================================*/ + + function fetch($URI) + { + + //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS); + $URI_PARTS = parse_url($URI); + if (!empty($URI_PARTS["user"])) + $this->user = $URI_PARTS["user"]; + if (!empty($URI_PARTS["pass"])) + $this->pass = $URI_PARTS["pass"]; + if (empty($URI_PARTS["query"])) + $URI_PARTS["query"] = ''; + if (empty($URI_PARTS["path"])) + $URI_PARTS["path"] = ''; + + switch(strtolower($URI_PARTS["scheme"])) + { + case "http": + $this->host = $URI_PARTS["host"]; + if(!empty($URI_PARTS["port"])) + $this->port = $URI_PARTS["port"]; + if($this->_connect($fp)) + { + if($this->_isproxy) + { + // using proxy, send entire URI + $this->_httprequest($URI,$fp,$URI,$this->_httpmethod); + } + else + { + $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : ""); + // no proxy, send only the path + $this->_httprequest($path, $fp, $URI, $this->_httpmethod); + } + + $this->_disconnect($fp); + + if($this->_redirectaddr) + { + /* url was redirected, check if we've hit the max depth */ + if($this->maxredirs > $this->_redirectdepth) + { + // only follow redirect if it's on this site, or offsiteok is true + if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) + { + /* follow the redirect */ + $this->_redirectdepth++; + $this->lastredirectaddr=$this->_redirectaddr; + $this->fetch($this->_redirectaddr); + } + } + } + + if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) + { + $frameurls = $this->_frameurls; + $this->_frameurls = array(); + + while(list(,$frameurl) = each($frameurls)) + { + if($this->_framedepth < $this->maxframes) + { + $this->fetch($frameurl); + $this->_framedepth++; + } + else + break; + } + } + } + else + { + return false; + } + return true; + break; + case "https": + if(!$this->curl_path) + return false; + if(function_exists("is_executable")) + if (!is_executable($this->curl_path)) + return false; + $this->host = $URI_PARTS["host"]; + if(!empty($URI_PARTS["port"])) + $this->port = $URI_PARTS["port"]; + if($this->_isproxy) + { + // using proxy, send entire URI + $this->_httpsrequest($URI,$URI,$this->_httpmethod); + } + else + { + $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : ""); + // no proxy, send only the path + $this->_httpsrequest($path, $URI, $this->_httpmethod); + } + + if($this->_redirectaddr) + { + /* url was redirected, check if we've hit the max depth */ + if($this->maxredirs > $this->_redirectdepth) + { + // only follow redirect if it's on this site, or offsiteok is true + if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) + { + /* follow the redirect */ + $this->_redirectdepth++; + $this->lastredirectaddr=$this->_redirectaddr; + $this->fetch($this->_redirectaddr); + } + } + } + + if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) + { + $frameurls = $this->_frameurls; + $this->_frameurls = array(); + + while(list(,$frameurl) = each($frameurls)) + { + if($this->_framedepth < $this->maxframes) + { + $this->fetch($frameurl); + $this->_framedepth++; + } + else + break; + } + } + return true; + break; + default: + // not a valid protocol + $this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n'; + return false; + break; + } + return true; + } + +/*======================================================================*\ + Function: submit + Purpose: submit an http form + Input: $URI the location to post the data + $formvars the formvars to use. + format: $formvars["var"] = "val"; + $formfiles an array of files to submit + format: $formfiles["var"] = "/dir/filename.ext"; + Output: $this->results the text output from the post +\*======================================================================*/ + + function submit($URI, $formvars="", $formfiles="") + { + unset($postdata); + + $postdata = $this->_prepare_post_body($formvars, $formfiles); + + $URI_PARTS = parse_url($URI); + if (!empty($URI_PARTS["user"])) + $this->user = $URI_PARTS["user"]; + if (!empty($URI_PARTS["pass"])) + $this->pass = $URI_PARTS["pass"]; + if (empty($URI_PARTS["query"])) + $URI_PARTS["query"] = ''; + if (empty($URI_PARTS["path"])) + $URI_PARTS["path"] = ''; + + switch(strtolower($URI_PARTS["scheme"])) + { + case "http": + $this->host = $URI_PARTS["host"]; + if(!empty($URI_PARTS["port"])) + $this->port = $URI_PARTS["port"]; + if($this->_connect($fp)) + { + if($this->_isproxy) + { + // using proxy, send entire URI + $this->_httprequest($URI,$fp,$URI,$this->_submit_method,$this->_submit_type,$postdata); + } + else + { + $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : ""); + // no proxy, send only the path + $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata); + } + + $this->_disconnect($fp); + + if($this->_redirectaddr) + { + /* url was redirected, check if we've hit the max depth */ + if($this->maxredirs > $this->_redirectdepth) + { + if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr)) + $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); + + // only follow redirect if it's on this site, or offsiteok is true + if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) + { + /* follow the redirect */ + $this->_redirectdepth++; + $this->lastredirectaddr=$this->_redirectaddr; + if( strpos( $this->_redirectaddr, "?" ) > 0 ) + $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get + else + $this->submit($this->_redirectaddr,$formvars, $formfiles); + } + } + } + + if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) + { + $frameurls = $this->_frameurls; + $this->_frameurls = array(); + + while(list(,$frameurl) = each($frameurls)) + { + if($this->_framedepth < $this->maxframes) + { + $this->fetch($frameurl); + $this->_framedepth++; + } + else + break; + } + } + + } + else + { + return false; + } + return true; + break; + case "https": + if(!$this->curl_path) + return false; + if(function_exists("is_executable")) + if (!is_executable($this->curl_path)) + return false; + $this->host = $URI_PARTS["host"]; + if(!empty($URI_PARTS["port"])) + $this->port = $URI_PARTS["port"]; + if($this->_isproxy) + { + // using proxy, send entire URI + $this->_httpsrequest($URI, $URI, $this->_submit_method, $this->_submit_type, $postdata); + } + else + { + $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : ""); + // no proxy, send only the path + $this->_httpsrequest($path, $URI, $this->_submit_method, $this->_submit_type, $postdata); + } + + if($this->_redirectaddr) + { + /* url was redirected, check if we've hit the max depth */ + if($this->maxredirs > $this->_redirectdepth) + { + if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr)) + $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); + + // only follow redirect if it's on this site, or offsiteok is true + if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) + { + /* follow the redirect */ + $this->_redirectdepth++; + $this->lastredirectaddr=$this->_redirectaddr; + if( strpos( $this->_redirectaddr, "?" ) > 0 ) + $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get + else + $this->submit($this->_redirectaddr,$formvars, $formfiles); + } + } + } + + if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) + { + $frameurls = $this->_frameurls; + $this->_frameurls = array(); + + while(list(,$frameurl) = each($frameurls)) + { + if($this->_framedepth < $this->maxframes) + { + $this->fetch($frameurl); + $this->_framedepth++; + } + else + break; + } + } + return true; + break; + + default: + // not a valid protocol + $this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n'; + return false; + break; + } + return true; + } + +/*======================================================================*\ + Function: fetchlinks + Purpose: fetch the links from a web page + Input: $URI where you are fetching from + Output: $this->results an array of the URLs +\*======================================================================*/ + + function fetchlinks($URI) + { + if ($this->fetch($URI)) + { + if($this->lastredirectaddr) + $URI = $this->lastredirectaddr; + if(is_array($this->results)) + { + for($x=0;$xresults);$x++) + $this->results[$x] = $this->_striplinks($this->results[$x]); + } + else + $this->results = $this->_striplinks($this->results); + + if($this->expandlinks) + $this->results = $this->_expandlinks($this->results, $URI); + return true; + } + else + return false; + } + +/*======================================================================*\ + Function: fetchform + Purpose: fetch the form elements from a web page + Input: $URI where you are fetching from + Output: $this->results the resulting html form +\*======================================================================*/ + + function fetchform($URI) + { + + if ($this->fetch($URI)) + { + + if(is_array($this->results)) + { + for($x=0;$xresults);$x++) + $this->results[$x] = $this->_stripform($this->results[$x]); + } + else + $this->results = $this->_stripform($this->results); + + return true; + } + else + return false; + } + + +/*======================================================================*\ + Function: fetchtext + Purpose: fetch the text from a web page, stripping the links + Input: $URI where you are fetching from + Output: $this->results the text from the web page +\*======================================================================*/ + + function fetchtext($URI) + { + if($this->fetch($URI)) + { + if(is_array($this->results)) + { + for($x=0;$xresults);$x++) + $this->results[$x] = $this->_striptext($this->results[$x]); + } + else + $this->results = $this->_striptext($this->results); + return true; + } + else + return false; + } + +/*======================================================================*\ + Function: submitlinks + Purpose: grab links from a form submission + Input: $URI where you are submitting from + Output: $this->results an array of the links from the post +\*======================================================================*/ + + function submitlinks($URI, $formvars="", $formfiles="") + { + if($this->submit($URI,$formvars, $formfiles)) + { + if($this->lastredirectaddr) + $URI = $this->lastredirectaddr; + if(is_array($this->results)) + { + for($x=0;$xresults);$x++) + { + $this->results[$x] = $this->_striplinks($this->results[$x]); + if($this->expandlinks) + $this->results[$x] = $this->_expandlinks($this->results[$x],$URI); + } + } + else + { + $this->results = $this->_striplinks($this->results); + if($this->expandlinks) + $this->results = $this->_expandlinks($this->results,$URI); + } + return true; + } + else + return false; + } + +/*======================================================================*\ + Function: submittext + Purpose: grab text from a form submission + Input: $URI where you are submitting from + Output: $this->results the text from the web page +\*======================================================================*/ + + function submittext($URI, $formvars = "", $formfiles = "") + { + if($this->submit($URI,$formvars, $formfiles)) + { + if($this->lastredirectaddr) + $URI = $this->lastredirectaddr; + if(is_array($this->results)) + { + for($x=0;$xresults);$x++) + { + $this->results[$x] = $this->_striptext($this->results[$x]); + if($this->expandlinks) + $this->results[$x] = $this->_expandlinks($this->results[$x],$URI); + } + } + else + { + $this->results = $this->_striptext($this->results); + if($this->expandlinks) + $this->results = $this->_expandlinks($this->results,$URI); + } + return true; + } + else + return false; + } + + + +/*======================================================================*\ + Function: set_submit_multipart + Purpose: Set the form submission content type to + multipart/form-data +\*======================================================================*/ + function set_submit_multipart() + { + $this->_submit_type = "multipart/form-data"; + } + + +/*======================================================================*\ + Function: set_submit_normal + Purpose: Set the form submission content type to + application/x-www-form-urlencoded +\*======================================================================*/ + function set_submit_normal() + { + $this->_submit_type = "application/x-www-form-urlencoded"; + } + + + + +/*======================================================================*\ + Private functions +\*======================================================================*/ + + +/*======================================================================*\ + Function: _striplinks + Purpose: strip the hyperlinks from an html document + Input: $document document to strip. + Output: $match an array of the links +\*======================================================================*/ + + function _striplinks($document) + { + preg_match_all("'<\s*a\s.*?href\s*=\s* # find ]+)) # if quote found, match up to next matching + # quote, otherwise match up to next space + 'isx",$document,$links); + + + // catenate the non-empty matches from the conditional subpattern + + while(list($key,$val) = each($links[2])) + { + if(!empty($val)) + $match[] = $val; + } + + while(list($key,$val) = each($links[3])) + { + if(!empty($val)) + $match[] = $val; + } + + // return the links + return $match; + } + +/*======================================================================*\ + Function: _stripform + Purpose: strip the form elements from an html document + Input: $document document to strip. + Output: $match an array of the links +\*======================================================================*/ + + function _stripform($document) + { + preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements); + + // catenate the matches + $match = implode("\r\n",$elements[0]); + + // return the links + return $match; + } + + + +/*======================================================================*\ + Function: _striptext + Purpose: strip the text from an html document + Input: $document document to strip. + Output: $text the resulting text +\*======================================================================*/ + + function _striptext($document) + { + + // I didn't use preg eval (//e) since that is only available in PHP 4.0. + // so, list your entities one by one here. I included some of the + // more common ones. + + $search = array("']*?>.*?'si", // strip out javascript + "'<[\/\!]*?[^<>]*?>'si", // strip out html tags + "'([\r\n])[\s]+'", // strip out white space + "'&(quot|#34|#034|#x22);'i", // replace html entities + "'&(amp|#38|#038|#x26);'i", // added hexadecimal values + "'&(lt|#60|#060|#x3c);'i", + "'&(gt|#62|#062|#x3e);'i", + "'&(nbsp|#160|#xa0);'i", + "'&(iexcl|#161);'i", + "'&(cent|#162);'i", + "'&(pound|#163);'i", + "'&(copy|#169);'i", + "'&(reg|#174);'i", + "'&(deg|#176);'i", + "'&(#39|#039|#x27);'", + "'&(euro|#8364);'i", // europe + "'&a(uml|UML);'", // german + "'&o(uml|UML);'", + "'&u(uml|UML);'", + "'&A(uml|UML);'", + "'&O(uml|UML);'", + "'&U(uml|UML);'", + "'ß'i", + ); + $replace = array( "", + "", + "\\1", + "\"", + "&", + "<", + ">", + " ", + chr(161), + chr(162), + chr(163), + chr(169), + chr(174), + chr(176), + chr(39), + chr(128), + "ä", + "ö", + "ü", + "Ä", + "Ö", + "Ü", + "ß", + ); + + $text = preg_replace($search,$replace,$document); + + return $text; + } + +/*======================================================================*\ + Function: _expandlinks + Purpose: expand each link into a fully qualified URL + Input: $links the links to qualify + $URI the full URI to get the base from + Output: $expandedLinks the expanded links +\*======================================================================*/ + + function _expandlinks($links,$URI) + { + + preg_match("/^[^\?]+/",$URI,$match); + + $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]); + $match = preg_replace("|/$|","",$match); + $match_part = parse_url($match); + $match_root = + $match_part["scheme"]."://".$match_part["host"]; + + $search = array( "|^http://".preg_quote($this->host)."|i", + "|^(\/)|i", + "|^(?!http://)(?!mailto:)|i", + "|/\./|", + "|/[^\/]+/\.\./|" + ); + + $replace = array( "", + $match_root."/", + $match."/", + "/", + "/" + ); + + $expandedLinks = preg_replace($search,$replace,$links); + + return $expandedLinks; + } + +/*======================================================================*\ + Function: _httprequest + Purpose: go get the http data from the server + Input: $url the url to fetch + $fp the current open file pointer + $URI the full URI + $body body contents to send if any (POST) + Output: +\*======================================================================*/ + + function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="") + { + $cookie_headers = ''; + if($this->passcookies && $this->_redirectaddr) + $this->setcookies(); + + $URI_PARTS = parse_url($URI); + if(empty($url)) + $url = "/"; + $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; + if(!empty($this->agent)) + $headers .= "User-Agent: ".$this->agent."\r\n"; + if(!empty($this->host) && !isset($this->rawheaders['Host'])) { + $headers .= "Host: ".$this->host; + if(!empty($this->port)) + $headers .= ":".$this->port; + $headers .= "\r\n"; + } + if(!empty($this->accept)) + $headers .= "Accept: ".$this->accept."\r\n"; + if(!empty($this->referer)) + $headers .= "Referer: ".$this->referer."\r\n"; + if(!empty($this->cookies)) + { + if(!is_array($this->cookies)) + $this->cookies = (array)$this->cookies; + + reset($this->cookies); + if ( count($this->cookies) > 0 ) { + $cookie_headers .= 'Cookie: '; + foreach ( $this->cookies as $cookieKey => $cookieVal ) { + $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; "; + } + $headers .= substr($cookie_headers,0,-2) . "\r\n"; + } + } + if(!empty($this->rawheaders)) + { + if(!is_array($this->rawheaders)) + $this->rawheaders = (array)$this->rawheaders; + while(list($headerKey,$headerVal) = each($this->rawheaders)) + $headers .= $headerKey.": ".$headerVal."\r\n"; + } + if(!empty($content_type)) { + $headers .= "Content-type: $content_type"; + if ($content_type == "multipart/form-data") + $headers .= "; boundary=".$this->_mime_boundary; + $headers .= "\r\n"; + } + if(!empty($body)) + $headers .= "Content-length: ".strlen($body)."\r\n"; + if(!empty($this->user) || !empty($this->pass)) + $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n"; + + //add proxy auth headers + if(!empty($this->proxy_user)) + $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n"; + + + $headers .= "\r\n"; + + // set the read timeout if needed + if ($this->read_timeout > 0) + socket_set_timeout($fp, $this->read_timeout); + $this->timed_out = false; + + fwrite($fp,$headers.$body,strlen($headers.$body)); + + $this->_redirectaddr = false; + unset($this->headers); + + while($currentHeader = fgets($fp,$this->_maxlinelen)) + { + if ($this->read_timeout > 0 && $this->_check_timeout($fp)) + { + $this->status=-100; + return false; + } + + if($currentHeader == "\r\n") + break; + + // if a header begins with Location: or URI:, set the redirect + if(preg_match("/^(Location:|URI:)/i",$currentHeader)) + { + // get URL portion of the redirect + preg_match("/^(Location:|URI:)[ ]+(.*)/i",chop($currentHeader),$matches); + // look for :// in the Location header to see if hostname is included + if(!preg_match("|\:\/\/|",$matches[2])) + { + // no host in the path, so prepend + $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port; + // eliminate double slash + if(!preg_match("|^/|",$matches[2])) + $this->_redirectaddr .= "/".$matches[2]; + else + $this->_redirectaddr .= $matches[2]; + } + else + $this->_redirectaddr = $matches[2]; + } + + if(preg_match("|^HTTP/|",$currentHeader)) + { + if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status)) + { + $this->status= $status[1]; + } + $this->response_code = $currentHeader; + } + + $this->headers[] = $currentHeader; + } + + $results = ''; + do { + $_data = fread($fp, $this->maxlength); + if (strlen($_data) == 0) { + break; + } + $results .= $_data; + } while(true); + + if ($this->read_timeout > 0 && $this->_check_timeout($fp)) + { + $this->status=-100; + return false; + } + + // check if there is a a redirect meta tag + + if(preg_match("']*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) + + { + $this->_redirectaddr = $this->_expandlinks($match[1],$URI); + } + + // have we hit our frame depth and is there frame src to fetch? + if(($this->_framedepth < $this->maxframes) && preg_match_all("']+)'i",$results,$match)) + { + $this->results[] = $results; + for($x=0; $x_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host); + } + // have we already fetched framed content? + elseif(is_array($this->results)) + $this->results[] = $results; + // no framed content + else + $this->results = $results; + + return true; + } + +/*======================================================================*\ + Function: _httpsrequest + Purpose: go get the https data from the server using curl + Input: $url the url to fetch + $URI the full URI + $body body contents to send if any (POST) + Output: +\*======================================================================*/ + + function _httpsrequest($url,$URI,$http_method,$content_type="",$body="") + { + if($this->passcookies && $this->_redirectaddr) + $this->setcookies(); + + $headers = array(); + + $URI_PARTS = parse_url($URI); + if(empty($url)) + $url = "/"; + // GET ... header not needed for curl + //$headers[] = $http_method." ".$url." ".$this->_httpversion; + if(!empty($this->agent)) + $headers[] = "User-Agent: ".$this->agent; + if(!empty($this->host)) + if(!empty($this->port)) + $headers[] = "Host: ".$this->host.":".$this->port; + else + $headers[] = "Host: ".$this->host; + if(!empty($this->accept)) + $headers[] = "Accept: ".$this->accept; + if(!empty($this->referer)) + $headers[] = "Referer: ".$this->referer; + if(!empty($this->cookies)) + { + if(!is_array($this->cookies)) + $this->cookies = (array)$this->cookies; + + reset($this->cookies); + if ( count($this->cookies) > 0 ) { + $cookie_str = 'Cookie: '; + foreach ( $this->cookies as $cookieKey => $cookieVal ) { + $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; "; + } + $headers[] = substr($cookie_str,0,-2); + } + } + if(!empty($this->rawheaders)) + { + if(!is_array($this->rawheaders)) + $this->rawheaders = (array)$this->rawheaders; + while(list($headerKey,$headerVal) = each($this->rawheaders)) + $headers[] = $headerKey.": ".$headerVal; + } + if(!empty($content_type)) { + if ($content_type == "multipart/form-data") + $headers[] = "Content-type: $content_type; boundary=".$this->_mime_boundary; + else + $headers[] = "Content-type: $content_type"; + } + if(!empty($body)) + $headers[] = "Content-length: ".strlen($body); + if(!empty($this->user) || !empty($this->pass)) + $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass); + + for($curr_header = 0; $curr_header < count($headers); $curr_header++) { + $safer_header = strtr( $headers[$curr_header], "\"", " " ); + $cmdline_params .= " -H \"".$safer_header."\""; + } + + if(!empty($body)) + $cmdline_params .= " -d \"$body\""; + + if($this->read_timeout > 0) + $cmdline_params .= " -m ".$this->read_timeout; + + $headerfile = tempnam($temp_dir, "sno"); + + $safer_URI = strtr( $URI, "\"", " " ); // strip quotes from the URI to avoid shell access + exec(escapeshellcmd($this->curl_path." -D \"$headerfile\"".$cmdline_params." \"".$safer_URI."\""),$results,$return); + + if($return) + { + $this->error = "Error: cURL could not retrieve the document, error $return."; + return false; + } + + + $results = implode("\r\n",$results); + + $result_headers = file("$headerfile"); + + $this->_redirectaddr = false; + unset($this->headers); + + for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++) + { + + // if a header begins with Location: or URI:, set the redirect + if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader])) + { + // get URL portion of the redirect + preg_match("/^(Location: |URI:)\s+(.*)/",chop($result_headers[$currentHeader]),$matches); + // look for :// in the Location header to see if hostname is included + if(!preg_match("|\:\/\/|",$matches[2])) + { + // no host in the path, so prepend + $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port; + // eliminate double slash + if(!preg_match("|^/|",$matches[2])) + $this->_redirectaddr .= "/".$matches[2]; + else + $this->_redirectaddr .= $matches[2]; + } + else + $this->_redirectaddr = $matches[2]; + } + + if(preg_match("|^HTTP/|",$result_headers[$currentHeader])) + $this->response_code = $result_headers[$currentHeader]; + + $this->headers[] = $result_headers[$currentHeader]; + } + + // check if there is a a redirect meta tag + + if(preg_match("']*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) + { + $this->_redirectaddr = $this->_expandlinks($match[1],$URI); + } + + // have we hit our frame depth and is there frame src to fetch? + if(($this->_framedepth < $this->maxframes) && preg_match_all("']+)'i",$results,$match)) + { + $this->results[] = $results; + for($x=0; $x_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host); + } + // have we already fetched framed content? + elseif(is_array($this->results)) + $this->results[] = $results; + // no framed content + else + $this->results = $results; + + unlink("$headerfile"); + + return true; + } + +/*======================================================================*\ + Function: setcookies() + Purpose: set cookies for a redirection +\*======================================================================*/ + + function setcookies() + { + for($x=0; $xheaders); $x++) + { + if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers[$x],$match)) + $this->cookies[$match[1]] = urldecode($match[2]); + } + } + + +/*======================================================================*\ + Function: _check_timeout + Purpose: checks whether timeout has occurred + Input: $fp file pointer +\*======================================================================*/ + + function _check_timeout($fp) + { + if ($this->read_timeout > 0) { + $fp_status = socket_get_status($fp); + if ($fp_status["timed_out"]) { + $this->timed_out = true; + return true; + } + } + return false; + } + +/*======================================================================*\ + Function: _connect + Purpose: make a socket connection + Input: $fp file pointer +\*======================================================================*/ + + function _connect(&$fp) + { + if(!empty($this->proxy_host) && !empty($this->proxy_port)) + { + $this->_isproxy = true; + + $host = $this->proxy_host; + $port = $this->proxy_port; + } + else + { + $host = $this->host; + $port = $this->port; + } + + $this->status = 0; + + if($fp = fsockopen( + $host, + $port, + $errno, + $errstr, + $this->_fp_timeout + )) + { + // socket connection succeeded + + return true; + } + else + { + // socket connection failed + $this->status = $errno; + switch($errno) + { + case -3: + $this->error="socket creation failed (-3)"; + case -4: + $this->error="dns lookup failure (-4)"; + case -5: + $this->error="connection refused or timed out (-5)"; + default: + $this->error="connection failed (".$errno.")"; + } + return false; + } + } +/*======================================================================*\ + Function: _disconnect + Purpose: disconnect a socket connection + Input: $fp file pointer +\*======================================================================*/ + + function _disconnect($fp) + { + return(fclose($fp)); + } + + +/*======================================================================*\ + Function: _prepare_post_body + Purpose: Prepare post body according to encoding type + Input: $formvars - form variables + $formfiles - form upload files + Output: post body +\*======================================================================*/ + + function _prepare_post_body($formvars, $formfiles) + { + settype($formvars, "array"); + settype($formfiles, "array"); + $postdata = ''; + + if (count($formvars) == 0 && count($formfiles) == 0) + return; + + switch ($this->_submit_type) { + case "application/x-www-form-urlencoded": + reset($formvars); + while(list($key,$val) = each($formvars)) { + if (is_array($val) || is_object($val)) { + while (list($cur_key, $cur_val) = each($val)) { + $postdata .= urlencode($key)."[]=".urlencode($cur_val)."&"; + } + } else + $postdata .= urlencode($key)."=".urlencode($val)."&"; + } + break; + + case "multipart/form-data": + $this->_mime_boundary = "Snoopy".md5(uniqid(microtime())); + + reset($formvars); + while(list($key,$val) = each($formvars)) { + if (is_array($val) || is_object($val)) { + while (list($cur_key, $cur_val) = each($val)) { + $postdata .= "--".$this->_mime_boundary."\r\n"; + $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n"; + $postdata .= "$cur_val\r\n"; + } + } else { + $postdata .= "--".$this->_mime_boundary."\r\n"; + $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n"; + $postdata .= "$val\r\n"; + } + } + + reset($formfiles); + while (list($field_name, $file_names) = each($formfiles)) { + settype($file_names, "array"); + while (list(, $file_name) = each($file_names)) { + if (!is_readable($file_name)) continue; + + $fp = fopen($file_name, "r"); + $file_content = fread($fp, filesize($file_name)); + fclose($fp); + $base_name = basename($file_name); + + $postdata .= "--".$this->_mime_boundary."\r\n"; + $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n"; + $postdata .= "$file_content\r\n"; + } + } + $postdata .= "--".$this->_mime_boundary."--\r\n"; + break; + } + + return $postdata; + } +} +endif; + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/classes.php b/projects/tests/tests/real/wordpress/wp-includes/classes.php new file mode 100644 index 00000000..3cf1dffb --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/classes.php @@ -0,0 +1,1676 @@ +is_single = false; + $this->is_page = false; + $this->is_archive = false; + $this->is_date = false; + $this->is_year = false; + $this->is_month = false; + $this->is_day = false; + $this->is_time = false; + $this->is_author = false; + $this->is_category = false; + $this->is_search = false; + $this->is_feed = false; + $this->is_trackback = false; + $this->is_home = false; + $this->is_404 = false; + $this->is_paged = false; + $this->is_admin = false; + $this->is_attachment = false; + } + + function init () { + unset($this->posts); + unset($this->query); + unset($this->query_vars); + unset($this->queried_object); + unset($this->queried_object_id); + $this->post_count = 0; + $this->current_post = -1; + $this->in_the_loop = false; + + $this->init_query_flags(); + } + + // Reparse the query vars. + function parse_query_vars() { + $this->parse_query(''); + } + + // Parse a query string and set query type booleans. + function parse_query ($query) { + if ( !empty($query) || !isset($this->query) ) { + $this->init(); + parse_str($query, $qv); + $this->query = $query; + $this->query_vars = $qv; + } + + if ('404' == $qv['error']) { + $this->is_404 = true; + if ( !empty($query) ) { + do_action('parse_query', array(&$this)); + } + return; + } + + $qv['m'] = (int) $qv['m']; + $qv['p'] = (int) $qv['p']; + + // Compat. Map subpost to attachment. + if ( '' != $qv['subpost'] ) + $qv['attachment'] = $qv['subpost']; + if ( '' != $qv['subpost_id'] ) + $qv['attachment_id'] = $qv['subpost_id']; + + if ( ('' != $qv['attachment']) || (int) $qv['attachment_id'] ) { + $this->is_single = true; + $this->is_attachment = true; + } elseif ('' != $qv['name']) { + $this->is_single = true; + } elseif ( $qv['p'] ) { + $this->is_single = true; + } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) { + // If year, month, day, hour, minute, and second are set, a single + // post is being queried. + $this->is_single = true; + } elseif ('' != $qv['static'] || '' != $qv['pagename'] || '' != $qv['page_id']) { + $this->is_page = true; + $this->is_single = false; + } elseif (!empty($qv['s'])) { + $this->is_search = true; + switch ($qv['show_post_type']) { + case 'page' : + $this->is_page = true; + break; + case 'attachment' : + $this->is_attachment = true; + break; + } + } else { + // Look for archive queries. Dates, categories, authors. + + if ( (int) $qv['second']) { + $this->is_time = true; + $this->is_date = true; + } + + if ( (int) $qv['minute']) { + $this->is_time = true; + $this->is_date = true; + } + + if ( (int) $qv['hour']) { + $this->is_time = true; + $this->is_date = true; + } + + if ( (int) $qv['day']) { + if (! $this->is_date) { + $this->is_day = true; + $this->is_date = true; + } + } + + if ( (int) $qv['monthnum']) { + if (! $this->is_date) { + $this->is_month = true; + $this->is_date = true; + } + } + + if ( (int) $qv['year']) { + if (! $this->is_date) { + $this->is_year = true; + $this->is_date = true; + } + } + + if ( (int) $qv['m']) { + $this->is_date = true; + if (strlen($qv['m']) > 9) { + $this->is_time = true; + } else if (strlen($qv['m']) > 7) { + $this->is_day = true; + } else if (strlen($qv['m']) > 5) { + $this->is_month = true; + } else { + $this->is_year = true; + } + } + + if ('' != $qv['w']) { + $this->is_date = true; + } + + if (empty($qv['cat']) || ($qv['cat'] == '0')) { + $this->is_category = false; + } else { + if (stristr($qv['cat'],'-')) { + $this->is_category = false; + } else { + $this->is_category = true; + } + } + + if ('' != $qv['category_name']) { + $this->is_category = true; + } + + if ((empty($qv['author'])) || ($qv['author'] == '0')) { + $this->is_author = false; + } else { + $this->is_author = true; + } + + if ('' != $qv['author_name']) { + $this->is_author = true; + } + + if ( ($this->is_date || $this->is_author || $this->is_category)) { + $this->is_archive = true; + } + + if ( 'attachment' == $qv['show_post_type'] ) { + $this->is_attachment = true; + } + } + + if ('' != $qv['feed']) { + $this->is_feed = true; + } + + if ('' != $qv['tb']) { + $this->is_trackback = true; + } + + if ('' != $qv['paged']) { + $this->is_paged = true; + } + + if ('' != $qv['comments_popup']) { + $this->is_comments_popup = true; + } + + if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { + $this->is_admin = true; + } + + if ( ! ($this->is_attachment || $this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) { + $this->is_home = true; + } + + if ( !empty($query) ) { + do_action('parse_query', array(&$this)); + } + } + + function set_404() { + $this->init_query_flags(); + $this->is_404 = true; + } + + function get($query_var) { + if (isset($this->query_vars[$query_var])) { + return $this->query_vars[$query_var]; + } + + return ''; + } + + function set($query_var, $value) { + $this->query_vars[$query_var] = $value; + } + + function &get_posts() { + global $wpdb, $pagenow, $request, $user_ID; + + do_action('pre_get_posts', array(&$this)); + + // Shorthand. + $q = $this->query_vars; + + // First let's clear some variables + $whichcat = ''; + $whichauthor = ''; + $whichpage = ''; + $result = ''; + $where = ''; + $limits = ''; + $distinct = ''; + $join = ''; + + if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 ) + $q['posts_per_page'] = get_settings('posts_per_page'); + if ( !isset($q['what_to_show']) ) + $q['what_to_show'] = get_settings('what_to_show'); + if ( isset($q['showposts']) && $q['showposts'] ) { + $q['showposts'] = (int) $q['showposts']; + $q['posts_per_page'] = $q['showposts']; + } + if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) + $q['posts_per_page'] = $q['posts_per_archive_page']; + if ( !isset($q['nopaging']) ) { + if ($q['posts_per_page'] == -1) { + $q['nopaging'] = true; + } else { + $q['nopaging'] = false; + } + } + if ( $this->is_feed ) { + $q['posts_per_page'] = get_settings('posts_per_rss'); + $q['what_to_show'] = 'posts'; + } + + if (isset($q['page'])) { + $q['page'] = trim($q['page'], '/'); + $q['page'] = (int) $q['page']; + } + + $add_hours = intval(get_settings('gmt_offset')); + $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours)); + $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"; + + // If a month is specified in the querystring, load that month + if ( (int) $q['m'] ) { + $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); + $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4); + if (strlen($q['m'])>5) + $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2); + if (strlen($q['m'])>7) + $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2); + if (strlen($q['m'])>9) + $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2); + if (strlen($q['m'])>11) + $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2); + if (strlen($q['m'])>13) + $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2); + } + + if ( (int) $q['hour'] ) { + $q['hour'] = '' . intval($q['hour']); + $where .= " AND HOUR(post_date)='" . $q['hour'] . "'"; + } + + if ( (int) $q['minute'] ) { + $q['minute'] = '' . intval($q['minute']); + $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'"; + } + + if ( (int) $q['second'] ) { + $q['second'] = '' . intval($q['second']); + $where .= " AND SECOND(post_date)='" . $q['second'] . "'"; + } + + if ( (int) $q['year'] ) { + $q['year'] = '' . intval($q['year']); + $where .= " AND YEAR(post_date)='" . $q['year'] . "'"; + } + + if ( (int) $q['monthnum'] ) { + $q['monthnum'] = '' . intval($q['monthnum']); + $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'"; + } + + if ( (int) $q['day'] ) { + $q['day'] = '' . intval($q['day']); + $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'"; + } + + // Compat. Map subpost to attachment. + if ( '' != $q['subpost'] ) + $q['attachment'] = $q['subpost']; + if ( '' != $q['subpost_id'] ) + $q['attachment_id'] = $q['subpost_id']; + + if ('' != $q['name']) { + $q['name'] = sanitize_title($q['name']); + $where .= " AND post_name = '" . $q['name'] . "'"; + } else if ('' != $q['pagename']) { + $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); + $page_paths = '/' . trim($q['pagename'], '/'); + $q['pagename'] = sanitize_title(basename($page_paths)); + $q['name'] = $q['pagename']; + $page_paths = explode('/', $page_paths); + foreach($page_paths as $pathdir) + $page_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); + + $all_page_ids = get_all_page_ids(); + $reqpage = 0; + foreach ( $all_page_ids as $page_id ) { + $page = get_page($page_id); + if ( $page->fullpath == $page_path ) { + $reqpage = $page_id; + break; + } + } + + $where .= " AND (ID = '$reqpage')"; + } elseif ('' != $q['attachment']) { + $q['attachment'] = sanitize_title($q['attachment']); + $q['name'] = $q['attachment']; + $where .= " AND post_name = '" . $q['attachment'] . "'"; + } + + if ( (int) $q['w'] ) { + $q['w'] = ''.intval($q['w']); + $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'"; + } + + if ( intval($q['comments_popup']) ) + $q['p'] = intval($q['comments_popup']); + + // If a attachment is requested by number, let it supercede any post number. + if ( ($q['attachment_id'] != '') && (intval($q['attachment_id']) != 0) ) + $q['p'] = (int) $q['attachment_id']; + + // If a post number is specified, load that post + if (($q['p'] != '') && intval($q['p']) != 0) { + $q['p'] = (int) $q['p']; + $where = ' AND ID = ' . $q['p']; + } + + if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) { + $q['page_id'] = intval($q['page_id']); + $q['p'] = $q['page_id']; + $where = ' AND ID = '.$q['page_id']; + } + + // If a search pattern is specified, load the posts that match + if (!empty($q['s'])) { + $q['s'] = addslashes_gpc($q['s']); + $search = ' AND ('; + $q['s'] = preg_replace('/, +/', ' ', $q['s']); + $q['s'] = str_replace(',', ' ', $q['s']); + $q['s'] = str_replace('"', ' ', $q['s']); + $q['s'] = trim($q['s']); + if ($q['exact']) { + $n = ''; + } else { + $n = '%'; + } + if (!$q['sentence']) { + $s_array = explode(' ',$q['s']); + $q['search_terms'] = $s_array; + $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))'; + for ( $i = 1; $i < count($s_array); $i = $i + 1) { + $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))'; + } + $search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')'; + $search .= ')'; + } else { + $search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))'; + } + } + + // Category stuff + + if ((empty($q['cat'])) || ($q['cat'] == '0') || + // Bypass cat checks if fetching specific posts + ( $this->is_single || $this->is_page )) { + $whichcat=''; + } else { + $q['cat'] = ''.urldecode($q['cat']).''; + $q['cat'] = addslashes_gpc($q['cat']); + if (stristr($q['cat'],'-')) { + // Note: if we have a negative, we ignore all the positives. It must + // always mean 'everything /except/ this one'. We should be able to do + // multiple negatives but we don't :-( + $eq = '!='; + $andor = 'AND'; + $q['cat'] = explode('-',$q['cat']); + $q['cat'] = intval($q['cat'][1]); + } else { + $eq = '='; + $andor = 'OR'; + } + $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) "; + $cat_array = preg_split('/[,\s]+/', $q['cat']); + $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]); + $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' '); + for ($i = 1; $i < (count($cat_array)); $i = $i + 1) { + $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]); + $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' '); + } + $whichcat .= ')'; + if ($eq == '!=') { + $q['cat'] = '-'.$q['cat']; // Put back the knowledge that we are excluding a category. + } + } + + // Category stuff for nice URIs + + global $cache_categories; + if ('' != $q['category_name']) { + $cat_paths = '/' . trim(urldecode($q['category_name']), '/'); + $q['category_name'] = sanitize_title(basename($cat_paths)); + $cat_paths = explode('/', $cat_paths); + foreach($cat_paths as $pathdir) + $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); + + $all_cat_ids = get_all_category_ids(); + $q['cat'] = 0; + foreach ( $all_cat_ids as $cat_id ) { + $cat = get_category($cat_id); + if ( $cat->fullpath == $cat_path ) { + $q['cat'] = $cat_id; + break; + } + } + + $tables = ", $wpdb->post2cat, $wpdb->categories"; + $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) "; + $whichcat = " AND (category_id = '" . $q['cat'] . "'"; + $whichcat .= get_category_children($q['cat'], " OR category_id = "); + $whichcat .= ")"; + } + + // Author/user stuff + + if ((empty($q['author'])) || ($q['author'] == '0')) { + $whichauthor=''; + } else { + $q['author'] = ''.urldecode($q['author']).''; + $q['author'] = addslashes_gpc($q['author']); + if (stristr($q['author'], '-')) { + $eq = '!='; + $andor = 'AND'; + $q['author'] = explode('-', $q['author']); + $q['author'] = ''.intval($q['author'][1]); + } else { + $eq = '='; + $andor = 'OR'; + } + $author_array = preg_split('/[,\s]+/', $q['author']); + $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]); + for ($i = 1; $i < (count($author_array)); $i = $i + 1) { + $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]); + } + $whichauthor .= ')'; + } + + // Author stuff for nice URIs + + if ('' != $q['author_name']) { + if (stristr($q['author_name'],'/')) { + $q['author_name'] = explode('/',$q['author_name']); + if ($q['author_name'][count($q['author_name'])-1]) { + $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash + } else { + $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash + } + } + $q['author_name'] = sanitize_title($q['author_name']); + $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'"); + $whichauthor .= ' AND (post_author = '.intval($q['author']).')'; + } + + $where .= $search.$whichcat.$whichauthor; + + if ((empty($q['order'])) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC'))) { + $q['order']='DESC'; + } + + // Order by + if (empty($q['orderby'])) { + $q['orderby']='date '.$q['order']; + } else { + // Used to filter values + $allowed_keys = array('author', 'date', 'category', 'title', 'modified'); + $q['orderby'] = urldecode($q['orderby']); + $q['orderby'] = addslashes_gpc($q['orderby']); + $orderby_array = explode(' ',$q['orderby']); + if (!in_array($orderby_array[0],$allowed_keys)) { + $orderby_array[0] = 'date'; + } + $q['orderby'] = $orderby_array[0].' '.$q['order']; + if (count($orderby_array)>1) { + for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) { + // Only allow certain values for safety + if (in_array($orderby_array[$i],$allowed_keys)) { + $q['orderby'] .= ',post_'.$orderby_array[$i].' '.$q['order']; + } + } + } + } + + $now = gmdate('Y-m-d H:i:59'); + + //only select past-dated posts, except if a logged in user is viewing a single: then, if they + //can edit the post, we let them through + if ($pagenow != 'post.php' && $pagenow != 'edit.php' && !($this->is_single && $user_ID)) { + $where .= " AND post_date_gmt <= '$now'"; + $distinct = 'DISTINCT'; + } + + if ( $this->is_attachment ) { + $where .= ' AND (post_status = "attachment")'; + } elseif ($this->is_page) { + $where .= ' AND (post_status = "static")'; + } elseif ($this->is_single) { + $where .= ' AND (post_status != "static")'; + } else { + $where .= ' AND (post_status = "publish"'; + + if (isset($user_ID) && ('' != intval($user_ID))) + $where .= " OR post_author = $user_ID AND post_status != 'draft' AND post_status != 'static')"; + else + $where .= ')'; + } + + if (! $this->is_attachment ) + $where .= ' AND post_status != "attachment"'; + + // Apply filters on where and join prior to paging so that any + // manipulations to them are reflected in the paging by day queries. + $where = apply_filters('posts_where', $where); + $join = apply_filters('posts_join', $join); + + // Paging + if (empty($q['nopaging']) && ! $this->is_single) { + $page = $q['paged']; + if (empty($page)) { + $page = 1; + } + + if (($q['what_to_show'] == 'posts')) { + $pgstrt = ''; + $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', '; + $limits = 'LIMIT '.$pgstrt.$q['posts_per_page']; + } elseif ($q['what_to_show'] == 'days') { + $startrow = $q['posts_per_page'] * (intval($page)-1); + $start_date = $wpdb->get_var("SELECT max(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $startrow,1"); + $endrow = $startrow + $q['posts_per_page'] - 1; + $end_date = $wpdb->get_var("SELECT min(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $endrow,1"); + + if ($page > 1) { + $where .= " AND post_date >= '$end_date' AND post_date <= '$start_date'"; + } else { + $where .= " AND post_date >= '$end_date'"; + } + } + } + + // Apply post-paging filters on where and join. Only plugins that + // manipulate paging queries should use these hooks. + $where = apply_filters('posts_where_paged', $where); + $groupby = " $wpdb->posts.ID "; + $groupby = apply_filters('posts_groupby', $groupby); + $join = apply_filters('posts_join_paged', $join); + $orderby = "post_" . $q['orderby']; + $orderby = apply_filters('posts_orderby', $orderby); + $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1" . $where . " GROUP BY " . $groupby . " ORDER BY " . $orderby . " $limits"; + $request = apply_filters('posts_request', $request); + + $this->posts = $wpdb->get_results($request); + + // Check post status to determine if post should be displayed. + if ($this->is_single) { + $status = get_post_status($this->posts[0]); + if ( ('publish' != $status) && ('static' != $status) ) { + if ( ! (isset($user_ID) && ('' != intval($user_ID))) ) { + // User must be logged in to view unpublished posts. + $this->posts = array(); + } else { + if ('draft' == $status) { + // User must have edit permissions on the draft to preview. + if (! current_user_can('edit_post', $this->posts[0]->ID)) { + $this->posts = array(); + } else { + $this->is_preview = true; + } + } else { + if (! current_user_can('read_post', $this->posts[0]->ID)) + $this->posts = array(); + } + } + } else { + if (mysql2date('U', $this->posts[0]->post_date_gmt) > mysql2date('U', $now)) { //it's future dated + $this->is_preview = true; + if (!current_user_can('edit_post', $this->posts[0]->ID)) { + $this->posts = array ( ); + } + } + } + } + + update_post_caches($this->posts); + + $this->posts = apply_filters('the_posts', $this->posts); + $this->post_count = count($this->posts); + if ($this->post_count > 0) { + $this->post = $this->posts[0]; + } + + // Save any changes made to the query vars. + $this->query_vars = $q; + return $this->posts; + } + + function next_post() { + + $this->current_post++; + + $this->post = $this->posts[$this->current_post]; + return $this->post; + } + + function the_post() { + global $post; + $this->in_the_loop = true; + $post = $this->next_post(); + setup_postdata($post); + + if ( $this->current_post == 0 ) // loop has just started + do_action('loop_start'); + } + + function have_posts() { + if ($this->current_post + 1 < $this->post_count) { + return true; + } elseif ($this->current_post + 1 == $this->post_count) { + do_action('loop_end'); + // Do some cleaning up after the loop + $this->rewind_posts(); + } + + $this->in_the_loop = false; + return false; + } + + function rewind_posts() { + $this->current_post = -1; + if ($this->post_count > 0) { + $this->post = $this->posts[0]; + } + } + + function &query($query) { + $this->parse_query($query); + return $this->get_posts(); + } + + function get_queried_object() { + if (isset($this->queried_object)) { + return $this->queried_object; + } + + $this->queried_object = NULL; + $this->queried_object_id = 0; + + if ($this->is_category) { + $cat = $this->get('cat'); + $category = &get_category($cat); + $this->queried_object = &$category; + $this->queried_object_id = $cat; + } else if ($this->is_single) { + $this->queried_object = $this->post; + $this->queried_object_id = $this->post->ID; + } else if ($this->is_page) { + $this->queried_object = $this->post; + $this->queried_object_id = $this->post->ID; + } else if ($this->is_author) { + $author_id = $this->get('author'); + $author = get_userdata($author_id); + $this->queried_object = $author; + $this->queried_object_id = $author_id; + } + + return $this->queried_object; + } + + function get_queried_object_id() { + $this->get_queried_object(); + + if (isset($this->queried_object_id)) { + return $this->queried_object_id; + } + + return 0; + } + + function WP_Query ($query = '') { + if (! empty($query)) { + $this->query($query); + } + } +} + +class retrospam_mgr { + var $spam_words; + var $comments_list; + var $found_comments; + + function retrospam_mgr() { + global $wpdb; + + $list = explode("\n", get_settings('moderation_keys') ); + $list = array_unique( $list ); + $this->spam_words = $list; + + $this->comment_list = $wpdb->get_results("SELECT comment_ID AS ID, comment_content AS text, comment_approved AS approved, comment_author_url AS url, comment_author_ip AS ip, comment_author_email AS email FROM $wpdb->comments ORDER BY comment_ID ASC"); + } // End of class constructor + + function move_spam( $id_list ) { + global $wpdb; + $cnt = 0; + $id_list = explode( ',', $id_list ); + + foreach ( $id_list as $comment ) { + if ( $wpdb->query("update $wpdb->comments set comment_approved = '0' where comment_ID = '$comment'") ) { + $cnt++; + } + } + echo "

    $cnt comment"; + if ($cnt != 1 ) echo "s"; + echo " moved to the moderation queue.

    \n"; + } // End function move_spam + + function find_spam() { + $in_queue = 0; + + foreach( $this->comment_list as $comment ) { + if( $comment->approved == 1 ) { + foreach( $this->spam_words as $word ) { + $word = trim($word); + if ( empty( $word ) ) + continue; + $fulltext = strtolower($comment->email.' '.$comment->url.' '.$comment->ip.' '.$comment->text); + if( strpos( $fulltext, strtolower($word) ) != FALSE ) { + $this->found_comments[] = $comment->ID; + break; + } + } + } else { + $in_queue++; + } + } + return array( 'found' => $this->found_comments, 'in_queue' => $in_queue ); + } // End function find_spam + + function display_edit_form( $counters ) { + $numfound = count($counters[found]); + $numqueue = $counters[in_queue]; + + $body = '

    ' . sprintf(__('Suspected spam comments: %s'), $numfound) . '

    '; + + if ( count($counters[found]) > 0 ) { + $id_list = implode( ',', $counters[found] ); + $body .= '

    '. __('Move suspect comments to moderation queue »') . '

    '; + + } + $head = '

    ' . __('Check Comments Results:') . '

    '; + + $foot .= '

    ' . __('« Return to Discussion Options page.') . '

    '; + + return $head . $body . $foot; + } // End function display_edit_form + +} + +class WP_Rewrite { + var $permalink_structure; + var $category_base; + var $category_structure; + var $author_base = 'author'; + var $author_structure; + var $date_structure; + var $page_structure; + var $search_base = 'search'; + var $search_structure; + var $comments_base = 'comments'; + var $feed_base = 'feed'; + var $comments_feed_structure; + var $feed_structure; + var $front; + var $root = ''; + var $index = 'index.php'; + var $matches = ''; + var $rules; + var $use_verbose_rules = false; + var $rewritecode = + array( + '%year%', + '%monthnum%', + '%day%', + '%hour%', + '%minute%', + '%second%', + '%postname%', + '%post_id%', + '%category%', + '%author%', + '%pagename%', + '%search%' + ); + + var $rewritereplace = + array( + '([0-9]{4})', + '([0-9]{1,2})', + '([0-9]{1,2})', + '([0-9]{1,2})', + '([0-9]{1,2})', + '([0-9]{1,2})', + '([^/]+)', + '([0-9]+)', + '(.+?)', + '([^/]+)', + '([^/]+)', + '(.+)' + ); + + var $queryreplace = + array ( + 'year=', + 'monthnum=', + 'day=', + 'hour=', + 'minute=', + 'second=', + 'name=', + 'p=', + 'category_name=', + 'author_name=', + 'pagename=', + 's=' + ); + + var $feeds = array ('feed', 'rdf', 'rss', 'rss2', 'atom'); + + function using_permalinks() { + if (empty($this->permalink_structure)) + return false; + else + return true; + } + + function using_index_permalinks() { + if (empty($this->permalink_structure)) { + return false; + } + + // If the index is not in the permalink, we're using mod_rewrite. + if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) { + return true; + } + + return false; + } + + function using_mod_rewrite_permalinks() { + if ( $this->using_permalinks() && ! $this->using_index_permalinks()) + return true; + else + return false; + } + + function preg_index($number) { + $match_prefix = '$'; + $match_suffix = ''; + + if (! empty($this->matches)) { + $match_prefix = '$' . $this->matches . '['; + $match_suffix = ']'; + } + + return "$match_prefix$number$match_suffix"; + } + + function page_rewrite_rules() { + $uris = get_settings('page_uris'); + + $rewrite_rules = array(); + $page_structure = $this->get_page_permastruct(); + if( is_array( $uris ) ) + { + foreach ($uris as $uri => $pagename) { + $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename='); + $rewrite_rules += $this->generate_rewrite_rules($page_structure); + } + } + + return $rewrite_rules; + } + + function get_date_permastruct() { + if (isset($this->date_structure)) { + return $this->date_structure; + } + + if (empty($this->permalink_structure)) { + $this->date_structure = ''; + return false; + } + + // The date permalink must have year, month, and day separated by slashes. + $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%'); + + $this->date_structure = ''; + $date_endian = ''; + + foreach ($endians as $endian) { + if (false !== strpos($this->permalink_structure, $endian)) { + $date_endian= $endian; + break; + } + } + + if ( empty($date_endian) ) + $date_endian = '%year%/%monthnum%/%day%'; + + // Do not allow the date tags and %post_id% to overlap in the permalink + // structure. If they do, move the date tags to $front/date/. + $front = $this->front; + preg_match_all('/%.+?%/', $this->permalink_structure, $tokens); + $tok_index = 1; + foreach ($tokens[0] as $token) { + if ( ($token == '%post_id%') && ($tok_index <= 3) ) { + $front = $front . 'date/'; + break; + } + } + + $this->date_structure = $front . $date_endian; + + return $this->date_structure; + } + + function get_year_permastruct() { + $structure = $this->get_date_permastruct($this->permalink_structure); + + if (empty($structure)) { + return false; + } + + $structure = str_replace('%monthnum%', '', $structure); + $structure = str_replace('%day%', '', $structure); + + $structure = preg_replace('#/+#', '/', $structure); + + return $structure; + } + + function get_month_permastruct() { + $structure = $this->get_date_permastruct($this->permalink_structure); + + if (empty($structure)) { + return false; + } + + $structure = str_replace('%day%', '', $structure); + + $structure = preg_replace('#/+#', '/', $structure); + + return $structure; + } + + function get_day_permastruct() { + return $this->get_date_permastruct($this->permalink_structure); + } + + function get_category_permastruct() { + if (isset($this->category_structure)) { + return $this->category_structure; + } + + if (empty($this->permalink_structure)) { + $this->category_structure = ''; + return false; + } + + if (empty($this->category_base)) + $this->category_structure = $this->front . 'category/'; + else + $this->category_structure = $this->category_base . '/'; + + $this->category_structure .= '%category%'; + + return $this->category_structure; + } + + function get_author_permastruct() { + if (isset($this->author_structure)) { + return $this->author_structure; + } + + if (empty($this->permalink_structure)) { + $this->author_structure = ''; + return false; + } + + $this->author_structure = $this->front . $this->author_base . '/%author%'; + + return $this->author_structure; + } + + function get_search_permastruct() { + if (isset($this->search_structure)) { + return $this->search_structure; + } + + if (empty($this->permalink_structure)) { + $this->search_structure = ''; + return false; + } + + $this->search_structure = $this->root . $this->search_base . '/%search%'; + + return $this->search_structure; + } + + function get_page_permastruct() { + if (isset($this->page_structure)) { + return $this->page_structure; + } + + if (empty($this->permalink_structure)) { + $this->page_structure = ''; + return false; + } + + $this->page_structure = $this->root . '%pagename%'; + + return $this->page_structure; + } + + function get_feed_permastruct() { + if (isset($this->feed_structure)) { + return $this->feed_structure; + } + + if (empty($this->permalink_structure)) { + $this->feed_structure = ''; + return false; + } + + $this->feed_structure = $this->root . $this->feed_base . '/%feed%'; + + return $this->feed_structure; + } + + function get_comment_feed_permastruct() { + if (isset($this->comment_feed_structure)) { + return $this->comment_feed_structure; + } + + if (empty($this->permalink_structure)) { + $this->comment_feed_structure = ''; + return false; + } + + $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%'; + + return $this->comment_feed_structure; + } + + function add_rewrite_tag($tag, $pattern, $query) { + // If the tag already exists, replace the existing pattern and query for + // that tag, otherwise add the new tag, pattern, and query to the end of + // the arrays. + $position = array_search($tag, $this->rewritecode); + if (FALSE !== $position && NULL !== $position) { + $this->rewritereplace[$position] = $pattern; + $this->queryreplace[$position] = $query; + } else { + $this->rewritecode[] = $tag; + $this->rewritereplace[] = $pattern; + $this->queryreplace[] = $query; + } + } + + function generate_rewrite_rules($permalink_structure, $page = true, $feed = true, $forcomments = false, $walk_dirs = true) { + $feedregex2 = ''; + foreach ($this->feeds as $feed_name) { + $feedregex2 .= $feed_name . '|'; + } + $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$'; + $feedregex = $this->feed_base . '/' . $feedregex2; + + $trackbackregex = 'trackback/?$'; + $pageregex = 'page/?([0-9]{1,})/?$'; + + $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); + preg_match_all('/%.+?%/', $permalink_structure, $tokens); + + $num_tokens = count($tokens[0]); + + $index = $this->index; + $feedindex = $index; + $trackbackindex = $index; + for ($i = 0; $i < $num_tokens; ++$i) { + if (0 < $i) { + $queries[$i] = $queries[$i - 1] . '&'; + } + + $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1); + $queries[$i] .= $query_token; + } + + $structure = $permalink_structure; + if ($front != '/') { + $structure = str_replace($front, '', $structure); + } + $structure = trim($structure, '/'); + if ($walk_dirs) { + $dirs = explode('/', $structure); + } else { + $dirs[] = $structure; + } + $num_dirs = count($dirs); + + $front = preg_replace('|^/+|', '', $front); + + $post_rewrite = array(); + $struct = $front; + for ($j = 0; $j < $num_dirs; ++$j) { + $struct .= $dirs[$j] . '/'; + $struct = ltrim($struct, '/'); + $match = str_replace($this->rewritecode, $this->rewritereplace, $struct); + $num_toks = preg_match_all('/%.+?%/', $struct, $toks); + $query = $queries[$num_toks - 1]; + + $pagematch = $match . $pageregex; + $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1); + + $feedmatch = $match . $feedregex; + $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); + + $feedmatch2 = $match . $feedregex2; + $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); + + if ($forcomments) { + $feedquery .= '&withcomments=1'; + $feedquery2 .= '&withcomments=1'; + } + + $rewrite = array(); + if ($feed) + $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2); + if ($page) + $rewrite = $rewrite + array($pagematch => $pagequery); + + if ($num_toks) { + $post = 0; + if (strstr($struct, '%postname%') || strstr($struct, '%post_id%') + || strstr($struct, '%pagename%') + || (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) { + $post = 1; + $trackbackmatch = $match . $trackbackregex; + $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; + $match = rtrim($match, '/'); + $submatchbase = str_replace(array('(',')'),'',$match); + $sub1 = $submatchbase . '/([^/]+)/'; + $sub1tb = $sub1 . $trackbackregex; + $sub1feed = $sub1 . $feedregex; + $sub1feed2 = $sub1 . $feedregex2; + $sub1 .= '?$'; + $sub2 = $submatchbase . '/attachment/([^/]+)/'; + $sub2tb = $sub2 . $trackbackregex; + $sub2feed = $sub2 . $feedregex; + $sub2feed2 = $sub2 . $feedregex2; + $sub2 .= '?$'; + $subquery = $index . '?attachment=' . $this->preg_index(1); + $subtbquery = $subquery . '&tb=1'; + $subfeedquery = $subquery . '&feed=' . $this->preg_index(2); + $match = $match . '(/[0-9]+)?/?$'; + $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1); + } else { + $match .= '?$'; + $query = $index . '?' . $query; + } + + $rewrite = $rewrite + array($match => $query); + + if ($post) { + $rewrite = array($trackbackmatch => $trackbackquery) + $rewrite + + array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery) + + array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery); + } + } + + $post_rewrite = $rewrite + $post_rewrite; + } + + return $post_rewrite; + } + + function generate_rewrite_rule($permalink_structure, $walk_dirs = false) { + return $this->generate_rewrite_rules($permalink_structure, false, false, false, $walk_dirs); + } + + /* rewrite_rules + * Construct rewrite matches and queries from permalink structure. + * Returns an associate array of matches and queries. + */ + function rewrite_rules() { + $rewrite = array(); + + if (empty($this->permalink_structure)) { + return $rewrite; + } + + // Post + $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure); + $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite); + + // Date + $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct()); + $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite); + + // Root + $root_rewrite = $this->generate_rewrite_rules($this->root . '/'); + $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite); + + // Comments + $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, true, true, true); + $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite); + + // Search + $search_structure = $this->get_search_permastruct(); + $search_rewrite = $this->generate_rewrite_rules($search_structure); + $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); + + // Categories + $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct()); + $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite); + + // Authors + $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct()); + $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite); + + // Pages + $page_rewrite = $this->page_rewrite_rules(); + $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite); + + // Put them together. + $this->rules = $page_rewrite + $root_rewrite + $comments_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite + $post_rewrite; + + do_action('generate_rewrite_rules', array(&$this)); + $this->rules = apply_filters('rewrite_rules_array', $this->rules); + + return $this->rules; + } + + function wp_rewrite_rules() { + $this->matches = 'matches'; + return $this->rewrite_rules(); + } + + function mod_rewrite_rules() { + if ( ! $this->using_permalinks()) { + return ''; + } + + $site_root = parse_url(get_settings('siteurl')); + $site_root = trailingslashit($site_root['path']); + + $home_root = parse_url(get_settings('home')); + $home_root = trailingslashit($home_root['path']); + + $rules = "\n"; + $rules .= "RewriteEngine On\n"; + $rules .= "RewriteBase $home_root\n"; + + if ($this->use_verbose_rules) { + $this->matches = ''; + $rewrite = $this->rewrite_rules(); + $num_rules = count($rewrite); + $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . + "RewriteCond %{REQUEST_FILENAME} -d\n" . + "RewriteRule ^.*$ - [S=$num_rules]\n"; + + foreach ($rewrite as $match => $query) { + // Apache 1.3 does not support the reluctant (non-greedy) modifier. + $match = str_replace('.+?', '.+', $match); + + // If the match is unanchored and greedy, prepend rewrite conditions + // to avoid infinite redirects and eclipsing of real files. + if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { + //nada. + } + + if (strstr($query, $this->index)) { + $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; + } else { + $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; + } + } + } else { + $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" . + "RewriteCond %{REQUEST_FILENAME} !-d\n" . + "RewriteRule . {$home_root}{$this->index}\n"; + } + + $rules .= "\n"; + + $rules = apply_filters('mod_rewrite_rules', $rules); + $rules = apply_filters('rewrite_rules', $rules); // Deprecated + + return $rules; + } + + function init() { + $this->permalink_structure = get_settings('permalink_structure'); + $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%')); + $this->root = ''; + if ($this->using_index_permalinks()) { + $this->root = $this->index . '/'; + } + $this->category_base = get_settings('category_base'); + unset($this->category_structure); + unset($this->author_structure); + unset($this->date_structure); + unset($this->page_structure); + unset($this->search_structure); + unset($this->feed_structure); + unset($this->comment_feed_structure); + } + + function set_permalink_structure($permalink_structure) { + if ($permalink_structure != $this->permalink_structure) { + update_option('permalink_structure', $permalink_structure); + $this->init(); + } + } + + function set_category_base($category_base) { + if ($category_base != $this->category_base) { + update_option('category_base', $category_base); + $this->init(); + } + } + + function WP_Rewrite() { + $this->init(); + } +} + +class WP { + var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id'); + + var $private_query_vars = array('posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'show_post_type'); + + var $query_vars; + var $query_string; + var $request; + var $matched_rule; + var $matched_query; + var $did_permalink = false; + + function parse_request($extra_query_vars = '') { + global $wp_rewrite; + + $this->query_vars = array(); + + if (! empty($extra_query_vars)) + parse_str($extra_query_vars, $extra_query_vars); + else + $extra_query_vars = array(); + + // Process PATH_INFO, REQUEST_URI, and 404 for permalinks. + + // Fetch the rewrite rules. + $rewrite = $wp_rewrite->wp_rewrite_rules(); + + if (! empty($rewrite)) { + // If we match a rewrite rule, this will be cleared. + $error = '404'; + $this->did_permalink = true; + + $pathinfo = $_SERVER['PATH_INFO']; + $pathinfo_array = explode('?', $pathinfo); + $pathinfo = $pathinfo_array[0]; + $req_uri = $_SERVER['REQUEST_URI']; + $req_uri_array = explode('?', $req_uri); + $req_uri = $req_uri_array[0]; + $self = $_SERVER['PHP_SELF']; + $home_path = parse_url(get_settings('home')); + $home_path = $home_path['path']; + + // Trim path info from the end and the leading home path from the + // front. For path info requests, this leaves us with the requesting + // filename, if any. For 404 requests, this leaves us with the + // requested permalink. + $req_uri = str_replace($pathinfo, '', $req_uri); + $req_uri = str_replace($home_path, '', $req_uri); + $req_uri = trim($req_uri, '/'); + $pathinfo = str_replace($home_path, '', $pathinfo); + $pathinfo = trim($pathinfo, '/'); + $self = str_replace($home_path, '', $self); + $self = trim($self, '/'); + + // The requested permalink is in $pathinfo for path info requests and + // $req_uri for other requests. + if ( ! empty($pathinfo) && ($wp_rewrite->index != $pathinfo) ) { + $request = $pathinfo; + } else { + // If the request uri is the index, blank it out so that we don't try to match it against a rule. + if ( $req_uri == $wp_rewrite->index ) + $req_uri = ''; + $request = $req_uri; + } + + $this->request = $request; + + // Look for matches. + $request_match = $request; + foreach ($rewrite as $match => $query) { + // If the requesting file is the anchor of the match, prepend it + // to the path info. + if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request)) { + $request_match = $req_uri . '/' . $request; + } + + if (preg_match("!^$match!", $request_match, $matches) || + preg_match("!^$match!", urldecode($request_match), $matches)) { + // Got a match. + $this->matched_rule = $match; + + // Trim the query of everything up to the '?'. + $query = preg_replace("!^.+\?!", '', $query); + + // Substitute the substring matches into the query. + eval("\$query = \"$query\";"); + $this->matched_query = $query; + + // Parse the query. + parse_str($query, $query_vars); + + // If we're processing a 404 request, clear the error var + // since we found something. + if (isset($_GET['error'])) + unset($_GET['error']); + + if (isset($error)) + unset($error); + + break; + } + } + + // If req_uri is empty or if it is a request for ourself, unset error. + if ( empty($request) || $req_uri == $self || strstr($_SERVER['PHP_SELF'], 'wp-admin/') ) { + if (isset($_GET['error'])) + unset($_GET['error']); + + if (isset($error)) + unset($error); + + if ( isset($query_vars) && strstr($_SERVER['PHP_SELF'], 'wp-admin/') ) + unset($query_vars); + + $this->did_permalink = false; + } + } + + $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars); + + for ($i=0; $ipublic_query_vars); $i += 1) { + $wpvar = $this->public_query_vars[$i]; + if (isset($extra_query_vars[$wpvar])) + $this->query_vars[$wpvar] = $extra_query_vars[$wpvar]; + elseif (isset($GLOBALS[$wpvar])) + $this->query_vars[$wpvar] = $GLOBALS[$wpvar]; + elseif (!empty($_POST[$wpvar])) + $this->query_vars[$wpvar] = $_POST[$wpvar]; + elseif (!empty($_GET[$wpvar])) + $this->query_vars[$wpvar] = $_GET[$wpvar]; + elseif (!empty($query_vars[$wpvar])) + $this->query_vars[$wpvar] = $query_vars[$wpvar]; + else + $this->query_vars[$wpvar] = ''; + } + + if ( isset($error) ) + $this->query_vars['error'] = $error; + } + + function send_headers() { + global $current_user; + @header('X-Pingback: '. get_bloginfo('pingback_url')); + if ( is_user_logged_in() ) + nocache_headers(); + if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) { + status_header( 404 ); + } else if ( empty($this->query_vars['feed']) ) { + @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); + } else { + // We're showing a feed, so WP is indeed the only thing that last changed + if ( $this->query_vars['withcomments'] ) + $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT'; + else + $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT'; + $wp_etag = '"' . md5($wp_last_modified) . '"'; + @header("Last-Modified: $wp_last_modified"); + @header("ETag: $wp_etag"); + + // Support for Conditional GET + if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']); + else $client_etag = false; + + $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']); + // If string is empty, return 0. If not, attempt to parse into a timestamp + $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0; + + // Make a timestamp for our most recent modification... + $wp_modified_timestamp = strtotime($wp_last_modified); + + if ( ($client_last_modified && $client_etag) ? + (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) : + (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) { + status_header( 304 ); + exit; + } + } + } + + function build_query_string() { + $this->query_string = ''; + + foreach ($this->public_query_vars as $wpvar) { + if (isset($this->query_vars[$wpvar]) && '' != $this->query_vars[$wpvar]) { + $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&'; + $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]); + } + } + + foreach ($this->private_query_vars as $wpvar) { + if (isset($GLOBALS[$wpvar]) && '' != $GLOBALS[$wpvar]) { + $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&'; + $this->query_string .= $wpvar . '=' . rawurlencode($GLOBALS[$wpvar]); + } + } + + $this->query_string = apply_filters('query_string', $this->query_string); + } + + function register_globals() { + global $wp_query; + // Extract updated query vars back into global namespace. + foreach ($wp_query->query_vars as $key => $value) { + $GLOBALS[$key] = $value; + } + + $GLOBALS['query_string'] = & $this->query_string; + $GLOBALS['posts'] = & $wp_query->posts; + $GLOBALS['post'] = & $wp_query->post; + + if ( is_single() || is_page() ) { + $GLOBALS['more'] = 1; + $GLOBALS['single'] = 1; + } + } + + function init() { + get_currentuserinfo(); + } + + function query_posts() { + $this->build_query_string(); + query_posts($this->query_string); + } + + function handle_404() { + global $wp_query; + // Issue a 404 if a permalink request doesn't match any posts. Don't + // issue a 404 if one was already issued, if the request was a search, + // or if the request was a regular query string request rather than a + // permalink request. + if ( (0 == count($wp_query->posts)) && !is_404() && !is_category() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) { + $wp_query->set_404(); + status_header( 404 ); + } elseif( is_404() != true ) { + status_header( 200 ); + } + } + + function main($query_args = '') { + $this->init(); + $this->parse_request($query_args); + $this->send_headers(); + $this->query_posts(); + $this->handle_404(); + $this->register_globals(); + } + + function WP() { + // Empty. + } +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/comment-functions.php b/projects/tests/tests/real/wordpress/wp-includes/comment-functions.php new file mode 100644 index 00000000..30bb0c88 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/comment-functions.php @@ -0,0 +1,887 @@ +get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date"); + } else { + $author_db = $wpdb->escape($comment_author); + $email_db = $wpdb->escape($comment_author_email); + $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date"); + } + + get_currentuserinfo(); + + define('COMMENTS_TEMPLATE', true); + $include = apply_filters('comments_template', TEMPLATEPATH . $file ); + if ( file_exists( $include ) ) + require( $include ); + else + require( ABSPATH . 'wp-content/themes/default/comments.php'); + + endif; +} + +function wp_new_comment( $commentdata ) { + $commentdata = apply_filters('preprocess_comment', $commentdata); + + $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; + $commentdata['user_ID'] = (int) $commentdata['user_ID']; + + $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; + $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT']; + + $commentdata['comment_date'] = current_time('mysql'); + $commentdata['comment_date_gmt'] = current_time('mysql', 1); + + + $commentdata = wp_filter_comment($commentdata); + + $commentdata['comment_approved'] = wp_allow_comment($commentdata); + + $comment_ID = wp_insert_comment($commentdata); + + do_action('comment_post', $comment_ID, $commentdata['comment_approved']); + + if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching + if ( '0' == $commentdata['comment_approved'] ) + wp_notify_moderator($comment_ID); + + $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment + + if ( get_settings('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] ) + wp_notify_postauthor($comment_ID, $commentdata['comment_type']); + } + + return $comment_ID; +} + +function wp_insert_comment($commentdata) { + global $wpdb; + extract($commentdata); + + if ( ! isset($comment_author_IP) ) + $comment_author_IP = $_SERVER['REMOTE_ADDR']; + if ( ! isset($comment_date) ) + $comment_date = current_time('mysql'); + if ( ! isset($comment_date_gmt) ) + $comment_date_gmt = gmdate('Y-m-d H:i:s', strtotime($comment_date) ); + if ( ! isset($comment_parent) ) + $comment_parent = 0; + + $result = $wpdb->query("INSERT INTO $wpdb->comments + (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id) + VALUES + ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id') + "); + + $id = $wpdb->insert_id; + + if ( $comment_approved == 1) { + $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'"); + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$comment_post_ID'" ); + } + return $id; +} + +function wp_filter_comment($commentdata) { + $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']); + $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']); + $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']); + $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']); + $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']); + $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']); + $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']); + $commentdata['filtered'] = true; + return $commentdata; +} + +function wp_allow_comment($commentdata) { + global $wpdb; + extract($commentdata); + + $comment_user_domain = apply_filters('pre_comment_user_domain', gethostbyaddr($comment_author_IP) ); + + // Simple duplicate check + $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' "; + if ( $comment_author_email ) + $dupe .= "OR comment_author_email = '$comment_author_email' "; + $dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; + if ( $wpdb->get_var($dupe) ) + die( __('Duplicate comment detected; it looks as though you\'ve already said that!') ); + + // Simple flood-protection + if ( $lasttime = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = '$comment_author_IP' OR comment_author_email = '$comment_author_email' ORDER BY comment_date DESC LIMIT 1") ) { + $time_lastcomment = mysql2date('U', $lasttime); + $time_newcomment = mysql2date('U', $comment_date_gmt); + if ( ($time_newcomment - $time_lastcomment) < 15 ) { + do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment); + die( __('Sorry, you can only post a new comment once every 15 seconds. Slow down cowboy.') ); + } + } + + if ( $user_id ) { + $userdata = get_userdata($user_id); + $user = new WP_User($user_id); + $post_author = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = '$comment_post_ID' LIMIT 1"); + } + + // The author and the admins get respect. + if ( $userdata && ( $user_id == $post_author || $user->has_cap('level_9') ) ) { + $approved = 1; + } + + // Everyone else's comments will be checked. + else { + if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) ) + $approved = 1; + else + $approved = 0; + if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) ) + $approved = 'spam'; + } + + $approved = apply_filters('pre_comment_approved', $approved); + return $approved; +} + + +function wp_update_comment($commentarr) { + global $wpdb; + + // First, get all of the original fields + $comment = get_comment($commentarr['comment_ID'], ARRAY_A); + + // Escape data pulled from DB. + foreach ($comment as $key => $value) + $comment[$key] = $wpdb->escape($value); + + // Merge old and new fields with new fields overwriting old ones. + $commentarr = array_merge($comment, $commentarr); + + // Now extract the merged array. + extract($commentarr); + + $comment_content = apply_filters('comment_save_pre', $comment_content); + + $result = $wpdb->query( + "UPDATE $wpdb->comments SET + comment_content = '$comment_content', + comment_author = '$comment_author', + comment_author_email = '$comment_author_email', + comment_approved = '$comment_approved', + comment_author_url = '$comment_author_url', + comment_date = '$comment_date' + WHERE comment_ID = $comment_ID" ); + + $rval = $wpdb->rows_affected; + + $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1' AND '".$_GET["row"]."'" ); + if( is_object( $c ) ) + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); + + do_action('edit_comment', $comment_ID); + + return $rval; +} + +function wp_delete_comment($comment_id) { + global $wpdb; + do_action('delete_comment', $comment_id); + + $comment = get_comment($comment_id); + + if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") ) + return false; + + $post_id = $comment->comment_post_ID; + if ( $post_id && $comment->comment_approved == 1 ) + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count - 1 WHERE ID = '$post_id'" ); + + do_action('wp_set_comment_status', $comment_id, 'delete'); + return true; +} + +function clean_url( $url ) { + if ('' == $url) return $url; + $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $url); + $url = str_replace(';//', '://', $url); + $url = (!strstr($url, '://')) ? 'http://'.$url : $url; + $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url); + return $url; +} + +function get_comments_number( $post_id = 0 ) { + global $wpdb, $comment_count_cache, $id; + $post_id = (int) $post_id; + + if ( !$post_id ) + $post_id = $id; + + if ( !isset($comment_count_cache[$post_id]) ) + $comment_count_cache[$id] = $wpdb->get_var("SELECT comment_count FROM $wpdb->posts WHERE ID = '$post_id'"); + + return apply_filters('get_comments_number', $comment_count_cache[$post_id]); +} + +function comments_number( $zero = 'No Comments', $one = '1 Comment', $more = '% Comments', $number = '' ) { + global $id, $comment; + $number = get_comments_number( $id ); + if ($number == 0) { + $blah = $zero; + } elseif ($number == 1) { + $blah = $one; + } elseif ($number > 1) { + $blah = str_replace('%', $number, $more); + } + echo apply_filters('comments_number', $blah); +} + +function get_comments_link() { + return get_permalink() . '#comments'; +} + +function get_comment_link() { + global $comment; + return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; +} + +function comments_link( $file = '', $echo = true ) { + echo get_comments_link(); +} + +function comments_popup_script($width=400, $height=400, $file='') { + global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript; + + if (empty ($file)) { + $wpcommentspopupfile = ''; // Use the index. + } else { + $wpcommentspopupfile = $file; + } + + $wpcommentsjavascript = 1; + $javascript = "\n"; + echo $javascript; +} + +function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') { + global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb; + global $comment_count_cache; + + if (! is_single() && ! is_page()) { + if ( !isset($comment_count_cache[$id]) ) + $comment_count_cache[$id] = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved = '1';"); + + $number = $comment_count_cache[$id]; + + if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) { + echo $none; + return; + } else { + if (!empty($post->post_password)) { // if there's a password + if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie + echo('Enter your password to view comments'); + return; + } + } + echo ''; + comments_number($zero, $one, $more, $number); + echo ''; + } + } +} + +function get_comment_ID() { + global $comment; + return apply_filters('get_comment_ID', $comment->comment_ID); +} + +function comment_ID() { + echo get_comment_ID(); +} + +function get_comment_author() { + global $comment; + if ( empty($comment->comment_author) ) + $author = __('Anonymous'); + else + $author = $comment->comment_author; + return apply_filters('get_comment_author', $author); +} + +function comment_author() { + $author = apply_filters('comment_author', get_comment_author() ); + echo $author; +} + +function get_comment_author_email() { + global $comment; + return apply_filters('get_comment_author_email', $comment->comment_author_email); +} + +function comment_author_email() { + echo apply_filters('author_email', get_comment_author_email() ); +} + +function get_comment_author_link() { + global $comment; + $url = get_comment_author_url(); + $author = get_comment_author(); + + if ( empty( $url ) || 'http://' == $url ) + $return = $author; + else + $return = "$author"; + return apply_filters('get_comment_author_link', $return); +} + +function comment_author_link() { + echo get_comment_author_link(); +} + +function get_comment_type() { + global $comment; + + if ( '' == $comment->comment_type ) + $comment->comment_type = 'comment'; + + return apply_filters('get_comment_type', $comment->comment_type); +} + +function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') { + $type = get_comment_type(); + switch( $type ) { + case 'trackback' : + echo $trackbacktxt; + break; + case 'pingback' : + echo $pingbacktxt; + break; + default : + echo $commenttxt; + } +} + +function get_comment_author_url() { + global $comment; + return apply_filters('get_comment_author_url', $comment->comment_author_url); +} + +function comment_author_url() { + echo apply_filters('comment_url', get_comment_author_url()); +} + +function comment_author_email_link($linktext='', $before='', $after='') { + global $comment; + $email = apply_filters('comment_email', $comment->comment_author_email); + if ((!empty($email)) && ($email != '@')) { + $display = ($linktext != '') ? $linktext : $email; + echo $before; + echo "$display"; + echo $after; + } +} + +function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) { + global $comment; + $url = get_comment_author_url(); + $display = ($linktext != '') ? $linktext : $url; + $return = "$before$display$after"; + return apply_filters('get_comment_author_url_link', $return); +} + +function comment_author_url_link( $linktext = '', $before = '', $after = '' ) { + echo get_comment_author_url_link( $linktext, $before, $after ); +} + +function get_comment_author_IP() { + global $comment; + return apply_filters('get_comment_author_IP', $comment->comment_author_IP); +} + +function comment_author_IP() { + echo get_comment_author_IP(); +} + +function get_comment_text() { + global $comment; + return apply_filters('get_comment_text', $comment->comment_content); +} + +function comment_text() { + echo apply_filters('comment_text', get_comment_text() ); +} + +function get_comment_excerpt() { + global $comment; + $comment_text = strip_tags($comment->comment_content); + $blah = explode(' ', $comment_text); + if (count($blah) > 20) { + $k = 20; + $use_dotdotdot = 1; + } else { + $k = count($blah); + $use_dotdotdot = 0; + } + $excerpt = ''; + for ($i=0; $i<$k; $i++) { + $excerpt .= $blah[$i] . ' '; + } + $excerpt .= ($use_dotdotdot) ? '...' : ''; + return apply_filters('get_comment_excerpt', $excerpt); +} + +function comment_excerpt() { + echo apply_filters('comment_excerpt', get_comment_excerpt() ); +} + +function get_comment_date( $d = '' ) { + global $comment; + if ( '' == $d ) + $date = mysql2date( get_settings('date_format'), $comment->comment_date); + else + $date = mysql2date($d, $comment->comment_date); + return apply_filters('get_comment_date', $date); +} + +function comment_date( $d = '' ) { + echo get_comment_date( $d ); +} + +function get_comment_time( $d = '', $gmt = false ) { + global $comment; + $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date; + if ( '' == $d ) + $date = mysql2date(get_settings('time_format'), $comment_date); + else + $date = mysql2date($d, $comment_date); + return apply_filters('get_comment_time', $date); +} + +function comment_time( $d = '' ) { + echo get_comment_time($d); +} + +function get_trackback_url() { + global $id; + $tb_url = get_settings('siteurl') . '/wp-trackback.php?p=' . $id; + + if ( '' != get_settings('permalink_structure') ) + $tb_url = trailingslashit(get_permalink()) . 'trackback/'; + + return $tb_url; +} +function trackback_url( $display = true ) { + if ( $display) + echo get_trackback_url(); + else + return get_trackback_url(); +} + +function trackback_rdf($timezone = 0) { + global $id; + if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) { + echo ' + \n"; + echo ''; + } +} + +function comments_open() { + global $post; + if ( 'open' == $post->comment_status ) + return true; + else + return false; +} + +function pings_open() { + global $post; + if ( 'open' == $post->ping_status ) + return true; + else + return false; +} + +// Non-template functions + +function get_lastcommentmodified($timezone = 'server') { + global $tablecomments, $cache_lastcommentmodified, $pagenow, $wpdb; + $add_seconds_blog = get_settings('gmt_offset') * 3600; + $add_seconds_server = date('Z'); + $now = current_time('mysql', 1); + if ( !isset($cache_lastcommentmodified[$timezone]) ) { + switch(strtolower($timezone)) { + case 'gmt': + $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1"); + break; + case 'blog': + $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1"); + break; + case 'server': + $lastcommentmodified = $wpdb->get_var("SELECT DATE_ADD(comment_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1"); + break; + } + $cache_lastcommentmodified[$timezone] = $lastcommentmodified; + } else { + $lastcommentmodified = $cache_lastcommentmodified[$timezone]; + } + return $lastcommentmodified; +} + +function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries + global $postc, $id, $commentdata, $wpdb; + if ($no_cache) { + $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'"; + if (false == $include_unapproved) { + $query .= " AND comment_approved = '1'"; + } + $myrow = $wpdb->get_row($query, ARRAY_A); + } else { + $myrow['comment_ID'] = $postc->comment_ID; + $myrow['comment_post_ID'] = $postc->comment_post_ID; + $myrow['comment_author'] = $postc->comment_author; + $myrow['comment_author_email'] = $postc->comment_author_email; + $myrow['comment_author_url'] = $postc->comment_author_url; + $myrow['comment_author_IP'] = $postc->comment_author_IP; + $myrow['comment_date'] = $postc->comment_date; + $myrow['comment_content'] = $postc->comment_content; + $myrow['comment_karma'] = $postc->comment_karma; + $myrow['comment_approved'] = $postc->comment_approved; + $myrow['comment_type'] = $postc->comment_type; + } + return $myrow; +} + +function pingback($content, $post_ID) { + global $wp_version, $wpdb; + include_once (ABSPATH . WPINC . '/class-IXR.php'); + + // original code by Mort (http://mort.mine.nu:8080) + $log = debug_fopen(ABSPATH . '/pingback.log', 'a'); + $post_links = array(); + debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n"); + + $pung = get_pung($post_ID); + + // Variables + $ltrs = '\w'; + $gunk = '/#~:.?+=&%@!\-'; + $punc = '.:?\-'; + $any = $ltrs . $gunk . $punc; + + // Step 1 + // Parsing the post, external links (if any) are stored in the $post_links array + // This regexp comes straight from phpfreaks.com + // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php + preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp); + + // Debug + debug_fwrite($log, 'Post contents:'); + debug_fwrite($log, $content."\n"); + + // Step 2. + // Walking thru the links array + // first we get rid of links pointing to sites, not to specific files + // Example: + // http://dummy-weblog.org + // http://dummy-weblog.org/ + // http://dummy-weblog.org/post.php + // We don't wanna ping first and second types, even if they have a valid + + foreach($post_links_temp[0] as $link_test) : + if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself + && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments. + $test = parse_url($link_test); + if (isset($test['query'])) + $post_links[] = $link_test; + elseif(($test['path'] != '/') && ($test['path'] != '')) + $post_links[] = $link_test; + do_action('pre_ping', array(&$post_links, &$pung)); + endif; + endforeach; + + foreach ($post_links as $pagelinkedto){ + debug_fwrite($log, "Processing -- $pagelinkedto\n"); + $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048); + + if ($pingback_server_url) { + @ set_time_limit( 60 ); + // Now, the RPC call + debug_fwrite($log, "Page Linked To: $pagelinkedto \n"); + debug_fwrite($log, 'Page Linked From: '); + $pagelinkedfrom = get_permalink($post_ID); + debug_fwrite($log, $pagelinkedfrom."\n"); + + // using a timeout of 3 seconds should be enough to cover slow servers + $client = new IXR_Client($pingback_server_url); + $client->timeout = 3; + $client->useragent .= ' -- WordPress/' . $wp_version; + + // when set to true, this outputs debug messages by itself + $client->debug = false; + + if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto ) ) + add_ping( $post_ID, $pagelinkedto ); + else + debug_fwrite($log, "Error.\n Fault code: ".$client->getErrorCode()." : ".$client->getErrorMessage()."\n"); + } + } + + debug_fwrite($log, "\nEND: ".time()."\n****************************\n"); + debug_fclose($log); +} + +function discover_pingback_server_uri($url, $timeout_bytes = 2048) { + global $wp_version; + + $byte_count = 0; + $contents = ''; + $headers = ''; + $pingback_str_dquote = 'rel="pingback"'; + $pingback_str_squote = 'rel=\'pingback\''; + $x_pingback_str = 'x-pingback: '; + $pingback_href_original_pos = 27; + + extract(parse_url($url)); + + if (!isset($host)) { + // Not an URL. This should never happen. + return false; + } + + $path = (!isset($path)) ? '/' : $path; + $path .= (isset($query)) ? '?'.$query : ''; + $port = (isset($port)) ? $port : 80; + + // Try to connect to the server at $host + $fp = @fsockopen($host, $port, $errno, $errstr, 2); + if (!$fp) { + // Couldn't open a connection to $host; + return false; + } + + // Send the GET request + $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n"; +// ob_end_flush(); + fputs($fp, $request); + + // Let's check for an X-Pingback header first + while (!feof($fp)) { + $line = fgets($fp, 512); + if (trim($line) == '') { + break; + } + $headers .= trim($line)."\n"; + $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str); + if ($x_pingback_header_offset) { + // We got it! + preg_match('#x-pingback: (.+)#is', $headers, $matches); + $pingback_server_url = trim($matches[1]); + return $pingback_server_url; + } + if(strpos(strtolower($headers), 'content-type: ')) { + preg_match('#content-type: (.+)#is', $headers, $matches); + $content_type = trim($matches[1]); + } + } + + if (preg_match('#(image|audio|video|model)/#is', $content_type)) { + // Not an (x)html, sgml, or xml page, no use going further + return false; + } + + while (!feof($fp)) { + $line = fgets($fp, 1024); + $contents .= trim($line); + $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote); + $pingback_link_offset_squote = strpos($contents, $pingback_str_squote); + if ($pingback_link_offset_dquote || $pingback_link_offset_squote) { + $quote = ($pingback_link_offset_dquote) ? '"' : '\''; + $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote; + $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset); + $pingback_href_start = $pingback_href_pos+6; + $pingback_href_end = @strpos($contents, $quote, $pingback_href_start); + $pingback_server_url_len = $pingback_href_end - $pingback_href_start; + $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len); + // We may find rel="pingback" but an incomplete pingback URI + if ($pingback_server_url_len > 0) { + // We got it! + return $pingback_server_url; + } + } + $byte_count += strlen($line); + if ($byte_count > $timeout_bytes) { + // It's no use going further, there probably isn't any pingback + // server to find in this file. (Prevents loading large files.) + return false; + } + } + + // We didn't find anything. + return false; +} + +function is_local_attachment($url) { + if ( !strstr($url, get_bloginfo('home') ) ) + return false; + if ( strstr($url, get_bloginfo('home') . '/?attachment_id=') ) + return true; + if ( $id = url_to_postid($url) ) { + $post = & get_post($id); + if ( 'attachment' == $post->post_status ) + return true; + } + return false; +} + +function wp_set_comment_status($comment_id, $comment_status) { + global $wpdb; + + switch($comment_status) { + case 'hold': + $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1"; + break; + case 'approve': + $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1"; + break; + case 'spam': + $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1"; + break; + case 'delete': + return wp_delete_comment($comment_id); + break; + default: + return false; + } + + if ($wpdb->query($query)) { + do_action('wp_set_comment_status', $comment_id, $comment_status); + + $comment = get_comment($comment_id); + $comment_post_ID = $comment->comment_post_ID; + $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); + if( is_object( $c ) ) + $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); + return true; + } else { + return false; + } +} + +function wp_get_comment_status($comment_id) { + global $wpdb; + + $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1"); + if ($result == NULL) { + return 'deleted'; + } else if ($result == '1') { + return 'approved'; + } else if ($result == '0') { + return 'unapproved'; + } else if ($result == 'spam') { + return 'spam'; + } else { + return false; + } +} + +function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) { + global $wpdb; + + if (1 == get_settings('comment_moderation')) return false; // If moderation is set to manual + + if ( (count(explode('http:', $comment)) - 1) >= get_settings('comment_max_links') ) + return false; // Check # of external links + + $mod_keys = trim( get_settings('moderation_keys') ); + if ( !empty($mod_keys) ) { + $words = explode("\n", $mod_keys ); + + foreach ($words as $word) { + $word = trim($word); + + // Skip empty lines + if (empty($word)) { continue; } + + // Do some escaping magic so that '#' chars in the + // spam words don't break things: + $word = preg_quote($word, '#'); + + $pattern = "#$word#i"; + if ( preg_match($pattern, $author) ) return false; + if ( preg_match($pattern, $email) ) return false; + if ( preg_match($pattern, $url) ) return false; + if ( preg_match($pattern, $comment) ) return false; + if ( preg_match($pattern, $user_ip) ) return false; + if ( preg_match($pattern, $user_agent) ) return false; + } + } + + // Comment whitelisting: + if ( 1 == get_settings('comment_whitelist')) { + if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll + $uri = parse_url($url); + $domain = $uri['host']; + $uri = parse_url( get_option('home') ); + $home_domain = $uri['host']; + if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") || $domain == $home_domain ) + return true; + else + return false; + } elseif( $author != '' && $email != '' ) { + $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1"); + if ( ( 1 == $ok_to_comment ) && + ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) ) + return true; + else + return false; + } else { + return false; + } + } + + return true; +} + +function get_approved_comments($post_id) { + global $wpdb; + return $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post_id AND comment_approved = '1' ORDER BY comment_date"); +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/default-filters.php b/projects/tests/tests/real/wordpress/wp-includes/default-filters.php new file mode 100644 index 00000000..e6ec97b4 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/default-filters.php @@ -0,0 +1,88 @@ + diff --git a/projects/tests/tests/real/wordpress/wp-includes/feed-functions.php b/projects/tests/tests/real/wordpress/wp-includes/feed-functions.php new file mode 100644 index 00000000..0b377bc3 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/feed-functions.php @@ -0,0 +1,164 @@ + $cut) { + $k = $cut; + $use_dotdotdot = 1; + } else { + $k = count($blah); + $use_dotdotdot = 0; + } + for ($i=0; $i<$k; $i++) { + $excerpt .= $blah[$i].' '; + } + $excerpt .= ($use_dotdotdot) ? '...' : ''; + $content = $excerpt; + } + $content = str_replace(']]>', ']]>', $content); + echo $content; +} + +function the_excerpt_rss() { + $output = get_the_excerpt(true); + echo apply_filters('the_excerpt_rss', $output); +} + +function permalink_single_rss($file = '') { + echo get_permalink(); +} + +function comment_link() { + echo get_comment_link(); +} + +function get_comment_author_rss() { + return apply_filters('comment_author_rss', get_comment_author() ); +} +function comment_author_rss() { + echo get_comment_author_rss(); +} + +function comment_text_rss() { + $comment_text = get_comment_text(); + $comment_text = apply_filters('comment_text_rss', $comment_text); + echo $comment_text; +} + +function comments_rss_link($link_text = 'Comments RSS', $commentsrssfilename = '') { + $url = comments_rss($commentsrssfilename); + echo "$link_text"; +} + +function comments_rss($commentsrssfilename = '') { + global $id; + + if ('' != get_settings('permalink_structure')) + $url = trailingslashit( get_permalink() ) . 'feed/'; + else + $url = get_settings('home') . "/$commentsrssfilename?feed=rss2&p=$id"; + + return apply_filters('post_comments_feed_link', $url); +} + +function get_author_rss_link($echo = false, $author_id, $author_nicename) { + $auth_ID = $author_id; + $permalink_structure = get_settings('permalink_structure'); + + if ('' == $permalink_structure) { + $link = get_settings('home') . '?feed=rss2&author=' . $author_id; + } else { + $link = get_author_link(0, $author_id, $author_nicename); + $link = $link . "feed/"; + } + + $link = apply_filters('author_feed_link', $link); + + if ($echo) echo $link; + return $link; +} + +function get_category_rss_link($echo = false, $cat_ID, $category_nicename) { + $permalink_structure = get_settings('permalink_structure'); + + if ('' == $permalink_structure) { + $link = get_settings('home') . '?feed=rss2&cat=' . $cat_ID; + } else { + $link = get_category_link($cat_ID); + $link = $link . "feed/"; + } + + $link = apply_filters('category_feed_link', $link); + + if ($echo) echo $link; + return $link; +} + +function the_category_rss($type = 'rss') { + $categories = get_the_category(); + $the_list = ''; + foreach ($categories as $category) { + $category->cat_name = convert_chars($category->cat_name); + if ('rdf' == $type) { + $the_list .= "\n\t$category->cat_name"; + } else { + $the_list .= "\n\t$category->cat_name"; + } + } + echo apply_filters('the_category_rss', $the_list, $type); +} + +function rss_enclosure() { + global $id, $post; + if (!empty($post->post_password) && ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password)) return; + + $custom_fields = get_post_custom(); + if( is_array( $custom_fields ) ) { + while( list( $key, $val ) = each( $custom_fields ) ) { + if( $key == 'enclosure' ) { + if (is_array($val)) { + foreach($val as $enc) { + $enclosure = split( "\n", $enc ); + print "\n"; + } + } + } + } + } +} + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-includes/functions-compat.php b/projects/tests/tests/real/wordpress/wp-includes/functions-compat.php new file mode 100644 index 00000000..a8ff6503 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/functions-compat.php @@ -0,0 +1,155 @@ + + if (get_class($object) == strtolower($class)) { + return true; + } else { + return is_subclass_of($object, $class); + } + } +} + +if (!function_exists('ob_clean')) { + function ob_clean() { + // by Aidan Lister + if (@ob_end_clean()) { + return ob_start(); + } + return false; + } +} + + +/* Added in PHP 4.3.0 */ + +function printr($var, $do_not_echo = false) { + // from php.net/print_r user contributed notes + ob_start(); + print_r($var); + $code = htmlentities(ob_get_contents()); + ob_clean(); + if (!$do_not_echo) { + echo "
    $code
    "; + } + return $code; +} + +if (!defined('CASE_LOWER')) { + define('CASE_LOWER', 0); +} + +if (!defined('CASE_UPPER')) { + define('CASE_UPPER', 1); +} + + +/** + * Replace array_change_key_case() + * + * @category PHP + * @package PHP_Compat + * @link http://php.net/function.array_change_key_case + * @author Stephan Schmidt + * @author Aidan Lister + * @version $Revision: 3280 $ + * @since PHP 4.2.0 + * @require PHP 4.0.0 (user_error) + */ +if (!function_exists('array_change_key_case')) { + function array_change_key_case($input, $case = CASE_LOWER) + { + if (!is_array($input)) { + user_error('array_change_key_case(): The argument should be an array', + E_USER_WARNING); + return false; + } + + $output = array (); + $keys = array_keys($input); + $casefunc = ($case == CASE_LOWER) ? 'strtolower' : 'strtoupper'; + + foreach ($keys as $key) { + $output[$casefunc($key)] = $input[$key]; + } + + return $output; + } +} + +/* Added in PHP 4.3.0 */ + +if( !function_exists('glob') ): +function glob($pattern) { + // get pathname (everything up until the last / or \) + $path=$output=null; +// if(PHP_OS=='WIN32') +// $slash='\\'; +// else +// $slash='/'; + $slash = '/'; + $lastpos=strrpos($pattern,$slash); + if(!($lastpos===false)) { + $path=substr($pattern,0,$lastpos); #negative length means take from the right + $pattern=substr($pattern,$lastpos+1); + } else { + //no dir info, use current dir + $path=getcwd(); + } + $handle=@ opendir($path); + if($handle===false) + return false; + while($dir=readdir($handle)) { + if ( '.' == $dir || '..' == $dir ) + continue; + if (pattern_match($pattern,$dir)) + $output[]=$path . '/' . $dir; + } + closedir($handle); + if(is_array($output)) + return $output; + + return false; +} + +function pattern_match($pattern,$string) { + // basically prepare a regular expression + $out=null; + $chunks=explode(';',$pattern); + foreach($chunks as $pattern) { + $escape=array('$','^','.','{','}','(',')','[',']','|'); + while(strpos($pattern,'**')!==false) + $pattern=str_replace('**','*',$pattern); + foreach($escape as $probe) + $pattern=str_replace($probe,"\\$probe",$pattern); + + $pattern=str_replace('?*','*', + str_replace('*?','*', + str_replace('*',".*", + str_replace('?','.{1,1}',$pattern)))); + $out[]=$pattern; + } + + if(count($out)==1) + return(eregi("^$out[0]$",$string)); + else + foreach($out as $tester) + if(eregi("^$tester$",$string)) + return true; + return false; +} +endif; + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/functions-formatting.php b/projects/tests/tests/real/wordpress/wp-includes/functions-formatting.php new file mode 100644 index 00000000..255b6ccc --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/functions-formatting.php @@ -0,0 +1,1011 @@ +)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE); + $stop = count($textarr); $next = true; // loop stuff + for ($i = 0; $i < $stop; $i++) { + $curl = $textarr[$i]; + + if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag + $curl = str_replace('---', '—', $curl); + $curl = str_replace(' -- ', ' — ', $curl); + $curl = str_replace('--', '–', $curl); + $curl = str_replace('xn–', 'xn--', $curl); + $curl = str_replace('...', '…', $curl); + $curl = str_replace('``', '“', $curl); + + // This is a hack, look at this more later. It works pretty well though. + $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause"); + $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause"); + $curl = str_replace($cockney, $cockneyreplace, $curl); + + $curl = preg_replace("/'s/", '’s', $curl); + $curl = preg_replace("/'(\d\d(?:’|')?s)/", "’$1", $curl); + $curl = preg_replace('/(\s|\A|")\'/', '$1‘', $curl); + $curl = preg_replace('/(\d+)"/', '$1″', $curl); + $curl = preg_replace("/(\d+)'/", '$1′', $curl); + $curl = preg_replace("/(\S)'([^'\s])/", "$1’$2", $curl); + $curl = preg_replace('/(\s|\A)"(?!\s)/', '$1“$2', $curl); + $curl = preg_replace('/"(\s|\S|\Z)/', '”$1', $curl); + $curl = preg_replace("/'([\s.]|\Z)/", '’$1', $curl); + $curl = preg_replace("/ \(tm\)/i", ' ™', $curl); + $curl = str_replace("''", '”', $curl); + + $curl = preg_replace('/(\d+)x(\d+)/', "$1×$2", $curl); + + } elseif (strstr($curl, '', '', $text); + $text = str_replace('

    ', "\n", $text); + $text = str_replace('

    ', '', $text); + return $text; +} + +function wpautop($pee, $br = 1) { + $pee = $pee . "\n"; // just to make things a little easier, pad the end + $pee = preg_replace('|
    \s*
    |', "\n\n", $pee); + // Space things out a little + $pee = preg_replace('!(<(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)!', "\n$1", $pee); + $pee = preg_replace('!()!', "$1\n\n", $pee); + $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines + $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates + $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "

    $1

    \n", $pee); // make paragraphs, including one at the end + $pee = preg_replace('|

    \s*?

    |', '', $pee); // under certain strange conditions it could create a P of entirely whitespace + $pee = preg_replace('!

    \s*(]*>)\s*

    !', "$1", $pee); // don't pee all over a tag + $pee = preg_replace("|

    (|", "$1", $pee); // problem with nested lists + $pee = preg_replace('|

    ]*)>|i', "

    ", $pee); + $pee = str_replace('

    ', '

    ', $pee); + $pee = preg_replace('!

    \s*(]*>)!', "$1", $pee); + $pee = preg_replace('!(]*>)\s*

    !', "$1", $pee); + if ($br) $pee = preg_replace('|(?)\s*\n|', "
    \n", $pee); // optionally make line breaks + $pee = preg_replace('!(]*>)\s*
    !', "$1", $pee); + $pee = preg_replace('!
    (\s*)!', '$1', $pee); + $pee = preg_replace('!()(.*?)
    !ise', " stripslashes('$1') . stripslashes(clean_pre('$2')) . '' ", $pee); + + return $pee; +} + + +function seems_utf8($Str) { # by bmorel at ssi dot fr + for ($i=0; $i', '>', $text); + if ( $quotes ) { + $text = str_replace('"', '"', $text); + $text = str_replace("'", ''', $text); + } + return $text; +} + +function utf8_uri_encode( $utf8_string ) { + $unicode = ''; + $values = array(); + $num_octets = 1; + + for ($i = 0; $i < strlen( $utf8_string ); $i++ ) { + + $value = ord( $utf8_string[ $i ] ); + + if ( $value < 128 ) { + $unicode .= chr($value); + } else { + if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3; + + $values[] = $value; + + if ( count( $values ) == $num_octets ) { + if ($num_octets == 3) { + $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]); + } else { + $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]); + } + + $values = array(); + $num_octets = 1; + } + } + } + + return $unicode; +} + +function remove_accents($string) { + if (seems_utf8($string)) { + $chars = array( + // Decompositions for Latin-1 Supplement + chr(195).chr(128) => 'A', chr(195).chr(129) => 'A', + chr(195).chr(130) => 'A', chr(195).chr(131) => 'A', + chr(195).chr(132) => 'A', chr(195).chr(133) => 'A', + chr(195).chr(135) => 'C', chr(195).chr(136) => 'E', + chr(195).chr(137) => 'E', chr(195).chr(138) => 'E', + chr(195).chr(139) => 'E', chr(195).chr(140) => 'I', + chr(195).chr(141) => 'I', chr(195).chr(142) => 'I', + chr(195).chr(143) => 'I', chr(195).chr(145) => 'N', + chr(195).chr(146) => 'O', chr(195).chr(147) => 'O', + chr(195).chr(148) => 'O', chr(195).chr(149) => 'O', + chr(195).chr(150) => 'O', chr(195).chr(153) => 'U', + chr(195).chr(154) => 'U', chr(195).chr(155) => 'U', + chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y', + chr(195).chr(159) => 's', chr(195).chr(160) => 'a', + chr(195).chr(161) => 'a', chr(195).chr(162) => 'a', + chr(195).chr(163) => 'a', chr(195).chr(164) => 'a', + chr(195).chr(165) => 'a', chr(195).chr(167) => 'c', + chr(195).chr(168) => 'e', chr(195).chr(169) => 'e', + chr(195).chr(170) => 'e', chr(195).chr(171) => 'e', + chr(195).chr(172) => 'i', chr(195).chr(173) => 'i', + chr(195).chr(174) => 'i', chr(195).chr(175) => 'i', + chr(195).chr(177) => 'n', chr(195).chr(178) => 'o', + chr(195).chr(179) => 'o', chr(195).chr(180) => 'o', + chr(195).chr(181) => 'o', chr(195).chr(182) => 'o', + chr(195).chr(182) => 'o', chr(195).chr(185) => 'u', + chr(195).chr(186) => 'u', chr(195).chr(187) => 'u', + chr(195).chr(188) => 'u', chr(195).chr(189) => 'y', + chr(195).chr(191) => 'y', + // Decompositions for Latin Extended-A + chr(196).chr(128) => 'A', chr(196).chr(129) => 'a', + chr(196).chr(130) => 'A', chr(196).chr(131) => 'a', + chr(196).chr(132) => 'A', chr(196).chr(133) => 'a', + chr(196).chr(134) => 'C', chr(196).chr(135) => 'c', + chr(196).chr(136) => 'C', chr(196).chr(137) => 'c', + chr(196).chr(138) => 'C', chr(196).chr(139) => 'c', + chr(196).chr(140) => 'C', chr(196).chr(141) => 'c', + chr(196).chr(142) => 'D', chr(196).chr(143) => 'd', + chr(196).chr(144) => 'D', chr(196).chr(145) => 'd', + chr(196).chr(146) => 'E', chr(196).chr(147) => 'e', + chr(196).chr(148) => 'E', chr(196).chr(149) => 'e', + chr(196).chr(150) => 'E', chr(196).chr(151) => 'e', + chr(196).chr(152) => 'E', chr(196).chr(153) => 'e', + chr(196).chr(154) => 'E', chr(196).chr(155) => 'e', + chr(196).chr(156) => 'G', chr(196).chr(157) => 'g', + chr(196).chr(158) => 'G', chr(196).chr(159) => 'g', + chr(196).chr(160) => 'G', chr(196).chr(161) => 'g', + chr(196).chr(162) => 'G', chr(196).chr(163) => 'g', + chr(196).chr(164) => 'H', chr(196).chr(165) => 'h', + chr(196).chr(166) => 'H', chr(196).chr(167) => 'h', + chr(196).chr(168) => 'I', chr(196).chr(169) => 'i', + chr(196).chr(170) => 'I', chr(196).chr(171) => 'i', + chr(196).chr(172) => 'I', chr(196).chr(173) => 'i', + chr(196).chr(174) => 'I', chr(196).chr(175) => 'i', + chr(196).chr(176) => 'I', chr(196).chr(177) => 'i', + chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij', + chr(196).chr(180) => 'J', chr(196).chr(181) => 'j', + chr(196).chr(182) => 'K', chr(196).chr(183) => 'k', + chr(196).chr(184) => 'k', chr(196).chr(185) => 'L', + chr(196).chr(186) => 'l', chr(196).chr(187) => 'L', + chr(196).chr(188) => 'l', chr(196).chr(189) => 'L', + chr(196).chr(190) => 'l', chr(196).chr(191) => 'L', + chr(197).chr(128) => 'l', chr(197).chr(129) => 'L', + chr(197).chr(130) => 'l', chr(197).chr(131) => 'N', + chr(197).chr(132) => 'n', chr(197).chr(133) => 'N', + chr(197).chr(134) => 'n', chr(197).chr(135) => 'N', + chr(197).chr(136) => 'n', chr(197).chr(137) => 'N', + chr(197).chr(138) => 'n', chr(197).chr(139) => 'N', + chr(197).chr(140) => 'O', chr(197).chr(141) => 'o', + chr(197).chr(142) => 'O', chr(197).chr(143) => 'o', + chr(197).chr(144) => 'O', chr(197).chr(145) => 'o', + chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe', + chr(197).chr(148) => 'R',chr(197).chr(149) => 'r', + chr(197).chr(150) => 'R',chr(197).chr(151) => 'r', + chr(197).chr(152) => 'R',chr(197).chr(153) => 'r', + chr(197).chr(154) => 'S',chr(197).chr(155) => 's', + chr(197).chr(156) => 'S',chr(197).chr(157) => 's', + chr(197).chr(158) => 'S',chr(197).chr(159) => 's', + chr(197).chr(160) => 'S', chr(197).chr(161) => 's', + chr(197).chr(162) => 'T', chr(197).chr(163) => 't', + chr(197).chr(164) => 'T', chr(197).chr(165) => 't', + chr(197).chr(166) => 'T', chr(197).chr(167) => 't', + chr(197).chr(168) => 'U', chr(197).chr(169) => 'u', + chr(197).chr(170) => 'U', chr(197).chr(171) => 'u', + chr(197).chr(172) => 'U', chr(197).chr(173) => 'u', + chr(197).chr(174) => 'U', chr(197).chr(175) => 'u', + chr(197).chr(176) => 'U', chr(197).chr(177) => 'u', + chr(197).chr(178) => 'U', chr(197).chr(179) => 'u', + chr(197).chr(180) => 'W', chr(197).chr(181) => 'w', + chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y', + chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z', + chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z', + chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z', + chr(197).chr(190) => 'z', chr(197).chr(191) => 's', + // Euro Sign + chr(226).chr(130).chr(172) => 'E'); + + $string = strtr($string, $chars); + } else { + // Assume ISO-8859-1 if not UTF-8 + $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158) + .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194) + .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202) + .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210) + .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218) + .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227) + .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235) + .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243) + .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251) + .chr(252).chr(253).chr(255); + + $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy"; + + $string = strtr($string, $chars['in'], $chars['out']); + $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254)); + $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th'); + $string = str_replace($double_chars['in'], $double_chars['out'], $string); + } + + return $string; +} + +function sanitize_user( $username ) { + return preg_replace('|a-z0-9 _.-|i', '', $username); +} + +function sanitize_title($title, $fallback_title = '') { + $title = strip_tags($title); + $title = apply_filters('sanitize_title', $title); + + if (empty($title)) { + $title = $fallback_title; + } + + return $title; +} + +function sanitize_title_with_dashes($title) { + $title = strip_tags($title); + // Preserve escaped octets. + $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title); + // Remove percent signs that are not part of an octet. + $title = str_replace('%', '', $title); + // Restore octets. + $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title); + + $title = remove_accents($title); + if (seems_utf8($title)) { + if (function_exists('mb_strtolower')) { + $title = mb_strtolower($title, 'UTF-8'); + } + $title = utf8_uri_encode($title); + } + + $title = strtolower($title); + $title = preg_replace('/&.+?;/', '', $title); // kill entities + $title = preg_replace('/[^%a-z0-9 _-]/', '', $title); + $title = preg_replace('/\s+/', '-', $title); + $title = preg_replace('|-+|', '-', $title); + $title = trim($title, '-'); + + return $title; +} + +function convert_chars($content, $flag = 'obsolete') { + // Translation of invalid Unicode references range to valid range + $wp_htmltranswinuni = array( + '€' => '€', // the Euro sign + '' => '', + '‚' => '‚', // these are Windows CP1252 specific characters + 'ƒ' => 'ƒ', // they would look weird on non-Windows browsers + '„' => '„', + '…' => '…', + '†' => '†', + '‡' => '‡', + 'ˆ' => 'ˆ', + '‰' => '‰', + 'Š' => 'Š', + '‹' => '‹', + 'Œ' => 'Œ', + '' => '', + 'Ž' => 'ž', + '' => '', + '' => '', + '‘' => '‘', + '’' => '’', + '“' => '“', + '”' => '”', + '•' => '•', + '–' => '–', + '—' => '—', + '˜' => '˜', + '™' => '™', + 'š' => 'š', + '›' => '›', + 'œ' => 'œ', + '' => '', + 'ž' => '', + 'Ÿ' => 'Ÿ' + ); + + // Remove metadata tags + $content = preg_replace('/(.+?)<\/title>/','',$content); + $content = preg_replace('/<category>(.+?)<\/category>/','',$content); + + // Converts lone & characters into & (a.k.a. &) + $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content); + + // Fix Word pasting + $content = strtr($content, $wp_htmltranswinuni); + + // Just a little XHTML help + $content = str_replace('<br>', '<br />', $content); + $content = str_replace('<hr>', '<hr />', $content); + + return $content; +} + +function funky_javascript_fix($text) { + // Fixes for browsers' javascript bugs + global $is_macIE, $is_winIE; + + if ( $is_winIE || $is_macIE ) + $text = preg_replace("/\%u([0-9A-F]{4,4})/e", "'&#'.base_convert('\\1',16,10).';'", $text); + + return $text; +} + +/* + balanceTags + + Balances Tags of string using a modified stack. + + @param text Text to be balanced + @return Returns balanced text + @author Leonard Lin (leonard@acm.org) + @version v1.1 + @date November 4, 2001 + @license GPL v2.0 + @notes + @changelog + --- Modified by Scott Reilly (coffee2code) 02 Aug 2004 + 1.2 ***TODO*** Make better - change loop condition to $text + 1.1 Fixed handling of append/stack pop order of end text + Added Cleaning Hooks + 1.0 First Version +*/ +function balanceTags($text, $is_comment = 0) { + + if ( get_option('use_balanceTags') == 0) + return $text; + + $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = ''; + + # WP bug fix for comments - in case you REALLY meant to type '< !--' + $text = str_replace('< !--', '< !--', $text); + # WP bug fix for LOVE <3 (and other situations with '<' before a number) + $text = preg_replace('#<([0-9]{1})#', '<$1', $text); + + while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) { + $newtext .= $tagqueue; + + $i = strpos($text,$regex[0]); + $l = strlen($regex[0]); + + // clear the shifter + $tagqueue = ''; + // Pop or Push + if ($regex[1][0] == "/") { // End Tag + $tag = strtolower(substr($regex[1],1)); + // if too many closing tags + if($stacksize <= 0) { + $tag = ''; + //or close to be safe $tag = '/' . $tag; + } + // if stacktop value = tag close value then pop + else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag + $tag = '</' . $tag . '>'; // Close Tag + // Pop + array_pop ($tagstack); + $stacksize--; + } else { // closing tag not at top, search for it + for ($j=$stacksize-1;$j>=0;$j--) { + if ($tagstack[$j] == $tag) { + // add tag to tagqueue + for ($k=$stacksize-1;$k>=$j;$k--){ + $tagqueue .= '</' . array_pop ($tagstack) . '>'; + $stacksize--; + } + break; + } + } + $tag = ''; + } + } else { // Begin Tag + $tag = strtolower($regex[1]); + + // Tag Cleaning + + // If self-closing or '', don't do anything. + if((substr($regex[2],-1) == '/') || ($tag == '')) { + } + // ElseIf it's a known single-entity tag but it doesn't close itself, do so + elseif ($tag == 'br' || $tag == 'img' || $tag == 'hr' || $tag == 'input') { + $regex[2] .= '/'; + } else { // Push the tag onto the stack + // If the top of the stack is the same as the tag we want to push, close previous tag + if (($stacksize > 0) && ($tag != 'div') && ($tagstack[$stacksize - 1] == $tag)) { + $tagqueue = '</' . array_pop ($tagstack) . '>'; + $stacksize--; + } + $stacksize = array_push ($tagstack, $tag); + } + + // Attributes + $attributes = $regex[2]; + if($attributes) { + $attributes = ' '.$attributes; + } + $tag = '<'.$tag.$attributes.'>'; + //If already queuing a close tag, then put this tag on, too + if ($tagqueue) { + $tagqueue .= $tag; + $tag = ''; + } + } + $newtext .= substr($text,0,$i) . $tag; + $text = substr($text,$i+$l); + } + + // Clear Tag Queue + $newtext .= $tagqueue; + + // Add Remaining text + $newtext .= $text; + + // Empty Stack + while($x = array_pop($tagstack)) { + $newtext .= '</' . $x . '>'; // Add remaining tags to close + } + + // WP fix for the bug with HTML comments + $newtext = str_replace("< !--","<!--",$newtext); + $newtext = str_replace("< !--","< !--",$newtext); + + return $newtext; +} + + +function format_to_edit($content, $richedit = false) { + $content = apply_filters('format_to_edit', $content); + if (! $richedit ) + $content = htmlspecialchars($content); + return $content; +} + +function format_to_post($content) { + global $wpdb; + $content = apply_filters('format_to_post', $content); + return $content; +} + +function zeroise($number,$threshold) { // function to add leading zeros when necessary + return sprintf('%0'.$threshold.'s', $number); + } + + +function backslashit($string) { + $string = preg_replace('/([a-z])/i', '\\\\\1', $string); + return $string; +} + +function trailingslashit($string) { + if ( '/' != substr($string, -1)) { + $string .= '/'; + } + return $string; +} + +function addslashes_gpc($gpc) { + global $wpdb; + + if (get_magic_quotes_gpc()) { + $gpc = stripslashes($gpc); + } + + return $wpdb->escape($gpc); +} + + +function stripslashes_deep($value) +{ + $value = is_array($value) ? + array_map('stripslashes_deep', $value) : + stripslashes($value); + + return $value; +} + +function antispambot($emailaddy, $mailto=0) { + $emailNOSPAMaddy = ''; + srand ((float) microtime() * 1000000); + for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) { + $j = floor(rand(0, 1+$mailto)); + if ($j==0) { + $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';'; + } elseif ($j==1) { + $emailNOSPAMaddy .= substr($emailaddy,$i,1); + } elseif ($j==2) { + $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2); + } + } + $emailNOSPAMaddy = str_replace('@','@',$emailNOSPAMaddy); + return $emailNOSPAMaddy; +} + +function make_clickable($ret) { + $ret = ' ' . $ret . ' '; + $ret = preg_replace("#([\s>])(https?)://([^\s<>{}()]+[^\s.,<>{}()])#i", "$1<a href='$2://$3' rel='nofollow'>$2://$3</a>", $ret); + $ret = preg_replace("#(\s)www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^ <>{}()\n\r]*[^., <>{}()\n\r]?)?)#i", "$1<a href='http://www.$2.$3$4' rel='nofollow'>www.$2.$3$4</a>", $ret); + $ret = preg_replace("#(\s)([a-z0-9\-_.]+)@([a-z0-9\-_.]+)\.([^,< \n\r]+)#i", "$1<a href=\"mailto:$2@$3.$4\">$2@$3.$4</a>", $ret); + $ret = trim($ret); + return $ret; +} + +function wp_rel_nofollow( $text ) { + $text = preg_replace('|<a (.+?)>|i', '<a $1 rel="nofollow">', $text); + return $text; +} + +function convert_smilies($text) { + global $wp_smiliessearch, $wp_smiliesreplace; + $output = ''; + if (get_settings('use_smilies')) { + // HTML loop taken from texturize function, could possible be consolidated + $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between + $stop = count($textarr);// loop stuff + for ($i = 0; $i < $stop; $i++) { + $content = $textarr[$i]; + if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag + $content = str_replace($wp_smiliessearch, $wp_smiliesreplace, $content); + } + $output .= $content; + } + } else { + // return default text. + $output = $text; + } + return $output; +} + + +function is_email($user_email) { + $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i"; + if(strstr($user_email, '@') && strstr($user_email, '.')) { + if (preg_match($chars, $user_email)) { + return true; + } else { + return false; + } + } else { + return false; + } +} + +// used by wp-mail to handle charsets in email subjects +function wp_iso_descrambler($string) { + /* this may only work with iso-8859-1, I'm afraid */ + if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) { + return $string; + } else { + $subject = str_replace('_', ' ', $matches[2]); + $subject = preg_replace('#\=([0-9a-f]{2})#ei', "chr(hexdec(strtolower('$1')))", $subject); + return $subject; + } +} + + +// give it a date, it will give you the same date as GMT +function get_gmt_from_date($string) { + // note: this only substracts $time_difference from the given date + preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches); + $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); + $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_settings('gmt_offset') * 3600); + return $string_gmt; +} + +// give it a GMT date, it will give you the same date with $time_difference added +function get_date_from_gmt($string) { + // note: this only adds $time_difference to the given date + preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches); + $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); + $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_settings('gmt_offset')*3600); + return $string_localtime; +} + +// computes an offset in seconds from an iso8601 timezone +function iso8601_timezone_to_offset($timezone) { + // $timezone is either 'Z' or '[+|-]hhmm' + if ($timezone == 'Z') { + $offset = 0; + } else { + $sign = (substr($timezone, 0, 1) == '+') ? 1 : -1; + $hours = intval(substr($timezone, 1, 2)); + $minutes = intval(substr($timezone, 3, 4)) / 60; + $offset = $sign * 3600 * ($hours + $minutes); + } + return $offset; +} + +// converts an iso8601 date to MySQL DateTime format used by post_date[_gmt] +function iso8601_to_datetime($date_string, $timezone = USER) { + if ($timezone == GMT) { + preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits); + if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset + $offset = iso8601_timezone_to_offset($date_bits[7]); + } else { // we don't have a timezone, so we assume user local timezone (not server's!) + $offset = 3600 * get_settings('gmt_offset'); + } + $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]); + $timestamp -= $offset; + return gmdate('Y-m-d H:i:s', $timestamp); + } elseif ($timezone == USER) { + return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string); + } +} + +function popuplinks($text) { + // Comment text in popup windows should be filtered through this. + // Right now it's a moderately dumb function, ideally it would detect whether + // a target or rel attribute was already there and adjust its actions accordingly. + $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text); + return $text; +} + +function sanitize_email($email) { + return preg_replace('/[^a-z0-9+_.@-]/i', '', $email); +} + +function human_time_diff( $from, $to = '' ) { + if ( empty($to) ) + $to = time(); + $diff = (int) abs($to - $from); + if ($diff <= 3600) { + $mins = round($diff / 60); + if ($mins <= 1) + $since = __('1 min'); + else + $since = sprintf( __('%s mins'), $mins); + } else if (($diff <= 86400) && ($diff > 3600)) { + $hours = round($diff / 3600); + if ($hours <= 1) + $since = __('1 hour'); + else + $since = sprintf( __('%s hours'), $hours ); + } elseif ($diff >= 86400) { + $days = round($diff / 86400); + if ($days <= 1) + $since = __('1 day'); + else + $since = sprintf( __('%s days'), $days ); + } + return $since; +} + +function wp_trim_excerpt($text) { // Fakes an excerpt if needed + global $post; + if ( '' == $text ) { + $text = $post->post_content; + $text = apply_filters('the_content', $text); + $text = str_replace(']]>', ']]>', $text); + $text = strip_tags($text); + $excerpt_length = 55; + $words = explode(' ', $text, $excerpt_length + 1); + if (count($words) > $excerpt_length) { + array_pop($words); + array_push($words, '[...]'); + $text = implode(' ', $words); + } + } + return $text; +} + +function ent2ncr($text) { + $to_ncr = array( + '"' => '"', + '&' => '&', + '⁄' => '/', + '<' => '<', + '>' => '>', + '|' => '|', + ' ' => ' ', + '¡' => '¡', + '¢' => '¢', + '£' => '£', + '¤' => '¤', + '¥' => '¥', + '¦' => '¦', + '&brkbar;' => '¦', + '§' => '§', + '¨' => '¨', + '¨' => '¨', + '©' => '©', + 'ª' => 'ª', + '«' => '«', + '¬' => '¬', + '­' => '­', + '®' => '®', + '¯' => '¯', + '&hibar;' => '¯', + '°' => '°', + '±' => '±', + '²' => '²', + '³' => '³', + '´' => '´', + 'µ' => 'µ', + '¶' => '¶', + '·' => '·', + '¸' => '¸', + '¹' => '¹', + 'º' => 'º', + '»' => '»', + '¼' => '¼', + '½' => '½', + '¾' => '¾', + '¿' => '¿', + 'À' => 'À', + 'Á' => 'Á', + 'Â' => 'Â', + 'Ã' => 'Ã', + 'Ä' => 'Ä', + 'Å' => 'Å', + 'Æ' => 'Æ', + 'Ç' => 'Ç', + 'È' => 'È', + 'É' => 'É', + 'Ê' => 'Ê', + 'Ë' => 'Ë', + 'Ì' => 'Ì', + 'Í' => 'Í', + 'Î' => 'Î', + 'Ï' => 'Ï', + 'Ð' => 'Ð', + 'Ñ' => 'Ñ', + 'Ò' => 'Ò', + 'Ó' => 'Ó', + 'Ô' => 'Ô', + 'Õ' => 'Õ', + 'Ö' => 'Ö', + '×' => '×', + 'Ø' => 'Ø', + 'Ù' => 'Ù', + 'Ú' => 'Ú', + 'Û' => 'Û', + 'Ü' => 'Ü', + 'Ý' => 'Ý', + 'Þ' => 'Þ', + 'ß' => 'ß', + 'à' => 'à', + 'á' => 'á', + 'â' => 'â', + 'ã' => 'ã', + 'ä' => 'ä', + 'å' => 'å', + 'æ' => 'æ', + 'ç' => 'ç', + 'è' => 'è', + 'é' => 'é', + 'ê' => 'ê', + 'ë' => 'ë', + 'ì' => 'ì', + 'í' => 'í', + 'î' => 'î', + 'ï' => 'ï', + 'ð' => 'ð', + 'ñ' => 'ñ', + 'ò' => 'ò', + 'ó' => 'ó', + 'ô' => 'ô', + 'õ' => 'õ', + 'ö' => 'ö', + '÷' => '÷', + 'ø' => 'ø', + 'ù' => 'ù', + 'ú' => 'ú', + 'û' => 'û', + 'ü' => 'ü', + 'ý' => 'ý', + 'þ' => 'þ', + 'ÿ' => 'ÿ', + 'Œ' => 'Œ', + 'œ' => 'œ', + 'Š' => 'Š', + 'š' => 'š', + 'Ÿ' => 'Ÿ', + 'ƒ' => 'ƒ', + 'ˆ' => 'ˆ', + '˜' => '˜', + 'Α' => 'Α', + 'Β' => 'Β', + 'Γ' => 'Γ', + 'Δ' => 'Δ', + 'Ε' => 'Ε', + 'Ζ' => 'Ζ', + 'Η' => 'Η', + 'Θ' => 'Θ', + 'Ι' => 'Ι', + 'Κ' => 'Κ', + 'Λ' => 'Λ', + 'Μ' => 'Μ', + 'Ν' => 'Ν', + 'Ξ' => 'Ξ', + 'Ο' => 'Ο', + 'Π' => 'Π', + 'Ρ' => 'Ρ', + 'Σ' => 'Σ', + 'Τ' => 'Τ', + 'Υ' => 'Υ', + 'Φ' => 'Φ', + 'Χ' => 'Χ', + 'Ψ' => 'Ψ', + 'Ω' => 'Ω', + 'α' => 'α', + 'β' => 'β', + 'γ' => 'γ', + 'δ' => 'δ', + 'ε' => 'ε', + 'ζ' => 'ζ', + 'η' => 'η', + 'θ' => 'θ', + 'ι' => 'ι', + 'κ' => 'κ', + 'λ' => 'λ', + 'μ' => 'μ', + 'ν' => 'ν', + 'ξ' => 'ξ', + 'ο' => 'ο', + 'π' => 'π', + 'ρ' => 'ρ', + 'ς' => 'ς', + 'σ' => 'σ', + 'τ' => 'τ', + 'υ' => 'υ', + 'φ' => 'φ', + 'χ' => 'χ', + 'ψ' => 'ψ', + 'ω' => 'ω', + 'ϑ' => 'ϑ', + 'ϒ' => 'ϒ', + 'ϖ' => 'ϖ', + ' ' => ' ', + ' ' => ' ', + ' ' => ' ', + '‌' => '‌', + '‍' => '‍', + '‎' => '‎', + '‏' => '‏', + '–' => '–', + '—' => '—', + '‘' => '‘', + '’' => '’', + '‚' => '‚', + '“' => '“', + '”' => '”', + '„' => '„', + '†' => '†', + '‡' => '‡', + '•' => '•', + '…' => '…', + '‰' => '‰', + '′' => '′', + '″' => '″', + '‹' => '‹', + '›' => '›', + '‾' => '‾', + '⁄' => '⁄', + '€' => '€', + 'ℑ' => 'ℑ', + '℘' => '℘', + 'ℜ' => 'ℜ', + '™' => '™', + 'ℵ' => 'ℵ', + '↵' => '↵', + '⇐' => '⇐', + '⇑' => '⇑', + '⇒' => '⇒', + '⇓' => '⇓', + '⇔' => '⇔', + '∀' => '∀', + '∂' => '∂', + '∃' => '∃', + '∅' => '∅', + '∇' => '∇', + '∈' => '∈', + '∉' => '∉', + '∋' => '∋', + '∏' => '∏', + '∑' => '∑', + '−' => '−', + '∗' => '∗', + '√' => '√', + '∝' => '∝', + '∞' => '∞', + '∠' => '∠', + '∧' => '∧', + '∨' => '∨', + '∩' => '∩', + '∪' => '∪', + '∫' => '∫', + '∴' => '∴', + '∼' => '∼', + '≅' => '≅', + '≈' => '≈', + '≠' => '≠', + '≡' => '≡', + '≤' => '≤', + '≥' => '≥', + '⊂' => '⊂', + '⊃' => '⊃', + '⊄' => '⊄', + '⊆' => '⊆', + '⊇' => '⊇', + '⊕' => '⊕', + '⊗' => '⊗', + '⊥' => '⊥', + '⋅' => '⋅', + '⌈' => '⌈', + '⌉' => '⌉', + '⌊' => '⌊', + '⌋' => '⌋', + '⟨' => '〈', + '⟩' => '〉', + '←' => '←', + '↑' => '↑', + '→' => '→', + '↓' => '↓', + '↔' => '↔', + '◊' => '◊', + '♠' => '♠', + '♣' => '♣', + '♥' => '♥', + '♦' => '♦' + ); + + foreach ($to_ncr as $entity => $ncr) { + $text = str_replace($entity, $ncr, $text); + } + return $text; +} + +function wp_richedit_pre($text) { + // Filtering a blank results in an annoying <br />\n + if ( empty($text) ) return apply_filters('richedit_pre', ''); + + $output = $text; + $output = convert_chars($output); + $output = wpautop($output); + + // These must be double-escaped or planets will collide. + $output = str_replace('<', '&lt;', $output); + $output = str_replace('>', '&gt;', $output); + + return apply_filters('richedit_pre', $output); +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/functions-post.php b/projects/tests/tests/real/wordpress/wp-includes/functions-post.php new file mode 100644 index 00000000..eaaab76d --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/functions-post.php @@ -0,0 +1,915 @@ +<?php + +/**** DB Functions ****/ + +/* + * generic function for inserting data into the posts table. + */ +function wp_insert_post($postarr = array()) { + global $wpdb, $allowedtags, $user_ID; + + if ( is_object($postarr) ) + $postarr = get_object_vars($postarr); + + // export array as variables + extract($postarr); + + // Are we updating or creating? + $update = false; + if ( !empty($ID) ) { + $update = true; + $post = & get_post($ID); + $previous_status = $post->post_status; + } + + // Get the basics. + $post_content = apply_filters('content_save_pre', $post_content); + $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); + $post_title = apply_filters('title_save_pre', $post_title); + $post_category = apply_filters('category_save_pre', $post_category); + $post_status = apply_filters('status_save_pre', $post_status); + $post_name = apply_filters('name_save_pre', $post_name); + $comment_status = apply_filters('comment_status_pre', $comment_status); + $ping_status = apply_filters('ping_status_pre', $ping_status); + + // Make sure we set a valid category + if (0 == count($post_category) || !is_array($post_category)) { + $post_category = array(get_option('default_category')); + } + $post_cat = $post_category[0]; + + if ( empty($post_author) ) + $post_author = $user_ID; + + if ( empty($post_status) ) + $post_status = 'draft'; + + // Get the post ID. + if ( $update ) + $post_ID = $ID; + + // Create a valid post name. Drafts are allowed to have an empty + // post name. + if ( empty($post_name) ) { + if ( 'draft' != $post_status ) + $post_name = sanitize_title($post_title); + } else { + $post_name = sanitize_title($post_name); + } + + + // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now + if (empty($post_date)) { + if ( 'draft' != $post_status ) + $post_date = current_time('mysql'); + } + + if (empty($post_date_gmt)) { + if ( 'draft' != $post_status ) + $post_date_gmt = get_gmt_from_date($post_date); + } + + if ( empty($comment_status) ) { + if ( $update ) + $comment_status = 'closed'; + else + $comment_status = get_settings('default_comment_status'); + } + if ( empty($ping_status) ) + $ping_status = get_settings('default_ping_status'); + if ( empty($post_pingback) ) + $post_pingback = get_option('default_pingback_flag'); + + if ( isset($to_ping) ) + $to_ping = preg_replace('|\s+|', "\n", $to_ping); + else + $to_ping = ''; + + if ( ! isset($pinged) ) + $pinged = ''; + + if ( isset($post_parent) ) + $post_parent = (int) $post_parent; + else + $post_parent = 0; + + if ( isset($menu_order) ) + $menu_order = (int) $menu_order; + else + $menu_order = 0; + + if ( !isset($post_password) ) + $post_password = ''; + + if ( ('publish' == $post_status) || ('static' == $post_status) ) { + $post_name_check = ('publish' == $post_status) + ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1") + : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1"); + + if ($post_name_check) { + $suffix = 2; + while ($post_name_check) { + $alt_post_name = $post_name . "-$suffix"; + $post_name_check = ('publish' == $post_status) + ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1") + : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1"); + $suffix++; + } + $post_name = $alt_post_name; + } + } + + if ($update) { + $wpdb->query( + "UPDATE $wpdb->posts SET + post_author = '$post_author', + post_date = '$post_date', + post_date_gmt = '$post_date_gmt', + post_content = '$post_content', + post_content_filtered = '$post_content_filtered', + post_title = '$post_title', + post_excerpt = '$post_excerpt', + post_status = '$post_status', + comment_status = '$comment_status', + ping_status = '$ping_status', + post_password = '$post_password', + post_name = '$post_name', + to_ping = '$to_ping', + pinged = '$pinged', + post_modified = '".current_time('mysql')."', + post_modified_gmt = '".current_time('mysql',1)."', + post_parent = '$post_parent', + menu_order = '$menu_order' + WHERE ID = $post_ID"); + } else { + $wpdb->query( + "INSERT INTO $wpdb->posts + (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type) + VALUES + ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')"); + $post_ID = $wpdb->insert_id; + } + + if ( empty($post_name) && 'draft' != $post_status ) { + $post_name = sanitize_title($post_title, $post_ID); + $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); + } + + wp_set_post_cats('', $post_ID, $post_category); + + if ( 'static' == $post_status ) { + clean_page_cache($post_ID); + wp_cache_delete($post_ID, 'pages'); + } else { + clean_post_cache($post_ID); + } + + // Set GUID + if ( ! $update ) + $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); + + if ( $update) { + if ($previous_status != 'publish' && $post_status == 'publish') { + // Reset GUID if transitioning to publish. + $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); + do_action('private_to_published', $post_ID); + } + + do_action('edit_post', $post_ID); + } + + if ($post_status == 'publish') { + do_action('publish_post', $post_ID); + + if ( !defined('WP_IMPORTING') ) { + if ( $post_pingback ) + $result = $wpdb->query(" + INSERT INTO $wpdb->postmeta + (post_id,meta_key,meta_value) + VALUES ('$post_ID','_pingme','1') + "); + $result = $wpdb->query(" + INSERT INTO $wpdb->postmeta + (post_id,meta_key,meta_value) + VALUES ('$post_ID','_encloseme','1') + "); + spawn_pinger(); + } + } else if ($post_status == 'static') { + generate_page_rewrite_rules(); + + if ( !empty($page_template) ) + if ( ! update_post_meta($post_ID, '_wp_page_template', $page_template)) + add_post_meta($post_ID, '_wp_page_template', $page_template, true); + } + + do_action('save_post', $post_ID); + do_action('wp_insert_post', $post_ID); + + return $post_ID; +} + +function wp_insert_attachment($object, $file = false, $post_parent = 0) { + global $wpdb, $user_ID; + + if ( is_object($object) ) + $object = get_object_vars($object); + + // Export array as variables + extract($object); + + // Get the basics. + $post_content = apply_filters('content_save_pre', $post_content); + $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); + $post_title = apply_filters('title_save_pre', $post_title); + $post_category = apply_filters('category_save_pre', $post_category); + $post_name = apply_filters('name_save_pre', $post_name); + $comment_status = apply_filters('comment_status_pre', $comment_status); + $ping_status = apply_filters('ping_status_pre', $ping_status); + $post_mime_type = apply_filters('post_mime_type_pre', $post_mime_type); + + // Make sure we set a valid category + if (0 == count($post_category) || !is_array($post_category)) { + $post_category = array(get_option('default_category')); + } + $post_cat = $post_category[0]; + + if ( empty($post_author) ) + $post_author = $user_ID; + + $post_status = 'attachment'; + + // Are we updating or creating? + $update = false; + if ( !empty($ID) ) { + $update = true; + $post_ID = $ID; + } + + // Create a valid post name. + if ( empty($post_name) ) + $post_name = sanitize_title($post_title); + else + $post_name = sanitize_title($post_name); + + if (empty($post_date)) + $post_date = current_time('mysql'); + if (empty($post_date_gmt)) + $post_date_gmt = current_time('mysql', 1); + + if ( empty($comment_status) ) { + if ( $update ) + $comment_status = 'closed'; + else + $comment_status = get_settings('default_comment_status'); + } + if ( empty($ping_status) ) + $ping_status = get_settings('default_ping_status'); + if ( empty($post_pingback) ) + $post_pingback = get_option('default_pingback_flag'); + + if ( isset($to_ping) ) + $to_ping = preg_replace('|\s+|', "\n", $to_ping); + else + $to_ping = ''; + + if ( isset($post_parent) ) + $post_parent = (int) $post_parent; + else + $post_parent = 0; + + if ( isset($menu_order) ) + $menu_order = (int) $menu_order; + else + $menu_order = 0; + + if ( !isset($post_password) ) + $post_password = ''; + + if ( isset($to_ping) ) + $to_ping = preg_replace('|\s+|', "\n", $to_ping); + else + $to_ping = ''; + + if ( ! isset($pinged) ) + $pinged = ''; + + if ($update) { + $wpdb->query( + "UPDATE $wpdb->posts SET + post_author = '$post_author', + post_date = '$post_date', + post_date_gmt = '$post_date_gmt', + post_content = '$post_content', + post_title = '$post_title', + post_excerpt = '$post_excerpt', + post_status = '$post_status', + comment_status = '$comment_status', + ping_status = '$ping_status', + post_password = '$post_password', + post_name = '$post_name', + to_ping = '$to_ping', + pinged = '$pinged', + post_modified = '".current_time('mysql')."', + post_modified_gmt = '".current_time('mysql',1)."', + post_parent = '$post_parent', + menu_order = '$menu_order', + post_mime_type = '$post_mime_type', + guid = '$guid' + WHERE ID = $post_ID"); + } else { + $wpdb->query( + "INSERT INTO $wpdb->posts + (post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid) + VALUES + ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type', '$guid')"); + $post_ID = $wpdb->insert_id; + } + + if ( empty($post_name) ) { + $post_name = sanitize_title($post_title, $post_ID); + $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); + } + + wp_set_post_cats('', $post_ID, $post_category); + + if ( $file ) + add_post_meta($post_ID, '_wp_attached_file', $file); + + clean_post_cache($post_ID); + + if ( $update) { + do_action('edit_attachment', $post_ID); + } else { + do_action('add_attachment', $post_ID); + } + + return $post_ID; +} + +function wp_delete_attachment($postid) { + global $wpdb; + $postid = (int) $postid; + + if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") ) + return $post; + + if ( 'attachment' != $post->post_status ) + return false; + + $meta = get_post_meta($postid, '_wp_attachment_metadata', true); + $file = get_post_meta($postid, '_wp_attached_file', true); + + $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid"); + + $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid"); + + $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid"); + + $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid"); + + if ( ! empty($meta['thumb']) ) { + // Don't delete the thumb if another attachment uses it + if (! $foo = $wpdb->get_row("SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE '%".$wpdb->escape($meta['thumb'])."%' AND post_id <> $postid")) + @ unlink(str_replace(basename($file), $meta['thumb'], $file)); + } + + if ( ! empty($file) ) + @ unlink($file); + + do_action('delete_attachment', $postid); + + return $post; +} + +function wp_get_single_post($postid = 0, $mode = OBJECT) { + global $wpdb; + + $post = get_post($postid, $mode); + + // Set categories + if($mode == OBJECT) { + $post->post_category = wp_get_post_cats('',$postid); + } + else { + $post['post_category'] = wp_get_post_cats('',$postid); + } + + return $post; +} + +function wp_get_recent_posts($num = 10) { + global $wpdb; + + // Set the limit clause, if we got a limit + if ($num) { + $limit = "LIMIT $num"; + } + + $sql = "SELECT * FROM $wpdb->posts WHERE post_status IN ('publish', 'draft', 'private') ORDER BY post_date DESC $limit"; + $result = $wpdb->get_results($sql,ARRAY_A); + + return $result?$result:array(); +} + +function wp_update_post($postarr = array()) { + global $wpdb; + + if ( is_object($postarr) ) + $postarr = get_object_vars($postarr); + + // First, get all of the original fields + $post = wp_get_single_post($postarr['ID'], ARRAY_A); + + // Escape data pulled from DB. + $post = add_magic_quotes($post); + + // Passed post category list overwrites existing category list if not empty. + if ( isset($postarr['post_category']) && is_array($postarr['post_category']) + && 0 != count($postarr['post_category']) ) + $post_cats = $postarr['post_category']; + else + $post_cats = $post['post_category']; + + // Drafts shouldn't be assigned a date unless explicitly done so by the user + if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) && + ('0000-00-00 00:00:00' == $post['post_date']) ) + $clear_date = true; + else + $clear_date = false; + + // Merge old and new fields with new fields overwriting old ones. + $postarr = array_merge($post, $postarr); + $postarr['post_category'] = $post_cats; + if ( $clear_date ) { + $postarr['post_date'] = ''; + $postarr['post_date_gmt'] = ''; + } + + if ($postarr['post_status'] == 'attachment') + return wp_insert_attachment($postarr); + + return wp_insert_post($postarr); +} + +function wp_get_post_cats($blogid = '1', $post_ID = 0) { + global $wpdb; + + $sql = "SELECT category_id + FROM $wpdb->post2cat + WHERE post_id = $post_ID + ORDER BY category_id"; + + $result = $wpdb->get_col($sql); + + if ( !$result ) + $result = array(); + + return array_unique($result); +} + +function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) { + global $wpdb; + // If $post_categories isn't already an array, make it one: + if (!is_array($post_categories) || 0 == count($post_categories)) + $post_categories = array(get_option('default_category')); + + $post_categories = array_unique($post_categories); + + // First the old categories + $old_categories = $wpdb->get_col(" + SELECT category_id + FROM $wpdb->post2cat + WHERE post_id = $post_ID"); + + if (!$old_categories) { + $old_categories = array(); + } else { + $old_categories = array_unique($old_categories); + } + + + $oldies = printr($old_categories,1); + $newbies = printr($post_categories,1); + + // Delete any? + $delete_cats = array_diff($old_categories,$post_categories); + + if ($delete_cats) { + foreach ($delete_cats as $del) { + $wpdb->query(" + DELETE FROM $wpdb->post2cat + WHERE category_id = $del + AND post_id = $post_ID + "); + } + } + + // Add any? + $add_cats = array_diff($post_categories, $old_categories); + + if ($add_cats) { + foreach ($add_cats as $new_cat) { + $wpdb->query(" + INSERT INTO $wpdb->post2cat (post_id, category_id) + VALUES ($post_ID, $new_cat)"); + } + } + + // Update category counts. + $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); + foreach ( $all_affected_cats as $cat_id ) { + $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'"); + $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); + wp_cache_delete($cat_id, 'category'); + } +} // wp_set_post_cats() + +function wp_delete_post($postid = 0) { + global $wpdb; + $postid = (int) $postid; + + if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") ) + return $post; + + if ( 'attachment' == $post->post_status ) + return wp_delete_attachment($postid); + + do_action('delete_post', $postid); + + if ( 'publish' == $post->post_status) { + $categories = wp_get_post_cats('', $post->ID); + if( is_array( $categories ) ) { + foreach ( $categories as $cat_id ) { + $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'"); + wp_cache_delete($cat_id, 'category'); + } + } + } + + if ( 'static' == $post->post_status ) + $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_status = 'static'"); + + $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid"); + + $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid"); + + $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid"); + + $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid"); + + if ( 'static' == $post->post_status ) + generate_page_rewrite_rules(); + + return $post; +} + +/**** /DB Functions ****/ + +/**** Misc ****/ + +// get permalink from post ID +function post_permalink($post_id = 0, $mode = '') { // $mode legacy + return get_permalink($post_id); +} + +// Get the name of a category from its ID +function get_cat_name($cat_id) { + global $wpdb; + + $cat_id -= 0; // force numeric + $name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID=$cat_id"); + + return $name; +} + +// Get the ID of a category from its name +function get_cat_ID($cat_name='General') { + global $wpdb; + + $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'"); + + return $cid?$cid:1; // default to cat 1 +} + +// Get author's preferred display name +function get_author_name( $auth_id ) { + $authordata = get_userdata( $auth_id ); + + return $authordata->display_name; +} + +// get extended entry info (<!--more-->) +function get_extended($post) { + list($main,$extended) = explode('<!--more-->', $post, 2); + + // Strip leading and trailing whitespace + $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main); + $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended); + + return array('main' => $main, 'extended' => $extended); +} + +// do trackbacks for a list of urls +// borrowed from edit.php +// accepts a comma-separated list of trackback urls and a post id +function trackback_url_list($tb_list, $post_id) { + if (!empty($tb_list)) { + // get post data + $postdata = wp_get_single_post($post_id, ARRAY_A); + + // import postdata as variables + extract($postdata); + + // form an excerpt + $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content); + + if (strlen($excerpt) > 255) { + $excerpt = substr($excerpt,0,252) . '...'; + } + + $trackback_urls = explode(',', $tb_list); + foreach($trackback_urls as $tb_url) { + $tb_url = trim($tb_url); + trackback($tb_url, stripslashes($post_title), $excerpt, $post_id); + } + } +} + +function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) { + global $wpdb; + + do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent); + + if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) { + foreach ($chars[1] as $char) { + // If it's an encoded char in the normal ASCII set, reject + if ($char < 128) + return true; + } + } + + $mod_keys = trim( get_settings('blacklist_keys') ); + if ('' == $mod_keys ) + return false; // If moderation keys are empty + $words = explode("\n", $mod_keys ); + + foreach ($words as $word) { + $word = trim($word); + + // Skip empty lines + if ( empty($word) ) { continue; } + + // Do some escaping magic so that '#' chars in the + // spam words don't break things: + $word = preg_quote($word, '#'); + + $pattern = "#$word#i"; + if ( preg_match($pattern, $author ) ) return true; + if ( preg_match($pattern, $email ) ) return true; + if ( preg_match($pattern, $url ) ) return true; + if ( preg_match($pattern, $comment ) ) return true; + if ( preg_match($pattern, $user_ip ) ) return true; + if ( preg_match($pattern, $user_agent) ) return true; + } + + if ( isset($_SERVER['REMOTE_ADDR']) ) { + if ( wp_proxy_check($_SERVER['REMOTE_ADDR']) ) return true; + } + + return false; +} + +function wp_proxy_check($ipnum) { + if ( get_option('open_proxy_check') && isset($ipnum) ) { + $rev_ip = implode( '.', array_reverse( explode( '.', $ipnum ) ) ); + $lookup = $rev_ip . '.opm.blitzed.org'; + if ( $lookup != gethostbyname( $lookup ) ) + return true; + } + + return false; +} + +function do_trackbacks($post_id) { + global $wpdb; + + $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id"); + $to_ping = get_to_ping($post_id); + $pinged = get_pung($post_id); + if ( empty($to_ping) ) + return; + if (empty($post->post_excerpt)) + $excerpt = apply_filters('the_content', $post->post_content); + else + $excerpt = apply_filters('the_excerpt', $post->post_excerpt); + $excerpt = str_replace(']]>', ']]>', $excerpt); + $excerpt = strip_tags($excerpt); + $excerpt = substr($excerpt, 0, 252) . '...'; + + $post_title = apply_filters('the_title', $post->post_title); + $post_title = strip_tags($post_title); + + if ($to_ping) : foreach ($to_ping as $tb_ping) : + $tb_ping = trim($tb_ping); + if ( !in_array($tb_ping, $pinged) ) { + trackback($tb_ping, $post_title, $excerpt, $post_id); + $pinged[] = $tb_ping; + } else { + $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'"); + } + endforeach; endif; +} + +function get_pung($post_id) { // Get URIs already pung for a post + global $wpdb; + $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id"); + $pung = trim($pung); + $pung = preg_split('/\s/', $pung); + $pung = apply_filters('get_pung', $pung); + return $pung; +} + +function get_enclosed($post_id) { // Get enclosures already enclosed for a post + global $wpdb; + $custom_fields = get_post_custom( $post_id ); + $pung = array(); + if ( !is_array( $custom_fields ) ) + return $pung; + + foreach ( $custom_fields as $key => $val ) { + if ( 'enclosure' != $key || !is_array( $val ) ) + continue; + foreach( $val as $enc ) { + $enclosure = split( "\n", $enc ); + $pung[] = trim( $enclosure[ 0 ] ); + } + } + $pung = apply_filters('get_enclosed', $pung); + return $pung; +} + +function get_to_ping($post_id) { // Get any URIs in the todo list + global $wpdb; + $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id"); + $to_ping = trim($to_ping); + $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); + $to_ping = apply_filters('get_to_ping', $to_ping); + return $to_ping; +} + +function add_ping($post_id, $uri) { // Add a URI to those already pung + global $wpdb; + $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id"); + $pung = trim($pung); + $pung = preg_split('/\s/', $pung); + $pung[] = $uri; + $new = implode("\n", $pung); + $new = apply_filters('add_ping', $new); + return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id"); +} + +//fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents +//immediately follow their parents +function get_page_hierarchy($posts, $parent = 0) { + $result = array ( ); + if ($posts) { foreach ($posts as $post) { + if ($post->post_parent == $parent) { + $result[$post->ID] = $post->post_name; + $children = get_page_hierarchy($posts, $post->ID); + $result += $children; //append $children to $result + } + } } + return $result; +} + +function generate_page_rewrite_rules() { + global $wpdb; + + //get pages in order of hierarchy, i.e. children after parents + $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'static'")); + //now reverse it, because we need parents after children for rewrite rules to work properly + $posts = array_reverse($posts, true); + + $page_rewrite_rules = array(); + + if ($posts) { + + foreach ($posts as $id => $post) { + // URI => page name + $uri = get_page_uri($id); + + $page_rewrite_rules[$uri] = $post; + } + + update_option('page_uris', $page_rewrite_rules); + + save_mod_rewrite_rules(); + } +} + +function get_post_status($ID = '') { + $post = get_post($ID); + + if ( is_object($post) ) { + if ( ('attachment' == $post->post_status) && $post->post_parent && ($post->ID != $post->post_parent) ) + return get_post_status($post->post_parent); + else + return $post->post_status; + } + + return false; +} + +// Takes a post ID, returns its mime type. +function get_post_mime_type($ID = '') { + $post = & get_post($ID); + + if ( is_object($post) ) + return $post->post_mime_type; + + return false; +} + +function get_attached_file($attachment_id) { + return get_post_meta($attachment_id, '_wp_attached_file', true); +} + +// Returns an array containing the current upload directory's path and url, or an error message. +function wp_upload_dir() { + if ( defined('UPLOADS') ) + $dir = UPLOADS; + else + $dir = 'wp-content/uploads'; + + $path = ABSPATH . $dir; + + // Give the new dirs the same perms as wp-content. + $stat = stat(ABSPATH . 'wp-content'); + $dir_perms = $stat['mode'] & 0000777; // Get the permission bits. + + // Make sure we have an uploads dir + if ( ! file_exists( $path ) ) { + if ( ! @ mkdir( $path ) ) + return array('error' => "Unable to create directory $path. Is its parent directory writable by the server?"); + @ chmod( $path, $dir_perms ); + } + + // Generate the yearly and monthly dirs + $time = current_time( 'mysql' ); + $y = substr( $time, 0, 4 ); + $m = substr( $time, 5, 2 ); + $pathy = "$path/$y"; + $pathym = "$path/$y/$m"; + + // Make sure we have a yearly dir + if ( ! file_exists( $pathy ) ) { + if ( ! @ mkdir( $pathy ) ) + return array('error' => "Unable to create directory $pathy. Is $path writable?"); + @ chmod( $pathy, $dir_perms ); + } + + // Make sure we have a monthly dir + if ( ! file_exists( $pathym ) ) { + if ( ! @ mkdir( $pathym ) ) + return array('error' => "Unable to create directory $pathym. Is $pathy writable?"); + @ chmod( $pathym, $dir_perms ); + } + + $uploads = array('path' => $pathym, 'url' => get_option('siteurl') . "/$dir/$y/$m", 'error' => false); + return apply_filters('upload_dir', $uploads); +} + +function wp_upload_bits($name, $type, $bits) { + if ( empty($name) ) + return array('error' => "Empty filename"); + + $upload = wp_upload_dir(); + + if ( $upload['error'] !== false ) + return $upload; + + $number = ''; + $filename = $name; + while ( file_exists($upload['path'] . "/$filename") ) + $filename = str_replace("$number.$ext", ++$number . ".$ext", $filename); + + $new_file = $uploads['path'] . "/$filename"; + $ifp = @ fopen($new_file, 'wb'); + if ( ! $ifp ) + return array('error' => "Could not write file $new_file."); + + $success = @ fwrite($ifp, $bits); + fclose($ifp); + // Set correct file permissions + $stat = @ stat(dirname($new_file)); + $perms = $stat['mode'] & 0000777; + @ chmod($new_file, $perms); + + // Compute the URL + $url = $upload['url'] . "/$filename"; + + return array('file' => $new_file, 'url' => $url); +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/functions.php b/projects/tests/tests/real/wordpress/wp-includes/functions.php new file mode 100644 index 00000000..66d14726 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/functions.php @@ -0,0 +1,2269 @@ +<?php + +require_once(dirname(__FILE__).'/functions-compat.php'); + +if ( !function_exists('_') ) { + function _($string) { + return $string; + } +} + +function get_profile($field, $user = false) { + global $wpdb; + if ( !$user ) + $user = $wpdb->escape($_COOKIE[USER_COOKIE]); + return $wpdb->get_var("SELECT $field FROM $wpdb->users WHERE user_login = '$user'"); +} + +function mysql2date($dateformatstring, $mysqlstring, $translate = true) { + global $month, $weekday, $month_abbrev, $weekday_abbrev; + $m = $mysqlstring; + if ( empty($m) ) { + return false; + } + $i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4)); + + if ( -1 == $i || false == $i ) + $i = 0; + + if ( !empty($month) && !empty($weekday) && $translate ) { + $datemonth = $month[date('m', $i)]; + $datemonth_abbrev = $month_abbrev[$datemonth]; + $dateweekday = $weekday[date('w', $i)]; + $dateweekday_abbrev = $weekday_abbrev[$dateweekday]; + $dateformatstring = ' '.$dateformatstring; + $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring); + $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring); + $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring); + $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring); + + $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1); + } + $j = @date($dateformatstring, $i); + if ( !$j ) { + // for debug purposes + // echo $i." ".$mysqlstring; + } + return $j; +} + +function current_time($type, $gmt = 0) { + switch ($type) { + case 'mysql': + if ( $gmt ) $d = gmdate('Y-m-d H:i:s'); + else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600))); + return $d; + break; + case 'timestamp': + if ( $gmt ) $d = time(); + else $d = time() + (get_settings('gmt_offset') * 3600); + return $d; + break; + } +} + +function date_i18n($dateformatstring, $unixtimestamp) { + global $month, $weekday, $month_abbrev, $weekday_abbrev; + $i = $unixtimestamp; + if ( (!empty($month)) && (!empty($weekday)) ) { + $datemonth = $month[date('m', $i)]; + $datemonth_abbrev = $month_abbrev[$datemonth]; + $dateweekday = $weekday[date('w', $i)]; + $dateweekday_abbrev = $weekday_abbrev[$dateweekday]; + $dateformatstring = ' '.$dateformatstring; + $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring); + $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring); + $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring); + $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring); + $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1); + } + $j = @date($dateformatstring, $i); + return $j; + } + +function get_weekstartend($mysqlstring, $start_of_week) { + $my = substr($mysqlstring,0,4); + $mm = substr($mysqlstring,8,2); + $md = substr($mysqlstring,5,2); + $day = mktime(0,0,0, $md, $mm, $my); + $weekday = date('w',$day); + $i = 86400; + + if ( $weekday < get_settings('start_of_week') ) + $weekday = 7 - (get_settings('start_of_week') - $weekday); + + while ($weekday > get_settings('start_of_week')) { + $weekday = date('w',$day); + if ( $weekday < get_settings('start_of_week') ) + $weekday = 7 - (get_settings('start_of_week') - $weekday); + + $day = $day - 86400; + $i = 0; + } + $week['start'] = $day + 86400 - $i; + // $week['end'] = $day - $i + 691199; + $week['end'] = $week['start'] + 604799; + return $week; +} + +function get_lastpostdate($timezone = 'server') { + global $cache_lastpostdate, $pagenow, $wpdb; + $add_seconds_blog = get_settings('gmt_offset') * 3600; + $add_seconds_server = date('Z'); + $now = current_time('mysql', 1); + if ( !isset($cache_lastpostdate[$timezone]) ) { + switch(strtolower($timezone)) { + case 'gmt': + $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1"); + break; + case 'blog': + $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1"); + break; + case 'server': + $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1"); + break; + } + $cache_lastpostdate[$timezone] = $lastpostdate; + } else { + $lastpostdate = $cache_lastpostdate[$timezone]; + } + return $lastpostdate; +} + +function get_lastpostmodified($timezone = 'server') { + global $cache_lastpostmodified, $pagenow, $wpdb; + $add_seconds_blog = get_settings('gmt_offset') * 3600; + $add_seconds_server = date('Z'); + $now = current_time('mysql', 1); + if ( !isset($cache_lastpostmodified[$timezone]) ) { + switch(strtolower($timezone)) { + case 'gmt': + $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1"); + break; + case 'blog': + $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1"); + break; + case 'server': + $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1"); + break; + } + $lastpostdate = get_lastpostdate($timezone); + if ( $lastpostdate > $lastpostmodified ) { + $lastpostmodified = $lastpostdate; + } + $cache_lastpostmodified[$timezone] = $lastpostmodified; + } else { + $lastpostmodified = $cache_lastpostmodified[$timezone]; + } + return $lastpostmodified; +} + +function user_pass_ok($user_login,$user_pass) { + global $cache_userdata; + if ( empty($cache_userdata[$user_login]) ) { + $userdata = get_userdatabylogin($user_login); + } else { + $userdata = $cache_userdata[$user_login]; + } + return (md5($user_pass) == $userdata->user_pass); +} + + +function get_usernumposts($userid) { + global $wpdb; + return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid' AND post_status = 'publish'"); +} + + +// examine a url (supposedly from this blog) and try to +// determine the post ID it represents. +function url_to_postid($url) { + global $wp_rewrite; + + // First, check to see if there is a 'p=N' or 'page_id=N' to match against + preg_match('#[?&](p|page_id)=(\d+)#', $url, $values); + $id = intval($values[2]); + if ( $id ) return $id; + + // Check to see if we are using rewrite rules + $rewrite = $wp_rewrite->wp_rewrite_rules(); + + // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options + if ( empty($rewrite) ) + return 0; + + // $url cleanup by Mark Jaquith + // This fixes things like #anchors, ?query=strings, missing 'www.', + // added 'www.', or added 'index.php/' that will mess up our WP_Query + // and return a false negative + + // Get rid of the #anchor + $url_split = explode('#', $url); + $url = $url_split[0]; + + // Get rid of URI ?query=string + $url_split = explode('?', $url); + $url = $url_split[0]; + + // Add 'www.' if it is absent and should be there + if ( false !== strpos(get_settings('home'), '://www.') && false === strpos($url, '://www.') ) + $url = str_replace('://', '://www.', $url); + + // Strip 'www.' if it is present and shouldn't be + if ( false === strpos(get_settings('home'), '://www.') ) + $url = str_replace('://www.', '://', $url); + + // Strip 'index.php/' if we're not using path info permalinks + if ( false === strpos($rewrite, 'index.php/') ) + $url = str_replace('index.php/', '', $url); + + if ( false !== strpos($url, get_settings('home')) ) { + // Chop off http://domain.com + $url = str_replace(get_settings('home'), '', $url); + } else { + // Chop off /path/to/blog + $home_path = parse_url(get_settings('home')); + $home_path = $home_path['path']; + $url = str_replace($home_path, '', $url); + } + + // Trim leading and lagging slashes + $url = trim($url, '/'); + + $request = $url; + + // Done with cleanup + + // Look for matches. + $request_match = $request; + foreach ($rewrite as $match => $query) { + // If the requesting file is the anchor of the match, prepend it + // to the path info. + if ( (! empty($url)) && (strpos($match, $url) === 0) ) { + $request_match = $url . '/' . $request; + } + + if ( preg_match("!^$match!", $request_match, $matches) ) { + // Got a match. + // Trim the query of everything up to the '?'. + $query = preg_replace("!^.+\?!", '', $query); + + // Substitute the substring matches into the query. + eval("\$query = \"$query\";"); + $query = new WP_Query($query); + if ( $query->is_single || $query->is_page ) + return $query->post->ID; + else + return 0; + } + } + return 0; +} + + +function maybe_unserialize($original) { + if ( false !== $gm = @ unserialize($original) ) + return $gm; + else + return $original; +} + +/* Options functions */ + +function get_settings($setting) { + global $wpdb; + + $value = wp_cache_get($setting, 'options'); + + if ( false === $value ) { + if ( defined('WP_INSTALLING') ) + $wpdb->hide_errors(); + $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1"); + if ( defined('WP_INSTALLING') ) + $wpdb->show_errors(); + + if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values + $value = $row->option_value; + wp_cache_set($setting, $value, 'options'); + } else { + return false; + } + } + + // If home is not set use siteurl. + if ( 'home' == $setting && '' == $value ) + return get_settings('siteurl'); + + if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting ) + $value = preg_replace('|/+$|', '', $value); + + return apply_filters( 'option_' . $setting, maybe_unserialize($value) ); +} + +function get_option($option) { + return get_settings($option); +} + +function get_user_option( $option, $user = 0 ) { + global $wpdb, $current_user; + + if ( empty($user) ) + $user = $current_user; + else + $user = get_userdata($user); + + if ( isset( $user->{$wpdb->prefix . $option} ) ) // Blog specific + return $user->{$wpdb->prefix . $option}; + elseif ( isset( $user->{$option} ) ) // User specific and cross-blog + return $user->{$option}; + else // Blog global + return get_option( $option ); +} + +function form_option($option) { + echo htmlspecialchars( get_option($option), ENT_QUOTES ); +} + +function get_alloptions() { + global $wpdb, $wp_queries; + $wpdb->hide_errors(); + if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) { + $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options"); + } + $wpdb->show_errors(); + + foreach ($options as $option) { + // "When trying to design a foolproof system, + // never underestimate the ingenuity of the fools :)" -- Dougal + if ( 'siteurl' == $option->option_name ) + $option->option_value = preg_replace('|/+$|', '', $option->option_value); + if ( 'home' == $option->option_name ) + $option->option_value = preg_replace('|/+$|', '', $option->option_value); + if ( 'category_base' == $option->option_name ) + $option->option_value = preg_replace('|/+$|', '', $option->option_value); + $value = maybe_unserialize($option->option_value); + $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value); + } + return apply_filters('all_options', $all_options); +} + +function update_option($option_name, $newvalue) { + global $wpdb; + + if ( is_string($newvalue) ) + $newvalue = trim($newvalue); + + // If the new and old values are the same, no need to update. + $oldvalue = get_option($option_name); + if ( $newvalue == $oldvalue ) + return false; + + if ( false === $oldvalue ) { + add_option($option_name, $newvalue); + return true; + } + + if ( is_array($newvalue) || is_object($newvalue) ) + $newvalue = serialize($newvalue); + + wp_cache_set($option_name, $newvalue, 'options'); + + $newvalue = $wpdb->escape($newvalue); + $option_name = $wpdb->escape($option_name); + $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'"); + return true; +} + +function update_user_option( $user_id, $option_name, $newvalue, $global = false ) { + global $wpdb; + if ( !$global ) + $option_name = $wpdb->prefix . $option_name; + return update_usermeta( $user_id, $option_name, $newvalue ); +} + +// thx Alex Stapleton, http://alex.vort-x.net/blog/ +function add_option($name, $value = '', $description = '', $autoload = 'yes') { + global $wpdb; + + // Make sure the option doesn't already exist + if ( false !== get_option($name) ) + return; + + if ( is_array($value) || is_object($value) ) + $value = serialize($value); + + wp_cache_set($name, $value, 'options'); + + $name = $wpdb->escape($name); + $value = $wpdb->escape($value); + $description = $wpdb->escape($description); + $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')"); + + return; +} + +function delete_option($name) { + global $wpdb; + // Get the ID, if no ID then return + $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'"); + if ( !$option_id ) return false; + $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'"); + wp_cache_delete($name, 'options'); + return true; +} + +function add_post_meta($post_id, $key, $value, $unique = false) { + global $wpdb, $post_meta_cache; + + if ( $unique ) { + if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key += '$key' AND post_id = '$post_id'") ) { + return false; + } + } + + $original = $value; + if ( is_array($value) || is_object($value) ) + $value = $wpdb->escape(serialize($value)); + + $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')"); + + $post_meta_cache['$post_id'][$key][] = $original; + + return true; +} + +function delete_post_meta($post_id, $key, $value = '') { + global $wpdb, $post_meta_cache; + + if ( empty($value) ) { + $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE +post_id = '$post_id' AND meta_key = '$key'"); + } else { + $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE +post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'"); + } + + if ( !$meta_id ) + return false; + + if ( empty($value) ) { + $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' +AND meta_key = '$key'"); + unset($post_meta_cache['$post_id'][$key]); + } else { + $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' +AND meta_key = '$key' AND meta_value = '$value'"); + $cache_key = $post_meta_cache['$post_id'][$key]; + foreach ( $cache_key as $index => $data ) + if ( $data == $value ) + unset($post_meta_cache['$post_id'][$key][$index]); + } + + unset($post_meta_cache['$post_id'][$key]); + + return true; +} + +function get_post_meta($post_id, $key, $single = false) { + global $wpdb, $post_meta_cache; + + if ( isset($post_meta_cache[$post_id][$key]) ) { + if ( $single ) { + return maybe_unserialize( $post_meta_cache[$post_id][$key][0] ); + } else { + return maybe_unserialize( $post_meta_cache[$post_id][$key][0] ); + } + } + + $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N); + + $values = array(); + if ( $metalist ) { + foreach ($metalist as $metarow) { + $values[] = $metarow[0]; + } + } + + if ( $single ) { + if ( count($values) ) { + $return = maybe_unserialize( $values[0] ); + } else { + return ''; + } + } else { + $return = $values; + } + + return maybe_unserialize($return); +} + +function update_post_meta($post_id, $key, $value, $prev_value = '') { + global $wpdb, $post_meta_cache; + + $original_value = $value; + if ( is_array($value) || is_object($value) ) + $value = $wpdb->escape(serialize($value)); + + $original_prev = $prev_value; + if ( is_array($prev_value) || is_object($prev_value) ) + $prev_value = $wpdb->escape(serialize($prev_value)); + + if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key += '$key' AND post_id = '$post_id'") ) { + return false; + } + + if ( empty($prev_value) ) { + $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE +meta_key = '$key' AND post_id = '$post_id'"); + $cache_key = $post_meta_cache['$post_id'][$key]; + if ( !empty($cache_key) ) + foreach ($cache_key as $index => $data) + $post_meta_cache['$post_id'][$key][$index] = $original_value; + } else { + $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE +meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'"); + $cache_key = $post_meta_cache['$post_id'][$key]; + if ( !empty($cache_key) ) + foreach ($cache_key as $index => $data) + if ( $data == $original_prev ) + $post_meta_cache['$post_id'][$key][$index] = $original_value; + } + + return true; +} + +// Deprecated. Use get_post(). +function get_postdata($postid) { + $post = &get_post($postid); + + $postdata = array ( + 'ID' => $post->ID, + 'Author_ID' => $post->post_author, + 'Date' => $post->post_date, + 'Content' => $post->post_content, + 'Excerpt' => $post->post_excerpt, + 'Title' => $post->post_title, + 'Category' => $post->post_category, + 'post_status' => $post->post_status, + 'comment_status' => $post->comment_status, + 'ping_status' => $post->ping_status, + 'post_password' => $post->post_password, + 'to_ping' => $post->to_ping, + 'pinged' => $post->pinged, + 'post_name' => $post->post_name + ); + + return $postdata; +} + +// Retrieves post data given a post ID or post object. +// Handles post caching. +function &get_post(&$post, $output = OBJECT) { + global $post_cache, $wpdb; + + if ( empty($post) ) { + if ( isset($GLOBALS['post']) ) + $_post = & $GLOBALS['post']; + else + $_post = null; + } elseif ( is_object($post) ) { + if ( 'static' == $post->post_status ) + return get_page($post, $output); + if ( !isset($post_cache[$post->ID]) ) + $post_cache[$post->ID] = &$post; + $_post = & $post_cache[$post->ID]; + } else { + if ( $_post = wp_cache_get($post, 'pages') ) + return get_page($_post, $output); + elseif ( isset($post_cache[$post]) ) + $_post = & $post_cache[$post]; + else { + $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1"; + $_post = & $wpdb->get_row($query); + if ( 'static' == $_post->post_status ) + return get_page($_post, $output); + $post_cache[$post] = & $_post; + } + } + + if ( $output == OBJECT ) { + return $_post; + } elseif ( $output == ARRAY_A ) { + return get_object_vars($_post); + } elseif ( $output == ARRAY_N ) { + return array_values(get_object_vars($_post)); + } else { + return $_post; + } +} + +function &get_children($post = 0, $output = OBJECT) { + global $post_cache, $wpdb; + + if ( empty($post) ) { + if ( isset($GLOBALS['post']) ) + $post_parent = & $GLOBALS['post']->post_parent; + else + return false; + } elseif ( is_object($post) ) { + $post_parent = $post->post_parent; + } else { + $post_parent = $post; + } + + $post_parent = (int) $post_parent; + + $query = "SELECT * FROM $wpdb->posts WHERE post_parent = $post_parent"; + + $children = $wpdb->get_results($query); + + if ( $children ) { + foreach ( $children as $key => $child ) { + $post_cache[$child->ID] =& $children[$key]; + $kids[$child->ID] =& $children[$key]; + } + } else { + return false; + } + + if ( $output == OBJECT ) { + return $kids; + } elseif ( $output == ARRAY_A ) { + foreach ( $kids as $kid ) + $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); + return $weeuns; + } elseif ( $output == ARRAY_N ) { + foreach ( $kids as $kid ) + $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); + return $babes; + } else { + return $kids; + } +} + +function set_page_path($page) { + $page->fullpath = '/' . $page->post_name; + $path = $page->fullpath; + $curpage = $page; + while ($curpage->post_parent != 0) { + $curpage = get_page($curpage->post_parent); + $path = '/' . $curpage->post_name . $path; + } + + $page->fullpath = $path; + + return $page; +} + +// Retrieves page data given a page ID or page object. +// Handles page caching. +function &get_page(&$page, $output = OBJECT) { + global $wpdb; + + if ( empty($page) ) { + if ( isset($GLOBALS['page']) ) { + $_page = & $GLOBALS['page']; + wp_cache_add($_page->ID, $_page, 'pages'); + } else { + $_page = null; + } + } elseif ( is_object($page) ) { + if ( 'static' != $page->post_status ) + return get_post($page, $output); + wp_cache_add($page->ID, $page, 'pages'); + $_page = $page; + } else { + if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) ) { + $_page = & $GLOBALS['page']; + wp_cache_add($_page->ID, $_page, 'pages'); + } elseif ( $_page = $GLOBALS['post_cache'][$page] ) { + return get_post($page, $output); + } elseif ( $_page = wp_cache_get($page, 'pages') ) { + // Got it. + } else { + $query = "SELECT * FROM $wpdb->posts WHERE ID= '$page' LIMIT 1"; + $_page = & $wpdb->get_row($query); + if ( 'static' != $_page->post_status ) + return get_post($_page, $output); + wp_cache_add($_page->ID, $_page, 'pages'); + } + } + + if (!isset($_page->fullpath)) { + $_page = set_page_path($_page); + wp_cache_replace($_page->cat_ID, $_page, 'pages'); + } + + if ( $output == OBJECT ) { + return $_page; + } elseif ( $output == ARRAY_A ) { + return get_object_vars($_page); + } elseif ( $output == ARRAY_N ) { + return array_values(get_object_vars($_page)); + } else { + return $_page; + } +} + +function set_category_path($cat) { + $cat->fullpath = '/' . $cat->category_nicename; + $path = $cat->fullpath; + $curcat = $cat; + while ($curcat->category_parent != 0) { + $curcat = get_category($curcat->category_parent); + $path = '/' . $curcat->category_nicename . $path; + } + + $cat->fullpath = $path; + + return $cat; +} + +// Retrieves category data given a category ID or category object. +// Handles category caching. +function &get_category(&$category, $output = OBJECT) { + global $wpdb; + + if ( empty($category) ) + return null; + + if ( is_object($category) ) { + wp_cache_add($category->cat_ID, $category, 'category'); + $_category = $category; + } else { + if ( ! $_category = wp_cache_get($category, 'category') ) { + $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1"); + wp_cache_add($category, $_category, 'category'); + } + } + + if ( !isset($_category->fullpath) ) { + $_category = set_category_path($_category); + wp_cache_replace($_category->cat_ID, $_category, 'category'); + } + + if ( $output == OBJECT ) { + return $_category; + } elseif ( $output == ARRAY_A ) { + return get_object_vars($_category); + } elseif ( $output == ARRAY_N ) { + return array_values(get_object_vars($_category)); + } else { + return $_category; + } +} + +// Retrieves comment data given a comment ID or comment object. +// Handles comment caching. +function &get_comment(&$comment, $output = OBJECT) { + global $comment_cache, $wpdb; + + if ( empty($comment) ) + return null; + + if ( is_object($comment) ) { + if ( !isset($comment_cache[$comment->comment_ID]) ) + $comment_cache[$comment->comment_ID] = &$comment; + $_comment = & $comment_cache[$comment->comment_ID]; + } else { + if ( !isset($comment_cache[$comment]) ) { + $_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1"); + $comment_cache[$comment->comment_ID] = & $_comment; + } else { + $_comment = & $comment_cache[$comment]; + } + } + + if ( $output == OBJECT ) { + return $_comment; + } elseif ( $output == ARRAY_A ) { + return get_object_vars($_comment); + } elseif ( $output == ARRAY_N ) { + return array_values(get_object_vars($_comment)); + } else { + return $_comment; + } +} + +function get_catname($cat_ID) { + $category = &get_category($cat_ID); + return $category->cat_name; +} + +function get_all_category_ids() { + global $wpdb; + + if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) { + $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories"); + wp_cache_add('all_category_ids', $cat_ids, 'category'); + } + + return $cat_ids; +} + +function get_all_page_ids() { + global $wpdb; + + if ( ! $page_ids = wp_cache_get('all_page_ids', 'posts') ) { + $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status='static'"); + wp_cache_add('all_page_ids', $page_ids, 'pages'); + } + + return $page_ids; +} + +function gzip_compression() { + if ( !get_settings('gzipcompression') ) return false; + + if ( extension_loaded('zlib') ) { + ob_start('ob_gzhandler'); + } +} + + +// functions to count the page generation time (from phpBB2) +// ( or just any time between timer_start() and timer_stop() ) + +function timer_stop($display = 0, $precision = 3) { //if called like timer_stop(1), will echo $timetotal + global $timestart, $timeend; + $mtime = microtime(); + $mtime = explode(' ',$mtime); + $mtime = $mtime[1] + $mtime[0]; + $timeend = $mtime; + $timetotal = $timeend-$timestart; + if ( $display ) + echo number_format($timetotal,$precision); + return $timetotal; +} + +function weblog_ping($server = '', $path = '') { + global $wp_version; + include_once (ABSPATH . WPINC . '/class-IXR.php'); + + // using a timeout of 3 seconds should be enough to cover slow servers + $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path)); + $client->timeout = 3; + $client->useragent .= ' -- WordPress/'.$wp_version; + + // when set to true, this outputs debug messages by itself + $client->debug = false; + $home = trailingslashit( get_option('home') ); + if ( !$client->query('weblogUpdates.extendedPing', get_settings('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping + $client->query('weblogUpdates.ping', get_settings('blogname'), $home); +} + +function generic_ping($post_id = 0) { + $services = get_settings('ping_sites'); + $services = preg_replace("|(\s)+|", '$1', $services); // Kill dupe lines + $services = trim($services); + if ( '' != $services ) { + $services = explode("\n", $services); + foreach ($services as $service) { + weblog_ping($service); + } + } + + return $post_id; +} + +// Send a Trackback +function trackback($trackback_url, $title, $excerpt, $ID) { + global $wpdb, $wp_version; + + if ( empty($trackback_url) ) + return; + + $title = urlencode($title); + $excerpt = urlencode($excerpt); + $blog_name = urlencode(get_settings('blogname')); + $tb_url = $trackback_url; + $url = urlencode(get_permalink($ID)); + $query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt"; + $trackback_url = parse_url($trackback_url); + $http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n"; + $http_request .= 'Host: '.$trackback_url['host']."\r\n"; + $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_settings('blog_charset')."\r\n"; + $http_request .= 'Content-Length: '.strlen($query_string)."\r\n"; + $http_request .= "User-Agent: WordPress/" . $wp_version; + $http_request .= "\r\n\r\n"; + $http_request .= $query_string; + if ( '' == $trackback_url['port'] ) + $trackback_url['port'] = 80; + $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4); + @fputs($fs, $http_request); +/* + $debug_file = 'trackback.log'; + $fp = fopen($debug_file, 'a'); + fwrite($fp, "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n"); + while(!@feof($fs)) { + fwrite($fp, @fgets($fs, 4096)); + } + fwrite($fp, "\n\n"); + fclose($fp); +*/ + @fclose($fs); + + $tb_url = addslashes( $tb_url ); + $wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'"); + return $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = '$ID'"); +} + +function make_url_footnote($content) { + preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); + $j = 0; + for ($i=0; $i<count($matches[0]); $i++) { + $links_summary = (!$j) ? "\n" : $links_summary; + $j++; + $link_match = $matches[0][$i]; + $link_number = '['.($i+1).']'; + $link_url = $matches[2][$i]; + $link_text = $matches[4][$i]; + $content = str_replace($link_match, $link_text.' '.$link_number, $content); + $link_url = ((strtolower(substr($link_url,0,7)) != 'http://')||(strtolower(substr($link_url,0,7)) != 'https://')) ? get_settings('home') . $link_url : $link_url; + $links_summary .= "\n".$link_number.' '.$link_url; + } + $content = strip_tags($content); + $content .= $links_summary; + return $content; +} + + +function xmlrpc_getposttitle($content) { + global $post_default_title; + if ( preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle) ) { + $post_title = $matchtitle[0]; + $post_title = preg_replace('/<title>/si', '', $post_title); + $post_title = preg_replace('/<\/title>/si', '', $post_title); + } else { + $post_title = $post_default_title; + } + return $post_title; +} + +function xmlrpc_getpostcategory($content) { + global $post_default_category; + if ( preg_match('/<category>(.+?)<\/category>/is', $content, $matchcat) ) { + $post_category = trim($matchcat[1], ','); + $post_category = explode(',', $post_category); + } else { + $post_category = $post_default_category; + } + return $post_category; +} + +function xmlrpc_removepostdata($content) { + $content = preg_replace('/<title>(.+?)<\/title>/si', '', $content); + $content = preg_replace('/<category>(.+?)<\/category>/si', '', $content); + $content = trim($content); + return $content; +} + +function debug_fopen($filename, $mode) { + global $debug; + if ( $debug == 1 ) { + $fp = fopen($filename, $mode); + return $fp; + } else { + return false; + } +} + +function debug_fwrite($fp, $string) { + global $debug; + if ( $debug == 1 ) { + fwrite($fp, $string); + } +} + +function debug_fclose($fp) { + global $debug; + if ( $debug == 1 ) { + fclose($fp); + } +} + +function spawn_pinger() { + global $wpdb; + $doping = false; + if ( $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE TRIM(to_ping) != '' LIMIT 1") ) + $doping = true; + + if ( $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_pingme' OR meta_key = '_encloseme' LIMIT 1") ) + $doping = true; + + if ( $doping ) { + $ping_url = get_settings('siteurl') .'/wp-admin/execute-pings.php'; + $parts = parse_url($ping_url); + $argyle = @ fsockopen($parts['host'], $_SERVER['SERVER_PORT'], $errno, $errstr, 0.01); + if ( $argyle ) + fputs($argyle, "GET {$parts['path']}?time=".time()." HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\n\r\n"); + } +} + +function do_enclose( $content, $post_ID ) { + global $wp_version, $wpdb; + include_once (ABSPATH . WPINC . '/class-IXR.php'); + + $log = debug_fopen(ABSPATH . '/enclosures.log', 'a'); + $post_links = array(); + debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n"); + + $pung = get_enclosed( $post_ID ); + + $ltrs = '\w'; + $gunk = '/#~:.?+=&%@!\-'; + $punc = '.:?\-'; + $any = $ltrs . $gunk . $punc; + + preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp); + + debug_fwrite($log, 'Post contents:'); + debug_fwrite($log, $content."\n"); + + foreach($post_links_temp[0] as $link_test) : + if ( !in_array($link_test, $pung) ) : // If we haven't pung it already + $test = parse_url($link_test); + if ( isset($test['query']) ) + $post_links[] = $link_test; + elseif (($test['path'] != '/') && ($test['path'] != '')) + $post_links[] = $link_test; + endif; + endforeach; + + foreach ($post_links as $url) : + if ( $url != '' && !$wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE post_id = '$post_ID' AND meta_key = 'enclosure' AND meta_value LIKE ('$url%')") ) { + if ( $headers = wp_get_http_headers( $url) ) { + $len = (int) $headers['content-length']; + $type = $wpdb->escape( $headers['content-type'] ); + $allowed_types = array( 'video', 'audio' ); + if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) { + $meta_value = "$url\n$len\n$type\n"; + $wpdb->query( "INSERT INTO `$wpdb->postmeta` ( `post_id` , `meta_key` , `meta_value` ) + VALUES ( '$post_ID', 'enclosure' , '$meta_value')" ); + } + } + } + endforeach; +} + +function wp_get_http_headers( $url, $red = 1 ) { + global $wp_version; + @set_time_limit( 60 ); + + if ( $red > 5 ) + return false; + + $parts = parse_url( $url ); + $file = $parts['path'] . ($parts['query'] ? '?'.$parts['query'] : ''); + $host = $parts['host']; + if ( !isset( $parts['port'] ) ) + $parts['port'] = 80; + + $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n"; + + $fp = @fsockopen($host, $parts['port'], $err_num, $err_msg, 3); + if ( !$fp ) + return false; + + $response = ''; + fputs( $fp, $head ); + while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false ) + $response .= fgets( $fp, 2048 ); + fclose( $fp ); + preg_match_all('/(.*?): (.*)\r/', $response, $matches); + $count = count($matches[1]); + for ( $i = 0; $i < $count; $i++) { + $key = strtolower($matches[1][$i]); + $headers["$key"] = $matches[2][$i]; + } + + $code = preg_replace('/.*?(\d{3}).*/i', '$1', $response); + + $headers['status_code'] = $code; + + if ( '302' == $code || '301' == $code ) + return wp_get_http_headers( $url, ++$red ); + + preg_match('/.*([0-9]{3}).*/', $response, $return); + $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404 + return $headers; +} + +// Deprecated. Use the new post loop. +function start_wp() { + global $wp_query, $post; + + // Since the old style loop is being used, advance the query iterator here. + $wp_query->next_post(); + + setup_postdata($post); +} + +// Setup global post data. +function setup_postdata($post) { + global $id, $postdata, $authordata, $day, $page, $pages, $multipage, $more, $numpages, $wp_query; + global $pagenow; + + $id = $post->ID; + + $authordata = get_userdata($post->post_author); + + $day = mysql2date('d.m.y', $post->post_date); + $currentmonth = mysql2date('m', $post->post_date); + $numpages = 1; + $page = get_query_var('page'); + if ( !$page ) + $page = 1; + if ( is_single() || is_page() ) + $more = 1; + $content = $post->post_content; + if ( preg_match('/<!--nextpage-->/', $content) ) { + if ( $page > 1 ) + $more = 1; + $multipage = 1; + $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content); + $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content); + $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content); + $pages = explode('<!--nextpage-->', $content); + $numpages = count($pages); + } else { + $pages[0] = $post->post_content; + $multipage = 0; + } + return true; +} + +function is_new_day() { + global $day, $previousday; + if ( $day != $previousday ) { + return(1); + } else { + return(0); + } +} + +// Filters: these are the core of WP's plugin architecture + +function merge_filters($tag) { + global $wp_filter; + if ( isset($wp_filter['all']) ) { + foreach ($wp_filter['all'] as $priority => $functions) { + if ( isset($wp_filter[$tag][$priority]) ) + $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], $wp_filter[$tag][$priority]); + else + $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], array()); + $wp_filter[$tag][$priority] = array_unique($wp_filter[$tag][$priority]); + } + } + + if ( isset($wp_filter[$tag]) ) + ksort( $wp_filter[$tag] ); +} + +function apply_filters($tag, $string) { + global $wp_filter; + + $args = array_slice(func_get_args(), 2); + + merge_filters($tag); + + if ( !isset($wp_filter[$tag]) ) { + return $string; + } + foreach ($wp_filter[$tag] as $priority => $functions) { + if ( !is_null($functions) ) { + foreach($functions as $function) { + + $all_args = array_merge(array($string), $args); + $function_name = $function['function']; + $accepted_args = $function['accepted_args']; + + if ( $accepted_args == 1 ) + $the_args = array($string); + elseif ( $accepted_args > 1 ) + $the_args = array_slice($all_args, 0, $accepted_args); + elseif ( $accepted_args == 0 ) + $the_args = NULL; + else + $the_args = $all_args; + + $string = call_user_func_array($function_name, $the_args); + } + } + } + return $string; +} + +function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { + global $wp_filter; + + // check that we don't already have the same filter at the same priority + if ( isset($wp_filter[$tag]["$priority"]) ) { + foreach($wp_filter[$tag]["$priority"] as $filter) { + // uncomment if we want to match function AND accepted_args + // if ( $filter == array($function, $accepted_args) ) { + if ( $filter['function'] == $function_to_add ) { + return true; + } + } + } + + // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]'] + $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args); + return true; +} + +function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { + global $wp_filter; + + // rebuild the list of filters + if ( isset($wp_filter[$tag]["$priority"]) ) { + foreach($wp_filter[$tag]["$priority"] as $filter) { + if ( $filter['function'] != $function_to_remove ) { + $new_function_list[] = $filter; + } + } + $wp_filter[$tag]["$priority"] = $new_function_list; + } + return true; +} + +// The *_action functions are just aliases for the *_filter functions, they take special strings instead of generic content + +function do_action($tag, $arg = '') { + global $wp_filter; + $extra_args = array_slice(func_get_args(), 2); + if ( is_array($arg) ) + $args = array_merge($arg, $extra_args); + else + $args = array_merge(array($arg), $extra_args); + + merge_filters($tag); + + if ( !isset($wp_filter[$tag]) ) { + return; + } + foreach ($wp_filter[$tag] as $priority => $functions) { + if ( !is_null($functions) ) { + foreach($functions as $function) { + + $function_name = $function['function']; + $accepted_args = $function['accepted_args']; + + if ( $accepted_args == 1 ) { + if ( is_array($arg) ) + $the_args = $arg; + else + $the_args = array($arg); + } elseif ( $accepted_args > 1 ) { + $the_args = array_slice($args, 0, $accepted_args); + } elseif ( $accepted_args == 0 ) { + $the_args = NULL; + } else { + $the_args = $args; + } + + $string = call_user_func_array($function_name, $the_args); + } + } + } +} + +function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { + add_filter($tag, $function_to_add, $priority, $accepted_args); +} + +function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { + remove_filter($tag, $function_to_remove, $priority, $accepted_args); +} + +function get_page_uri($page_id) { + $page = get_page($page_id); + $uri = urldecode($page->post_name); + + // A page cannot be it's own parent. + if ( $page->post_parent == $page->ID ) + return $uri; + + while ($page->post_parent != 0) { + $page = get_page($page->post_parent); + $uri = urldecode($page->post_name) . "/" . $uri; + } + + return $uri; +} + +function get_posts($args) { + global $wpdb; + parse_str($args, $r); + if ( !isset($r['numberposts']) ) + $r['numberposts'] = 5; + if ( !isset($r['offset']) ) + $r['offset'] = 0; + if ( !isset($r['category']) ) + $r['category'] = ''; + if ( !isset($r['orderby']) ) + $r['orderby'] = 'post_date'; + if ( !isset($r['order']) ) + $r['order'] = 'DESC'; + + $now = current_time('mysql'); + + $posts = $wpdb->get_results( + "SELECT DISTINCT * FROM $wpdb->posts " . + ( empty( $r['category'] ) ? "" : ", $wpdb->post2cat " ) . + " WHERE post_date <= '$now' AND (post_status = 'publish') ". + ( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $r['category']. " " ) . + " GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . " LIMIT " . $r['offset'] . ',' . $r['numberposts'] ); + + update_post_caches($posts); + + return $posts; +} + +function &query_posts($query) { + global $wp_query; + return $wp_query->query($query); +} + +function update_post_cache(&$posts) { + global $post_cache; + + if ( !$posts ) + return; + + for ($i = 0; $i < count($posts); $i++) { + $post_cache[$posts[$i]->ID] = &$posts[$i]; + } +} + +function clean_post_cache($id) { + global $post_cache; + + if ( isset( $post_cache[$id] ) ) + unset( $post_cache[$id] ); +} + +function update_page_cache(&$pages) { + global $page_cache; + + if ( !$pages ) + return; + + for ($i = 0; $i < count($pages); $i++) { + $page_cache[$pages[$i]->ID] = &$pages[$i]; + wp_cache_add($pages[$i]->ID, $pages[$i], 'pages'); + } +} + + +function clean_page_cache($id) { + global $page_cache; + + if ( isset( $page_cache[$id] ) ) + unset( $page_cache[$id] ); +} + +function update_post_category_cache($post_ids) { + global $wpdb, $category_cache; + + if ( empty($post_ids) ) + return; + + if ( is_array($post_ids) ) + $post_ids = implode(',', $post_ids); + + $dogs = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat WHERE post_id IN ($post_ids)"); + + if ( empty($dogs) ) + return; + + foreach ($dogs as $catt) + $category_cache[$catt->post_id][$catt->category_id] = &get_category($catt->category_id); +} + +function update_post_caches(&$posts) { + global $post_cache, $category_cache, $comment_count_cache, $post_meta_cache; + global $wpdb; + + // No point in doing all this work if we didn't match any posts. + if ( !$posts ) + return; + + // Get the categories for all the posts + for ($i = 0; $i < count($posts); $i++) { + $post_id_array[] = $posts[$i]->ID; + $post_cache[$posts[$i]->ID] = &$posts[$i]; + $comment_count_cache[$posts[$i]->ID] = $posts[$i]->comment_count; + } + + $post_id_list = implode(',', $post_id_array); + + update_post_category_cache($post_id_list); + + // Get post-meta info + if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) { + // Change from flat structure to hierarchical: + $post_meta_cache = array(); + foreach ($meta_list as $metarow) { + $mpid = $metarow['post_id']; + $mkey = $metarow['meta_key']; + $mval = $metarow['meta_value']; + + // Force subkeys to be array type: + if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) ) + $post_meta_cache[$mpid] = array(); + if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) ) + $post_meta_cache[$mpid]["$mkey"] = array(); + + // Add a value to the current pid/key: + $post_meta_cache[$mpid][$mkey][] = $mval; + } + } +} + +function update_category_cache() { + return true; +} + +function wp_head() { + do_action('wp_head'); +} + +function wp_footer() { + do_action('wp_footer'); +} + +function is_single ($post = '') { + global $wp_query; + + if ( !$wp_query->is_single ) + return false; + + if ( empty( $post) ) + return true; + + $post_obj = $wp_query->get_queried_object(); + + if ( $post == $post_obj->ID ) + return true; + elseif ( $post == $post_obj->post_title ) + return true; + elseif ( $post == $post_obj->post_name ) + return true; + + return false; +} + +function is_page ($page = '') { + global $wp_query; + + if ( !$wp_query->is_page ) + return false; + + if ( empty($page) ) + return true; + + $page_obj = $wp_query->get_queried_object(); + + if ( $page == $page_obj->ID ) + return true; + elseif ( $page == $page_obj->post_title ) + return true; + else if ( $page == $page_obj->post_name ) + return true; + + return false; +} + +function is_attachment () { + global $wp_query; + + return $wp_query->is_attachment; +} + +function is_preview() { + global $wp_query; + + return $wp_query->is_preview; +} + +function is_archive () { + global $wp_query; + + return $wp_query->is_archive; +} + +function is_date () { + global $wp_query; + + return $wp_query->is_date; +} + +function is_year () { + global $wp_query; + + return $wp_query->is_year; +} + +function is_month () { + global $wp_query; + + return $wp_query->is_month; +} + +function is_day () { + global $wp_query; + + return $wp_query->is_day; +} + +function is_time () { + global $wp_query; + + return $wp_query->is_time; +} + +function is_author ($author = '') { + global $wp_query; + + if ( !$wp_query->is_author ) + return false; + + if ( empty($author) ) + return true; + + $author_obj = $wp_query->get_queried_object(); + + if ( $author == $author_obj->ID ) + return true; + elseif ( $author == $author_obj->nickname ) + return true; + elseif ( $author == $author_obj->user_nicename ) + return true; + + return false; +} + +function is_category ($category = '') { + global $wp_query; + + if ( !$wp_query->is_category ) + return false; + + if ( empty($category) ) + return true; + + $cat_obj = $wp_query->get_queried_object(); + + if ( $category == $cat_obj->cat_ID ) + return true; + else if ( $category == $cat_obj->cat_name ) + return true; + elseif ( $category == $cat_obj->category_nicename ) + return true; + + return false; +} + +function is_search () { + global $wp_query; + + return $wp_query->is_search; +} + +function is_feed () { + global $wp_query; + + return $wp_query->is_feed; +} + +function is_trackback () { + global $wp_query; + + return $wp_query->is_trackback; +} + +function is_admin () { + global $wp_query; + + return ( $wp_query->is_admin || strstr($_SERVER['REQUEST_URI'], 'wp-admin/') ); +} + +function is_home () { + global $wp_query; + + return $wp_query->is_home; +} + +function is_404 () { + global $wp_query; + + return $wp_query->is_404; +} + +function is_comments_popup () { + global $wp_query; + + return $wp_query->is_comments_popup; +} + +function is_paged () { + global $wp_query; + + return $wp_query->is_paged; +} + +function in_the_loop() { + global $wp_query; + + return $wp_query->in_the_loop; +} + +function get_query_var($var) { + global $wp_query; + + return $wp_query->get($var); +} + +function have_posts() { + global $wp_query; + + return $wp_query->have_posts(); +} + +function rewind_posts() { + global $wp_query; + + return $wp_query->rewind_posts(); +} + +function the_post() { + global $wp_query; + + $wp_query->the_post(); +} + +function get_theme_root() { + return apply_filters('theme_root', ABSPATH . "wp-content/themes"); +} + +function get_theme_root_uri() { + return apply_filters('theme_root_uri', get_settings('siteurl') . "/wp-content/themes", get_settings('siteurl')); +} + +function get_stylesheet() { + return apply_filters('stylesheet', get_settings('stylesheet')); +} + +function get_stylesheet_directory() { + $stylesheet = get_stylesheet(); + $stylesheet_dir = get_theme_root() . "/$stylesheet"; + return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet); +} + +function get_stylesheet_directory_uri() { + $stylesheet = rawurlencode( get_stylesheet() ); + $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet"; + return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet); +} + +function get_stylesheet_uri() { + $stylesheet_dir_uri = get_stylesheet_directory_uri(); + $stylesheet_uri = $stylesheet_dir_uri . "/style.css"; + return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri); +} + +function get_template() { + return apply_filters('template', get_settings('template')); +} + +function get_template_directory() { + $template = get_template(); + $template_dir = get_theme_root() . "/$template"; + return apply_filters('template_directory', $template_dir, $template); +} + +function get_template_directory_uri() { + $template = get_template(); + $template_dir_uri = get_theme_root_uri() . "/$template"; + return apply_filters('template_directory_uri', $template_dir_uri, $template); +} + +function get_theme_data($theme_file) { + $theme_data = implode('', file($theme_file)); + preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name); + preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri); + preg_match("|Description:(.*)|i", $theme_data, $description); + preg_match("|Author:(.*)|i", $theme_data, $author_name); + preg_match("|Author URI:(.*)|i", $theme_data, $author_uri); + preg_match("|Template:(.*)|i", $theme_data, $template); + if ( preg_match("|Version:(.*)|i", $theme_data, $version) ) + $version = $version[1]; + else + $version =''; + if ( preg_match("|Status:(.*)|i", $theme_data, $status) ) + $status = $status[1]; + else + $status ='publish'; + + $description = wptexturize($description[1]); + + $name = $theme_name[1]; + $name = trim($name); + $theme = $name; + + if ( '' == $author_uri[1] ) { + $author = $author_name[1]; + } else { + $author = '<a href="' . $author_uri[1] . '" title="' . __('Visit author homepage') . '">' . $author_name[1] . '</a>'; + } + + return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1], 'Status' => $status); +} + +function get_themes() { + global $wp_themes; + global $wp_broken_themes; + + if ( isset($wp_themes) ) + return $wp_themes; + + $themes = array(); + $wp_broken_themes = array(); + $theme_root = get_theme_root(); + $theme_loc = str_replace(ABSPATH, '', $theme_root); + + // Files in wp-content/themes directory + $themes_dir = @ dir($theme_root); + if ( $themes_dir ) { + while(($theme_dir = $themes_dir->read()) !== false) { + if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) { + if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) { + continue; + } + $stylish_dir = @ dir($theme_root . '/' . $theme_dir); + $found_stylesheet = false; + while (($theme_file = $stylish_dir->read()) !== false) { + if ( $theme_file == 'style.css' ) { + $theme_files[] = $theme_dir . '/' . $theme_file; + $found_stylesheet = true; + break; + } + } + if ( !$found_stylesheet ) { + $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.')); + } + } + } + } + + if ( !$themes_dir || !$theme_files ) { + return $themes; + } + + sort($theme_files); + + foreach($theme_files as $theme_file) { + if ( ! is_readable("$theme_root/$theme_file") ) { + $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.')); + continue; + } + + $theme_data = get_theme_data("$theme_root/$theme_file"); + + $name = $theme_data['Name']; + $title = $theme_data['Title']; + $description = wptexturize($theme_data['Description']); + $version = $theme_data['Version']; + $author = $theme_data['Author']; + $template = $theme_data['Template']; + $stylesheet = dirname($theme_file); + + $screenshot = glob("$theme_root/$stylesheet/screenshot.*"); + if ( !empty( $screenshot ) ) + $screenshot = basename( $screenshot[0] ); + else + $screenshot = false; + + if ( empty($name) ) { + $name = dirname($theme_file); + $title = $name; + } + + if ( empty($template) ) { + if ( file_exists(dirname("$theme_root/$theme_file/index.php")) ) { + $template = dirname($theme_file); + } else { + continue; + } + } + + $template = trim($template); + + if ( !file_exists("$theme_root/$template/index.php") ) { + $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.')); + continue; + } + + $stylesheet_files = array(); + $stylesheet_dir = @ dir("$theme_root/$stylesheet"); + if ( $stylesheet_dir ) { + while(($file = $stylesheet_dir->read()) !== false) { + if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) ) + $stylesheet_files[] = "$theme_loc/$stylesheet/$file"; + } + } + + $template_files = array(); + $template_dir = @ dir("$theme_root/$template"); + if ( $template_dir ) { + while(($file = $template_dir->read()) !== false) { + if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) ) + $template_files[] = "$theme_loc/$template/$file"; + } + } + + $template_dir = dirname($template_files[0]); + $stylesheet_dir = dirname($stylesheet_files[0]); + + if ( empty($template_dir) ) + $template_dir = '/'; + if ( empty($stylesheet_dir) ) + $stylesheet_dir = '/'; + + // Check for theme name collision. This occurs if a theme is copied to + // a new theme directory and the theme header is not updated. Whichever + // theme is first keeps the name. Subsequent themes get a suffix applied. + // The Default and Classic themes always trump their pretenders. + if ( isset($themes[$name]) ) { + if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) && + ('default' == $stylesheet || 'classic' == $stylesheet) ) { + // If another theme has claimed to be one of our default themes, move + // them aside. + $suffix = $themes[$name]['Stylesheet']; + $new_name = "$name/$suffix"; + $themes[$new_name] = $themes[$name]; + $themes[$new_name]['Name'] = $new_name; + } else { + $name = "$name/$stylesheet"; + } + } + + $themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status'], 'Screenshot' => $screenshot); + } + + // Resolve theme dependencies. + $theme_names = array_keys($themes); + + foreach ($theme_names as $theme_name) { + $themes[$theme_name]['Parent Theme'] = ''; + if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) { + foreach ($theme_names as $parent_theme_name) { + if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) { + $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name']; + break; + } + } + } + } + + $wp_themes = $themes; + + return $themes; +} + +function get_theme($theme) { + $themes = get_themes(); + + if ( array_key_exists($theme, $themes) ) + return $themes[$theme]; + + return NULL; +} + +function get_current_theme() { + $themes = get_themes(); + $theme_names = array_keys($themes); + $current_template = get_settings('template'); + $current_stylesheet = get_settings('stylesheet'); + $current_theme = 'WordPress Default'; + + if ( $themes ) { + foreach ($theme_names as $theme_name) { + if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet && + $themes[$theme_name]['Template'] == $current_template ) { + $current_theme = $themes[$theme_name]['Name']; + break; + } + } + } + + return $current_theme; +} + +function get_query_template($type) { + $template = ''; + if ( file_exists(TEMPLATEPATH . "/{$type}.php") ) + $template = TEMPLATEPATH . "/{$type}.php"; + + return apply_filters("{$type}_template", $template); +} + +function get_404_template() { + return get_query_template('404'); +} + +function get_archive_template() { + return get_query_template('archive'); +} + +function get_author_template() { + return get_query_template('author'); +} + +function get_category_template() { + $template = ''; + if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') ) + $template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php'; + else if ( file_exists(TEMPLATEPATH . "/category.php") ) + $template = TEMPLATEPATH . "/category.php"; + + return apply_filters('category_template', $template); +} + +function get_date_template() { + return get_query_template('date'); +} + +function get_home_template() { + $template = ''; + + if ( file_exists(TEMPLATEPATH . "/home.php") ) + $template = TEMPLATEPATH . "/home.php"; + else if ( file_exists(TEMPLATEPATH . "/index.php") ) + $template = TEMPLATEPATH . "/index.php"; + + return apply_filters('home_template', $template); +} + +function get_page_template() { + global $wp_query; + + $id = $wp_query->post->ID; + $template = get_post_meta($id, '_wp_page_template', true); + + if ( 'default' == $template ) + $template = ''; + + if ( ! empty($template) && file_exists(TEMPLATEPATH . "/$template") ) + $template = TEMPLATEPATH . "/$template"; + else if ( file_exists(TEMPLATEPATH . "/page.php") ) + $template = TEMPLATEPATH . "/page.php"; + else + $template = ''; + + return apply_filters('page_template', $template); +} + +function get_paged_template() { + return get_query_template('paged'); +} + +function get_search_template() { + return get_query_template('search'); +} + +function get_single_template() { + return get_query_template('single'); +} + +function get_attachment_template() { + global $posts; + $type = explode('/', $posts[0]->post_mime_type); + if ( $template = get_query_template($type[0]) ) + return $template; + elseif ( $template = get_query_template($type[1]) ) + return $template; + elseif ( $template = get_query_template("$type[0]_$type[1]") ) + return $template; + else + return get_query_template('attachment'); +} + +function get_comments_popup_template() { + if ( file_exists( TEMPLATEPATH . '/comments-popup.php') ) + $template = TEMPLATEPATH . '/comments-popup.php'; + else + $template = get_theme_root() . '/default/comments-popup.php'; + + return apply_filters('comments_popup_template', $template); +} + +// Borrowed from the PHP Manual user notes. Convert entities, while +// preserving already-encoded entities: +function htmlentities2($myHTML) { + $translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES); + $translation_table[chr(38)] = '&'; + return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" , strtr($myHTML, $translation_table)); +} + + +function is_plugin_page() { + global $plugin_page; + + if ( isset($plugin_page) ) + return true; + + return false; +} + +/* +add_query_arg: Returns a modified querystring by adding +a single key & value or an associative array. +Setting a key value to emptystring removes the key. +Omitting oldquery_or_uri uses the $_SERVER value. + +Parameters: +add_query_arg(newkey, newvalue, oldquery_or_uri) or +add_query_arg(associative_array, oldquery_or_uri) +*/ +function add_query_arg() { + $ret = ''; + if ( is_array(func_get_arg(0)) ) { + $uri = @func_get_arg(1); + } else { + if ( @func_num_args() < 3 ) + $uri = $_SERVER['REQUEST_URI']; + else + $uri = @func_get_arg(2); + } + + if ( strstr($uri, '?') ) { + $parts = explode('?', $uri, 2); + if ( 1 == count($parts) ) { + $base = '?'; + $query = $parts[0]; + } else { + $base = $parts[0] . '?'; + $query = $parts[1]; + } + } + else if ( strstr($uri, '/') ) { + $base = $uri . '?'; + $query = ''; + } else { + $base = ''; + $query = $uri; + } + + parse_str($query, $qs); + if ( is_array(func_get_arg(0)) ) { + $kayvees = func_get_arg(0); + $qs = array_merge($qs, $kayvees); + } else { + $qs[func_get_arg(0)] = func_get_arg(1); + } + + foreach($qs as $k => $v) { + if ( $v != '' ) { + if ( $ret != '' ) + $ret .= '&'; + $ret .= "$k=$v"; + } + } + $ret = $base . $ret; + return trim($ret, '?'); +} + +function remove_query_arg($key, $query) { + return add_query_arg($key, '', $query); +} + +function load_template($file) { + global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, + $wp_rewrite, $wpdb; + + extract($wp_query->query_vars); + + require_once($file); +} + +function add_magic_quotes($array) { + global $wpdb; + + foreach ($array as $k => $v) { + if ( is_array($v) ) { + $array[$k] = add_magic_quotes($v); + } else { + $array[$k] = $wpdb->escape($v); + } + } + return $array; +} + +function wp_remote_fopen( $uri ) { + if ( ini_get('allow_url_fopen') ) { + $fp = fopen( $uri, 'r' ); + if ( !$fp ) + return false; + $linea = ''; + while( $remote_read = fread($fp, 4096) ) + $linea .= $remote_read; + fclose($fp); + return $linea; + } else if ( function_exists('curl_init') ) { + $handle = curl_init(); + curl_setopt ($handle, CURLOPT_URL, $uri); + curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1); + curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1); + $buffer = curl_exec($handle); + curl_close($handle); + return $buffer; + } else { + return false; + } +} + +function wp($query_vars = '') { + global $wp; + + $wp->main($query_vars); +} + +function status_header( $header ) { + if ( 200 == $header ) + $text = 'OK'; + elseif ( 301 == $header ) + $text = 'Moved Permanently'; + elseif ( 302 == $header ) + $text = 'Moved Temporarily'; + elseif ( 304 == $header ) + $text = 'Not Modified'; + elseif ( 404 == $header ) + $text = 'Not Found'; + elseif ( 410 == $header ) + $text = 'Gone'; + + @header("HTTP/1.1 $header $text"); + @header("Status: $header $text"); +} + +function nocache_headers() { + @ header('Expires: Wed, 11 Jan 1984 05:00:00 GMT'); + @ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + @ header('Cache-Control: no-cache, must-revalidate, max-age=0'); + @ header('Pragma: no-cache'); +} + +function get_usermeta( $user_id, $meta_key = '') { + global $wpdb; + $user_id = (int) $user_id; + + if ( !empty($meta_key) ) { + $meta_key = preg_replace('|a-z0-9_|i', '', $meta_key); + $metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); + } else { + $metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'"); + } + + if ( empty($metas) ) { + if ( empty($meta_key) ) + return array(); + else + return ''; + } + + foreach ($metas as $index => $meta) { + @ $value = unserialize($meta->meta_value); + if ( $value === FALSE ) + $value = $meta->meta_value; + + $values[] = $value; + } + + if ( count($values) == 1 ) + return $values[0]; + else + return $values; +} + +function update_usermeta( $user_id, $meta_key, $meta_value ) { + global $wpdb; + if ( !is_numeric( $user_id ) ) + return false; + $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); + + if ( is_array($meta_value) || is_object($meta_value) ) + $meta_value = serialize($meta_value); + $meta_value = trim( $meta_value ); + + if ( '' == $meta_value ) + return false; + + $cur = $wpdb->get_row("SELECT * FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); + if ( !$cur ) { + $wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value ) + VALUES + ( '$user_id', '$meta_key', '$meta_value' )"); + } else if ( $cur->meta_value != $meta_value ) { + $wpdb->query("UPDATE $wpdb->usermeta SET meta_value = '$meta_value' WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); + } else { + return false; + } + + $user = get_userdata($user_id); + wp_cache_delete($user_id, 'users'); + wp_cache_delete($user->user_login, 'userlogins'); + + return true; +} + +function delete_usermeta( $user_id, $meta_key, $meta_value = '' ) { + global $wpdb; + if ( !is_numeric( $user_id ) ) + return false; + $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); + + if ( is_array($meta_value) || is_object($meta_value) ) + $meta_value = serialize($meta_value); + $meta_value = trim( $meta_value ); + + if ( ! empty($meta_value) ) + $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key' AND meta_value = '$meta_value'"); + else + $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); + + $user = get_userdata($user_id); + wp_cache_delete($user_id, 'users'); + wp_cache_delete($user->user_login, 'userlogins'); + + return true; +} + +function register_activation_hook($file, $function) { + $file = plugin_basename($file); + + add_action('activate_' . $file, $function); +} + +function register_deactivation_hook($file, $function) { + $file = plugin_basename($file); + + add_action('deactivate_' . $file, $function); +} + +function plugin_basename($file) { + $file = preg_replace('|\\\\+|', '\\\\', $file); + $file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $file); + return $file; +} + +function get_num_queries() { + global $wpdb; + return $wpdb->num_queries; +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/gettext.php b/projects/tests/tests/real/wordpress/wp-includes/gettext.php new file mode 100644 index 00000000..ad605cfd --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/gettext.php @@ -0,0 +1,361 @@ +<?php +/* + Copyright (c) 2003 Danilo Segan <danilo@kvota.net>. + Copyright (c) 2005 Nico Kaiser <nico@siriux.net> + + This file is part of PHP-gettext. + + PHP-gettext is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + PHP-gettext is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with PHP-gettext; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +/** + * Provides a simple gettext replacement that works independently from + * the system's gettext abilities. + * It can read MO files and use them for translating strings. + * The files are passed to gettext_reader as a Stream (see streams.php) + * + * This version has the ability to cache all strings and translations to + * speed up the string lookup. + * While the cache is enabled by default, it can be switched off with the + * second parameter in the constructor (e.g. whenusing very large MO files + * that you don't want to keep in memory) + */ +class gettext_reader { + //public: + var $error = 0; // public variable that holds error code (0 if no error) + + //private: + var $BYTEORDER = 0; // 0: low endian, 1: big endian + var $STREAM = NULL; + var $short_circuit = false; + var $enable_cache = false; + var $originals = NULL; // offset of original table + var $translations = NULL; // offset of translation table + var $pluralheader = NULL; // cache header field for plural forms + var $total = 0; // total string count + var $table_originals = NULL; // table for original strings (offsets) + var $table_translations = NULL; // table for translated strings (offsets) + var $cache_translations = NULL; // original -> translation mapping + + + /* Methods */ + + + /** + * Reads a 32bit Integer from the Stream + * + * @access private + * @return Integer from the Stream + */ + function readint() { + $stream = $this->STREAM->read(4); + if ($this->BYTEORDER == 0) { + // low endian + $unpacked = unpack('V',$stream); + return array_shift($unpacked); + } else { + // big endian + $unpacked = unpack('N',$stream); + return array_shift($unpacked); + } + } + + /** + * Reads an array of Integers from the Stream + * + * @param int count How many elements should be read + * @return Array of Integers + */ + function readintarray($count) { + if ($this->BYTEORDER == 0) { + // low endian + return unpack('V'.$count, $this->STREAM->read(4 * $count)); + } else { + // big endian + return unpack('N'.$count, $this->STREAM->read(4 * $count)); + } + } + + /** + * Constructor + * + * @param object Reader the StreamReader object + * @param boolean enable_cache Enable or disable caching of strings (default on) + */ + function gettext_reader($Reader, $enable_cache = true) { + // If there isn't a StreamReader, turn on short circuit mode. + if (! $Reader) { + $this->short_circuit = true; + return; + } + + // Caching can be turned off + $this->enable_cache = $enable_cache; + + // $MAGIC1 = (int)0x950412de; //bug in PHP 5 + $MAGIC1 = (int) - 1794895138; + // $MAGIC2 = (int)0xde120495; //bug + $MAGIC2 = (int) - 569244523; + + $this->STREAM = $Reader; + $magic = $this->readint(); + if ($magic == $MAGIC1) { + $this->BYTEORDER = 0; + } elseif ($magic == $MAGIC2) { + $this->BYTEORDER = 1; + } else { + $this->error = 1; // not MO file + return false; + } + + // FIXME: Do we care about revision? We should. + $revision = $this->readint(); + + $this->total = $this->readint(); + $this->originals = $this->readint(); + $this->translations = $this->readint(); + } + + /** + * Loads the translation tables from the MO file into the cache + * If caching is enabled, also loads all strings into a cache + * to speed up translation lookups + * + * @access private + */ + function load_tables() { + if (is_array($this->cache_translations) && + is_array($this->table_originals) && + is_array($this->table_translations)) + return; + + /* get original and translations tables */ + $this->STREAM->seekto($this->originals); + $this->table_originals = $this->readintarray($this->total * 2); + $this->STREAM->seekto($this->translations); + $this->table_translations = $this->readintarray($this->total * 2); + + if ($this->enable_cache) { + $this->cache_translations = array (); + /* read all strings in the cache */ + for ($i = 0; $i < $this->total; $i++) { + $this->STREAM->seekto($this->table_originals[$i * 2 + 2]); + $original = $this->STREAM->read($this->table_originals[$i * 2 + 1]); + $this->STREAM->seekto($this->table_translations[$i * 2 + 2]); + $translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]); + $this->cache_translations[$original] = $translation; + } + } + } + + /** + * Returns a string from the "originals" table + * + * @access private + * @param int num Offset number of original string + * @return string Requested string if found, otherwise '' + */ + function get_original_string($num) { + $length = $this->table_originals[$num * 2 + 1]; + $offset = $this->table_originals[$num * 2 + 2]; + if (! $length) + return ''; + $this->STREAM->seekto($offset); + $data = $this->STREAM->read($length); + return (string)$data; + } + + /** + * Returns a string from the "translations" table + * + * @access private + * @param int num Offset number of original string + * @return string Requested string if found, otherwise '' + */ + function get_translation_string($num) { + $length = $this->table_translations[$num * 2 + 1]; + $offset = $this->table_translations[$num * 2 + 2]; + if (! $length) + return ''; + $this->STREAM->seekto($offset); + $data = $this->STREAM->read($length); + return (string)$data; + } + + /** + * Binary search for string + * + * @access private + * @param string string + * @param int start (internally used in recursive function) + * @param int end (internally used in recursive function) + * @return int string number (offset in originals table) + */ + function find_string($string, $start = -1, $end = -1) { + if (($start == -1) or ($end == -1)) { + // find_string is called with only one parameter, set start end end + $start = 0; + $end = $this->total; + } + if (abs($start - $end) <= 1) { + // We're done, now we either found the string, or it doesn't exist + $txt = $this->get_original_string($start); + if ($string == $txt) + return $start; + else + return -1; + } else if ($start > $end) { + // start > end -> turn around and start over + return $this->find_string($string, $end, $start); + } else { + // Divide table in two parts + $half = (int)(($start + $end) / 2); + $cmp = strcmp($string, $this->get_original_string($half)); + if ($cmp == 0) + // string is exactly in the middle => return it + return $half; + else if ($cmp < 0) + // The string is in the upper half + return $this->find_string($string, $start, $half); + else + // The string is in the lower half + return $this->find_string($string, $half, $end); + } + } + + /** + * Translates a string + * + * @access public + * @param string string to be translated + * @return string translated string (or original, if not found) + */ + function translate($string) { + if ($this->short_circuit) + return $string; + $this->load_tables(); + + if ($this->enable_cache) { + // Caching enabled, get translated string from cache + if (array_key_exists($string, $this->cache_translations)) + return $this->cache_translations[$string]; + else + return $string; + } else { + // Caching not enabled, try to find string + $num = $this->find_string($string); + if ($num == -1) + return $string; + else + return $this->get_translation_string($num); + } + } + + /** + * Get possible plural forms from MO header + * + * @access private + * @return string plural form header + */ + function get_plural_forms() { + // lets assume message number 0 is header + // this is true, right? + $this->load_tables(); + + // cache header field for plural forms + if (! is_string($this->pluralheader)) { + if ($this->enable_cache) { + $header = $this->cache_translations[""]; + } else { + $header = $this->get_translation_string(0); + } + if (eregi("plural-forms: (.*)\n", $header, $regs)) + $expr = $regs[1]; + else + $expr = "nplurals=2; plural=n == 1 ? 0 : 1;"; + $this->pluralheader = $expr; + } + return $this->pluralheader; + } + + /** + * Detects which plural form to take + * + * @access private + * @param n count + * @return int array index of the right plural form + */ + function select_string($n) { + $string = $this->get_plural_forms(); + $string = str_replace('nplurals',"\$total",$string); + $string = str_replace("n",$n,$string); + $string = str_replace('plural',"\$plural",$string); + + $total = 0; + $plural = 0; + + eval("$string"); + if ($plural >= $total) $plural = 0; + return $plural; + } + + /** + * Plural version of gettext + * + * @access public + * @param string single + * @param string plural + * @param string number + * @return translated plural form + */ + function ngettext($single, $plural, $number) { + if ($this->short_circuit) { + if ($number != 1) + return $plural; + else + return $single; + } + + // find out the appropriate form + $select = $this->select_string($number); + + // this should contains all strings separated by NULLs + $key = $single.chr(0).$plural; + + + if ($this->enable_cache) { + if (! array_key_exists($key, $this->cache_translations)) { + return ($number != 1) ? $plural : $single; + } else { + $result = $this->cache_translations[$key]; + $list = explode(chr(0), $result); + return $list[$select]; + } + } else { + $num = $this->find_string($key); + if ($num == -1) { + return ($number != 1) ? $plural : $single; + } else { + $result = $this->get_translation_string($num); + $list = explode(chr(0), $result); + return $list[$select]; + } + } + } + +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_arrow.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_arrow.gif new file mode 100644 index 00000000..2880055c Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_arrow.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_biggrin.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_biggrin.gif new file mode 100644 index 00000000..d3527723 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_biggrin.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_confused.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_confused.gif new file mode 100644 index 00000000..0c49e069 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_confused.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_cool.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_cool.gif new file mode 100644 index 00000000..cead0306 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_cool.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_cry.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_cry.gif new file mode 100644 index 00000000..7d54b1f9 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_cry.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_eek.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_eek.gif new file mode 100644 index 00000000..5d397810 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_eek.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_evil.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_evil.gif new file mode 100644 index 00000000..ab1aa8e1 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_evil.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_exclaim.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_exclaim.gif new file mode 100644 index 00000000..6e50e2ee Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_exclaim.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_idea.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_idea.gif new file mode 100644 index 00000000..a40ae0d7 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_idea.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_lol.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_lol.gif new file mode 100644 index 00000000..374ba150 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_lol.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_mad.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_mad.gif new file mode 100644 index 00000000..1f6c3c2f Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_mad.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_mrgreen.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_mrgreen.gif new file mode 100644 index 00000000..b54cd0f9 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_mrgreen.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_neutral.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_neutral.gif new file mode 100644 index 00000000..4f311567 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_neutral.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_question.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_question.gif new file mode 100644 index 00000000..9d072265 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_question.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_razz.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_razz.gif new file mode 100644 index 00000000..29da2a2f Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_razz.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_redface.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_redface.gif new file mode 100644 index 00000000..ad762832 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_redface.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_rolleyes.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_rolleyes.gif new file mode 100644 index 00000000..d7f5f2f4 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_rolleyes.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_sad.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_sad.gif new file mode 100644 index 00000000..d2ac78c0 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_sad.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_smile.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_smile.gif new file mode 100644 index 00000000..7b1f6d30 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_smile.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_surprised.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_surprised.gif new file mode 100644 index 00000000..cb214243 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_surprised.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_twisted.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_twisted.gif new file mode 100644 index 00000000..502fe247 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_twisted.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_wink.gif b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_wink.gif new file mode 100644 index 00000000..d1482880 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/images/smilies/icon_wink.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/colorpicker.js b/projects/tests/tests/real/wordpress/wp-includes/js/colorpicker.js new file mode 100644 index 00000000..2f0438ed --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/colorpicker.js @@ -0,0 +1,707 @@ +// =================================================================== +// Author: Matt Kruse <matt@mattkruse.com> +// WWW: http://www.mattkruse.com/ +// +// NOTICE: You may use this code for any purpose, commercial or +// private, without any further permission from the author. You may +// remove this notice from your final code if you wish, however it is +// appreciated by the author if at least my web site address is kept. +// +// You may *NOT* re-distribute this code in any way except through its +// use. That means, you can include it in your product, or your web +// site, or any other form where the code is actually being used. You +// may not put the plain javascript up on your site for download or +// include it in your javascript libraries for download. +// If you wish to share this code with others, please just point them +// to the URL instead. +// Please DO NOT link directly to my .js files from your site. Copy +// the files to your server and use them there. Thank you. +// =================================================================== + + +/* SOURCE FILE: AnchorPosition.js */ + +/* +AnchorPosition.js +Author: Matt Kruse +Last modified: 10/11/02 + +DESCRIPTION: These functions find the position of an <A> tag in a document, +so other elements can be positioned relative to it. + +COMPATABILITY: Netscape 4.x,6.x,Mozilla, IE 5.x,6.x on Windows. Some small +positioning errors - usually with Window positioning - occur on the +Macintosh platform. + +FUNCTIONS: +getAnchorPosition(anchorname) + Returns an Object() having .x and .y properties of the pixel coordinates + of the upper-left corner of the anchor. Position is relative to the PAGE. + +getAnchorWindowPosition(anchorname) + Returns an Object() having .x and .y properties of the pixel coordinates + of the upper-left corner of the anchor, relative to the WHOLE SCREEN. + +NOTES: + +1) For popping up separate browser windows, use getAnchorWindowPosition. + Otherwise, use getAnchorPosition + +2) Your anchor tag MUST contain both NAME and ID attributes which are the + same. For example: + <A NAME="test" ID="test"> </A> + +3) There must be at least a space between <A> </A> for IE5.5 to see the + anchor tag correctly. Do not do <A></A> with no space. +*/ + +// getAnchorPosition(anchorname) +// This function returns an object having .x and .y properties which are the coordinates +// of the named anchor, relative to the page. +function getAnchorPosition(anchorname) { + // This function will return an Object with x and y properties + var useWindow=false; + var coordinates=new Object(); + var x=0,y=0; + // Browser capability sniffing + var use_gebi=false, use_css=false, use_layers=false; + if (document.getElementById) { use_gebi=true; } + else if (document.all) { use_css=true; } + else if (document.layers) { use_layers=true; } + // Logic to find position + if (use_gebi && document.all) { + x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]); + y=AnchorPosition_getPageOffsetTop(document.all[anchorname]); + } + else if (use_gebi) { + var o=document.getElementById(anchorname); + x=AnchorPosition_getPageOffsetLeft(o); + y=AnchorPosition_getPageOffsetTop(o); + } + else if (use_css) { + x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]); + y=AnchorPosition_getPageOffsetTop(document.all[anchorname]); + } + else if (use_layers) { + var found=0; + for (var i=0; i<document.anchors.length; i++) { + if (document.anchors[i].name==anchorname) { found=1; break; } + } + if (found==0) { + coordinates.x=0; coordinates.y=0; return coordinates; + } + x=document.anchors[i].x; + y=document.anchors[i].y; + } + else { + coordinates.x=0; coordinates.y=0; return coordinates; + } + coordinates.x=x; + coordinates.y=y; + return coordinates; + } + +// getAnchorWindowPosition(anchorname) +// This function returns an object having .x and .y properties which are the coordinates +// of the named anchor, relative to the window +function getAnchorWindowPosition(anchorname) { + var coordinates=getAnchorPosition(anchorname); + var x=0; + var y=0; + if (document.getElementById) { + if (isNaN(window.screenX)) { + x=coordinates.x-document.body.scrollLeft+window.screenLeft; + y=coordinates.y-document.body.scrollTop+window.screenTop; + } + else { + x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset; + y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset; + } + } + else if (document.all) { + x=coordinates.x-document.body.scrollLeft+window.screenLeft; + y=coordinates.y-document.body.scrollTop+window.screenTop; + } + else if (document.layers) { + x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset; + y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset; + } + coordinates.x=x; + coordinates.y=y; + return coordinates; + } + +// Functions for IE to get position of an object +function AnchorPosition_getPageOffsetLeft (el) { + var ol=el.offsetLeft; + while ((el=el.offsetParent) != null) { ol += el.offsetLeft; } + return ol; + } +function AnchorPosition_getWindowOffsetLeft (el) { + return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft; + } +function AnchorPosition_getPageOffsetTop (el) { + var ot=el.offsetTop; + while((el=el.offsetParent) != null) { ot += el.offsetTop; } + return ot; + } +function AnchorPosition_getWindowOffsetTop (el) { + return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop; + } + +/* SOURCE FILE: PopupWindow.js */ + +/* +PopupWindow.js +Author: Matt Kruse +Last modified: 02/16/04 + +DESCRIPTION: This object allows you to easily and quickly popup a window +in a certain place. The window can either be a DIV or a separate browser +window. + +COMPATABILITY: Works with Netscape 4.x, 6.x, IE 5.x on Windows. Some small +positioning errors - usually with Window positioning - occur on the +Macintosh platform. Due to bugs in Netscape 4.x, populating the popup +window with <STYLE> tags may cause errors. + +USAGE: +// Create an object for a WINDOW popup +var win = new PopupWindow(); + +// Create an object for a DIV window using the DIV named 'mydiv' +var win = new PopupWindow('mydiv'); + +// Set the window to automatically hide itself when the user clicks +// anywhere else on the page except the popup +win.autoHide(); + +// Show the window relative to the anchor name passed in +win.showPopup(anchorname); + +// Hide the popup +win.hidePopup(); + +// Set the size of the popup window (only applies to WINDOW popups +win.setSize(width,height); + +// Populate the contents of the popup window that will be shown. If you +// change the contents while it is displayed, you will need to refresh() +win.populate(string); + +// set the URL of the window, rather than populating its contents +// manually +win.setUrl("http://www.site.com/"); + +// Refresh the contents of the popup +win.refresh(); + +// Specify how many pixels to the right of the anchor the popup will appear +win.offsetX = 50; + +// Specify how many pixels below the anchor the popup will appear +win.offsetY = 100; + +NOTES: +1) Requires the functions in AnchorPosition.js + +2) Your anchor tag MUST contain both NAME and ID attributes which are the + same. For example: + <A NAME="test" ID="test"> </A> + +3) There must be at least a space between <A> </A> for IE5.5 to see the + anchor tag correctly. Do not do <A></A> with no space. + +4) When a PopupWindow object is created, a handler for 'onmouseup' is + attached to any event handler you may have already defined. Do NOT define + an event handler for 'onmouseup' after you define a PopupWindow object or + the autoHide() will not work correctly. +*/ + +// Set the position of the popup window based on the anchor +function PopupWindow_getXYPosition(anchorname) { + var coordinates; + if (this.type == "WINDOW") { + coordinates = getAnchorWindowPosition(anchorname); + } + else { + coordinates = getAnchorPosition(anchorname); + } + this.x = coordinates.x; + this.y = coordinates.y; + } +// Set width/height of DIV/popup window +function PopupWindow_setSize(width,height) { + this.width = width; + this.height = height; + } +// Fill the window with contents +function PopupWindow_populate(contents) { + this.contents = contents; + this.populated = false; + } +// Set the URL to go to +function PopupWindow_setUrl(url) { + this.url = url; + } +// Set the window popup properties +function PopupWindow_setWindowProperties(props) { + this.windowProperties = props; + } +// Refresh the displayed contents of the popup +function PopupWindow_refresh() { + if (this.divName != null) { + // refresh the DIV object + if (this.use_gebi) { + document.getElementById(this.divName).innerHTML = this.contents; + } + else if (this.use_css) { + document.all[this.divName].innerHTML = this.contents; + } + else if (this.use_layers) { + var d = document.layers[this.divName]; + d.document.open(); + d.document.writeln(this.contents); + d.document.close(); + } + } + else { + if (this.popupWindow != null && !this.popupWindow.closed) { + if (this.url!="") { + this.popupWindow.location.href=this.url; + } + else { + this.popupWindow.document.open(); + this.popupWindow.document.writeln(this.contents); + this.popupWindow.document.close(); + } + this.popupWindow.focus(); + } + } + } +// Position and show the popup, relative to an anchor object +function PopupWindow_showPopup(anchorname) { + this.getXYPosition(anchorname); + this.x += this.offsetX; + this.y += this.offsetY; + if (!this.populated && (this.contents != "")) { + this.populated = true; + this.refresh(); + } + if (this.divName != null) { + // Show the DIV object + if (this.use_gebi) { + document.getElementById(this.divName).style.left = this.x + "px"; + document.getElementById(this.divName).style.top = this.y; + document.getElementById(this.divName).style.visibility = "visible"; + } + else if (this.use_css) { + document.all[this.divName].style.left = this.x; + document.all[this.divName].style.top = this.y; + document.all[this.divName].style.visibility = "visible"; + } + else if (this.use_layers) { + document.layers[this.divName].left = this.x; + document.layers[this.divName].top = this.y; + document.layers[this.divName].visibility = "visible"; + } + } + else { + if (this.popupWindow == null || this.popupWindow.closed) { + // If the popup window will go off-screen, move it so it doesn't + if (this.x<0) { this.x=0; } + if (this.y<0) { this.y=0; } + if (screen && screen.availHeight) { + if ((this.y + this.height) > screen.availHeight) { + this.y = screen.availHeight - this.height; + } + } + if (screen && screen.availWidth) { + if ((this.x + this.width) > screen.availWidth) { + this.x = screen.availWidth - this.width; + } + } + var avoidAboutBlank = window.opera || ( document.layers && !navigator.mimeTypes['*'] ) || navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ); + this.popupWindow = window.open(avoidAboutBlank?"":"about:blank","window_"+anchorname,this.windowProperties+",width="+this.width+",height="+this.height+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+""); + } + this.refresh(); + } + } +// Hide the popup +function PopupWindow_hidePopup() { + if (this.divName != null) { + if (this.use_gebi) { + document.getElementById(this.divName).style.visibility = "hidden"; + } + else if (this.use_css) { + document.all[this.divName].style.visibility = "hidden"; + } + else if (this.use_layers) { + document.layers[this.divName].visibility = "hidden"; + } + } + else { + if (this.popupWindow && !this.popupWindow.closed) { + this.popupWindow.close(); + this.popupWindow = null; + } + } + } +// Pass an event and return whether or not it was the popup DIV that was clicked +function PopupWindow_isClicked(e) { + if (this.divName != null) { + if (this.use_layers) { + var clickX = e.pageX; + var clickY = e.pageY; + var t = document.layers[this.divName]; + if ((clickX > t.left) && (clickX < t.left+t.clip.width) && (clickY > t.top) && (clickY < t.top+t.clip.height)) { + return true; + } + else { return false; } + } + else if (document.all) { // Need to hard-code this to trap IE for error-handling + var t = window.event.srcElement; + while (t.parentElement != null) { + if (t.id==this.divName) { + return true; + } + t = t.parentElement; + } + return false; + } + else if (this.use_gebi && e) { + var t = e.originalTarget; + while (t.parentNode != null) { + if (t.id==this.divName) { + return true; + } + t = t.parentNode; + } + return false; + } + return false; + } + return false; + } + +// Check an onMouseDown event to see if we should hide +function PopupWindow_hideIfNotClicked(e) { + if (this.autoHideEnabled && !this.isClicked(e)) { + this.hidePopup(); + } + } +// Call this to make the DIV disable automatically when mouse is clicked outside it +function PopupWindow_autoHide() { + this.autoHideEnabled = true; + } +// This global function checks all PopupWindow objects onmouseup to see if they should be hidden +function PopupWindow_hidePopupWindows(e) { + for (var i=0; i<popupWindowObjects.length; i++) { + if (popupWindowObjects[i] != null) { + var p = popupWindowObjects[i]; + p.hideIfNotClicked(e); + } + } + } +// Run this immediately to attach the event listener +function PopupWindow_attachListener() { + if (document.layers) { + document.captureEvents(Event.MOUSEUP); + } + window.popupWindowOldEventListener = document.onmouseup; + if (window.popupWindowOldEventListener != null) { + document.onmouseup = new Function("window.popupWindowOldEventListener(); PopupWindow_hidePopupWindows();"); + } + else { + document.onmouseup = PopupWindow_hidePopupWindows; + } + } +// CONSTRUCTOR for the PopupWindow object +// Pass it a DIV name to use a DHTML popup, otherwise will default to window popup +function PopupWindow() { + if (!window.popupWindowIndex) { window.popupWindowIndex = 0; } + if (!window.popupWindowObjects) { window.popupWindowObjects = new Array(); } + if (!window.listenerAttached) { + window.listenerAttached = true; + PopupWindow_attachListener(); + } + this.index = popupWindowIndex++; + popupWindowObjects[this.index] = this; + this.divName = null; + this.popupWindow = null; + this.width=0; + this.height=0; + this.populated = false; + this.visible = false; + this.autoHideEnabled = false; + + this.contents = ""; + this.url=""; + this.windowProperties="toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,alwaysRaised,dependent,titlebar=no"; + if (arguments.length>0) { + this.type="DIV"; + this.divName = arguments[0]; + } + else { + this.type="WINDOW"; + } + this.use_gebi = false; + this.use_css = false; + this.use_layers = false; + if (document.getElementById) { this.use_gebi = true; } + else if (document.all) { this.use_css = true; } + else if (document.layers) { this.use_layers = true; } + else { this.type = "WINDOW"; } + this.offsetX = 0; + this.offsetY = 0; + // Method mappings + this.getXYPosition = PopupWindow_getXYPosition; + this.populate = PopupWindow_populate; + this.setUrl = PopupWindow_setUrl; + this.setWindowProperties = PopupWindow_setWindowProperties; + this.refresh = PopupWindow_refresh; + this.showPopup = PopupWindow_showPopup; + this.hidePopup = PopupWindow_hidePopup; + this.setSize = PopupWindow_setSize; + this.isClicked = PopupWindow_isClicked; + this.autoHide = PopupWindow_autoHide; + this.hideIfNotClicked = PopupWindow_hideIfNotClicked; + } + +/* SOURCE FILE: ColorPicker2.js */ + +/* +Last modified: 02/24/2003 + +DESCRIPTION: This widget is used to select a color, in hexadecimal #RRGGBB +form. It uses a color "swatch" to display the standard 216-color web-safe +palette. The user can then click on a color to select it. + +COMPATABILITY: See notes in AnchorPosition.js and PopupWindow.js. +Only the latest DHTML-capable browsers will show the color and hex values +at the bottom as your mouse goes over them. + +USAGE: +// Create a new ColorPicker object using DHTML popup +var cp = new ColorPicker(); + +// Create a new ColorPicker object using Window Popup +var cp = new ColorPicker('window'); + +// Add a link in your page to trigger the popup. For example: +<A HREF="#" onClick="cp.show('pick');return false;" NAME="pick" ID="pick">Pick</A> + +// Or use the built-in "select" function to do the dirty work for you: +<A HREF="#" onClick="cp.select(document.forms[0].color,'pick');return false;" NAME="pick" ID="pick">Pick</A> + +// If using DHTML popup, write out the required DIV tag near the bottom +// of your page. +<SCRIPT LANGUAGE="JavaScript">cp.writeDiv()</SCRIPT> + +// Write the 'pickColor' function that will be called when the user clicks +// a color and do something with the value. This is only required if you +// want to do something other than simply populate a form field, which is +// what the 'select' function will give you. +function pickColor(color) { + field.value = color; + } + +NOTES: +1) Requires the functions in AnchorPosition.js and PopupWindow.js + +2) Your anchor tag MUST contain both NAME and ID attributes which are the + same. For example: + <A NAME="test" ID="test"> </A> + +3) There must be at least a space between <A> </A> for IE5.5 to see the + anchor tag correctly. Do not do <A></A> with no space. + +4) When a ColorPicker object is created, a handler for 'onmouseup' is + attached to any event handler you may have already defined. Do NOT define + an event handler for 'onmouseup' after you define a ColorPicker object or + the color picker will not hide itself correctly. +*/ +ColorPicker_targetInput = null; +function ColorPicker_writeDiv() { + document.writeln("<DIV ID=\"colorPickerDiv\" STYLE=\"position:absolute;visibility:hidden;\"> </DIV>"); + } + +function ColorPicker_show(anchorname) { + this.showPopup(anchorname); + } + +function ColorPicker_pickColor(color,obj) { + obj.hidePopup(); + pickColor(color); + } + +// A Default "pickColor" function to accept the color passed back from popup. +// User can over-ride this with their own function. +function pickColor(color) { + if (ColorPicker_targetInput==null) { + alert("Target Input is null, which means you either didn't use the 'select' function or you have no defined your own 'pickColor' function to handle the picked color!"); + return; + } + ColorPicker_targetInput.value = color; + } + +// This function is the easiest way to popup the window, select a color, and +// have the value populate a form field, which is what most people want to do. +function ColorPicker_select(inputobj,linkname) { + if (inputobj.type!="text" && inputobj.type!="hidden" && inputobj.type!="textarea") { + alert("colorpicker.select: Input object passed is not a valid form input object"); + window.ColorPicker_targetInput=null; + return; + } + window.ColorPicker_targetInput = inputobj; + this.show(linkname); + } + +// This function runs when you move your mouse over a color block, if you have a newer browser +function ColorPicker_highlightColor(c) { + var thedoc = (arguments.length>1)?arguments[1]:window.document; + var d = thedoc.getElementById("colorPickerSelectedColor"); + d.style.backgroundColor = c; + d = thedoc.getElementById("colorPickerSelectedColorValue"); + d.innerHTML = c; + } + +function ColorPicker() { + var windowMode = false; + // Create a new PopupWindow object + if (arguments.length==0) { + var divname = "colorPickerDiv"; + } + else if (arguments[0] == "window") { + var divname = ''; + windowMode = true; + } + else { + var divname = arguments[0]; + } + + if (divname != "") { + var cp = new PopupWindow(divname); + } + else { + var cp = new PopupWindow(); + cp.setSize(225,250); + } + + // Object variables + cp.currentValue = "#FFFFFF"; + + // Method Mappings + cp.writeDiv = ColorPicker_writeDiv; + cp.highlightColor = ColorPicker_highlightColor; + cp.show = ColorPicker_show; + cp.select = ColorPicker_select; + + // Code to populate color picker window + var colors = new Array( "#4180B6","#69AEE7","#000000","#000033","#000066","#000099","#0000CC","#0000FF","#330000","#330033","#330066","#330099", + "#3300CC","#3300FF","#660000","#660033","#660066","#660099","#6600CC","#6600FF","#990000","#990033","#990066","#990099", + "#9900CC","#9900FF","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#FF0000","#FF0033","#FF0066","#FF0099", + "#FF00CC","#FF00FF","#7FFFFF","#7FFFFF","#7FF7F7","#7FEFEF","#7FE7E7","#7FDFDF","#7FD7D7","#7FCFCF","#7FC7C7","#7FBFBF", + "#7FB7B7","#7FAFAF","#7FA7A7","#7F9F9F","#7F9797","#7F8F8F","#7F8787","#7F7F7F","#7F7777","#7F6F6F","#7F6767","#7F5F5F", + "#7F5757","#7F4F4F","#7F4747","#7F3F3F","#7F3737","#7F2F2F","#7F2727","#7F1F1F","#7F1717","#7F0F0F","#7F0707","#7F0000", + + "#4180B6","#69AEE7","#003300","#003333","#003366","#003399","#0033CC","#0033FF","#333300","#333333","#333366","#333399", + "#3333CC","#3333FF","#663300","#663333","#663366","#663399","#6633CC","#6633FF","#993300","#993333","#993366","#993399", + "#9933CC","#9933FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF","#FF3300","#FF3333","#FF3366","#FF3399", + "#FF33CC","#FF33FF","#FF7FFF","#FF7FFF","#F77FF7","#EF7FEF","#E77FE7","#DF7FDF","#D77FD7","#CF7FCF","#C77FC7","#BF7FBF", + "#B77FB7","#AF7FAF","#A77FA7","#9F7F9F","#977F97","#8F7F8F","#877F87","#7F7F7F","#777F77","#6F7F6F","#677F67","#5F7F5F", + "#577F57","#4F7F4F","#477F47","#3F7F3F","#377F37","#2F7F2F","#277F27","#1F7F1F","#177F17","#0F7F0F","#077F07","#007F00", + + "#4180B6","#69AEE7","#006600","#006633","#006666","#006699","#0066CC","#0066FF","#336600","#336633","#336666","#336699", + "#3366CC","#3366FF","#666600","#666633","#666666","#666699","#6666CC","#6666FF","#996600","#996633","#996666","#996699", + "#9966CC","#9966FF","#CC6600","#CC6633","#CC6666","#CC6699","#CC66CC","#CC66FF","#FF6600","#FF6633","#FF6666","#FF6699", + "#FF66CC","#FF66FF","#FFFF7F","#FFFF7F","#F7F77F","#EFEF7F","#E7E77F","#DFDF7F","#D7D77F","#CFCF7F","#C7C77F","#BFBF7F", + "#B7B77F","#AFAF7F","#A7A77F","#9F9F7F","#97977F","#8F8F7F","#87877F","#7F7F7F","#77777F","#6F6F7F","#67677F","#5F5F7F", + "#57577F","#4F4F7F","#47477F","#3F3F7F","#37377F","#2F2F7F","#27277F","#1F1F7F","#17177F","#0F0F7F","#07077F","#00007F", + + "#4180B6","#69AEE7","#009900","#009933","#009966","#009999","#0099CC","#0099FF","#339900","#339933","#339966","#339999", + "#3399CC","#3399FF","#669900","#669933","#669966","#669999","#6699CC","#6699FF","#999900","#999933","#999966","#999999", + "#9999CC","#9999FF","#CC9900","#CC9933","#CC9966","#CC9999","#CC99CC","#CC99FF","#FF9900","#FF9933","#FF9966","#FF9999", + "#FF99CC","#FF99FF","#3FFFFF","#3FFFFF","#3FF7F7","#3FEFEF","#3FE7E7","#3FDFDF","#3FD7D7","#3FCFCF","#3FC7C7","#3FBFBF", + "#3FB7B7","#3FAFAF","#3FA7A7","#3F9F9F","#3F9797","#3F8F8F","#3F8787","#3F7F7F","#3F7777","#3F6F6F","#3F6767","#3F5F5F", + "#3F5757","#3F4F4F","#3F4747","#3F3F3F","#3F3737","#3F2F2F","#3F2727","#3F1F1F","#3F1717","#3F0F0F","#3F0707","#3F0000", + + "#4180B6","#69AEE7","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#33CC00","#33CC33","#33CC66","#33CC99", + "#33CCCC","#33CCFF","#66CC00","#66CC33","#66CC66","#66CC99","#66CCCC","#66CCFF","#99CC00","#99CC33","#99CC66","#99CC99", + "#99CCCC","#99CCFF","#CCCC00","#CCCC33","#CCCC66","#CCCC99","#CCCCCC","#CCCCFF","#FFCC00","#FFCC33","#FFCC66","#FFCC99", + "#FFCCCC","#FFCCFF","#FF3FFF","#FF3FFF","#F73FF7","#EF3FEF","#E73FE7","#DF3FDF","#D73FD7","#CF3FCF","#C73FC7","#BF3FBF", + "#B73FB7","#AF3FAF","#A73FA7","#9F3F9F","#973F97","#8F3F8F","#873F87","#7F3F7F","#773F77","#6F3F6F","#673F67","#5F3F5F", + "#573F57","#4F3F4F","#473F47","#3F3F3F","#373F37","#2F3F2F","#273F27","#1F3F1F","#173F17","#0F3F0F","#073F07","#003F00", + + "#4180B6","#69AEE7","#00FF00","#00FF33","#00FF66","#00FF99","#00FFCC","#00FFFF","#33FF00","#33FF33","#33FF66","#33FF99", + "#33FFCC","#33FFFF","#66FF00","#66FF33","#66FF66","#66FF99","#66FFCC","#66FFFF","#99FF00","#99FF33","#99FF66","#99FF99", + "#99FFCC","#99FFFF","#CCFF00","#CCFF33","#CCFF66","#CCFF99","#CCFFCC","#CCFFFF","#FFFF00","#FFFF33","#FFFF66","#FFFF99", + "#FFFFCC","#FFFFFF","#FFFF3F","#FFFF3F","#F7F73F","#EFEF3F","#E7E73F","#DFDF3F","#D7D73F","#CFCF3F","#C7C73F","#BFBF3F", + "#B7B73F","#AFAF3F","#A7A73F","#9F9F3F","#97973F","#8F8F3F","#87873F","#7F7F3F","#77773F","#6F6F3F","#67673F","#5F5F3F", + "#57573F","#4F4F3F","#47473F","#3F3F3F","#37373F","#2F2F3F","#27273F","#1F1F3F","#17173F","#0F0F3F","#07073F","#00003F", + + "#4180B6","#69AEE7","#FFFFFF","#FFEEEE","#FFDDDD","#FFCCCC","#FFBBBB","#FFAAAA","#FF9999","#FF8888","#FF7777","#FF6666", + "#FF5555","#FF4444","#FF3333","#FF2222","#FF1111","#FF0000","#FF0000","#FF0000","#FF0000","#EE0000","#DD0000","#CC0000", + "#BB0000","#AA0000","#990000","#880000","#770000","#660000","#550000","#440000","#330000","#220000","#110000","#000000", + "#000000","#000000","#000000","#001111","#002222","#003333","#004444","#005555","#006666","#007777","#008888","#009999", + "#00AAAA","#00BBBB","#00CCCC","#00DDDD","#00EEEE","#00FFFF","#00FFFF","#00FFFF","#00FFFF","#11FFFF","#22FFFF","#33FFFF", + "#44FFFF","#55FFFF","#66FFFF","#77FFFF","#88FFFF","#99FFFF","#AAFFFF","#BBFFFF","#CCFFFF","#DDFFFF","#EEFFFF","#FFFFFF", + + "#4180B6","#69AEE7","#FFFFFF","#EEFFEE","#DDFFDD","#CCFFCC","#BBFFBB","#AAFFAA","#99FF99","#88FF88","#77FF77","#66FF66", + "#55FF55","#44FF44","#33FF33","#22FF22","#11FF11","#00FF00","#00FF00","#00FF00","#00FF00","#00EE00","#00DD00","#00CC00", + "#00BB00","#00AA00","#009900","#008800","#007700","#006600","#005500","#004400","#003300","#002200","#001100","#000000", + "#000000","#000000","#000000","#110011","#220022","#330033","#440044","#550055","#660066","#770077","#880088","#990099", + "#AA00AA","#BB00BB","#CC00CC","#DD00DD","#EE00EE","#FF00FF","#FF00FF","#FF00FF","#FF00FF","#FF11FF","#FF22FF","#FF33FF", + "#FF44FF","#FF55FF","#FF66FF","#FF77FF","#FF88FF","#FF99FF","#FFAAFF","#FFBBFF","#FFCCFF","#FFDDFF","#FFEEFF","#FFFFFF", + + "#4180B6","#69AEE7","#FFFFFF","#EEEEFF","#DDDDFF","#CCCCFF","#BBBBFF","#AAAAFF","#9999FF","#8888FF","#7777FF","#6666FF", + "#5555FF","#4444FF","#3333FF","#2222FF","#1111FF","#0000FF","#0000FF","#0000FF","#0000FF","#0000EE","#0000DD","#0000CC", + "#0000BB","#0000AA","#000099","#000088","#000077","#000066","#000055","#000044","#000033","#000022","#000011","#000000", + "#000000","#000000","#000000","#111100","#222200","#333300","#444400","#555500","#666600","#777700","#888800","#999900", + "#AAAA00","#BBBB00","#CCCC00","#DDDD00","#EEEE00","#FFFF00","#FFFF00","#FFFF00","#FFFF00","#FFFF11","#FFFF22","#FFFF33", + "#FFFF44","#FFFF55","#FFFF66","#FFFF77","#FFFF88","#FFFF99","#FFFFAA","#FFFFBB","#FFFFCC","#FFFFDD","#FFFFEE","#FFFFFF", + + "#4180B6","#69AEE7","#FFFFFF","#FFFFFF","#FBFBFB","#F7F7F7","#F3F3F3","#EFEFEF","#EBEBEB","#E7E7E7","#E3E3E3","#DFDFDF", + "#DBDBDB","#D7D7D7","#D3D3D3","#CFCFCF","#CBCBCB","#C7C7C7","#C3C3C3","#BFBFBF","#BBBBBB","#B7B7B7","#B3B3B3","#AFAFAF", + "#ABABAB","#A7A7A7","#A3A3A3","#9F9F9F","#9B9B9B","#979797","#939393","#8F8F8F","#8B8B8B","#878787","#838383","#7F7F7F", + "#7B7B7B","#777777","#737373","#6F6F6F","#6B6B6B","#676767","#636363","#5F5F5F","#5B5B5B","#575757","#535353","#4F4F4F", + "#4B4B4B","#474747","#434343","#3F3F3F","#3B3B3B","#373737","#333333","#2F2F2F","#2B2B2B","#272727","#232323","#1F1F1F", + "#1B1B1B","#171717","#131313","#0F0F0F","#0B0B0B","#070707","#030303","#000000","#000000","#000000","#000000","#000000"); + var total = colors.length; + var width = 72; + var cp_contents = ""; + var windowRef = (windowMode)?"window.opener.":""; + if (windowMode) { + cp_contents += "<html><head><title>Select Color"; + cp_contents += "
    "; + } + cp_contents += ""; + var use_highlight = (document.getElementById || document.all)?true:false; + for (var i=0; i '; + if ( ((i+1)>=total) || (((i+1) % width) == 0)) { + cp_contents += ""; + } + } + // If the browser supports dynamically changing TD cells, add the fancy stuff + if (document.getElementById) { + var width1 = Math.floor(width/2); + var width2 = width = width1; + cp_contents += ""; + } + cp_contents += "
     #FFFFFF
    "; + if (windowMode) { + cp_contents += "
    "; + } + // end populate code + + // Write the contents to the popup object + cp.populate(cp_contents+"\n"); + // Move the table down a bit so you can see it + cp.offsetY = 25; + cp.autoHide(); + return cp; + } diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/dbx-key.js b/projects/tests/tests/real/wordpress/wp-includes/js/dbx-key.js new file mode 100644 index 00000000..f7155de8 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/dbx-key.js @@ -0,0 +1,39 @@ + +//initialisation function +addLoadEvent( function() +{ + //create new docking boxes group + var meta = new dbxGroup( + 'grabit', // container ID [/-_a-zA-Z0-9/] + 'vertical', // orientation ['vertical'|'horizontal'] + '10', // drag threshold ['n' pixels] + 'no', // restrict drag movement to container axis ['yes'|'no'] + '10', // animate re-ordering [frames per transition, or '0' for no effect] + 'yes', // include open/close toggle buttons ['yes'|'no'] + 'closed', // default state ['open'|'closed'] + 'open', // word for "open", as in "open this box" + 'close', // word for "close", as in "close this box" + 'click-down and drag to move this box', // sentence for "move this box" by mouse + 'click to %toggle% this box', // pattern-match sentence for "(open|close) this box" by mouse + 'use the arrow keys to move this box', // sentence for "move this box" by keyboard + ', or press the enter key to %toggle% it', // pattern-match sentence-fragment for "(open|close) this box" by keyboard + '%mytitle% [%dbxtitle%]' // pattern-match syntax for title-attribute conflicts + ); + + var advanced = new dbxGroup( + 'advancedstuff', // container ID [/-_a-zA-Z0-9/] + 'vertical', // orientation ['vertical'|'horizontal'] + '10', // drag threshold ['n' pixels] + 'yes', // restrict drag movement to container axis ['yes'|'no'] + '10', // animate re-ordering [frames per transition, or '0' for no effect] + 'yes', // include open/close toggle buttons ['yes'|'no'] + 'closed', // default state ['open'|'closed'] + 'open', // word for "open", as in "open this box" + 'close', // word for "close", as in "close this box" + 'click-down and drag to move this box', // sentence for "move this box" by mouse + 'click to %toggle% this box', // pattern-match sentence for "(open|close) this box" by mouse + 'use the arrow keys to move this box', // sentence for "move this box" by keyboard + ', or press the enter key to %toggle% it', // pattern-match sentence-fragment for "(open|close) this box" by keyboard + '%mytitle% [%dbxtitle%]' // pattern-match syntax for title-attribute conflicts + ); +}); diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/dbx.js b/projects/tests/tests/real/wordpress/wp-includes/js/dbx.js new file mode 100644 index 00000000..8bf5b463 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/dbx.js @@ -0,0 +1,6 @@ +// DBX2.01 :: Docking Boxes (dbx) +// ***************************************************** +// DOM scripting by brothercake -- http://www.brothercake.com/ +// GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html +//****************************************************** +var dbx;function dbxManager(sid){dbx = this;if(!/^[-_a-z0-9]+$/i.test(sid)) { alert('Error from dbxManager:\n"' + sid + '" is an invalid session ID'); return; }this.supported = !(document.getElementsByTagName('*').length == 0 || (navigator.vendor == 'KDE' && typeof window.sidebar == 'undefined'));if(!this.supported) { return; }this.etype = typeof document.addEventListener != 'undefined' ? 'addEventListener' : typeof document.attachEvent != 'undefined' ? 'attachEvent' : 'none';this.eprefix = (this.etype == 'attachEvent' ? 'on' : '');if(typeof window.opera != 'undefined' && parseFloat(navigator.userAgent.toLowerCase().split(/opera[\/ ]/)[1].split(' ')[0], 10) < 7.5){this.etype = 'none';}if(this.etype == 'none') { this.supported = false; return; }this.running = 0;this.sid = sid;this.savedata = {};this.cookiestate = this.getCookieState();};dbxManager.prototype.setCookieState = function(){var now = new Date();now.setTime(now.getTime() + (365*24*60*60*1000));var str = '';for(j in this.savedata){if(typeof this.savedata[j]!='function'){str += j + '=' + this.savedata[j] + '&'}}this.state = str.replace(/^(.+)&$/, '$1');if(typeof this.onstatechange == 'undefined' || this.onstatechange()){document.cookie = 'dbx-' + this.sid + '='+ this.state+ '; expires=' + now.toGMTString()+ '; path=/';}};dbxManager.prototype.getCookieState = function(){this.cookiestate = null;if(document.cookie){if(document.cookie.indexOf('dbx-' + this.sid)!=-1){this.cookie = document.cookie.split('dbx-' + this.sid + '=')[1].split('&');for(var i in this.cookie){if(typeof this.cookie[i]!='function'){this.cookie[i] = this.cookie[i].split('=');this.cookie[i][1] = this.cookie[i][1].split(',');}}this.cookiestate = {};for(i in this.cookie){if(typeof this.cookie[i]!='function'){this.cookiestate[this.cookie[i][0]] = this.cookie[i][1];}}}}return this.cookiestate;};dbxManager.prototype.addDataMember = function(gid, order){this.savedata[gid] = order;};dbxManager.prototype.createElement = function(tag){return typeof document.createElementNS != 'undefined' ? document.createElementNS('http://www.w3.org/1999/xhtml', tag) : document.createElement(tag);};dbxManager.prototype.getTarget = function(e, pattern, node){if(typeof node != 'undefined'){var target = node;}else{target = typeof e.target != 'undefined' ? e.target : e.srcElement;}var regex = new RegExp(pattern, '');while(!regex.test(target.className)){target = target.parentNode;}return target;};function dbxGroup(gid, dir, thresh, fix, ani, togs, def, open, close, move, toggle, kmove, ktoggle, syntax){if(!/^[-_a-z0-9]+$/i.test(gid)) { alert('Error from dbxGroup:\n"' + gid + '" is an invalid container ID'); return; }this.container = document.getElementById(gid);if(this.container == null || !dbx.supported) { return; }var self = this;this.gid = gid;this.dragok = false;this.box = null;this.vertical = dir == 'vertical';this.threshold = parseInt(thresh, 10);this.restrict = fix == 'yes';this.resolution = parseInt(ani, 10);this.toggles = togs == 'yes';this.defopen = def != 'closed';this.vocab = {'open' : open,'close' : close,'move' : move,'toggle' : toggle,'kmove' : kmove,'ktoggle' : ktoggle,'syntax' : syntax};this.container.style.position = 'relative';this.container.style.display = 'block';if(typeof window.opera != 'undefined'){this.container.style.display = 'run-in';}this.boxes = [];this.buttons = [];this.order = [];this.eles = this.container.getElementsByTagName('*');for(var i=0; i 0)){var sibling = this.boxes[positions[i + (positive ? 1 : -1)][0]];if(this.resolution > 0){var visipos = { 'x' : parent.offsetLeft, 'y' : parent.offsetTop };var siblingpos = { 'x' : sibling.offsetLeft, 'y' : sibling.offsetTop };}var obj = { 'insert' : (positive ? sibling : parent), 'before' : (positive ? parent : sibling) };this.container.insertBefore(obj.insert, obj.before);if(this.resolution > 0){var animators ={'sibling' : new dbxAnimator(this, sibling, siblingpos, this.resolution, true, anchor),'parent' : new dbxAnimator(this, parent, visipos, this.resolution, true, anchor)};}else{anchor.focus();}break;}}}this.getBoxOrder();}};dbxGroup.prototype.compare = function(a, b){return a[1] - b[1];};dbxGroup.prototype.createTooltip = function(isopen, anchor){if(this.keydown){this.tooltip = this.container.appendChild(dbx.createElement('span'));this.tooltip.style.visibility = 'hidden';this.tooltip.className = 'dbx-tooltip';if(isopen != null){this.tooltip.appendChild(document.createTextNode(this.vocab.kmove + this.vocab.ktoggle.replace('%toggle%', isopen ? this.vocab.close : this.vocab.open)));}else{this.tooltip.appendChild(document.createTextNode(this.vocab.kmove));}var parent = dbx.getTarget(null, 'dbx\-box', anchor);this.tooltip.style.left = parent.offsetLeft + 'px';this.tooltip.style.top = parent.offsetTop + 'px';var tooltip = this.tooltip;window.setTimeout(function(){if(tooltip != null) { tooltip.style.visibility = 'visible'; }}, 500);}};dbxGroup.prototype.removeTooltip = function(){if(this.tooltip != null){this.tooltip.parentNode.removeChild(this.tooltip);this.tooltip = null;}};dbxGroup.prototype.mousedown = function(e, box){var node = typeof e.target != 'undefined' ? e.target : e.srcElement;if(node.nodeName == '#text') { node = node.parentNode; }if(!/dbx\-(toggle|box|group)/i.test(node.className)){while(!/dbx\-(handle|box|group)/i.test(node.className)){node = node.parentNode;}}if(/dbx\-handle/i.test(node.className)){this.removeTooltip();this.released = false;this.initial = { 'x' : e.clientX, 'y' : e.clientY };this.current = { 'x' : 0, 'y' : 0 };this.createCloneBox(box);if(typeof e.preventDefault != 'undefined' ) { e.preventDefault(); }if(typeof document.onselectstart != 'undefined'){document.onselectstart = function() { return false; }}}};dbxGroup.prototype.mousemove = function(e){if(this.dragok && this.box != null){this.positive = this.vertical ? (e.clientY > this.current.y ? true : false) : (e.clientX > this.current.x ? true : false);this.current = { 'x' : e.clientX, 'y' : e.clientY };var overall = { 'x' : this.current.x - this.initial.x, 'y' : this.current.y - this.initial.y };if(((overall.x >= 0 && overall.x <= this.threshold) || (overall.x <= 0 && overall.x >= 0 - this.threshold))&&((overall.y >= 0 && overall.y <= this.threshold) || (overall.y <= 0 && overall.y >= 0 - this.threshold))){this.current.x -= overall.x;this.current.y -= overall.y;}if(this.released || overall.x > this.threshold || overall.x < (0 - this.threshold) || overall.y > this.threshold || overall.y < (0 - this.threshold)){dbx.group = this.container;dbx.box = this.box;dbx.event = e;if(typeof dbx.onboxdrag == 'undefined' || dbx.onboxdrag()){this.released = true;if(!this.restrict || !this.vertical) { this.boxclone.style.left = (this.current.x - this.difference.x) + 'px'; }if(!this.restrict || this.vertical) { this.boxclone.style.top = (this.current.y - this.difference.y) + 'px'; }this.moveOriginalToPosition(this.current.x, this.current.y);if(typeof e.preventDefault != 'undefined' ) { e.preventDefault(); }}}}return true;};dbxGroup.prototype.mouseup = function(e){if(this.box != null){this.moveOriginalToPosition(e.clientX, e.clientY);this.removeCloneBox();this.getBoxOrder();if(typeof document.onselectstart != 'undefined'){document.onselectstart = function() { return true; }}}this.dragok = false;};dbxGroup.prototype.keypress = function(e, anchor){if(/^(3[7-9])|(40)$/.test(e.keyCode)){this.removeTooltip();if((this.vertical && /^(38|40)$/.test(e.keyCode)) || (!this.vertical && /^(37|39)$/.test(e.keyCode))){this.shiftBoxPosition(e, anchor, /^[3][78]$/.test(e.keyCode) ? false : true);if(typeof e.preventDefault != 'undefined') { e.preventDefault(); }else { return false; }typeof e.stopPropagation != 'undefined' ? e.stopPropagation() : e.cancelBubble = true;this.keydown = false;}}return true;};dbxGroup.prototype.getBoxOrder = function(){this.order = [];var len = this.eles.length;for(var j=0; j boxprops.xy && cloneprops.xy < boxprops.xy)||(!this.positive && cloneprops.xy < boxprops.xy && cloneprops.xy + cloneprops.wh > boxprops.xy)){if(this.boxes[i] == this.box) { return; }var sibling = this.box.nextSibling;while(sibling.className == null || !/dbx\-box/.test(sibling.className)){sibling = sibling.nextSibling;}if(this.boxes[i] == sibling) { return; }if(this.resolution > 0){if(this.box[this.vertical ? 'offsetTop' : 'offsetLeft'] < boxprops.xy){var visibox = this.boxes[i].previousSibling;while(visibox.className == null || !/dbx\-box/.test(visibox.className)){visibox = visibox.previousSibling;}}else{visibox = this.boxes[i];}var visipos = { 'x' : visibox.offsetLeft, 'y' : visibox.offsetTop };}var prepos = { 'x' : this.box.offsetLeft, 'y' : this.box.offsetTop };this.container.insertBefore(this.box, this.boxes[i]);this.initial.x += (this.box.offsetLeft - prepos.x);this.initial.y += (this.box.offsetTop - prepos.y);if(this.resolution > 0 && visibox != this.box){var animator = new dbxAnimator(this, visibox, visipos, this.resolution, false, null);}else{}break;}}};function dbxAnimator(caller, box, pos, res, kbd, anchor){this.caller = caller;this.box = box;this.timer = null;var before = pos[this.caller.vertical ? 'y' : 'x'];var after = this.box[this.caller.vertical ? 'offsetTop' : 'offsetLeft'];if(before != after){if(dbx.running > this.caller.boxes.length - 1) { return; }var clone = this.caller.createClone(this.box, 29999, arguments[2]);clone.style.visibility = 'visible';this.box.style.visibility = 'hidden';this.animateClone(clone,before,after > before ? after - before : 0 - (before - after),this.caller.vertical ? 'top' : 'left',res,kbd,anchor);}};dbxAnimator.prototype.animateClone = function(clone, current, change, dir, res, kbd, anchor){var self = this;var count = 0;dbx.running ++;this.timer = window.setInterval(function(){count ++;current += change / res;clone.style[dir] = current + 'px';if(count == res){window.clearTimeout(self.timer);self.timer = null;dbx.running --;self.caller.container.removeChild(clone);self.box.style.visibility = 'visible';if(kbd){if(anchor != null && anchor.parentNode.style.visibility != 'hidden'){anchor.focus();}else if(self.caller.toggles){var button = self.caller.buttons[parseInt(self.box.className.split('dbxid')[1],10)];if(button != null && typeof button.isactive != 'undefined'){button.focus();}}}}}, 20);};if(typeof window.attachEvent != 'undefined'){window.attachEvent('onunload', function(){var ev = ['mousedown', 'mousemove', 'mouseup', 'mouseout', 'click', 'keydown', 'keyup', 'focus', 'blur', 'selectstart', 'statechange', 'boxdrag', 'boxopen', 'boxclose'];var el = ev.length;var dl = document.all.length;for(var i=0; i' +,'' +,'b' +); + +edButtons[edButtons.length] = +new edButton('ed_em' +,'i' +,'' +,'' +,'i' +); + +edButtons[edButtons.length] = +new edButton('ed_link' +,'link' +,'' +,'' +,'a' +); // special case + +edButtons[edButtons.length] = +new edButton('ed_block' +,'b-quote' +,'\n\n
    ' +,'
    \n\n' +,'q' +); + + +edButtons[edButtons.length] = +new edButton('ed_del' +,'del' +,'' +,'' +,'d' +); + +edButtons[edButtons.length] = +new edButton('ed_ins' +,'ins' +,'' +,'' +,'s' +); + +edButtons[edButtons.length] = +new edButton('ed_img' +,'img' +,'' +,'' +,'m' +,-1 +); // special case + +edButtons[edButtons.length] = +new edButton('ed_ul' +,'ul' +,'
      \n' +,'
    \n\n' +,'u' +); + +edButtons[edButtons.length] = +new edButton('ed_ol' +,'ol' +,'
      \n' +,'
    \n\n' +,'o' +); + +edButtons[edButtons.length] = +new edButton('ed_li' +,'li' +,'\t
  • ' +,'
  • \n' +,'l' +); + +edButtons[edButtons.length] = +new edButton('ed_code' +,'code' +,'' +,'' +,'c' +); + +edButtons[edButtons.length] = +new edButton('ed_more' +,'more' +,'' +,'' +,'t' +,-1 +); +/* +edButtons[edButtons.length] = +new edButton('ed_next' +,'page' +,'' +,'' +,'p' +,-1 +); +*/ +function edLink() { + this.display = ''; + this.URL = ''; + this.newWin = 0; +} + +edLinks[edLinks.length] = new edLink('WordPress' + ,'http://wordpress.org/' + ); + +edLinks[edLinks.length] = new edLink('alexking.org' + ,'http://www.alexking.org/' + ); + +function edShowButton(button, i) { + if (button.id == 'ed_img') { + document.write(''); + } + else if (button.id == 'ed_link') { + document.write(''); + } + else { + document.write(''); + } +} + +function edShowLinks() { + var tempStr = ''; + document.write(tempStr); +} + +function edAddTag(button) { + if (edButtons[button].tagEnd != '') { + edOpenTags[edOpenTags.length] = button; + document.getElementById(edButtons[button].id).value = '/' + document.getElementById(edButtons[button].id).value; + } +} + +function edRemoveTag(button) { + for (i = 0; i < edOpenTags.length; i++) { + if (edOpenTags[i] == button) { + edOpenTags.splice(i, 1); + document.getElementById(edButtons[button].id).value = document.getElementById(edButtons[button].id).value.replace('/', ''); + } + } +} + +function edCheckOpenTags(button) { + var tag = 0; + for (i = 0; i < edOpenTags.length; i++) { + if (edOpenTags[i] == button) { + tag++; + } + } + if (tag > 0) { + return true; // tag found + } + else { + return false; // tag not found + } +} + +function edCloseAllTags() { + var count = edOpenTags.length; + for (o = 0; o < count; o++) { + edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]); + } +} + +function edQuickLink(i, thisSelect) { + if (i > -1) { + var newWin = ''; + if (edLinks[i].newWin == 1) { + newWin = ' target="_blank"'; + } + var tempStr = '' + + edLinks[i].display + + ''; + thisSelect.selectedIndex = 0; + edInsertContent(edCanvas, tempStr); + } + else { + thisSelect.selectedIndex = 0; + } +} + +function edSpell(myField) { + var word = ''; + if (document.selection) { + myField.focus(); + var sel = document.selection.createRange(); + if (sel.text.length > 0) { + word = sel.text; + } + } + else if (myField.selectionStart || myField.selectionStart == '0') { + var startPos = myField.selectionStart; + var endPos = myField.selectionEnd; + if (startPos != endPos) { + word = myField.value.substring(startPos, endPos); + } + } + if (word == '') { + word = prompt('Enter a word to look up:', ''); + } + if (word !== null && /^\w[\w ]*$/.test(word)) { + window.open('http://www.answers.com/' + escape(word)); + } +} + +function edToolbar() { + document.write('
    '); + for (i = 0; i < edButtons.length; i++) { + edShowButton(edButtons[i], i); + } + document.write(''); + document.write(''); +// edShowLinks(); // disabled by default + document.write('
    '); +} + +// insertion code + +function edInsertTag(myField, i) { + //IE support + if (document.selection) { + myField.focus(); + sel = document.selection.createRange(); + if (sel.text.length > 0) { + sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd; + } + else { + if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') { + sel.text = edButtons[i].tagStart; + edAddTag(i); + } + else { + sel.text = edButtons[i].tagEnd; + edRemoveTag(i); + } + } + myField.focus(); + } + //MOZILLA/NETSCAPE support + else if (myField.selectionStart || myField.selectionStart == '0') { + var startPos = myField.selectionStart; + var endPos = myField.selectionEnd; + var cursorPos = endPos; + var scrollTop = myField.scrollTop; + + if (startPos != endPos) { + myField.value = myField.value.substring(0, startPos) + + edButtons[i].tagStart + + myField.value.substring(startPos, endPos) + + edButtons[i].tagEnd + + myField.value.substring(endPos, myField.value.length); + cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length; + } + else { + if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') { + myField.value = myField.value.substring(0, startPos) + + edButtons[i].tagStart + + myField.value.substring(endPos, myField.value.length); + edAddTag(i); + cursorPos = startPos + edButtons[i].tagStart.length; + } + else { + myField.value = myField.value.substring(0, startPos) + + edButtons[i].tagEnd + + myField.value.substring(endPos, myField.value.length); + edRemoveTag(i); + cursorPos = startPos + edButtons[i].tagEnd.length; + } + } + myField.focus(); + myField.selectionStart = cursorPos; + myField.selectionEnd = cursorPos; + myField.scrollTop = scrollTop; + } + else { + if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') { + myField.value += edButtons[i].tagStart; + edAddTag(i); + } + else { + myField.value += edButtons[i].tagEnd; + edRemoveTag(i); + } + myField.focus(); + } +} + +function edInsertContent(myField, myValue) { + //IE support + if (document.selection) { + myField.focus(); + sel = document.selection.createRange(); + sel.text = myValue; + myField.focus(); + } + //MOZILLA/NETSCAPE support + else if (myField.selectionStart || myField.selectionStart == '0') { + var startPos = myField.selectionStart; + var endPos = myField.selectionEnd; + myField.value = myField.value.substring(0, startPos) + + myValue + + myField.value.substring(endPos, myField.value.length); + myField.focus(); + myField.selectionStart = startPos + myValue.length; + myField.selectionEnd = startPos + myValue.length; + } else { + myField.value += myValue; + myField.focus(); + } +} + +function edInsertLink(myField, i, defaultValue) { + if (!defaultValue) { + defaultValue = 'http://'; + } + if (!edCheckOpenTags(i)) { + var URL = prompt('Enter the URL' ,defaultValue); + if (URL) { + edButtons[i].tagStart = ''; + edInsertTag(myField, i); + } + } + else { + edInsertTag(myField, i); + } +} + +function edInsertImage(myField) { + var myValue = prompt('Enter the URL of the image', 'http://'); + if (myValue) { + myValue = '' + prompt('Enter a description of the image', '') 
+				+ ''; + edInsertContent(myField, myValue); + } +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/blank.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/blank.htm new file mode 100644 index 00000000..c1ff8352 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/blank.htm @@ -0,0 +1,9 @@ + + + blank_page + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/langs/en.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/langs/en.js new file mode 100644 index 00000000..6d74cd87 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/langs/en.js @@ -0,0 +1,39 @@ +// UK lang variables + +tinyMCELang['lang_bold_desc'] = 'Bold (Alt+b)'; +tinyMCELang['lang_italic_desc'] = 'Italic (Alt+i)'; +tinyMCELang['lang_underline_desc'] = 'Underline'; +tinyMCELang['lang_striketrough_desc'] = 'Striketrough (Alt-d)'; +tinyMCELang['lang_justifyleft_desc'] = 'Align left (Alt-f)'; +tinyMCELang['lang_justifycenter_desc'] = 'Align center (Alt-c)'; +tinyMCELang['lang_justifyright_desc'] = 'Align right (Alt-r)'; +tinyMCELang['lang_justifyfull_desc'] = 'Align full'; +tinyMCELang['lang_bullist_desc'] = 'Unordered list (Alt-l)'; +tinyMCELang['lang_numlist_desc'] = 'Ordered list (Alt-o)'; +tinyMCELang['lang_outdent_desc'] = 'Outdent (Alt-w)'; +tinyMCELang['lang_indent_desc'] = 'Indent/Blockquote (Alt-q)'; +tinyMCELang['lang_undo_desc'] = 'Undo (Alt-u)'; +tinyMCELang['lang_redo_desc'] = 'Redo (Alt-y)'; +tinyMCELang['lang_link_desc'] = 'Insert/edit link (Alt-a)'; +tinyMCELang['lang_unlink_desc'] = 'Unlink (Alt-s)'; +tinyMCELang['lang_image_desc'] = 'Insert/edit image (Alt-m)'; +tinyMCELang['lang_cleanup_desc'] = 'Cleanup messy code'; +tinyMCELang['lang_focus_alert'] = 'A editor instance must be focused before using this command.'; +tinyMCELang['lang_edit_confirm'] = 'Do you want to use the WYSIWYG mode for this textarea?'; +tinyMCELang['lang_insert_link_title'] = 'Insert/edit link'; +tinyMCELang['lang_insert'] = 'Insert'; +tinyMCELang['lang_update'] = 'Update'; +tinyMCELang['lang_cancel'] = 'Cancel'; +tinyMCELang['lang_insert_link_url'] = 'Link URL'; +tinyMCELang['lang_insert_link_target'] = 'Target'; +tinyMCELang['lang_insert_link_target_same'] = 'Open link in the same window'; +tinyMCELang['lang_insert_link_target_blank'] = 'Open link in a new window'; +tinyMCELang['lang_insert_image_title'] = 'Insert/edit image'; +tinyMCELang['lang_insert_image_src'] = 'Image URL'; +tinyMCELang['lang_insert_image_alt'] = 'Image description'; +tinyMCELang['lang_help_desc'] = 'Help'; +tinyMCELang['lang_bold_img'] = "bold.gif"; +tinyMCELang['lang_italic_img'] = "italic.gif"; +tinyMCELang['lang_underline_img'] = "underline.gif"; +tinyMCELang['lang_clipboard_msg'] = 'Copy/Cut/Paste is not available in Mozilla and Firefox.\nDo you want more information about this issue?'; +tinyMCELang['lang_popup_blocked'] = 'Sorry, but we have noticed that your popup-blocker has disabled a window that provides application functionality. You will need to disable popup blocking on this site in order to fully utilize this tool.'; diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/license.html b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/license.html new file mode 100644 index 00000000..c0c9c9ad --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/license.html @@ -0,0 +1,465 @@ + + + +TinyMCE License (LGPL) + + + + +
    +

    TinyMCE License (LGPL)

    +
    + +
    +

    +Visit the FAQ for general answers surrounding TinyMCE. Or visit http://www.fsf.org for more information about Open-Source licenses. +

    +
    +		  GNU LIBRARY GENERAL PUBLIC LICENSE
    +		       Version 2, June 1991
    +
    + Copyright (C) 1991 Free Software Foundation, Inc.
    + 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    + Everyone is permitted to copy and distribute verbatim copies
    + of this license document, but changing it is not allowed.
    +
    +[This is the first released version of the library GPL.  It is
    + numbered 2 because it goes with version 2 of the ordinary GPL.]
    +
    +			    Preamble
    +
    +  The licenses for most software are designed to take away your
    +freedom to share and change it.  By contrast, the GNU General Public
    +Licenses are intended to guarantee your freedom to share and change
    +free software--to make sure the software is free for all its users.
    +
    +  This license, the Library General Public License, applies to some
    +specially designated Free Software Foundation software, and to any
    +other libraries whose authors decide to use it.  You can use it for
    +your libraries, too.
    +
    +  When we speak of free software, we are referring to freedom, not
    +price.  Our General Public Licenses are designed to make sure that you
    +have the freedom to distribute copies of free software (and charge for
    +this service if you wish), that you receive source code or can get it
    +if you want it, that you can change the software or use pieces of it
    +in new free programs; and that you know you can do these things.
    +
    +  To protect your rights, we need to make restrictions that forbid
    +anyone to deny you these rights or to ask you to surrender the rights.
    +These restrictions translate to certain responsibilities for you if
    +you distribute copies of the library, or if you modify it.
    +
    +  For example, if you distribute copies of the library, whether gratis
    +or for a fee, you must give the recipients all the rights that we gave
    +you.  You must make sure that they, too, receive or can get the source
    +code.  If you link a program with the library, you must provide
    +complete object files to the recipients so that they can relink them
    +with the library, after making changes to the library and recompiling
    +it.  And you must show them these terms so they know their rights.
    +
    +  Our method of protecting your rights has two steps: (1) copyright
    +the library, and (2) offer you this license which gives you legal
    +permission to copy, distribute and/or modify the library.
    +
    +  Also, for each distributor's protection, we want to make certain
    +that everyone understands that there is no warranty for this free
    +library.  If the library is modified by someone else and passed on, we
    +want its recipients to know that what they have is not the original
    +version, so that any problems introduced by others will not reflect on
    +the original authors' reputations.
    +.
    +  Finally, any free program is threatened constantly by software
    +patents.  We wish to avoid the danger that companies distributing free
    +software will individually obtain patent licenses, thus in effect
    +transforming the program into proprietary software.  To prevent this,
    +we have made it clear that any patent must be licensed for everyone's
    +free use or not licensed at all.
    +
    +  Most GNU software, including some libraries, is covered by the ordinary
    +GNU General Public License, which was designed for utility programs.  This
    +license, the GNU Library General Public License, applies to certain
    +designated libraries.  This license is quite different from the ordinary
    +one; be sure to read it in full, and don't assume that anything in it is
    +the same as in the ordinary license.
    +
    +  The reason we have a separate public license for some libraries is that
    +they blur the distinction we usually make between modifying or adding to a
    +program and simply using it.  Linking a program with a library, without
    +changing the library, is in some sense simply using the library, and is
    +analogous to running a utility program or application program.  However, in
    +a textual and legal sense, the linked executable is a combined work, a
    +derivative of the original library, and the ordinary General Public License
    +treats it as such.
    +
    +  Because of this blurred distinction, using the ordinary General
    +Public License for libraries did not effectively promote software
    +sharing, because most developers did not use the libraries.  We
    +concluded that weaker conditions might promote sharing better.
    +
    +  However, unrestricted linking of non-free programs would deprive the
    +users of those programs of all benefit from the free status of the
    +libraries themselves.  This Library General Public License is intended to
    +permit developers of non-free programs to use free libraries, while
    +preserving your freedom as a user of such programs to change the free
    +libraries that are incorporated in them.  (We have not seen how to achieve
    +this as regards changes in header files, but we have achieved it as regards
    +changes in the actual functions of the Library.)  The hope is that this
    +will lead to faster development of free libraries.
    +
    +  The precise terms and conditions for copying, distribution and
    +modification follow.  Pay close attention to the difference between a
    +"work based on the library" and a "work that uses the library".  The
    +former contains code derived from the library, while the latter only
    +works together with the library.
    +
    +  Note that it is possible for a library to be covered by the ordinary
    +General Public License rather than by this special one.
    +.
    +		  GNU LIBRARY GENERAL PUBLIC LICENSE
    +   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
    +
    +  0. This License Agreement applies to any software library which
    +contains a notice placed by the copyright holder or other authorized
    +party saying it may be distributed under the terms of this Library
    +General Public License (also called "this License").  Each licensee is
    +addressed as "you".
    +
    +  A "library" means a collection of software functions and/or data
    +prepared so as to be conveniently linked with application programs
    +(which use some of those functions and data) to form executables.
    +
    +  The "Library", below, refers to any such software library or work
    +which has been distributed under these terms.  A "work based on the
    +Library" means either the Library or any derivative work under
    +copyright law: that is to say, a work containing the Library or a
    +portion of it, either verbatim or with modifications and/or translated
    +straightforwardly into another language.  (Hereinafter, translation is
    +included without limitation in the term "modification".)
    +
    +  "Source code" for a work means the preferred form of the work for
    +making modifications to it.  For a library, complete source code means
    +all the source code for all modules it contains, plus any associated
    +interface definition files, plus the scripts used to control compilation
    +and installation of the library.
    +
    +  Activities other than copying, distribution and modification are not
    +covered by this License; they are outside its scope.  The act of
    +running a program using the Library is not restricted, and output from
    +such a program is covered only if its contents constitute a work based
    +on the Library (independent of the use of the Library in a tool for
    +writing it).  Whether that is true depends on what the Library does
    +and what the program that uses the Library does.
    +  
    +  1. You may copy and distribute verbatim copies of the Library's
    +complete source code as you receive it, in any medium, provided that
    +you conspicuously and appropriately publish on each copy an
    +appropriate copyright notice and disclaimer of warranty; keep intact
    +all the notices that refer to this License and to the absence of any
    +warranty; and distribute a copy of this License along with the
    +Library.
    +
    +  You may charge a fee for the physical act of transferring a copy,
    +and you may at your option offer warranty protection in exchange for a
    +fee.
    +.
    +  2. You may modify your copy or copies of the Library or any portion
    +of it, thus forming a work based on the Library, and copy and
    +distribute such modifications or work under the terms of Section 1
    +above, provided that you also meet all of these conditions:
    +
    +    a) The modified work must itself be a software library.
    +
    +    b) You must cause the files modified to carry prominent notices
    +    stating that you changed the files and the date of any change.
    +
    +    c) You must cause the whole of the work to be licensed at no
    +    charge to all third parties under the terms of this License.
    +
    +    d) If a facility in the modified Library refers to a function or a
    +    table of data to be supplied by an application program that uses
    +    the facility, other than as an argument passed when the facility
    +    is invoked, then you must make a good faith effort to ensure that,
    +    in the event an application does not supply such function or
    +    table, the facility still operates, and performs whatever part of
    +    its purpose remains meaningful.
    +
    +    (For example, a function in a library to compute square roots has
    +    a purpose that is entirely well-defined independent of the
    +    application.  Therefore, Subsection 2d requires that any
    +    application-supplied function or table used by this function must
    +    be optional: if the application does not supply it, the square
    +    root function must still compute square roots.)
    +
    +These requirements apply to the modified work as a whole.  If
    +identifiable sections of that work are not derived from the Library,
    +and can be reasonably considered independent and separate works in
    +themselves, then this License, and its terms, do not apply to those
    +sections when you distribute them as separate works.  But when you
    +distribute the same sections as part of a whole which is a work based
    +on the Library, the distribution of the whole must be on the terms of
    +this License, whose permissions for other licensees extend to the
    +entire whole, and thus to each and every part regardless of who wrote
    +it.
    +
    +Thus, it is not the intent of this section to claim rights or contest
    +your rights to work written entirely by you; rather, the intent is to
    +exercise the right to control the distribution of derivative or
    +collective works based on the Library.
    +
    +In addition, mere aggregation of another work not based on the Library
    +with the Library (or with a work based on the Library) on a volume of
    +a storage or distribution medium does not bring the other work under
    +the scope of this License.
    +
    +  3. You may opt to apply the terms of the ordinary GNU General Public
    +License instead of this License to a given copy of the Library.  To do
    +this, you must alter all the notices that refer to this License, so
    +that they refer to the ordinary GNU General Public License, version 2,
    +instead of to this License.  (If a newer version than version 2 of the
    +ordinary GNU General Public License has appeared, then you can specify
    +that version instead if you wish.)  Do not make any other change in
    +these notices.
    +.
    +  Once this change is made in a given copy, it is irreversible for
    +that copy, so the ordinary GNU General Public License applies to all
    +subsequent copies and derivative works made from that copy.
    +
    +  This option is useful when you wish to copy part of the code of
    +the Library into a program that is not a library.
    +
    +  4. You may copy and distribute the Library (or a portion or
    +derivative of it, under Section 2) in object code or executable form
    +under the terms of Sections 1 and 2 above provided that you accompany
    +it with the complete corresponding machine-readable source code, which
    +must be distributed under the terms of Sections 1 and 2 above on a
    +medium customarily used for software interchange.
    +
    +  If distribution of object code is made by offering access to copy
    +from a designated place, then offering equivalent access to copy the
    +source code from the same place satisfies the requirement to
    +distribute the source code, even though third parties are not
    +compelled to copy the source along with the object code.
    +
    +  5. A program that contains no derivative of any portion of the
    +Library, but is designed to work with the Library by being compiled or
    +linked with it, is called a "work that uses the Library".  Such a
    +work, in isolation, is not a derivative work of the Library, and
    +therefore falls outside the scope of this License.
    +
    +  However, linking a "work that uses the Library" with the Library
    +creates an executable that is a derivative of the Library (because it
    +contains portions of the Library), rather than a "work that uses the
    +library".  The executable is therefore covered by this License.
    +Section 6 states terms for distribution of such executables.
    +
    +  When a "work that uses the Library" uses material from a header file
    +that is part of the Library, the object code for the work may be a
    +derivative work of the Library even though the source code is not.
    +Whether this is true is especially significant if the work can be
    +linked without the Library, or if the work is itself a library.  The
    +threshold for this to be true is not precisely defined by law.
    +
    +  If such an object file uses only numerical parameters, data
    +structure layouts and accessors, and small macros and small inline
    +functions (ten lines or less in length), then the use of the object
    +file is unrestricted, regardless of whether it is legally a derivative
    +work.  (Executables containing this object code plus portions of the
    +Library will still fall under Section 6.)
    +
    +  Otherwise, if the work is a derivative of the Library, you may
    +distribute the object code for the work under the terms of Section 6.
    +Any executables containing that work also fall under Section 6,
    +whether or not they are linked directly with the Library itself.
    +.
    +  6. As an exception to the Sections above, you may also compile or
    +link a "work that uses the Library" with the Library to produce a
    +work containing portions of the Library, and distribute that work
    +under terms of your choice, provided that the terms permit
    +modification of the work for the customer's own use and reverse
    +engineering for debugging such modifications.
    +
    +  You must give prominent notice with each copy of the work that the
    +Library is used in it and that the Library and its use are covered by
    +this License.  You must supply a copy of this License.  If the work
    +during execution displays copyright notices, you must include the
    +copyright notice for the Library among them, as well as a reference
    +directing the user to the copy of this License.  Also, you must do one
    +of these things:
    +
    +    a) Accompany the work with the complete corresponding
    +    machine-readable source code for the Library including whatever
    +    changes were used in the work (which must be distributed under
    +    Sections 1 and 2 above); and, if the work is an executable linked
    +    with the Library, with the complete machine-readable "work that
    +    uses the Library", as object code and/or source code, so that the
    +    user can modify the Library and then relink to produce a modified
    +    executable containing the modified Library.  (It is understood
    +    that the user who changes the contents of definitions files in the
    +    Library will not necessarily be able to recompile the application
    +    to use the modified definitions.)
    +
    +    b) Accompany the work with a written offer, valid for at
    +    least three years, to give the same user the materials
    +    specified in Subsection 6a, above, for a charge no more
    +    than the cost of performing this distribution.
    +
    +    c) If distribution of the work is made by offering access to copy
    +    from a designated place, offer equivalent access to copy the above
    +    specified materials from the same place.
    +
    +    d) Verify that the user has already received a copy of these
    +    materials or that you have already sent this user a copy.
    +
    +  For an executable, the required form of the "work that uses the
    +Library" must include any data and utility programs needed for
    +reproducing the executable from it.  However, as a special exception,
    +the source code distributed need not include anything that is normally
    +distributed (in either source or binary form) with the major
    +components (compiler, kernel, and so on) of the operating system on
    +which the executable runs, unless that component itself accompanies
    +the executable.
    +
    +  It may happen that this requirement contradicts the license
    +restrictions of other proprietary libraries that do not normally
    +accompany the operating system.  Such a contradiction means you cannot
    +use both them and the Library together in an executable that you
    +distribute.
    +.
    +  7. You may place library facilities that are a work based on the
    +Library side-by-side in a single library together with other library
    +facilities not covered by this License, and distribute such a combined
    +library, provided that the separate distribution of the work based on
    +the Library and of the other library facilities is otherwise
    +permitted, and provided that you do these two things:
    +
    +    a) Accompany the combined library with a copy of the same work
    +    based on the Library, uncombined with any other library
    +    facilities.  This must be distributed under the terms of the
    +    Sections above.
    +
    +    b) Give prominent notice with the combined library of the fact
    +    that part of it is a work based on the Library, and explaining
    +    where to find the accompanying uncombined form of the same work.
    +
    +  8. You may not copy, modify, sublicense, link with, or distribute
    +the Library except as expressly provided under this License.  Any
    +attempt otherwise to copy, modify, sublicense, link with, or
    +distribute the Library is void, and will automatically terminate your
    +rights under this License.  However, parties who have received copies,
    +or rights, from you under this License will not have their licenses
    +terminated so long as such parties remain in full compliance.
    +
    +  9. You are not required to accept this License, since you have not
    +signed it.  However, nothing else grants you permission to modify or
    +distribute the Library or its derivative works.  These actions are
    +prohibited by law if you do not accept this License.  Therefore, by
    +modifying or distributing the Library (or any work based on the
    +Library), you indicate your acceptance of this License to do so, and
    +all its terms and conditions for copying, distributing or modifying
    +the Library or works based on it.
    +
    +  10. Each time you redistribute the Library (or any work based on the
    +Library), the recipient automatically receives a license from the
    +original licensor to copy, distribute, link with or modify the Library
    +subject to these terms and conditions.  You may not impose any further
    +restrictions on the recipients' exercise of the rights granted herein.
    +You are not responsible for enforcing compliance by third parties to
    +this License.
    +.
    +  11. If, as a consequence of a court judgment or allegation of patent
    +infringement or for any other reason (not limited to patent issues),
    +conditions are imposed on you (whether by court order, agreement or
    +otherwise) that contradict the conditions of this License, they do not
    +excuse you from the conditions of this License.  If you cannot
    +distribute so as to satisfy simultaneously your obligations under this
    +License and any other pertinent obligations, then as a consequence you
    +may not distribute the Library at all.  For example, if a patent
    +license would not permit royalty-free redistribution of the Library by
    +all those who receive copies directly or indirectly through you, then
    +the only way you could satisfy both it and this License would be to
    +refrain entirely from distribution of the Library.
    +
    +If any portion of this section is held invalid or unenforceable under any
    +particular circumstance, the balance of the section is intended to apply,
    +and the section as a whole is intended to apply in other circumstances.
    +
    +It is not the purpose of this section to induce you to infringe any
    +patents or other property right claims or to contest validity of any
    +such claims; this section has the sole purpose of protecting the
    +integrity of the free software distribution system which is
    +implemented by public license practices.  Many people have made
    +generous contributions to the wide range of software distributed
    +through that system in reliance on consistent application of that
    +system; it is up to the author/donor to decide if he or she is willing
    +to distribute software through any other system and a licensee cannot
    +impose that choice.
    +
    +This section is intended to make thoroughly clear what is believed to
    +be a consequence of the rest of this License.
    +
    +  12. If the distribution and/or use of the Library is restricted in
    +certain countries either by patents or by copyrighted interfaces, the
    +original copyright holder who places the Library under this License may add
    +an explicit geographical distribution limitation excluding those countries,
    +so that distribution is permitted only in or among countries not thus
    +excluded.  In such case, this License incorporates the limitation as if
    +written in the body of this License.
    +
    +  13. The Free Software Foundation may publish revised and/or new
    +versions of the Library General Public License from time to time.
    +Such new versions will be similar in spirit to the present version,
    +but may differ in detail to address new problems or concerns.
    +
    +Each version is given a distinguishing version number.  If the Library
    +specifies a version number of this License which applies to it and
    +"any later version", you have the option of following the terms and
    +conditions either of that version or of any later version published by
    +the Free Software Foundation.  If the Library does not specify a
    +license version number, you may choose any version ever published by
    +the Free Software Foundation.
    +.
    +  14. If you wish to incorporate parts of the Library into other free
    +programs whose distribution conditions are incompatible with these,
    +write to the author to ask for permission.  For software which is
    +copyrighted by the Free Software Foundation, write to the Free
    +Software Foundation; we sometimes make exceptions for this.  Our
    +decision will be guided by the two goals of preserving the free status
    +of all derivatives of our free software and of promoting the sharing
    +and reuse of software generally.
    +
    +			    NO WARRANTY
    +
    +  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
    +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
    +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
    +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
    +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
    +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    +PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
    +LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
    +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
    +
    +  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
    +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
    +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
    +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
    +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
    +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
    +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
    +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
    +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
    +DAMAGES.
    +
    +		     END OF TERMS AND CONDITIONS
    +
    +
    + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/license.txt b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/license.txt new file mode 100644 index 00000000..3b0396a2 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/license.txt @@ -0,0 +1,437 @@ + GNU LIBRARY GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the library GPL. It is + numbered 2 because it goes with version 2 of the ordinary GPL.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Library General Public License, applies to some +specially designated Free Software Foundation software, and to any +other libraries whose authors decide to use it. You can use it for +your libraries, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if +you distribute copies of the library, or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link a program with the library, you must provide +complete object files to the recipients so that they can relink them +with the library, after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + Our method of protecting your rights has two steps: (1) copyright +the library, and (2) offer you this license which gives you legal +permission to copy, distribute and/or modify the library. + + Also, for each distributor's protection, we want to make certain +that everyone understands that there is no warranty for this free +library. If the library is modified by someone else and passed on, we +want its recipients to know that what they have is not the original +version, so that any problems introduced by others will not reflect on +the original authors' reputations. +. + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that companies distributing free +software will individually obtain patent licenses, thus in effect +transforming the program into proprietary software. To prevent this, +we have made it clear that any patent must be licensed for everyone's +free use or not licensed at all. + + Most GNU software, including some libraries, is covered by the ordinary +GNU General Public License, which was designed for utility programs. This +license, the GNU Library General Public License, applies to certain +designated libraries. This license is quite different from the ordinary +one; be sure to read it in full, and don't assume that anything in it is +the same as in the ordinary license. + + The reason we have a separate public license for some libraries is that +they blur the distinction we usually make between modifying or adding to a +program and simply using it. Linking a program with a library, without +changing the library, is in some sense simply using the library, and is +analogous to running a utility program or application program. However, in +a textual and legal sense, the linked executable is a combined work, a +derivative of the original library, and the ordinary General Public License +treats it as such. + + Because of this blurred distinction, using the ordinary General +Public License for libraries did not effectively promote software +sharing, because most developers did not use the libraries. We +concluded that weaker conditions might promote sharing better. + + However, unrestricted linking of non-free programs would deprive the +users of those programs of all benefit from the free status of the +libraries themselves. This Library General Public License is intended to +permit developers of non-free programs to use free libraries, while +preserving your freedom as a user of such programs to change the free +libraries that are incorporated in them. (We have not seen how to achieve +this as regards changes in header files, but we have achieved it as regards +changes in the actual functions of the Library.) The hope is that this +will lead to faster development of free libraries. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, while the latter only +works together with the library. + + Note that it is possible for a library to be covered by the ordinary +General Public License rather than by this special one. +. + GNU LIBRARY GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library which +contains a notice placed by the copyright holder or other authorized +party saying it may be distributed under the terms of this Library +General Public License (also called "this License"). Each licensee is +addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. +. + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. +. + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. +. + 6. As an exception to the Sections above, you may also compile or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + c) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + d) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the source code distributed need not include anything that is normally +distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. +. + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. +. + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Library General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. +. + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/editor_plugin.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/editor_plugin.js new file mode 100644 index 00000000..28d7ae76 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/editor_plugin.js @@ -0,0 +1,2 @@ +/* Import plugin specific language pack */ + tinyMCE.importPluginLanguagePack('autosave','en,sv,cs,he,no,hu,de,da,ru,ru_KOI8-R,ru_UTF-8,fi,cy,es,is,pl');function TinyMCE_autosave_getInfo(){return{longname:'Auto save',author:'Moxiecode Systems',authorurl:'http://tinymce.moxiecode.com',infourl:'http://tinymce.moxiecode.com/tinymce/docs/plugin_autosave.html',version:tinyMCE.majorVersion+"."+tinyMCE.minorVersion};};function TinyMCE_autosave_beforeUnloadHandler(){var msg=tinyMCE.getLang("lang_autosave_unload_msg");var anyDirty=false;for(var n in tinyMCE.instances){var inst=tinyMCE.instances[n];if(!tinyMCE.isInstance(inst))continue;if(inst.isDirty())return msg;}return;}window.onbeforeunload=TinyMCE_autosave_beforeUnloadHandler; \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/editor_plugin_src.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/editor_plugin_src.js new file mode 100644 index 00000000..102d69b4 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/editor_plugin_src.js @@ -0,0 +1,30 @@ +/* Import plugin specific language pack */ +tinyMCE.importPluginLanguagePack('autosave', 'en,sv,cs,he,no,hu,de,da,ru,ru_KOI8-R,ru_UTF-8,fi,cy,es,is,pl'); + +function TinyMCE_autosave_getInfo() { + return { + longname : 'Auto save', + author : 'Moxiecode Systems', + authorurl : 'http://tinymce.moxiecode.com', + infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_autosave.html', + version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion + }; +}; + +function TinyMCE_autosave_beforeUnloadHandler() { + var msg = tinyMCE.getLang("lang_autosave_unload_msg"); + + var anyDirty = false; + for (var n in tinyMCE.instances) { + var inst = tinyMCE.instances[n]; + if (!tinyMCE.isInstance(inst)) + continue; + + if (inst.isDirty()) + return msg; + } + + return; +} + +window.onbeforeunload = TinyMCE_autosave_beforeUnloadHandler; diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/langs/cs.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/langs/cs.js new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/langs/en.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/langs/en.js new file mode 100644 index 00000000..01951332 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/langs/en.js @@ -0,0 +1,5 @@ +// EN lang variables + +tinyMCE.addToLang('',{ +autosave_unload_msg : 'The changes you made will be lost if you navigate away from this page.' +}); diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/langs/sv.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/langs/sv.js new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/readme.txt b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/readme.txt new file mode 100644 index 00000000..4fdb78ae --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/autosave/readme.txt @@ -0,0 +1 @@ +Check the TinyMCE documentation for details on this plugin. diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/editor_plugin.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/editor_plugin.js new file mode 100644 index 00000000..c866b53d --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/editor_plugin.js @@ -0,0 +1,83 @@ +/* Import plugin specific language pack */ +tinyMCE.importPluginLanguagePack('directionality', 'en,sv,fr_ca,zh_cn,cs,da,he,no,de,hu,ru,ru_KOI8-R,ru_UTF-8,es,cy,is,pl'); + +function TinyMCE_directionality_getInfo() { + return { + longname : 'Directionality', + author : 'Moxiecode Systems', + authorurl : 'http://tinymce.moxiecode.com', + infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_directionality.html', + version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion + }; +}; + +function TinyMCE_directionality_getControlHTML(control_name) { + switch (control_name) { + case "ltr": + var cmd = 'tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceDirectionLTR\');return false;'; + return '' + + '
    '; + + case "rtl": + var cmd = 'tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceDirectionRTL\');return false;'; + return '' + + '
    '; + } + + return ""; +} + +function TinyMCE_directionality_execCommand(editor_id, element, command, user_interface, value) { + // Handle commands + switch (command) { + case "mceDirectionLTR": + var inst = tinyMCE.getInstanceById(editor_id); + var elm = tinyMCE.getParentElement(inst.getFocusElement(), "p,div,td,h1,h2,h3,h4,h5,h6,pre,address"); + + if (elm) + elm.setAttribute("dir", "ltr"); + + tinyMCE.triggerNodeChange(false); + return true; + + case "mceDirectionRTL": + var inst = tinyMCE.getInstanceById(editor_id); + var elm = tinyMCE.getParentElement(inst.getFocusElement(), "p,div,td,h1,h2,h3,h4,h5,h6,pre,address"); + + if (elm) + elm.setAttribute("dir", "rtl"); + + tinyMCE.triggerNodeChange(false); + return true; + } + + // Pass to next handler in chain + return false; +} + +function TinyMCE_directionality_handleNodeChange(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) { + function getAttrib(elm, name) { + return elm.getAttribute(name) ? elm.getAttribute(name) : ""; + } + + tinyMCE.switchClassSticky(editor_id + '_ltr', 'mceButtonNormal', false); + tinyMCE.switchClassSticky(editor_id + '_rtl', 'mceButtonNormal', false); + + if (node == null) + return; + + var elm = tinyMCE.getParentElement(node, "p,div,td,h1,h2,h3,h4,h5,h6,pre,address"); + if (!elm) { + tinyMCE.switchClassSticky(editor_id + '_ltr', 'mceButtonDisabled', true); + tinyMCE.switchClassSticky(editor_id + '_rtl', 'mceButtonDisabled', true); + return; + } + + var dir = getAttrib(elm, "dir"); + if (dir == "ltr" || dir == "") + tinyMCE.switchClassSticky(editor_id + '_ltr', 'mceButtonSelected', false); + else + tinyMCE.switchClassSticky(editor_id + '_rtl', 'mceButtonSelected', false); + + return true; +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/images/ltr.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/images/ltr.gif new file mode 100644 index 00000000..ac8f30c1 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/images/ltr.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/images/rtl.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/images/rtl.gif new file mode 100644 index 00000000..0348f996 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/images/rtl.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/langs/en.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/langs/en.js new file mode 100644 index 00000000..5b392fe1 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/directionality/langs/en.js @@ -0,0 +1,6 @@ +// UK lang variables + +tinyMCE.addToLang('',{ +directionality_ltr_desc : 'Direction left to right (Alt-.)', +directionality_rtl_desc : 'Direction right to left (Alt-,)' +}); diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/css/inlinepopup.css b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/css/inlinepopup.css new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/editor_plugin.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/editor_plugin.js new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/editor_plugin_src.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/editor_plugin_src.js new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/spacer.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/spacer.gif new file mode 100644 index 00000000..fc256098 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/spacer.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_close.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_close.gif new file mode 100644 index 00000000..3469e5ae Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_close.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_maximize.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_maximize.gif new file mode 100644 index 00000000..fcae73e9 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_maximize.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_minimize.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_minimize.gif new file mode 100644 index 00000000..94f167a4 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_minimize.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_resize.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_resize.gif new file mode 100644 index 00000000..6c402d0b Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/images/window_resize.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/jscripts/mcwindows.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/jscripts/mcwindows.js new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/readme.txt b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/inlinepopups/readme.txt new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js new file mode 100644 index 00000000..9f0957fb --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js @@ -0,0 +1,285 @@ +/* Import plugin specific language pack */ +tinyMCE.importPluginLanguagePack('wordpress', ''); + +function TinyMCE_wordpress_initInstance(inst) { + if (!tinyMCE.settings['wordpress_skip_plugin_css']) + tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/wordpress/wordpress.css"); +} + +function TinyMCE_wordpress_getControlHTML(control_name) { + switch (control_name) { + case "wordpress": + var titleMore = tinyMCE.getLang('lang_wordpress_more_button'); + var titlePage = tinyMCE.getLang('lang_wordpress_page_button'); + var titleHelp = tinyMCE.getLang('lang_wordpress_help_button'); + var buttons = ''; + // Add this to the buttons var to put the Page button into the toolbar. + // ''; + return buttons; + } + + return ''; +} + +function TinyMCE_wordpress_parseAttributes(attribute_string) { + var attributeName = ""; + var attributeValue = ""; + var withInName; + var withInValue; + var attributes = new Array(); + var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g'); + var titleText = tinyMCE.getLang('lang_wordpress_more'); + var titleTextPage = tinyMCE.getLang('lang_wordpress_page'); + + if (attribute_string == null || attribute_string.length < 2) + return null; + + withInName = withInValue = false; + + for (var i=0; i'; + tinyMCE.execCommand("mceInsertContent",true,html); + tinyMCE.selectedInstance.repaint(); + return true; + case "mcewordpresspage": + var flag = ""; + var template = new Array(); + var altPage = tinyMCE.getLang('lang_wordpress_more_alt'); + + // Is selection a image + if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") { + flag = getAttrib(focusElm, 'name'); + + if (flag != 'mce_plugin_wordpress_page') // Not a wordpress + return true; + + action = "update"; + } + + html = '' + + ''; + tinyMCE.execCommand("mceInsertContent",true,html); + tinyMCE.selectedInstance.repaint(); + return true; + } + + // Pass to next handler in chain + return false; +} + +function TinyMCE_wordpress_cleanup(type, content) { + switch (type) { + + case "insert_to_editor": + var startPos = 0; + var altMore = tinyMCE.getLang('lang_wordpress_more_alt'); + var altPage = tinyMCE.getLang('lang_wordpress_page_alt'); + + // Parse all tags and replace them with images + while ((startPos = content.indexOf('', startPos)) != -1) { + // Insert image + var contentAfter = content.substring(startPos + 11); + content = content.substring(0, startPos); + content += ''; + content += contentAfter; + + startPos++; + } + var startPos = 0; + + // Parse all tags and replace them with images + while ((startPos = content.indexOf('', startPos)) != -1) { + // Insert image + var contentAfter = content.substring(startPos + 15); + content = content.substring(0, startPos); + content += ''; + content += contentAfter; + + startPos++; + } + + // It's supposed to be WYSIWYG, right? + content = content.replace(new RegExp('&', 'g'), '&'); + + break; + + case "get_from_editor": + // Parse all img tags and replace them with + var startPos = -1; + while ((startPos = content.indexOf('', startPos); + var attribs = TinyMCE_wordpress_parseAttributes(content.substring(startPos + 4, endPos)); + + if (attribs['class'] == "mce_plugin_wordpress_more") { + endPos += 2; + + var embedHTML = ''; + + // Insert embed/object chunk + chunkBefore = content.substring(0, startPos); + chunkAfter = content.substring(endPos); + content = chunkBefore + embedHTML + chunkAfter; + } + if (attribs['class'] == "mce_plugin_wordpress_page") { + endPos += 2; + + var embedHTML = ''; + + // Insert embed/object chunk + chunkBefore = content.substring(0, startPos); + chunkAfter = content.substring(endPos); + content = chunkBefore + embedHTML + chunkAfter; + } + } + + // If it says & in the WYSIWYG editor, it should say & in the html. + content = content.replace(new RegExp('&', 'g'), '&'); + content = content.replace(new RegExp('&nbsp;', 'g'), ' '); + + // Remove anonymous, empty paragraphs. + content = content.replace(new RegExp('

    (\\s| )*

    ', 'mg'), ''); + + // Handle table badness. + content = content.replace(new RegExp('<(table( [^>]*)?)>.*?<((tr|thead)( [^>]*)?)>', 'mg'), '<$1><$3>'); + content = content.replace(new RegExp('<(tr|thead|tfoot)>.*?<((td|th)( [^>]*)?)>', 'mg'), '<$1><$2>'); + content = content.replace(new RegExp('.*?<(td( [^>]*)?|th( [^>]*)?|/tr|/thead|/tfoot)>', 'mg'), '<$2>'); + content = content.replace(new RegExp('.*?<(tr|/table)>', 'mg'), '<$1>'); + content = content.replace(new RegExp('<(/?(table|tbody|tr|th|td)[^>]*)>(\\s*|(
    )*)*', 'g'), '<$1>'); + + // Pretty it up for the source editor. + var blocklist = 'blockquote|ul|ol|li|table|thead|tr|th|td|div|h\\d|pre|p'; + content = content.replace(new RegExp('\\s*\\s*', 'mg'), '\n'); + content = content.replace(new RegExp('\\s*<(('+blocklist+')[^>]*)>\\s*', 'mg'), '\n<$1>'); + content = content.replace(new RegExp('<((li|/?tr|/?thead|/?tfoot)( [^>]*)?)>', 'g'), '\t<$1>'); + content = content.replace(new RegExp('<((td|th)( [^>]*)?)>', 'g'), '\t\t<$1>'); + content = content.replace(new RegExp('\\s*
    \\s*', 'mg'), '
    \n'); + content = content.replace(new RegExp('^\\s*', ''), ''); + content = content.replace(new RegExp('\\s*$', ''), ''); + + break; + } + + // Pass through to next handler in chain + return content; +} + +function TinyMCE_wordpress_handleNodeChange(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) { + function getAttrib(elm, name) { + return elm.getAttribute(name) ? elm.getAttribute(name) : ""; + } + + tinyMCE.switchClassSticky(editor_id + '_wordpress_more', 'mceButtonNormal'); + tinyMCE.switchClassSticky(editor_id + '_wordpress_page', 'mceButtonNormal'); + + if (node == null) + return; + + do { + if (node.nodeName.toLowerCase() == "img" && getAttrib(node, 'class').indexOf('mce_plugin_wordpress_more') == 0) + tinyMCE.switchClassSticky(editor_id + '_wordpress_more', 'mceButtonSelected'); + if (node.nodeName.toLowerCase() == "img" && getAttrib(node, 'class').indexOf('mce_plugin_wordpress_page') == 0) + tinyMCE.switchClassSticky(editor_id + '_wordpress_page', 'mceButtonSelected'); + } while ((node = node.parentNode)); + + return true; +} + +function wp_save_callback(el, content, body) { + // We have a TON of cleanup to do. + + // Mark

    if it has any attributes. + content = content.replace(new RegExp('(]+>.*?)

    ', 'mg'), '$1'); + + // Decode the ampersands of time. + content = content.replace(new RegExp('&', 'g'), '&'); + + // Get it ready for wpautop. + content = content.replace(new RegExp('[\\s]*

    [\\s]*', 'mgi'), ''); + content = content.replace(new RegExp('[\\s]*

    [\\s]*', 'mgi'), '\n\n'); + content = content.replace(new RegExp('\\n\\s*\\n\\s*\\n*', 'mgi'), '\n\n'); + content = content.replace(new RegExp('\\s*
    \\s*', 'gi'), '\n'); + + // Fix some block element newline issues + var blocklist = 'blockquote|ul|ol|li|table|thead|tr|th|td|div|h\\d|pre'; + content = content.replace(new RegExp('\\s*<(('+blocklist+') ?[^>]*)\\s*>', 'mg'), '\n<$1>'); + content = content.replace(new RegExp('\\s*\\s*', 'mg'), '\n'); + content = content.replace(new RegExp('
  • ', 'g'), '\t
  • '); + + // Unmark special paragraph closing tags + content = content.replace(new RegExp('', 'g'), '

    \n'); + content = content.replace(new RegExp('\\s*(]+>.*

    )', 'mg'), '\n$1'); + + // Trim any whitespace + content = content.replace(new RegExp('^\\s*', ''), ''); + content = content.replace(new RegExp('\\s*$', ''), ''); + + // Hope. + return content; + +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/help.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/help.gif new file mode 100644 index 00000000..933d853a Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/help.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/more.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/more.gif new file mode 100644 index 00000000..4ff564d5 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/more.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/more_bug.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/more_bug.gif new file mode 100644 index 00000000..4589cb4d Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/more_bug.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/page.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/page.gif new file mode 100644 index 00000000..1cea78ac Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/page.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/page_bug.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/page_bug.gif new file mode 100644 index 00000000..9ea35656 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/images/page_bug.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/langs/en.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/langs/en.js new file mode 100644 index 00000000..30dda2f1 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/langs/en.js @@ -0,0 +1,8 @@ +// EN lang variables + +tinyMCE.addToLang('',{ +wordpress_more_button : 'Split post with More tag (Alt-t)', +wordpress_page_button : 'Split post with Page tag', +wordpress_more_alt : 'More...', +wordpress_page_alt : '...page...' +}); diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/wordpress.css b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/wordpress.css new file mode 100644 index 00000000..b51a0984 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wordpress/wordpress.css @@ -0,0 +1,21 @@ +.mce_plugin_wordpress_more { + border: 0px; + border-top: 1px dotted #cccccc; + display:block; + background-color: #ffffff; + margin-top:15px; + background-image: url(images/more_bug.gif); + background-repeat: no-repeat; + background-position: right top; +} + +.mce_plugin_wordpress_page { + border: 0px; + border-top: 1px dotted #cccccc; + display:block; + background-color: #ffffff; + margin-top:15px; + background-image: url(images/page_bug.gif); + background-repeat: no-repeat; + background-position: right top; +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/editor_plugin.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/editor_plugin.js new file mode 100644 index 00000000..204e4471 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/editor_plugin.js @@ -0,0 +1,57 @@ +/* Import plugin specific language pack */ +tinyMCE.importPluginLanguagePack('wphelp', ''); + +function TinyMCE_wphelp_getControlHTML(control_name) { + switch (control_name) { + case "wphelp": + var titleHelp = tinyMCE.getLang('lang_help_button_title'); + var buttons = ''; + var hiddenControls = '
    ' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
    '; + return buttons+hiddenControls; + } + + return ""; +} + +function TinyMCE_wphelp_execCommand(editor_id, element, command, user_interface, value) { + + // Handle commands + switch (command) { + case "mceWordPressHelp": + var template = new Array(); + + template['file'] = tinyMCE.baseURL + '/wp-mce-help.php'; + template['width'] = 480; + template['height'] = 380; + + args = { + resizable : 'yes', + scrollbars : 'yes' + }; + + tinyMCE.openWindow(template, args); + return true; + } + + // Pass to next handler in chain + return false; +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/images/help.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/images/help.gif new file mode 100644 index 00000000..51a1ee42 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/images/help.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/langs/en.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/langs/en.js new file mode 100644 index 00000000..b7b2aba7 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/plugins/wphelp/langs/en.js @@ -0,0 +1,5 @@ +// EN lang variables + +tinyMCE.addToLang('',{ +help_button_title : 'Help (Alt+h)' +}); diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/about.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/about.htm new file mode 100644 index 00000000..bbd8d138 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/about.htm @@ -0,0 +1,52 @@ + + + {$lang_about_title} + + + + + + + + +
    +
    +

    {$lang_about_title}

    +

    Version: {$tinymce_version} ({$tinymce_releasedate})

    +

    TinyMCE is a platform independent web based Javascript HTML WYSIWYG editor control released as Open Source under LGPL + by Moxiecode Systems AB. It has the ability to convert HTML TEXTAREA fields or other HTML elements to editor instances.

    +

    Copyright © 2005, Moxiecode Systems AB, All rights reserved.

    +

    For more information about this software visit the TinyMCE website.

    + +
    +
    + +
    +
    +

    {$lang_loaded_plugins}

    + +
    +
    + +

     

    +
    +
    + +
    +
    +
    +
    + +
    +
    + +
    +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/anchor.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/anchor.htm new file mode 100644 index 00000000..48ba245f --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/anchor.htm @@ -0,0 +1,32 @@ + + + {$lang_insert_anchor_title} + + + + +
    + + + + + + + + + +
    {$lang_insert_anchor_title}
    {$lang_insert_anchor_name}:
    + +
    +
    + +
    + +
    + +
    +
    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/charmap.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/charmap.htm new file mode 100644 index 00000000..bcebff24 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/charmap.htm @@ -0,0 +1,52 @@ + + + {$lang_theme_charmap_title} + + + + + + + + + + + + + + + + +
    {$lang_theme_charmap_title}
    + + + + + + + + + +
     
     
    +
    + + + + + + + + + + + + + + + + +
    HTML-Code
     
     
    NUM-Code
     
    +
    + + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/color_picker.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/color_picker.htm new file mode 100644 index 00000000..e35d711e --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/color_picker.htm @@ -0,0 +1,12 @@ + + + {$lang_theme_colorpicker_title} + + + + +
    + +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_content.css b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_content.css new file mode 100644 index 00000000..7dbe1feb --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_content.css @@ -0,0 +1,49 @@ +/* This file contains the CSS data for the editable area(iframe) of TinyMCE */ +/* You can extend this CSS by adding your own CSS file with the the content_css option */ + +body { + background-color: #FFFFFF; + font-family: Verdana, Arial, Helvetica, sans-serif; + font-size: 0.9em; + line-height: 1.2em; + padding: .3em; +} + +td { + font-family: Verdana, Arial, Helvetica, sans-serif; + font-size: 10px; +} + +pre { + font-family: Verdana, Arial, Helvetica, sans-serif; + font-size: 10px; +} + +.mceVisualAid { + border: 1px dashed #BBBBBB !important; +} + +.mceItemAnchor { + width: 12px; + line-height: 6px; + overflow: hidden; + padding-left: 12px; + background-image: url('../images/anchor_symbol.gif'); + background-position: bottom; + background-repeat: no-repeat; +} + +/* Important is needed in Gecko browsers inorder to style links */ +/* +a { + color: green !important; +} +*/ + +/* Style selection range colors in Gecko browsers */ +/* +::-moz-selection { + background-color: red; + color: green; +} +*/ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_popup.css b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_popup.css new file mode 100644 index 00000000..14f83221 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_popup.css @@ -0,0 +1,319 @@ +/* This file contains the CSS data for all popups in TinyMCE */ + +body { + background-color: #F0F0EE; + font-family: Verdana, Arial, Helvetica, sans-serif; + font-size: 11px; + scrollbar-3dlight-color: #F0F0EE; + scrollbar-arrow-color: #676662; + scrollbar-base-color: #F0F0EE; + scrollbar-darkshadow-color: #DDDDDD; + scrollbar-face-color: #E0E0DD; + scrollbar-highlight-color: #F0F0EE; + scrollbar-shadow-color: #F0F0EE; + scrollbar-track-color: #F5F5F5; + margin: 8px; +} + +td { + font-family: Verdana, Arial, Helvetica, sans-serif; + font-size: 11px; +} + +input { + background: #FFFFFF; + border: 1px solid #cccccc; +} + +td, input, select, textarea { + font-family: Verdana, Arial, Helvetica, sans-serif; + font-size: 10px; +} + +input, select, textarea { + border: 1px solid #808080; +} + +.input_noborder { + border: 0px solid #808080; +} + +#insert { + font-weight: bold; + width: 90px; + height: 21px; + border: 0px; + background-image: url('../images/insert_button_bg.gif'); + cursor: pointer; +} + +#cancel { + font-weight: bold; + width: 90px; + height: 21px; + border: 0px; + background-image: url('../images/cancel_button_bg.gif'); + cursor: pointer; +} + +/* Mozilla only style */ +html>body #insert, html>body #cancel { + padding-bottom: 2px; +} + +.title { + font-size: 12px; + font-weight: bold; + color: #2B6FB6; +} + +table.charmap { + border-style: solid; + border-width: 1px; + border-color: #AAAAAA; +} + +td.charmap, td.charmapOver { + color: #000000; + border-color: #AAAAAA; + border-style: solid; + border-width: 1px; + text-align: center; + font-size: 12px; +} + +td.charmapOver { + background-color: #CCCCCC; + cursor: arrow; +} + +a.charmap { + color: #000000; + text-decoration: none +} + +.wordWrapCode { + vertical-align: middle; + border: 1px none #000000; + background-color: transparent; +} + +input.radio { + border: 1px none #000000; + background-color: transparent; + vertical-align: middle; +} + +input.checkbox { + border: 1px none #000000; + background-color: transparent; + vertical-align: middle; +} + +.mceButtonNormal, .mceButtonOver, .mceButtonDown, .mceSeparator, .mceButtonDisabled, .mceButtonSelected { + margin-left: 1px; +} + +.mceButtonNormal { + border-top: 1px solid; + border-left: 1px solid; + border-bottom: 1px solid; + border-right: 1px solid; + border-color: #F0F0EE; + cursor: arrow; +} + +.mceButtonOver { + border: 1px solid #0A246A; + cursor: arrow; + background-color: #B6BDD2; +} + +.mceButtonDown { + cursor: arrow; + border: 1px solid #0A246A; + background-color: #8592B5; +} + +.mceButtonDisabled { + filter:progid:DXImageTransform.Microsoft.Alpha(opacity=30); + -moz-opacity:0.3; + opacity: 0.3; + border-top: 1px solid; + border-left: 1px solid; + border-bottom: 1px solid; + border-right: 1px solid; + border-color: #F0F0EE; + cursor: arrow; +} + +.mceActionPanel { + margin-top: 5px; +} + +/* Tabs classes */ + +.tabs { + float: left; + width: 100%; + line-height: normal; + background-image: url("../images/xp/tabs_bg.gif"); +} + +.tabs ul { + margin: 0; + padding: 0px 0px 0; + list-style: none; +} + +.tabs li { + float: left; + background: url("../images/xp/tab_bg.gif") no-repeat left top; + margin: 0; + margin-left: 0px; + margin-right: 2px; + padding: 0 0 0 10px; + line-height: 18px; +} + +.tabs li.current { + background: url("../images/xp/tab_sel_bg.gif") no-repeat left top; + margin-right: 2px; +} + +.tabs span { + float: left; + display: block; + background: url("../images/xp/tab_end.gif") no-repeat right top; + padding: 0px 10px 0px 0px; +} + +.tabs .current span { + background: url("../images/xp/tab_sel_end.gif") no-repeat right top; +} + +.tabs a { + text-decoration: none; + font-family: Verdana, Arial; + font-size: 10px; +} + +.tabs a:link, .tabs a:visited, .tabs a:hover { + color: black; +} + +.tabs a:hover { +} + +.tabs .current { +} + +.tabs .current a, .tabs .current a:link, .tabs .current a:visited { +} + +.panel_wrapper div.panel { + display: none; +} + +.panel_wrapper div.current { + display: block; + width: 100%; + height: 300px; + overflow: visible; /* Should be auto but that breaks Safari */ +} + +.panel_wrapper { + border: 1px solid #919B9C; + border-top: 0px; + padding: 10px; + padding-top: 5px; + clear: both; + background-color: white; +} + +fieldset { + border: 1px solid #919B9C; + font-family: Verdana, Arial; + font-size: 10px; + padding: 0px; + margin: 0px; + padding: 4px; +} + +legend { + color: #2B6FB6; + font-weight: bold; +} + +.properties { + width: 100%; +} + +.properties .column1 { +} + +.properties .column2 { + text-align: left; +} + +a:link, a:visited { + color: black; +} + +a:hover { + color: #2B6FB6; +} + +#plugintable thead { + font-weight: bold; + background-color: #DDDDDD; +} + +#plugintable, #about #plugintable td { + border: 1px solid #919B9C; +} + +#plugintable { + width: 99%; + margin-top: 10px; +} + +#pluginscontainer { + height: 290px; + overflow: auto; +} + +/* MSIE Specific styles */ + +* html .panel_wrapper { + width: 100%; +} + +.column { + float: left; +} + +h1, h2, h3, h4 { + color: #2B6FB6; + margin: 0px; + padding: 0px; + padding-top: 5px; +} + +h3 { + font-size: 14px; +} + +/* Disables the advanced tab in the table plugin. */ +/* +#table #advanced_tab { + display: none; +} +*/ + +/* Disables the border input field and label in the table plugin. */ +/* +#table #border, #table #borderlabel { + display: none; +} +*/ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_ui.css b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_ui.css new file mode 100644 index 00000000..f2f5effe --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/css/editor_ui.css @@ -0,0 +1,181 @@ +/* This file contains the CSS data for the editor UI of TinyMCE instances */ + +.mceToolbarTop a, .mceToolbarTop a:visited, .mceToolbarTop a:hover, .mceToolbarBottom a, .mceToolbarBottom a:visited, .mceToolbarBottom a:hover { + border: 0px; margin: 0px; padding: 0px; background: transparent; +} + +.mceButtonNormal, .mceButtonOver, .mceButtonDown, .mceSeparator, .mceSeparatorLine, .mceButtonDisabled, .mceButtonSelected { + border: 0px; margin: 0px; padding: 0px; background: transparent; + margin-top: 1px; + margin-left: 1px; +} + +.mceButtonNormal { + border-top: 1px solid; + border-left: 1px solid; + border-bottom: 1px solid; + border-right: 1px solid; + border-color: #F0F0EE; + cursor: arrow; +} + +.mceButtonOver { + border: 1px solid #0A246A; + cursor: arrow; + background-color: #B6BDD2; +} + +.mceButtonDown { + cursor: arrow; + border: 1px solid #0A246A; + background-color: #8592B5; +} + +.mceButtonSelected { + border: 1px solid; + border-color: #C0C0BB; + cursor: arrow; +} + +.mceButtonDisabled { + filter:progid:DXImageTransform.Microsoft.Alpha(opacity=30); + -moz-opacity:0.3; + opacity: 0.3; + border-top: 1px solid; + border-left: 1px solid; + border-bottom: 1px solid; + border-right: 1px solid; + border-color: #F0F0EE; + cursor: arrow; +} + +.mceSeparator { + border-top: 1px solid buttonhighlight; + border-left: 1px solid buttonhighlight; + border-bottom: 1px solid buttonshadow; + border-right: 1px solid buttonshadow; + margin-right: 2px; + margin-left: 2px; +} + +.mceSeparatorLine { + margin:2px; + margin-left: 4px; + background-color: #F0F0EE; + border-top: 1px solid buttonshadow; + border-left: 1px solid buttonshadow; + border-bottom: 1px solid buttonhighlight; + border-right: 1px solid buttonhighlight; + width: 0px; + height: 15px; +} + +.mceSelectList { + font-family: "MS Sans Serif"; + font-size: 7pt; + font-weight: normal; + margin-top: 3px; + padding: 0px; + display: inline; + vertical-align: top; + background-color: #F0F0EE +} + +.mceLabel, .mceLabelDisabled { + font-family: 'MS Sans Serif', sans-serif, Verdana, Arial; + font-size: 9pt; +} + +.mceLabel { + color: #000000; +} + +.mceLabelDisabled { + cursor: text; + color: #999999; +} + +.mceEditor { + background: #F0F0EE; + border: 1px solid #cccccc; + padding: 0px; + margin: 0px; +} + +.mceEditorArea { + font-family: 'MS Sans Serif', sans-serif, Verdana, Arial; + background: #FFFFFF; + padding: 0px; + margin: 0px; +} + +.mceToolbarTop, .mceToolbarBottom { + background: #F0F0EE; + line-height: 1px; + font-size: 1px; +} + +.mceToolbarTop { + border-bottom: 1px solid #cccccc; +} + +.mceToolbarBottom { + border-top: 1px solid #cccccc; +} + +.mceStatusbarTop, .mceStatusbarBottom, .mceStatusbar { + height: 20px; +} + +.mceStatusbarTop .mceStatusbarPathText, .mceStatusbarBottom .mceStatusbarPathText, .mceStatusbar .mceStatusbarPathText { + font-family: 'MS Sans Serif', sans-serif, Verdana, Arial; + font-size: 9pt; + padding: 2px; + line-height: 16px; + overflow: visible; +} + +.mceStatusbarTop { + border-bottom: 1px solid #cccccc; +} + +.mceStatusbarBottom { + border-top: 1px solid #cccccc; +} + +.mceStatusbar { + border-bottom: 1px solid #cccccc; +} + +.mcePathItem, .mcePathItem:link, .mcePathItem:visited, .mcePathItem:hover { + text-decoration: none; + font-family: 'MS Sans Serif', sans-serif, Verdana, Arial; + font-size: 9pt; + color: #000000; +} + +.mcePathItem:hover { + text-decoration: underline; +} + +.mceStatusbarPathText { + float: left; +} + +.mceStatusbarResize { + float: right; + background-image: url('../images/statusbar_resize.gif'); + background-repeat: no-repeat; + width: 11px; + height: 20px; + cursor: se-resize; +} + +.mceResizeBox { + width: 10px; + height: 10px; + display: none; + border: 1px dotted gray; + margin: 0px; + padding: 0px; +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/editor_template.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/editor_template.js new file mode 100644 index 00000000..0dd4074f --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/editor_template.js @@ -0,0 +1,13 @@ +/* Import theme specific language pack */ + tinyMCE.importThemeLanguagePack('advanced');var TinyMCE_advanced_autoImportCSSClasses=true;var TinyMCE_advanced_resizer=new Object();var TinyMCE_advanced_buttons=[['bold','{$lang_bold_img}','{$lang_bold_desc}','Bold'],['italic','{$lang_italic_img}','{$lang_italic_desc}','Italic'],['underline','{$lang_underline_img}','{$lang_underline_desc}','Underline'],['strikethrough','strikethrough.gif','{$lang_striketrough_desc}','Strikethrough'],['justifyleft','left.gif','{$lang_justifyleft_desc}','JustifyLeft'],['justifycenter','center.gif','{$lang_justifycenter_desc}','JustifyCenter'],['justifyright','right.gif','{$lang_justifyright_desc}','JustifyRight'],['justifyfull','full.gif','{$lang_justifyfull_desc}','JustifyFull'],['bullist','bullist.gif','{$lang_bullist_desc}','InsertUnorderedList'],['numlist','numlist.gif','{$lang_numlist_desc}','InsertOrderedList'],['outdent','outdent.gif','{$lang_outdent_desc}','Outdent'],['indent','indent.gif','{$lang_indent_desc}','Indent'],['cut','cut.gif','{$lang_cut_desc}','Cut'],['copy','copy.gif','{$lang_copy_desc}','Copy'],['paste','paste.gif','{$lang_paste_desc}','Paste'],['undo','undo.gif','{$lang_undo_desc}','Undo'],['redo','redo.gif','{$lang_redo_desc}','Redo'],['link','link.gif','{$lang_link_desc}','mceLink',true],['unlink','unlink.gif','{$lang_unlink_desc}','unlink'],['image','image.gif','{$lang_image_desc}','mceImage',true],['cleanup','cleanup.gif','{$lang_cleanup_desc}','mceCleanup'],['help','help.gif','{$lang_help_desc}','mceHelp'],['code','code.gif','{$lang_theme_code_desc}','mceCodeEditor'],['hr','hr.gif','{$lang_theme_hr_desc}','inserthorizontalrule'],['removeformat','removeformat.gif','{$lang_theme_removeformat_desc}','removeformat'],['sub','sub.gif','{$lang_theme_sub_desc}','subscript'],['sup','sup.gif','{$lang_theme_sup_desc}','superscript'],['forecolor','forecolor.gif','{$lang_theme_forecolor_desc}','mceForeColor',true],['backcolor','backcolor.gif','{$lang_theme_backcolor_desc}','mceBackColor',true],['charmap','charmap.gif','{$lang_theme_charmap_desc}','mceCharMap'],['visualaid','visualaid.gif','{$lang_theme_visualaid_desc}','mceToggleVisualAid'],['anchor','anchor.gif','{$lang_theme_anchor_desc}','mceInsertAnchor'],['newdocument','newdocument.gif','{$lang_newdocument_desc}','mceNewDocument']];function TinyMCE_advanced_getControlHTML(button_name){var buttonTileMap=new Array('anchor.gif','backcolor.gif','bullist.gif','center.gif','charmap.gif','cleanup.gif','code.gif','copy.gif','custom_1.gif','cut.gif','forecolor.gif','full.gif','help.gif','hr.gif','image.gif','indent.gif','left.gif','link.gif','numlist.gif','outdent.gif','paste.gif','redo.gif','removeformat.gif','right.gif','strikethrough.gif','sub.gif','sup.gif','undo.gif','unlink.gif','visualaid.gif');for(var i=0;i4?but[4]:false)+(but.length>5?',\''+but[5]+'\'':'')+')';return '';}}}var cmd='tinyMCE.execInstanceCommand(\'{$editor_id}\',\''+but[3]+'\','+(but.length>4?but[4]:false)+(but.length>5?',\''+but[5]+'\'':'')+')';return '';}}switch(button_name){case "formatselect":var html='';return html;case "styleselect":return '';case "fontselect":var fontHTML='';return fontHTML;case "fontsizeselect":return '';case "|":case "separator":return '';case "spacer":return '';case "rowseparator":return '
    ';}return "";}function TinyMCE_advanced_execCommand(editor_id,element,command,user_interface,value){switch(command){case "mceForeColor":var template=new Array();var elm=tinyMCE.selectedInstance.getFocusElement();var inputColor=tinyMCE.getAttrib(elm,"color");if(inputColor=='')inputColor=elm.style.color;if(!inputColor)inputColor="#000000";template['file']='color_picker.htm';template['width']=220;template['height']=190;tinyMCE.openWindow(template,{editor_id:editor_id,inline:"yes",command:"forecolor",input_color:inputColor});return true;case "mceBackColor":var template=new Array();var elm=tinyMCE.selectedInstance.getFocusElement();var inputColor=elm.style.backgroundColor;if(!inputColor)inputColor="#000000";template['file']='color_picker.htm';template['width']=220;template['height']=190;template['width']+=tinyMCE.getLang('lang_theme_advanced_backcolor_delta_width',0);template['height']+=tinyMCE.getLang('lang_theme_advanced_backcolor_delta_height',0);tinyMCE.openWindow(template,{editor_id:editor_id,inline:"yes",command:"HiliteColor",input_color:inputColor});return true;case "mceColorPicker":if(user_interface){var template=new Array();var inputColor=value['document'].getElementById(value['element_id']).value;template['file']='color_picker.htm';template['width']=220;template['height']=190;template['close_previous']="no";template['width']+=tinyMCE.getLang('lang_theme_advanced_colorpicker_delta_width',0);template['height']+=tinyMCE.getLang('lang_theme_advanced_colorpicker_delta_height',0);if(typeof(value['store_selection'])=="undefined")value['store_selection']=true;tinyMCE.lastColorPickerValue=value;tinyMCE.openWindow(template,{editor_id:editor_id,mce_store_selection:value['store_selection'],inline:"yes",command:"mceColorPicker",input_color:inputColor});}else{var savedVal=tinyMCE.lastColorPickerValue;var elm=savedVal['document'].getElementById(savedVal['element_id']);elm.value=value;eval('elm.onchange();');}return true;case "mceCodeEditor":var template=new Array();template['file']='source_editor.htm';template['width']=parseInt(tinyMCE.getParam("theme_advanced_source_editor_width",500));template['height']=parseInt(tinyMCE.getParam("theme_advanced_source_editor_height",400));tinyMCE.openWindow(template,{editor_id:editor_id,resizable:"yes",scrollbars:"no",inline:"yes"});return true;case "mceCharMap":var template=new Array();template['file']='charmap.htm';template['width']=550+(tinyMCE.isOpera?40:0);template['height']=250;template['width']+=tinyMCE.getLang('lang_theme_advanced_charmap_delta_width',0);template['height']+=tinyMCE.getLang('lang_theme_advanced_charmap_delta_height',0);tinyMCE.openWindow(template,{editor_id:editor_id,inline:"yes"});return true;case "mceInsertAnchor":var template=new Array();template['file']='anchor.htm';template['width']=320;template['height']=90+(tinyMCE.isNS7?30:0);template['width']+=tinyMCE.getLang('lang_theme_advanced_anchor_delta_width',0);template['height']+=tinyMCE.getLang('lang_theme_advanced_anchor_delta_height',0);tinyMCE.openWindow(template,{editor_id:editor_id,inline:"yes"});return true;case "mceNewDocument":if(confirm(tinyMCE.getLang('lang_newdocument')))tinyMCE.execInstanceCommand(editor_id,'mceSetContent',false,'');return true;}return false;}function TinyMCE_advanced_getEditorTemplate(settings,editorId){function removeFromArray(in_array,remove_array){var outArray=new Array();for(var i=0;i 

    ';var layoutManager=tinyMCE.getParam("theme_advanced_layout_manager","SimpleLayout");var styleSelectHTML='';if(settings['theme_advanced_styles']){var stylesAr=settings['theme_advanced_styles'].split(';');for(var i=0;i'+key+'';}TinyMCE_advanced_autoImportCSSClasses=false;}switch(layoutManager){case "SimpleLayout":var toolbarHTML="";var toolbarLocation=tinyMCE.getParam("theme_advanced_toolbar_location","bottom");var toolbarAlign=tinyMCE.getParam("theme_advanced_toolbar_align","center");var pathLocation=tinyMCE.getParam("theme_advanced_path_location","none");var statusbarLocation=tinyMCE.getParam("theme_advanced_statusbar_location",pathLocation);var defVals={theme_advanced_buttons1:"bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,styleselect,formatselect",theme_advanced_buttons2:"bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,link,unlink,anchor,image,cleanup,help,code",theme_advanced_buttons3:"hr,removeformat,visualaid,separator,sub,sup,separator,charmap"};/*toolbarHTML+='';*/for(var i=1;i<100;i++){var def=defVals["theme_advanced_buttons"+i];var buttons=tinyMCE.getParam("theme_advanced_buttons"+i,def==null?'':def,true,',');if(buttons.length==0)break;buttons=removeFromArray(buttons,tinyMCE.getParam("theme_advanced_disable","",true,','));buttons=addToArray(buttons,tinyMCE.getParam("theme_advanced_buttons"+i+"_add","",true,','));buttons=addToArray(tinyMCE.getParam("theme_advanced_buttons"+i+"_add_before","",true,','),buttons);for(var b=0;b0){toolbarHTML+="
    ";deltaHeight-=23;}}toolbarHTML+='';template['html']='';if(toolbarLocation=="top"){template['html']+='';}if(statusbarLocation=="top"){template['html']+='';deltaHeight-=23;}template['html']+='';if(toolbarLocation=="bottom"){template['html']+='';}if(toolbarLocation=="external"){var bod=document.body;var elm=document.createElement("div");toolbarHTML=tinyMCE.replaceVars(toolbarHTML,tinyMCE.settings);toolbarHTML=tinyMCE.replaceVars(toolbarHTML,tinyMCELang);toolbarHTML=tinyMCE.replaceVar(toolbarHTML,'style_select_options',styleSelectHTML);toolbarHTML=tinyMCE.replaceVar(toolbarHTML,"editor_id",editorId);toolbarHTML=tinyMCE.applyTemplate(toolbarHTML);elm.className="mceToolbarExternal";elm.id=editorId+"_toolbar";elm.innerHTML='
    '+toolbarHTML+'
    '+statusbarHTML+'
    '+toolbarHTML+'
    '+toolbarHTML+'
    ';bod.appendChild(elm);deltaHeight=0;tinyMCE.getInstanceById(editorId).toolbarElement=elm;}else{tinyMCE.getInstanceById(editorId).toolbarElement=null;}if(statusbarLocation=="bottom"){template['html']+=''+statusbarHTML+'';deltaHeight-=23;}template['html']+='';break;case "RowLayout":template['html']='';var containers=tinyMCE.getParam("theme_advanced_containers","",true,",");var defaultContainerCSS=tinyMCE.getParam("theme_advanced_containers_default_class","container");var defaultContainerAlign=tinyMCE.getParam("theme_advanced_containers_default_align","center");for(var i=0;i';}else if(containers[i]=="mceElementpath"||containers[i]=="mceStatusbar"){var pathClass="mceStatusbar";if(i==containers.length-1){pathClass="mceStatusbarBottom";}else if(i==0){pathClass="mceStatusbar";}else{deltaHeight-=2;}template['html']+='';deltaHeight-=22;}else{var curContainer=tinyMCE.getParam("theme_advanced_container_"+containers[i],"",true,',');var curContainerHTML="";var curAlign=tinyMCE.getParam("theme_advanced_container_"+containers[i]+"_align",defaultContainerAlign);var curCSS=tinyMCE.getParam("theme_advanced_container_"+containers[i]+"_class",defaultContainerCSS);for(var j=0;j0){curContainerHTML+="
    ";deltaHeight-=23;}template['html']+='
    ';}}template['html']+='
    \ + \ +
    '+statusbarHTML+'
    '+curContainerHTML+'
    ';break;case "BorderLayout":break;case "CustomLayout":var customLayout=tinyMCE.getParam("theme_advanced_custom_layout","");if(customLayout!=""&&eval("typeof("+customLayout+")")!="undefined"){template=eval(customLayout+"(template);");}break;default:alert('UNDEFINED LAYOUT MANAGER! PLEASE CHECK YOUR TINYMCE CONFIG!');break;}template['html']+='
    ';template['html']=tinyMCE.replaceVar(template['html'],'style_select_options',styleSelectHTML);template['delta_width']=0;template['delta_height']=deltaHeight;return template;}function TinyMCE_advanced_setResizing(e,editor_id,state){e=typeof(e)=="undefined"?window.event:e;var resizer=TinyMCE_advanced_resizer;var editorContainer=document.getElementById(editor_id+'_parent');var editorArea=document.getElementById(editor_id+'_parent').firstChild;var resizeBox=document.getElementById(editor_id+'_resize_box');var inst=tinyMCE.getInstanceById(editor_id);if(state){var width=editorArea.clientWidth;var height=editorArea.clientHeight;resizeBox.style.width=width+"px";resizeBox.style.height=height+"px";resizer.iframeWidth=inst.iframeElement.clientWidth;resizer.iframeHeight=inst.iframeElement.clientHeight;editorArea.style.display="none";resizeBox.style.display="block";if(!resizer.eventHandlers){if(tinyMCE.isMSIE)tinyMCE.addEvent(document,"mousemove",TinyMCE_advanced_resizeEventHandler);else tinyMCE.addEvent(window,"mousemove",TinyMCE_advanced_resizeEventHandler);tinyMCE.addEvent(document,"mouseup",TinyMCE_advanced_resizeEventHandler);resizer.eventHandlers=true;}resizer.resizing=true;resizer.downX=e.screenX;resizer.downY=e.screenY;resizer.width=parseInt(resizeBox.style.width);resizer.height=parseInt(resizeBox.style.height);resizer.editorId=editor_id;resizer.resizeBox=resizeBox;resizer.horizontal=tinyMCE.getParam("theme_advanced_resize_horizontal",true);}else{resizer.resizing=false;resizeBox.style.display="none";editorArea.style.display=tinyMCE.isMSIE?"block":"table";tinyMCE.execCommand('mceResetDesignMode');}}function TinyMCE_advanced_initInstance(inst){if(tinyMCE.getParam("theme_advanced_resizing",false)){if(tinyMCE.getParam("theme_advanced_resizing_use_cookie",true)){var w=TinyMCE_advanced_getCookie("TinyMCE_"+inst.editorId+"_width");var h=TinyMCE_advanced_getCookie("TinyMCE_"+inst.editorId+"_height");TinyMCE_advanced_resizeTo(inst,w,h,tinyMCE.getParam("theme_advanced_resize_horizontal",true));}}}function TinyMCE_advanced_setCookie(name,value,expires,path,domain,secure){var curCookie=name+"="+escape(value)+((expires)?"; expires="+expires.toGMTString():"")+((path)?"; path="+escape(path):"")+((domain)?"; domain="+domain:"")+((secure)?"; secure":"");document.cookie=curCookie;}function TinyMCE_advanced_getCookie(name){var dc=document.cookie;var prefix=name+"=";var begin=dc.indexOf("; "+prefix);if(begin==-1){begin=dc.indexOf(prefix);if(begin!=0)return null;}else begin+=2;var end=document.cookie.indexOf(";",begin);if(end==-1)end=dc.length;return unescape(dc.substring(begin+prefix.length,end));}function TinyMCE_advanced_resizeTo(inst,w,h,set_w){var editorContainer=document.getElementById(inst.editorId+'_parent');var tableElm=editorContainer.firstChild;var iframe=inst.iframeElement;if(w==null||w=="null"){set_w=false;w=0;}if(h==null||h=="null")return;w=parseInt(w);h=parseInt(h);if(tinyMCE.isGecko){w+=2;h+=2;}var dx=w-tableElm.clientWidth;var dy=h-tableElm.clientHeight;if(set_w)tableElm.style.width=w+"px";tableElm.style.height=h+"px";iw=iframe.clientWidth+dx;ih=iframe.clientHeight+dy;if(tinyMCE.isGecko){iw-=2;ih-=2;}if(set_w)iframe.style.width=iw+"px";iframe.style.height=ih+"px";if(set_w){var tableBodyElm=tableElm.firstChild;var minIframeWidth=tableBodyElm.scrollWidth;if(inst.iframeElement.clientWidth=0;i--){var nodeName=path[i].nodeName.toLowerCase();var nodeData="";if(nodeName=="b"){nodeName="strong";}if(nodeName=="i"){nodeName="em";}if(nodeName=="span"){var cn=tinyMCE.getAttrib(path[i],"class");if(cn!=""&&cn.indexOf('mceItem')==-1)nodeData+="class: "+cn+" ";var st=tinyMCE.getAttrib(path[i],"style");if(st!=""){st=tinyMCE.serializeStyle(tinyMCE.parseStyle(st));nodeData+="style: "+st+" ";}}if(nodeName=="font"){if(tinyMCE.getParam("convert_fonts_to_spans"))nodeName="span";var face=tinyMCE.getAttrib(path[i],"face");if(face!="")nodeData+="font: "+face+" ";var size=tinyMCE.getAttrib(path[i],"size");if(size!="")nodeData+="size: "+size+" ";var color=tinyMCE.getAttrib(path[i],"color");if(color!="")nodeData+="color: "+color+" ";}if(getAttrib(path[i],'id')!=""){nodeData+="id: "+path[i].getAttribute('id')+" ";}var className=tinyMCE.getVisualAidClass(tinyMCE.getAttrib(path[i],"class"),false);if(className!=""&&className.indexOf('mceItem')==-1)nodeData+="class: "+className+" ";if(getAttrib(path[i],'src')!=""){nodeData+="src: "+path[i].getAttribute('src')+" ";}if(getAttrib(path[i],'href')!=""){nodeData+="href: "+path[i].getAttribute('href')+" ";}if(nodeName=="img"&&tinyMCE.getAttrib(path[i],"class").indexOf('mceItemFlash')!=-1){nodeName="flash";nodeData="src: "+path[i].getAttribute('title');}if(nodeName=="a"&&(anchor=tinyMCE.getAttrib(path[i],"name"))!=""){nodeName="a";nodeName+="#"+anchor;nodeData="";}if(getAttrib(path[i],'name').indexOf("mce_")!=0){var className=tinyMCE.getVisualAidClass(tinyMCE.getAttrib(path[i],"class"),false);if(className!=""&&className.indexOf('mceItem')==-1){nodeName+="."+className;}}var cmd='tinyMCE.execInstanceCommand(\''+editor_id+'\',\'mceSelectNodeDepth\',false,\''+i+'\');';html+=''+nodeName+'';if(i>0){html+=" » ";}}pathElm.innerHTML=''+tinyMCE.getLang('lang_theme_path')+": "+html+' ';}tinyMCE.switchClassSticky(editor_id+'_justifyleft','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_justifyright','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_justifycenter','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_justifyfull','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_bold','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_italic','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_underline','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_strikethrough','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_bullist','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_numlist','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_sub','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_sup','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_anchor','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_link','mceButtonDisabled',true);tinyMCE.switchClassSticky(editor_id+'_unlink','mceButtonDisabled',true);tinyMCE.switchClassSticky(editor_id+'_outdent','mceButtonDisabled',true);tinyMCE.switchClassSticky(editor_id+'_image','mceButtonNormal');tinyMCE.switchClassSticky(editor_id+'_hr','mceButtonNormal');if(node.nodeName=="A"&&tinyMCE.getAttrib(node,"class").indexOf('mceItemAnchor')!=-1)tinyMCE.switchClassSticky(editor_id+'_anchor','mceButtonSelected');var anchorLink=tinyMCE.getParentElement(node,"a","href");if(anchorLink||any_selection){tinyMCE.switchClassSticky(editor_id+'_link',anchorLink?'mceButtonSelected':'mceButtonNormal',false);tinyMCE.switchClassSticky(editor_id+'_unlink',anchorLink?'mceButtonSelected':'mceButtonNormal',false);}tinyMCE.switchClassSticky(editor_id+'_visualaid',visual_aid?'mceButtonSelected':'mceButtonNormal',false);if(undo_levels!=-1){tinyMCE.switchClassSticky(editor_id+'_undo','mceButtonDisabled',true);tinyMCE.switchClassSticky(editor_id+'_redo','mceButtonDisabled',true);}if(tinyMCE.getParentElement(node,"li,blockquote")){tinyMCE.switchClassSticky(editor_id+'_outdent','mceButtonNormal',false);}if(undo_index!=-1&&(undo_index0)){tinyMCE.switchClassSticky(editor_id+'_redo','mceButtonNormal',false);}if(undo_index!=-1&&(undo_index>0&&undo_levels>0)){tinyMCE.switchClassSticky(editor_id+'_undo','mceButtonNormal',false);}var selectElm=document.getElementById(editor_id+"_styleSelect");if(selectElm){TinyMCE_advanced_setupCSSClasses(editor_id);classNode=node;breakOut=false;var index=0;do{if(classNode&&classNode.className){for(var i=0;i");}else{selectByValue(selectElm,"");}}var selectElm=document.getElementById(editor_id+"_fontNameSelect");if(selectElm){if(!tinyMCE.isSafari&&!(tinyMCE.isMSIE&&!tinyMCE.isOpera)){var face=doc.queryCommandValue('FontName');face=face==null||face==""?"":face;selectByValue(selectElm,face,face!="");}else{var elm=tinyMCE.getParentElement(node,"font","face");if(elm){var family=tinyMCE.getAttrib(elm,"face");if(family=='')family=''+elm.style.fontFamily;if(!selectByValue(selectElm,family,family!=""))selectByValue(selectElm,"");}else selectByValue(selectElm,"");}}var selectElm=document.getElementById(editor_id+"_fontSizeSelect");if(selectElm){if(!tinyMCE.isSafari&&!tinyMCE.isOpera){var size=doc.queryCommandValue('FontSize');selectByValue(selectElm,size==null||size==""?"0":size);}else{var elm=tinyMCE.getParentElement(node,"font","size");if(elm){var size=tinyMCE.getAttrib(elm,"size");if(size==''){var sizes=new Array('','8px','10px','12px','14px','18px','24px','36px');size=''+elm.style.fontSize;for(var i=0;i0){selectElm.setAttribute('cssImported','true');}}}; diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/editor_template_src.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/editor_template_src.js new file mode 100644 index 00000000..e69de29b diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/image.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/image.htm new file mode 100644 index 00000000..0b5c7db6 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/image.htm @@ -0,0 +1,112 @@ + + + {$lang_insert_image_title} + + + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    {$lang_insert_image_title}
    {$lang_insert_image_src}: + + + + +
     
    {$lang_insert_image_alt}:
    {$lang_insert_image_align}:
    +
    +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/anchor.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/anchor.gif new file mode 100644 index 00000000..34ab7153 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/anchor.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/anchor_symbol.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/anchor_symbol.gif new file mode 100644 index 00000000..2eafd795 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/anchor_symbol.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/backcolor.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/backcolor.gif new file mode 100644 index 00000000..8a532e5e Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/backcolor.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold.gif new file mode 100644 index 00000000..d6a9cc2c Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_de_se.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_de_se.gif new file mode 100644 index 00000000..9b129de2 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_de_se.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_fr.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_fr.gif new file mode 100644 index 00000000..28164545 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_fr.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_ru.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_ru.gif new file mode 100644 index 00000000..e000d461 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bold_ru.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/browse.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/browse.gif new file mode 100644 index 00000000..c786d0b2 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/browse.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bullist.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bullist.gif new file mode 100644 index 00000000..6e19467c Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/bullist.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/buttons.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/buttons.gif new file mode 100644 index 00000000..5d5e08b5 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/buttons.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cancel_button_bg.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cancel_button_bg.gif new file mode 100644 index 00000000..4b4aeefc Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cancel_button_bg.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/center.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/center.gif new file mode 100644 index 00000000..42d609a9 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/center.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/charmap.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/charmap.gif new file mode 100644 index 00000000..3cdc4ac9 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/charmap.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cleanup.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cleanup.gif new file mode 100644 index 00000000..16491f6c Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cleanup.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/close.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/close.gif new file mode 100644 index 00000000..679ca2aa Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/close.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/code.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/code.gif new file mode 100644 index 00000000..c5d5a672 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/code.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/color.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/color.gif new file mode 100644 index 00000000..1ecd5743 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/color.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/copy.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/copy.gif new file mode 100644 index 00000000..dc146865 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/copy.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/custom_1.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/custom_1.gif new file mode 100644 index 00000000..4cbccdad Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/custom_1.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cut.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cut.gif new file mode 100644 index 00000000..4e9a70b6 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/cut.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/forecolor.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/forecolor.gif new file mode 100644 index 00000000..d5e38142 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/forecolor.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/full.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/full.gif new file mode 100644 index 00000000..c8504f62 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/full.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/hr.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/hr.gif new file mode 100644 index 00000000..1a1ba2a0 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/hr.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/image.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/image.gif new file mode 100644 index 00000000..4b88eddc Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/image.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/indent.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/indent.gif new file mode 100644 index 00000000..acd315bb Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/indent.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/insert_button_bg.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/insert_button_bg.gif new file mode 100644 index 00000000..69c131ce Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/insert_button_bg.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic.gif new file mode 100644 index 00000000..8bb330bd Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic_de_se.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic_de_se.gif new file mode 100644 index 00000000..feb0309e Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic_de_se.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic_ru.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic_ru.gif new file mode 100644 index 00000000..a2bb69a7 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/italic_ru.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/left.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/left.gif new file mode 100644 index 00000000..e8f7e427 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/left.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/link.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/link.gif new file mode 100644 index 00000000..1accf426 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/link.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/newdocument.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/newdocument.gif new file mode 100644 index 00000000..a9d29384 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/newdocument.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/numlist.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/numlist.gif new file mode 100644 index 00000000..a2683522 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/numlist.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/outdent.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/outdent.gif new file mode 100644 index 00000000..23f6aa40 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/outdent.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/paste.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/paste.gif new file mode 100644 index 00000000..1b45000a Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/paste.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/redo.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/redo.gif new file mode 100644 index 00000000..3af90697 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/redo.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/removeformat.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/removeformat.gif new file mode 100644 index 00000000..0fa3cb79 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/removeformat.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/right.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/right.gif new file mode 100644 index 00000000..e4cea971 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/right.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/spacer.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/spacer.gif new file mode 100644 index 00000000..38848651 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/spacer.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/statusbar_resize.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/statusbar_resize.gif new file mode 100644 index 00000000..af89d803 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/statusbar_resize.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/strikethrough.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/strikethrough.gif new file mode 100644 index 00000000..32646359 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/strikethrough.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/sub.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/sub.gif new file mode 100644 index 00000000..4d7ce30f Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/sub.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/sup.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/sup.gif new file mode 100644 index 00000000..a7145e01 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/sup.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table.gif new file mode 100644 index 00000000..2911830c Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_delete_col.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_delete_col.gif new file mode 100644 index 00000000..91f53af0 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_delete_col.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_delete_row.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_delete_row.gif new file mode 100644 index 00000000..7025733f Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_delete_row.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_col_after.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_col_after.gif new file mode 100644 index 00000000..85058080 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_col_after.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_col_before.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_col_before.gif new file mode 100644 index 00000000..b669d4fa Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_col_before.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_row_after.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_row_after.gif new file mode 100644 index 00000000..b9c14466 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_row_after.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_row_before.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_row_before.gif new file mode 100644 index 00000000..157d3736 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/table_insert_row_before.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline.gif new file mode 100644 index 00000000..1dfeb5f6 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline_fr.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline_fr.gif new file mode 100644 index 00000000..551d9148 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline_fr.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline_ru.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline_ru.gif new file mode 100644 index 00000000..b78e2a49 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/underline_ru.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/undo.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/undo.gif new file mode 100644 index 00000000..520796d6 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/undo.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/unlink.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/unlink.gif new file mode 100644 index 00000000..5c8a33db Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/unlink.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/visualaid.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/visualaid.gif new file mode 100644 index 00000000..63caf180 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/visualaid.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_bg.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_bg.gif new file mode 100644 index 00000000..897a01f2 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_bg.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_end.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_end.gif new file mode 100644 index 00000000..aee442be Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_end.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_sel_bg.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_sel_bg.gif new file mode 100644 index 00000000..9dc8abe1 Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_sel_bg.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_sel_end.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_sel_end.gif new file mode 100644 index 00000000..616a889d Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tab_sel_end.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tabs_bg.gif b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tabs_bg.gif new file mode 100644 index 00000000..c303f66d Binary files /dev/null and b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/images/xp/tabs_bg.gif differ diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/about.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/about.js new file mode 100644 index 00000000..9cb015d3 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/about.js @@ -0,0 +1,74 @@ +function init() { + tinyMCEPopup.resizeToInnerSize(); + + // Give FF some time + window.setTimeout('insertHelpIFrame();', 10); + + var tcont = document.getElementById('plugintablecontainer'); + var plugins = tinyMCE.getParam('plugins', '', true, ','); + if (plugins.length == 0) + document.getElementById('plugins_tab').style.display = 'none'; + + var html = ""; + html += ''; + html += ''; + html += ''; + html += ''; + html += ''; + html += ''; + html += ''; + html += ''; + html += ''; + + for (var i=0; i' + info.longname + ''; + else + html += ''; + + if (info.authorurl != null && info.authorurl != '') + html += ''; + else + html += ''; + + html += ''; + html += ''; + } + + html += ''; + html += '
    ' + tinyMCE.getLang('lang_plugin') + '' + tinyMCE.getLang('lang_author') + '' + tinyMCE.getLang('lang_version') + '
    ' + info.longname + '' + info.author + '' + info.author + '' + info.version + '
    '; + + tcont.innerHTML = html; +} + +function getPluginInfo(name) { + var fn = eval('tinyMCEPopup.windowOpener.TinyMCE_' + name + '_getInfo'); + + if (typeof(fn) != 'undefined') + return fn(); + + return { + longname : name, + authorurl : '', + infourl : '', + author : '--', + version : '--' + }; +} + +function insertHelpIFrame() { + var html = ''; + + document.getElementById('iframecontainer').innerHTML = html; + + html = ''; + html += 'Got Moxie? '; + html += 'Hosted By Sourceforge '; + html += 'Also on freshmeat '; + + document.getElementById('buttoncontainer').innerHTML = html; +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/anchor.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/anchor.js new file mode 100644 index 00000000..a89f9729 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/anchor.js @@ -0,0 +1,59 @@ +var action, element; + +function init() { + tinyMCEPopup.resizeToInnerSize(); + + var inst = tinyMCE.getInstanceById(tinyMCE.getWindowArg('editor_id')); + var anchor = tinyMCE.getParentElement(inst.getFocusElement(), "a", "name"); + var img = inst.getFocusElement(); + action = 'insert'; + + if (anchor != null) { + element = anchor; + action = "update"; + } + + if (tinyMCE.getAttrib(img, "class") == "mceItemAnchor") { + element = img; + action = "update"; + } + + if (action == "update") + document.forms[0].anchorName.value = element.nodeName == "IMG" ? element.getAttribute("title") : element.getAttribute("name"); + + document.forms[0].insert.value = tinyMCE.getLang('lang_' + action, 'Insert', true); +} + +function insertAnchor() { + var inst = tinyMCE.getInstanceById(tinyMCE.getWindowArg('editor_id')); + var name = document.forms[0].anchorName.value; + + tinyMCEPopup.execCommand("mceBeginUndoLevel"); + + if (action == "update") { + if (element.nodeName == "IMG") + element.setAttribute("title", name); + else + element.setAttribute("name", name); + } else { + var rng = inst.getRng(); + + if (rng.collapse) + rng.collapse(false); + + name = name.replace(/&/g, '&'); + name = name.replace(/\"/g, '"'); + name = name.replace(//g, '&gr;'); + + html = ''; + + tinyMCEPopup.execCommand("mceInsertContent", false, html); + tinyMCE.handleVisualAid(inst.getBody(), true, inst.visualAid, inst); + } + + tinyMCEPopup.execCommand("mceEndUndoLevel"); + + tinyMCE.triggerNodeChange(); + tinyMCEPopup.close(); +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/charmap.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/charmap.js new file mode 100644 index 00000000..2f758cfa --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/charmap.js @@ -0,0 +1,324 @@ +function init() { + tinyMCEPopup.resizeToInnerSize(); +} + +var charmap = new Array(); + +// for mor details please see w3c.org +// now here is the complete list ;) + +charmap = [ + [' ', ' ', true, 'no-break space'], + ['&', '&', true, 'ampersand'], + ['"', '"', true, 'quotation mark'], +// finance + ['¢', '¢', true, 'cent sign'], + ['€', '€', true, 'euro sign'], + ['£', '£', true, 'pound sign'], + ['¥', '¥', true, 'yen sign'], +// signs + ['©', '©', true, 'copyright sign'], + ['®', '®', true, 'registered sign'], + ['™', '™', true, 'trade mark sign'], + ['‰', '‰', true, 'per mille sign'], + ['µ', 'µ', true, 'micro sign'], + ['·', '·', true, 'middle dot'], + ['•', '•', true, 'bullet'], + ['…', '…', true, 'three dot leader'], + ['′', '′', true, 'minutes / feet'], + ['″', '″', true, 'seconds / inches'], + ['§', '§', true, 'section sign'], + ['¶', '¶', true, 'paragraph sign'], + ['ß', 'ß', true, 'sharp s / ess-zed'], +// quotations + ['‹', '‹', true, 'single left-pointing angle quotation mark'], + ['›', '›', true, 'single right-pointing angle quotation mark'], + ['«', '«', true, 'left pointing guillemet'], + ['»', '»', true, 'right pointing guillemet'], + ['‘', '‘', true, 'left single quotation mark'], + ['’', '’', true, 'right single quotation mark'], + ['“', '“', true, 'left double quotation mark'], + ['”', '”', true, 'right double quotation mark'], + ['‚', '‚', true, 'single low-9 quotation mark'], + ['„', '„', true, 'double low-9 quotation mark'], + ['<', '<', true, 'less-than sign'], + ['>', '>', true, 'greater-than sign'], + ['≤', '≤', true, 'less-than or equal to'], + ['≥', '≥', true, 'greater-than or equal to'], + ['–', '–', true, 'en dash'], + ['—', '—', true, 'em dash'], + ['¯', '¯', true, 'macron'], + ['‾', '‾', true, 'overline'], + ['¤', '¤', true, 'currency sign'], + ['¦', '¦', true, 'broken bar'], + ['¨', '¨', true, 'diaeresis'], + ['¡', '¡', true, 'inverted exclamation mark'], + ['¿', '¿', true, 'turned question mark'], + ['ˆ', 'ˆ', true, 'circumflex accent'], + ['˜', '˜', true, 'small tilde'], + ['°', '°', true, 'degree sign'], + ['−', '−', true, 'minus sign'], + ['±', '±', true, 'plus-minus sign'], + ['÷', '÷', true, 'division sign'], + ['⁄', '⁄', true, 'fraction slash'], + ['×', '×', true, 'multiplication sign'], + ['¹', '¹', true, 'superscript one'], + ['²', '²', true, 'superscript two'], + ['³', '³', true, 'superscript three'], + ['¼', '¼', true, 'fraction one quarter'], + ['½', '½', true, 'fraction one half'], + ['¾', '¾', true, 'fraction three quarters'], +// math / logical + ['ƒ', 'ƒ', true, 'function / florin'], + ['∫', '∫', true, 'integral'], + ['∑', '∑', true, 'n-ary sumation'], + ['∞', '∞', true, 'infinity'], + ['√', '√', true, 'square root'], + ['∼', '∼', false,'similar to'], + ['≅', '≅', false,'approximately equal to'], + ['≈', '≈', true, 'almost equal to'], + ['≠', '≠', true, 'not equal to'], + ['≡', '≡', true, 'identical to'], + ['∈', '∈', false,'element of'], + ['∉', '∉', false,'not an element of'], + ['∋', '∋', false,'contains as member'], + ['∏', '∏', true, 'n-ary product'], + ['∧', '∧', false,'logical and'], + ['∨', '∨', false,'logical or'], + ['¬', '¬', true, 'not sign'], + ['∩', '∩', true, 'intersection'], + ['∪', '∪', false,'union'], + ['∂', '∂', true, 'partial differential'], + ['∀', '∀', false,'for all'], + ['∃', '∃', false,'there exists'], + ['∅', '∅', false,'diameter'], + ['∇', '∇', false,'backward difference'], + ['∗', '∗', false,'asterisk operator'], + ['∝', '∝', false,'proportional to'], + ['∠', '∠', false,'angle'], +// undefined + ['´', '´', true, 'acute accent'], + ['¸', '¸', true, 'cedilla'], + ['ª', 'ª', true, 'feminine ordinal indicator'], + ['º', 'º', true, 'masculine ordinal indicator'], + ['†', '†', true, 'dagger'], + ['‡', '‡', true, 'double dagger'], +// alphabetical special chars + ['À', 'À', true, 'A - grave'], + ['Á', 'Á', true, 'A - acute'], + ['Â', 'Â', true, 'A - circumflex'], + ['Ã', 'Ã', true, 'A - tilde'], + ['Ä', 'Ä', true, 'A - diaeresis'], + ['Å', 'Å', true, 'A - ring above'], + ['Æ', 'Æ', true, 'ligature AE'], + ['Ç', 'Ç', true, 'C - cedilla'], + ['È', 'È', true, 'E - grave'], + ['É', 'É', true, 'E - acute'], + ['Ê', 'Ê', true, 'E - circumflex'], + ['Ë', 'Ë', true, 'E - diaeresis'], + ['Ì', 'Ì', true, 'I - grave'], + ['Í', 'Í', true, 'I - acute'], + ['Î', 'Î', true, 'I - circumflex'], + ['Ï', 'Ï', true, 'I - diaeresis'], + ['Ð', 'Ð', true, 'ETH'], + ['Ñ', 'Ñ', true, 'N - tilde'], + ['Ò', 'Ò', true, 'O - grave'], + ['Ó', 'Ó', true, 'O - acute'], + ['Ô', 'Ô', true, 'O - circumflex'], + ['Õ', 'Õ', true, 'O - tilde'], + ['Ö', 'Ö', true, 'O - diaeresis'], + ['Ø', 'Ø', true, 'O - slash'], + ['Œ', 'Œ', true, 'ligature OE'], + ['Š', 'Š', true, 'S - caron'], + ['Ù', 'Ù', true, 'U - grave'], + ['Ú', 'Ú', true, 'U - acute'], + ['Û', 'Û', true, 'U - circumflex'], + ['Ü', 'Ü', true, 'U - diaeresis'], + ['Ý', 'Ý', true, 'Y - acute'], + ['Ÿ', 'Ÿ', true, 'Y - diaeresis'], + ['Þ', 'Þ', true, 'THORN'], + ['à', 'à', true, 'a - grave'], + ['á', 'á', true, 'a - acute'], + ['â', 'â', true, 'a - circumflex'], + ['ã', 'ã', true, 'a - tilde'], + ['ä', 'ä', true, 'a - diaeresis'], + ['å', 'å', true, 'a - ring above'], + ['æ', 'æ', true, 'ligature ae'], + ['ç', 'ç', true, 'c - cedilla'], + ['è', 'è', true, 'e - grave'], + ['é', 'é', true, 'e - acute'], + ['ê', 'ê', true, 'e - circumflex'], + ['ë', 'ë', true, 'e - diaeresis'], + ['ì', 'ì', true, 'i - grave'], + ['í', 'í', true, 'i - acute'], + ['î', 'î', true, 'i - circumflex'], + ['ï', 'ï', true, 'i - diaeresis'], + ['ð', 'ð', true, 'eth'], + ['ñ', 'ñ', true, 'n - tilde'], + ['ò', 'ò', true, 'o - grave'], + ['ó', 'ó', true, 'o - acute'], + ['ô', 'ô', true, 'o - circumflex'], + ['õ', 'õ', true, 'o - tilde'], + ['ö', 'ö', true, 'o - diaeresis'], + ['ø', 'ø', true, 'o slash'], + ['œ', 'œ', true, 'ligature oe'], + ['š', 'š', true, 's - caron'], + ['ù', 'ù', true, 'u - grave'], + ['ú', 'ú', true, 'u - acute'], + ['û', 'û', true, 'u - circumflex'], + ['ü', 'ü', true, 'u - diaeresis'], + ['ý', 'ý', true, 'y - acute'], + ['þ', 'þ', true, 'thorn'], + ['ÿ', 'ÿ', true, 'y - diaeresis'], +// ['Α', 'Α', true, 'Alpha'], + ['Β', 'Β', true, 'Beta'], + ['Γ', 'Γ', true, 'Gamma'], + ['Δ', 'Δ', true, 'Delta'], + ['Ε', 'Ε', true, 'Epsilon'], + ['Ζ', 'Ζ', true, 'Zeta'], + ['Η', 'Η', true, 'Eta'], + ['Θ', 'Θ', true, 'Theta'], + ['Ι', 'Ι', true, 'Iota'], + ['Κ', 'Κ', true, 'Kappa'], + ['Λ', 'Λ', true, 'Lambda'], + ['Μ', 'Μ', true, 'Mu'], + ['Ν', 'Ν', true, 'Nu'], + ['Ξ', 'Ξ', true, 'Xi'], + ['Ο', 'Ο', true, 'Omicron'], + ['Π', 'Π', true, 'Pi'], + ['Ρ', 'Ρ', true, 'Rho'], + ['Σ', 'Σ', true, 'Sigma'], + ['Τ', 'Τ', true, 'Tau'], + ['Υ', 'Υ', true, 'Upsilon'], + ['Φ', 'Φ', true, 'Phi'], + ['Χ', 'Χ', true, 'Chi'], + ['Ψ', 'Ψ', true, 'Psi'], + ['Ω', 'Ω', true, 'Omega'], + ['α', 'α', true, 'alpha'], + ['β', 'β', true, 'beta'], + ['γ', 'γ', true, 'gamma'], + ['δ', 'δ', true, 'delta'], + ['ε', 'ε', true, 'epsilon'], + ['ζ', 'ζ', true, 'zeta'], + ['η', 'η', true, 'eta'], + ['θ', 'θ', true, 'theta'], + ['ι', 'ι', true, 'iota'], + ['κ', 'κ', true, 'kappa'], + ['λ', 'λ', true, 'lambda'], + ['μ', 'μ', true, 'mu'], + ['ν', 'ν', true, 'nu'], + ['ξ', 'ξ', true, 'xi'], + ['ο', 'ο', true, 'omicron'], + ['π', 'π', true, 'pi'], + ['ρ', 'ρ', true, 'rho'], + ['ς', 'ς', true, 'final sigma'], + ['σ', 'σ', true, 'sigma'], + ['τ', 'τ', true, 'tau'], + ['υ', 'υ', true, 'upsilon'], + ['φ', 'φ', true, 'phi'], + ['χ', 'χ', true, 'chi'], + ['ψ', 'ψ', true, 'psi'], + ['ω', 'ω', true, 'omega'], +// symbols + ['ℵ', 'ℵ', false,'alef symbol'], + ['ϖ', 'ϖ', false,'pi symbol'], + ['ℜ', 'ℜ', false,'real part symbol'], + ['ϑ','ϑ', false,'theta symbol'], + ['ϒ', 'ϒ', false,'upsilon - hook symbol'], + ['℘', '℘', false,'Weierstrass p'], + ['ℑ', 'ℑ', false,'imaginary part'], +// arrows + ['←', '←', true, 'leftwards arrow'], + ['↑', '↑', true, 'upwards arrow'], + ['→', '→', true, 'rightwards arrow'], + ['↓', '↓', true, 'downwards arrow'], + ['↔', '↔', true, 'left right arrow'], + ['↵', '↵', false,'carriage return'], + ['⇐', '⇐', false,'leftwards double arrow'], + ['⇑', '⇑', false,'upwards double arrow'], + ['⇒', '⇒', false,'rightwards double arrow'], + ['⇓', '⇓', false,'downwards double arrow'], + ['⇔', '⇔', false,'left right double arrow'], + ['∴', '∴', false,'therefore'], + ['⊂', '⊂', false,'subset of'], + ['⊃', '⊃', false,'superset of'], + ['⊄', '⊄', false,'not a subset of'], + ['⊆', '⊆', false,'subset of or equal to'], + ['⊇', '⊇', false,'superset of or equal to'], + ['⊕', '⊕', false,'circled plus'], + ['⊗', '⊗', false,'circled times'], + ['⊥', '⊥', false,'perpendicular'], + ['⋅', '⋅', false,'dot operator'], + ['⌈', '⌈', false,'left ceiling'], + ['⌉', '⌉', false,'right ceiling'], + ['⌊', '⌊', false,'left floor'], + ['⌋', '⌋', false,'right floor'], + ['⟨', '〈', false,'left-pointing angle bracket'], + ['⟩', '〉', false,'right-pointing angle bracket'], + ['◊', '◊', true,'lozenge'], + ['♠', '♠', false,'black spade suit'], + ['♣', '♣', true, 'black club suit'], + ['♥', '♥', true, 'black heart suit'], + ['♦', '♦', true, 'black diamond suit'], + [' ', ' ', false,'en space'], + [' ', ' ', false,'em space'], + [' ', ' ', false,'thin space'], + ['‌', '‌', false,'zero width non-joiner'], + ['‍', '‍', false,'zero width joiner'], + ['‎', '‎', false,'left-to-right mark'], + ['‏', '‏', false,'right-to-left mark'], + ['­', '­', false,'soft hyphen'] +]; + +function renderCharMapHTML() { + var charsPerRow = 20, tdWidth=20, tdHeight=20; + var html = ''; + var cols=-1; + for (var i=0; i' + + charmap[i][1] + + ''; + if ((cols+1) % charsPerRow == 0) + html += ''; + } + } + if (cols % charsPerRow > 0) { + var padd = charsPerRow - (cols % charsPerRow); + for (var i=0; i '; + } + html += '
    '; + document.write(html); +} + +function insertChar(chr) { + tinyMCEPopup.execCommand('mceInsertContent', false, '\&#' + chr + ';'); + + // Refocus in window + if (tinyMCEPopup.isWindow) + window.focus(); +} + +function previewChar(codeA, codeB, codeN) { + var elmA = document.getElementById('codeA'); + var elmB = document.getElementById('codeB'); + var elmV = document.getElementById('codeV'); + var elmN = document.getElementById('codeN'); + + if (codeA=='#160;') { + elmV.innerHTML = '__'; + } else { + elmV.innerHTML = '&' + codeA; + } + + elmB.innerHTML = '&' + codeA; + elmA.innerHTML = '&' + codeB; + elmN.innerHTML = codeN; +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/color_picker.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/color_picker.js new file mode 100644 index 00000000..1d585aa3 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/color_picker.js @@ -0,0 +1,107 @@ +function init() { + if (tinyMCE.isMSIE) + tinyMCEPopup.resizeToInnerSize(); +} + +function selectColor() { + var color = document.getElementById("selectedColorBox").value; + + tinyMCEPopup.execCommand(tinyMCE.getWindowArg('command'), false, color); + tinyMCEPopup.close(); +} + +function showColor(color) { + document.getElementById("selectedColor").style.backgroundColor = color; + document.getElementById("selectedColorBox").value = color; +} + +var colors = new Array( + "#000000","#000033","#000066","#000099","#0000cc","#0000ff","#330000","#330033", + "#330066","#330099","#3300cc","#3300ff","#660000","#660033","#660066","#660099", + "#6600cc","#6600ff","#990000","#990033","#990066","#990099","#9900cc","#9900ff", + "#cc0000","#cc0033","#cc0066","#cc0099","#cc00cc","#cc00ff","#ff0000","#ff0033", + "#ff0066","#ff0099","#ff00cc","#ff00ff","#003300","#003333","#003366","#003399", + "#0033cc","#0033ff","#333300","#333333","#333366","#333399","#3333cc","#3333ff", + "#663300","#663333","#663366","#663399","#6633cc","#6633ff","#993300","#993333", + "#993366","#993399","#9933cc","#9933ff","#cc3300","#cc3333","#cc3366","#cc3399", + "#cc33cc","#cc33ff","#ff3300","#ff3333","#ff3366","#ff3399","#ff33cc","#ff33ff", + "#006600","#006633","#006666","#006699","#0066cc","#0066ff","#336600","#336633", + "#336666","#336699","#3366cc","#3366ff","#666600","#666633","#666666","#666699", + "#6666cc","#6666ff","#996600","#996633","#996666","#996699","#9966cc","#9966ff", + "#cc6600","#cc6633","#cc6666","#cc6699","#cc66cc","#cc66ff","#ff6600","#ff6633", + "#ff6666","#ff6699","#ff66cc","#ff66ff","#009900","#009933","#009966","#009999", + "#0099cc","#0099ff","#339900","#339933","#339966","#339999","#3399cc","#3399ff", + "#669900","#669933","#669966","#669999","#6699cc","#6699ff","#999900","#999933", + "#999966","#999999","#9999cc","#9999ff","#cc9900","#cc9933","#cc9966","#cc9999", + "#cc99cc","#cc99ff","#ff9900","#ff9933","#ff9966","#ff9999","#ff99cc","#ff99ff", + "#00cc00","#00cc33","#00cc66","#00cc99","#00cccc","#00ccff","#33cc00","#33cc33", + "#33cc66","#33cc99","#33cccc","#33ccff","#66cc00","#66cc33","#66cc66","#66cc99", + "#66cccc","#66ccff","#99cc00","#99cc33","#99cc66","#99cc99","#99cccc","#99ccff", + "#cccc00","#cccc33","#cccc66","#cccc99","#cccccc","#ccccff","#ffcc00","#ffcc33", + "#ffcc66","#ffcc99","#ffcccc","#ffccff","#00ff00","#00ff33","#00ff66","#00ff99", + "#00ffcc","#00ffff","#33ff00","#33ff33","#33ff66","#33ff99","#33ffcc","#33ffff", + "#66ff00","#66ff33","#66ff66","#66ff99","#66ffcc","#66ffff","#99ff00","#99ff33", + "#99ff66","#99ff99","#99ffcc","#99ffff","#ccff00","#ccff33","#ccff66","#ccff99", + "#ccffcc","#ccffff","#ffff00","#ffff33","#ffff66","#ffff99","#ffffcc","#ffffff" +); + +function convertRGBToHex(col) { + var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi"); + + var rgb = col.replace(re, "$1,$2,$3").split(','); + if (rgb.length == 3) { + r = parseInt(rgb[0]).toString(16); + g = parseInt(rgb[1]).toString(16); + b = parseInt(rgb[2]).toString(16); + + r = r.length == 1 ? '0' + r : r; + g = g.length == 1 ? '0' + g : g; + b = b.length == 1 ? '0' + b : b; + + return "#" + r + g + b; + } + + return col; +} + +function convertHexToRGB(col) { + if (col.indexOf('#') != -1) { + col = col.replace(new RegExp('[^0-9A-F]', 'gi'), ''); + + r = parseInt(col.substring(0, 2), 16); + g = parseInt(col.substring(2, 4), 16); + b = parseInt(col.substring(4, 6), 16); + + return "rgb(" + r + "," + g + "," + b + ")"; + } + + return col; +} + +function renderColorMap() { + var html = ""; + var inputColor = convertRGBToHex(tinyMCE.getWindowArg('input_color')); + + html += '' + + ''; + for (var i=0; i' + + '' + + '' + colors[i] +  ''; + if ((i+1) % 18 == 0) + html += ''; + } + html += '' + + '
    ' + + '' + + '' + + '
    ' + + '' + + '' + + '' + + '
    ' + + '' + + '
    '; + + document.write(html); +} \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/image.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/image.js new file mode 100644 index 00000000..752e421d --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/jscripts/image.js @@ -0,0 +1,82 @@ +var url = tinyMCE.getParam("external_image_list_url"); +if (url != null) { + // Fix relative + if (url.charAt(0) != '/' && url.indexOf('://') == -1) + url = tinyMCE.documentBasePath + "/" + url; + + document.write(''); +} + +function insertImage() { + if (window.opener) { + var src = document.forms[0].src.value; + var alt = document.forms[0].alt.value; + var border = '';//document.forms[0].border.value; + var vspace = '';//document.forms[0].vspace.value; + var hspace = '';//document.forms[0].hspace.value; + var width = '';//document.forms[0].width.value; + var height = '';//document.forms[0].height.value; + var align = document.forms[0].align.options[document.forms[0].align.selectedIndex].value; + + window.opener.tinyMCE.insertImage(src, alt, border, hspace, vspace, width, height, align); + top.close(); + } +} + +function init() { + tinyMCEPopup.resizeToInnerSize(); + + document.getElementById('srcbrowsercontainer').innerHTML = getBrowserHTML('srcbrowser','src','image','theme_advanced_image'); + + var formObj = document.forms[0]; + + for (var i=0; i 0) { + for (var i=0; i'); +} + +function init() { + tinyMCEPopup.resizeToInnerSize(); + +// document.getElementById('hrefbrowsercontainer').innerHTML = getBrowserHTML('hrefbrowser','href','file','theme_advanced_link'); + + var formObj = document.forms[0]; + + for (var i=0; i 0) { + var formObj = document.forms[0]; + + for (var i=0; i', 'gi'),'\n'); + html = tinyMCE.regexpReplace(html, '
    ','
    \n','gi'); + html = tinyMCE.regexpReplace(html, '\n\n','\n','gi'); + return html; +} + +function onLoadInit() { + tinyMCEPopup.resizeToInnerSize(); + + document.forms[0].htmlSource.value = fixContent(tinyMCE.getContent(tinyMCE.getWindowArg('editor_id'))); + resizeInputs(); + setWrap('off'); +} + +function setWrap(val) { + // hard soft off + document.forms[0].htmlSource.wrap = val; +} + +function toggleWordWrap(elm) { + if (elm.checked) + setWrap('soft'); + else + setWrap('off'); +} + +var wHeight=0, wWidth=0, owHeight=0, owWidth=0; + +function resizeInputs() { + if (!tinyMCE.isMSIE) { + wHeight = self.innerHeight-80; + wWidth = self.innerWidth-16; + } else { + wHeight = document.body.clientHeight - 80; + wWidth = document.body.clientWidth - 16; + } + + document.forms[0].htmlSource.style.height = Math.abs(wHeight) + 'px'; + document.forms[0].htmlSource.style.width = Math.abs(wWidth) + 'px'; +} + +function renderWordWrap() { + if (tinyMCE.isMSIE) + document.write(''); +} diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/langs/en.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/langs/en.js new file mode 100644 index 00000000..14657ba5 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/langs/en.js @@ -0,0 +1,76 @@ +// UK lang variables + +tinyMCE.addToLang('',{ +theme_style_select : '-- Styles --', +theme_code_desc : 'Edit HTML Source (Alt+e)', +theme_code_title : 'HTML Source Editor', +theme_code_wordwrap : 'Word wrap', +theme_sub_desc : 'Subscript', +theme_sup_desc : 'Superscript', +theme_hr_desc : 'Insert horizontal ruler', +theme_removeformat_desc : 'Remove formatting', +theme_custom1_desc : 'Your custom description here', +insert_image_border : 'Border', +insert_image_dimensions : 'Dimensions', +insert_image_vspace : 'Vertical space', +insert_image_hspace : 'Horizontal space', +insert_image_align : 'Alignment', +insert_image_align_default : '-- Not set --', +insert_image_align_baseline : 'Baseline', +insert_image_align_top : 'Top', +insert_image_align_middle : 'Middle', +insert_image_align_bottom : 'Bottom', +insert_image_align_texttop : 'TextTop', +insert_image_align_absmiddle : 'Absolute Middle', +insert_image_align_absbottom : 'Absolute Bottom', +insert_image_align_left : 'Left', +insert_image_align_right : 'Right', +theme_font_size : '-- Font size --', +theme_fontdefault : '-- Font family --', +theme_block : '-- Format --', +theme_paragraph : 'Paragraph', +theme_div : 'Div', +theme_address : 'Address', +theme_pre : 'Preformatted', +theme_h1 : 'Heading 1', +theme_h2 : 'Heading 2', +theme_h3 : 'Heading 3', +theme_h4 : 'Heading 4', +theme_h5 : 'Heading 5', +theme_h6 : 'Heading 6', +theme_colorpicker_title : 'Select a color', +theme_colorpicker_apply : 'Apply', +theme_forecolor_desc : 'Select text color', +theme_backcolor_desc : 'Select background color', +theme_charmap_title : 'Select custom character', +theme_charmap_desc : 'Insert custom character', +theme_visualaid_desc : 'Toggle guidelines/invisible elements', +insert_anchor_title : 'Insert/edit anchor', +insert_anchor_name : 'Anchor name', +theme_anchor_desc : 'Insert/edit anchor', +theme_insert_link_titlefield : 'Title', +theme_clipboard_msg : 'Copy/Cut/Paste is not available in Mozilla and Firefox.\nDo you want more information about this issue?', +theme_path : 'Path', +cut_desc : 'Cut', +copy_desc : 'Copy', +paste_desc : 'Paste', +link_list : 'Link list', +image_list : 'Image list', +browse : 'Browse', +image_props_desc : 'Image properties', +newdocument_desc : 'New document', +class_name : 'Class', +newdocument : 'Are you sure you want clear all contents?', +about_title : 'About TinyMCE', +about : 'About', +license : 'License', +plugins : 'Plugins', +plugin : 'Plugin', +author : 'Author', +version : 'Version', +loaded_plugins : 'Loaded plugins', +help : 'Help', +not_set : '-- Not set --', +close : 'Close', +toolbar_focus : 'Jump to tool buttons - Alt+Q, Jump to editor - Alt-Z, Jump to element path - Alt-X' +}); diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/link.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/link.htm new file mode 100644 index 00000000..4b866fa0 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/link.htm @@ -0,0 +1,113 @@ + + + {$lang_insert_link_title} + + + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    {$lang_insert_link_title}
    {$lang_insert_link_url}: + + + + +
     
    {$lang_insert_link_target}:
    {$lang_theme_insert_link_titlefield}:
    +
    +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/source_editor.htm b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/source_editor.htm new file mode 100644 index 00000000..d8329ad5 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/themes/advanced/source_editor.htm @@ -0,0 +1,51 @@ + + + + {$lang_theme_code_title} + + + + + +
    +
    {$lang_theme_code_title}
    + +
    + +
    + + + +
    +
    + +
    + +
    + +
    +
    +
    + + diff --git a/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/tiny_mce.js b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/tiny_mce.js new file mode 100644 index 00000000..3f6c6bf3 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/js/tinymce/tiny_mce.js @@ -0,0 +1,6081 @@ +/** + * $RCSfile: tiny_mce_src.js,v $ + * $Revision: 1.281 $ + * $Date: 2005/12/02 08:12:07 $ + * + * @author Moxiecode + * @copyright Copyright © 2004, Moxiecode Systems AB, All rights reserved. + */ + +function TinyMCE() { + this.majorVersion = "2"; + this.minorVersion = "0"; + this.releaseDate = "2005-12-01"; + + this.instances = new Array(); + this.stickyClassesLookup = new Array(); + this.windowArgs = new Array(); + this.loadedFiles = new Array(); + this.configs = new Array(); + this.currentConfig = 0; + this.eventHandlers = new Array(); + + // Browser check + var ua = navigator.userAgent; + this.isMSIE = (navigator.appName == "Microsoft Internet Explorer"); + this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1); + this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1); + this.isGecko = ua.indexOf('Gecko') != -1; + this.isSafari = ua.indexOf('Safari') != -1; + this.isOpera = ua.indexOf('Opera') != -1; + this.isMac = ua.indexOf('Mac') != -1; + this.isNS7 = ua.indexOf('Netscape/7') != -1; + this.isNS71 = ua.indexOf('Netscape/7.1') != -1; + this.dialogCounter = 0; + + // Fake MSIE on Opera and if Opera fakes IE, Gecko or Safari cancel those + if (this.isOpera) { + this.isMSIE = true; + this.isGecko = false; + this.isSafari = false; + } + + // TinyMCE editor id instance counter + this.idCounter = 0; +}; + +TinyMCE.prototype.defParam = function(key, def_val) { + this.settings[key] = tinyMCE.getParam(key, def_val); +}; + +TinyMCE.prototype.init = function(settings) { + var theme; + + this.settings = settings; + + // Check if valid browser has execcommand support + if (typeof(document.execCommand) == 'undefined') + return; + + // Get script base path + if (!tinyMCE.baseURL) { + var elements = document.getElementsByTagName('script'); + + for (var i=0; i'); + this.defParam("font_size_classes", ''); + this.defParam("font_size_style_values", 'xx-small,x-small,small,medium,large,x-large,xx-large'); + this.defParam("event_elements", 'a,img'); + this.defParam("convert_urls", true); + this.defParam("table_inline_editing", false); + this.defParam("object_resizing", true); + + // Browser check IE + if (this.isMSIE && this.settings['browsers'].indexOf('msie') == -1) + return; + + // Browser check Gecko + if (this.isGecko && this.settings['browsers'].indexOf('gecko') == -1) + return; + + // Browser check Safari + if (this.isSafari && this.settings['browsers'].indexOf('safari') == -1) + return; + + // Browser check Opera + if (this.isOpera && this.settings['browsers'].indexOf('opera') == -1) + return; + + // If not super absolute make it so + var baseHREF = tinyMCE.settings['document_base_url']; + var h = document.location.href; + var p = h.indexOf('://'); + if (p > 0 && document.location.protocol != "file:") { + p = h.indexOf('/', p + 3); + h = h.substring(0, p); + + if (baseHREF.indexOf('://') == -1) + baseHREF = h + baseHREF; + + tinyMCE.settings['document_base_url'] = baseHREF; + tinyMCE.settings['document_base_prefix'] = h; + } + + // Trim away query part + if (baseHREF.indexOf('?') != -1) + baseHREF = baseHREF.substring(0, baseHREF.indexOf('?')); + + this.settings['base_href'] = baseHREF.substring(0, baseHREF.lastIndexOf('/')) + "/"; + + theme = this.settings['theme']; + this.blockRegExp = new RegExp("^(h[1-6]|p|div|address|pre|form|table|li|ol|ul|td|blockquote|center|dl|dir|fieldset|form|noscript|noframes|menu|isindex)$", "i"); + this.posKeyCodes = new Array(13,45,36,35,33,34,37,38,39,40); + this.uniqueURL = 'http://tinymce.moxiecode.cp/mce_temp_url'; // Make unique URL non real URL + this.uniqueTag = ''; + + // Theme url + this.settings['theme_href'] = tinyMCE.baseURL + "/themes/" + theme; + + if (!tinyMCE.isMSIE) + this.settings['force_br_newlines'] = false; + + if (tinyMCE.getParam("content_css", false)) { + var cssPath = tinyMCE.getParam("content_css", ""); + + // Is relative + if (cssPath.indexOf('://') == -1 && cssPath.charAt(0) != '/') + this.settings['content_css'] = this.documentBasePath + "/" + cssPath; + else + this.settings['content_css'] = cssPath; + } else + this.settings['content_css'] = ''; + + if (tinyMCE.getParam("popups_css", false)) { + var cssPath = tinyMCE.getParam("popups_css", ""); + + // Is relative + if (cssPath.indexOf('://') == -1 && cssPath.charAt(0) != '/') + this.settings['popups_css'] = this.documentBasePath + "/" + cssPath; + else + this.settings['popups_css'] = cssPath; + } else + this.settings['popups_css'] = tinyMCE.baseURL + "/themes/" + theme + "/css/editor_popup.css"; + + if (tinyMCE.getParam("editor_css", false)) { + var cssPath = tinyMCE.getParam("editor_css", ""); + + // Is relative + if (cssPath.indexOf('://') == -1 && cssPath.charAt(0) != '/') + this.settings['editor_css'] = this.documentBasePath + "/" + cssPath; + else + this.settings['editor_css'] = cssPath; + } else + this.settings['editor_css'] = tinyMCE.baseURL + "/themes/" + theme + "/css/editor_ui.css"; + + if (tinyMCE.settings['debug']) { + var msg = "Debug: \n"; + + msg += "baseURL: " + this.baseURL + "\n"; + msg += "documentBasePath: " + this.documentBasePath + "\n"; + msg += "content_css: " + this.settings['content_css'] + "\n"; + msg += "popups_css: " + this.settings['popups_css'] + "\n"; + msg += "editor_css: " + this.settings['editor_css'] + "\n"; + + alert(msg); + } + + // Init HTML cleanup + this._initCleanup(); + + // Only do this once + if (this.configs.length == 0) { + // Is Safari enabled + if (this.isSafari && this.getParam('safari_warning', true)) + alert("Safari support is very limited and should be considered experimental.\nSo there is no need to even submit bugreports on this early version.\nYou can disable this message by setting: safari_warning option to false"); + + tinyMCE.addEvent(window, "load", TinyMCE.prototype.onLoad); + + if (tinyMCE.isMSIE) { + if (tinyMCE.settings['add_unload_trigger']) { + tinyMCE.addEvent(window, "unload", TinyMCE.prototype.unloadHandler); + tinyMCE.addEvent(window.document, "beforeunload", TinyMCE.prototype.unloadHandler); + } + } else { + if (tinyMCE.settings['add_unload_trigger']) + tinyMCE.addEvent(window, "unload", function () {tinyMCE.triggerSave(true, true);}); + } + } + + this.loadScript(tinyMCE.baseURL + '/themes/' + this.settings['theme'] + '/editor_template' + tinyMCE.srcMode + '.js'); + this.loadScript(tinyMCE.baseURL + '/langs/' + this.settings['language'] + '.js'); + this.loadCSS(this.settings['editor_css']); + + // Add plugins + var themePlugins = tinyMCE.getParam('plugins', '', true, ','); + if (this.settings['plugins'] != '') { + for (var i=0; i'); + + this.loadedFiles[this.loadedFiles.length] = url; +}; + +TinyMCE.prototype.loadCSS = function(url) { + for (var i=0; i'); + + this.loadedFiles[this.loadedFiles.length] = url; +}; + +TinyMCE.prototype.importCSS = function(doc, css_file) { + if (css_file == '') + return; + + if (typeof(doc.createStyleSheet) == "undefined") { + var elm = doc.createElement("link"); + + elm.rel = "stylesheet"; + elm.href = css_file; + + if ((headArr = doc.getElementsByTagName("head")) != null && headArr.length > 0) + headArr[0].appendChild(elm); + } else + var styleSheet = doc.createStyleSheet(css_file); +}; + +TinyMCE.prototype.confirmAdd = function(e, settings) { + var elm = tinyMCE.isMSIE ? event.srcElement : e.target; + var elementId = elm.name ? elm.name : elm.id; + + tinyMCE.settings = settings; + + if (!elm.getAttribute('mce_noask') && confirm(tinyMCELang['lang_edit_confirm'])) + tinyMCE.addMCEControl(elm, elementId); + + elm.setAttribute('mce_noask', 'true'); +}; + +TinyMCE.prototype.updateContent = function(form_element_name) { + // Find MCE instance linked to given form element and copy it's value + var formElement = document.getElementById(form_element_name); + for (var n in tinyMCE.instances) { + var inst = tinyMCE.instances[n]; + if (!tinyMCE.isInstance(inst)) + continue; + + inst.switchSettings(); + + if (inst.formElement == formElement) { + var doc = inst.getDoc(); + + tinyMCE._setHTML(doc, inst.formElement.value); + + if (!tinyMCE.isMSIE) + doc.body.innerHTML = tinyMCE._cleanupHTML(inst, doc, this.settings, doc.body, inst.visualAid); + } + } +}; + +TinyMCE.prototype.addMCEControl = function(replace_element, form_element_name, target_document) { + var id = "mce_editor_" + tinyMCE.idCounter++; + var inst = new TinyMCEControl(tinyMCE.settings); + + inst.editorId = id; + this.instances[id] = inst; + + inst.onAdd(replace_element, form_element_name, target_document); +}; + +TinyMCE.prototype.triggerSave = function(skip_cleanup, skip_callback) { + // Cleanup and set all form fields + for (var n in tinyMCE.instances) { + var inst = tinyMCE.instances[n]; + if (!tinyMCE.isInstance(inst)) + continue; + + inst.switchSettings(); + + tinyMCE.settings['preformatted'] = false; + + // Default to false + if (typeof(skip_cleanup) == "undefined") + skip_cleanup = false; + + // Default to false + if (typeof(skip_callback) == "undefined") + skip_callback = false; + + tinyMCE._setHTML(inst.getDoc(), inst.getBody().innerHTML); + + // Remove visual aids when cleanup is disabled + if (inst.settings['cleanup'] == false) { + tinyMCE.handleVisualAid(inst.getBody(), true, false, inst); + tinyMCE._setEventsEnabled(inst.getBody(), true); + } + + tinyMCE._customCleanup(inst, "submit_content_dom", inst.contentWindow.document.body); + var htm = skip_cleanup ? inst.getBody().innerHTML : tinyMCE._cleanupHTML(inst, inst.getDoc(), this.settings, inst.getBody(), this.visualAid, true); + htm = tinyMCE._customCleanup(inst, "submit_content", htm); + + if (tinyMCE.settings["encoding"] == "xml" || tinyMCE.settings["encoding"] == "html") + htm = tinyMCE.convertStringToXML(htm); + + if (!skip_callback && tinyMCE.settings['save_callback'] != "") + var content = eval(tinyMCE.settings['save_callback'] + "(inst.formTargetElementId,htm,inst.getBody());"); + + // Use callback content if available + if ((typeof(content) != "undefined") && content != null) + htm = content; + + // Replace some weird entities (Bug: #1056343) + htm = tinyMCE.regexpReplace(htm, "(", "(", "gi"); + htm = tinyMCE.regexpReplace(htm, ")", ")", "gi"); + htm = tinyMCE.regexpReplace(htm, ";", ";", "gi"); + htm = tinyMCE.regexpReplace(htm, """, """, "gi"); + htm = tinyMCE.regexpReplace(htm, "^", "^", "gi"); + + if (inst.formElement) + inst.formElement.value = htm; + } +}; + +TinyMCE.prototype._setEventsEnabled = function(node, state) { + var events = new Array('onfocus','onblur','onclick','ondblclick', + 'onmousedown','onmouseup','onmouseover','onmousemove', + 'onmouseout','onkeypress','onkeydown','onkeydown','onkeyup'); + + var evs = tinyMCE.settings['event_elements'].split(','); + for (var y=0; y", "gi"); + content = tinyMCE.regexpReplace(content, "\r", "
    ", "gi"); + content = tinyMCE.regexpReplace(content, "\n", "
    ", "gi"); + } + + // Open closed anchors +// content = content.replace(new RegExp('', 'gi'), ''); + + // Call custom cleanup code + content = tinyMCE.storeAwayURLs(content); + content = tinyMCE._customCleanup(inst, "insert_to_editor", content); + + if (tinyMCE.isMSIE) { + // Ugly!!! + window.setInterval('try{tinyMCE.getCSSClasses(document.frames["' + editor_id + '"].document, "' + editor_id + '");}catch(e){}', 500); + + if (tinyMCE.settings["force_br_newlines"]) + document.frames[editor_id].document.styleSheets[0].addRule("p", "margin: 0px;"); + + var body = document.frames[editor_id].document.body; + + tinyMCE.addEvent(body, "beforepaste", TinyMCE.prototype.eventPatch); + tinyMCE.addEvent(body, "beforecut", TinyMCE.prototype.eventPatch); + + body.editorId = editor_id; + } + + content = tinyMCE.cleanupHTMLCode(content); + + // Fix for bug #958637 + if (!tinyMCE.isMSIE) { + var contentElement = inst.getDoc().createElement("body"); + var doc = inst.getDoc(); + + contentElement.innerHTML = content; + + // Remove weridness! + if (tinyMCE.isGecko && tinyMCE.settings['remove_lt_gt']) + content = content.replace(new RegExp('<>', 'g'), ""); + + if (tinyMCE.settings['cleanup_on_startup']) + tinyMCE.setInnerHTML(inst.getBody(), tinyMCE._cleanupHTML(inst, doc, this.settings, contentElement)); + else { + // Convert all strong/em to b/i + content = tinyMCE.regexpReplace(content, "", "", "gi"); + content = tinyMCE.regexpReplace(content, "", "", "gi"); + content = tinyMCE.regexpReplace(content, "", "", "gi"); + tinyMCE.setInnerHTML(inst.getBody(), content); + } + + inst.convertAllRelativeURLs(); + } else { + if (tinyMCE.settings['cleanup_on_startup']) { + tinyMCE._setHTML(inst.getDoc(), content); + + // Produces permission denied error in MSIE 5.5 + eval('try {tinyMCE.setInnerHTML(inst.getBody(), tinyMCE._cleanupHTML(inst, inst.contentDocument, this.settings, inst.getBody()));} catch(e) {}'); + } else + tinyMCE._setHTML(inst.getDoc(), content); + } + + // Fix for bug #957681 + //inst.getDoc().designMode = inst.getDoc().designMode; + + // Setup element references + var parentElm = document.getElementById(inst.editorId + '_parent'); + if (parentElm.lastChild.nodeName == "INPUT") + inst.formElement = tinyMCE.isGecko ? parentElm.firstChild : parentElm.lastChild; + else + inst.formElement = tinyMCE.isGecko ? parentElm.previousSibling : parentElm.nextSibling; + + tinyMCE.handleVisualAid(inst.getBody(), true, tinyMCE.settings['visual'], inst); + tinyMCE.executeCallback('setupcontent_callback', '_setupContent', 0, editor_id, inst.getBody(), inst.getDoc()); + + // Re-add design mode on mozilla + if (!tinyMCE.isMSIE) + TinyMCE.prototype.addEventHandlers(editor_id); + + // Add blur handler + if (tinyMCE.isMSIE) + tinyMCE.addEvent(inst.getBody(), "blur", TinyMCE.prototype.eventPatch); + + // Trigger node change, this call locks buttons for tables and so forth + tinyMCE.selectedInstance = inst; + tinyMCE.selectedElement = inst.contentWindow.document.body; + + if (!inst.isHidden()) + tinyMCE.triggerNodeChange(false, true); + + // Call custom DOM cleanup + tinyMCE._customCleanup(inst, "insert_to_editor_dom", inst.getBody()); + tinyMCE._customCleanup(inst, "setup_content_dom", inst.getBody()); + tinyMCE._setEventsEnabled(inst.getBody(), false); + tinyMCE.cleanupAnchors(inst.getDoc()); + + if (tinyMCE.getParam("convert_fonts_to_spans")) + tinyMCE.convertSpansToFonts(inst.getDoc()); + + inst.startContent = tinyMCE.trim(inst.getBody().innerHTML); + inst.undoLevels[inst.undoLevels.length] = inst.startContent; + + tinyMCE.operaOpacityCounter = -1; +}; + +TinyMCE.prototype.cleanupHTMLCode = function(s) { + s = s.replace(/

    /gi, '

     

    '); + s = s.replace(/

    \s*<\/p>/gi, '

     

    '); + + // Open closed tags like to +// tinyMCE.debug("f:" + s); + s = s.replace(/<(h[1-6]|p|div|address|pre|form|table|li|ol|ul|td|b|em|strong|i|strike|u|span|a|ul|ol|li|blockquote)([a-z]*)([^\\|>]*?)\/>/gi, '<$1$2$3>'); +// tinyMCE.debug("e:" + s); + + // Remove trailing space to + s = s.replace(new RegExp('\\s+> to + s = s.replace(/<(img|br|hr)(.*?)><\/(img|br|hr)>/gi, '<$1$2 />'); + + // Weird MSIE bug,


    breaks runtime? + if (tinyMCE.isMSIE) + s = s.replace(/


    <\/p>/gi, "
    "); + + // Convert relative anchors to absolute URLs ex: #something to file.htm#something + s = s.replace(new RegExp('(href=\"?)(\\s*?#)', 'gi'), '$1' + tinyMCE.settings['document_base_url'] + "#"); + + return s; +}; + +TinyMCE.prototype.storeAwayURLs = function(s) { + // Remove all mce_src, mce_href and replace them with new ones + s = s.replace(new RegExp('mce_src\\s*=\\s*\"[^ >\"]*\"', 'gi'), ''); + s = s.replace(new RegExp('mce_href\\s*=\\s*\"[^ >\"]*\"', 'gi'), ''); + s = s.replace(new RegExp('src\\s*=\\s*\"([^ >\"]*)\"', 'gi'), 'src="$1" mce_src="$1"'); + s = s.replace(new RegExp('href\\s*=\\s*\"([^ >\"]*)\"', 'gi'), 'href="$1" mce_href="$1"'); + + return s; +}; + +TinyMCE.prototype.cancelEvent = function(e) { + if (tinyMCE.isMSIE) { + e.returnValue = false; + e.cancelBubble = true; + } else + e.preventDefault(); +}; + +TinyMCE.prototype.removeTinyMCEFormElements = function(form_obj) { + // Check if form is valid + if (typeof(form_obj) == "undefined" || form_obj == null) + return; + + // If not a form, find the form + if (form_obj.nodeName != "FORM") { + if (form_obj.form) + form_obj = form_obj.form; + else + form_obj = tinyMCE.getParentElement(form_obj, "form"); + } + + // Still nothing + if (form_obj == null) + return; + + // Disable all UI form elements that TinyMCE created + for (var i=0; i'); + } + } + + // Return key pressed + if (tinyMCE.isMSIE && tinyMCE.settings['force_br_newlines'] && e.keyCode == 13) { + if (e.target.editorId) + tinyMCE.selectedInstance = tinyMCE.instances[e.target.editorId]; + + if (tinyMCE.selectedInstance) { + var sel = tinyMCE.selectedInstance.getDoc().selection; + var rng = sel.createRange(); + + if (tinyMCE.getParentElement(rng.parentElement(), "li") != null) + return false; + + // Cancel event + e.returnValue = false; + e.cancelBubble = true; + + // Insert BR element + rng.pasteHTML("
    "); + rng.collapse(false); + rng.select(); + + tinyMCE.execCommand("mceAddUndoLevel"); + tinyMCE.triggerNodeChange(false); + return false; + } + } + + // Backspace or delete + if (e.keyCode == 8 || e.keyCode == 46) { + tinyMCE.selectedElement = e.target; + tinyMCE.linkElement = tinyMCE.getParentElement(e.target, "a"); + tinyMCE.imgElement = tinyMCE.getParentElement(e.target, "img"); + tinyMCE.triggerNodeChange(false); + } + + return false; + break; + + case "keyup": + case "keydown": + if (e.target.editorId) + tinyMCE.selectedInstance = tinyMCE.instances[e.target.editorId]; + else + return; + + if (tinyMCE.selectedInstance) + tinyMCE.selectedInstance.switchSettings(); + + var inst = tinyMCE.selectedInstance; + + // Handle backspace + if (tinyMCE.isGecko && tinyMCE.settings['force_p_newlines'] && (e.keyCode == 8 || e.keyCode == 46) && !e.shiftKey) { + // Insert P element instead of BR + if (tinyMCE.selectedInstance._handleBackSpace(e.type)) { + // Cancel event + tinyMCE.execCommand("mceAddUndoLevel"); + e.preventDefault(); + return false; + } + } + + tinyMCE.selectedElement = null; + tinyMCE.selectedNode = null; + var elm = tinyMCE.selectedInstance.getFocusElement(); + tinyMCE.linkElement = tinyMCE.getParentElement(elm, "a"); + tinyMCE.imgElement = tinyMCE.getParentElement(elm, "img"); + tinyMCE.selectedElement = elm; + + // Update visualaids on tabs + if (tinyMCE.isGecko && e.type == "keyup" && e.keyCode == 9) + tinyMCE.handleVisualAid(tinyMCE.selectedInstance.getBody(), true, tinyMCE.settings['visual'], tinyMCE.selectedInstance); + + // Fix empty elements on return/enter, check where enter occured + if (tinyMCE.isMSIE && e.type == "keydown" && e.keyCode == 13) + tinyMCE.enterKeyElement = tinyMCE.selectedInstance.getFocusElement(); + + // Fix empty elements on return/enter + if (tinyMCE.isMSIE && e.type == "keyup" && e.keyCode == 13) { + var elm = tinyMCE.enterKeyElement; + if (elm) { + var re = new RegExp('^HR|IMG|BR$','g'); // Skip these + var dre = new RegExp('^H[1-6]$','g'); // Add double on these + + if (!elm.hasChildNodes() && !re.test(elm.nodeName)) { + if (dre.test(elm.nodeName)) + elm.innerHTML = "  "; + else + elm.innerHTML = " "; + } + } + } + + // Check if it's a position key + var keys = tinyMCE.posKeyCodes; + var posKey = false; + for (var i=0; i -1) + suffix = '?rnd=' + this.operaOpacityCounter++; + + element.src = tinyMCE.baseURL + "/themes/" + tinyMCE.getParam("theme") + "/images/opacity.png" + suffix; + element.style.backgroundImage = "url('" + element.mceOldSrc + "')"; + } else { + if (element.mceOldSrc) { + element.src = element.mceOldSrc; + element.parentNode.style.backgroundImage = ""; + element.mceOldSrc = null; + } + } + } + } +}; + +TinyMCE.prototype.restoreClass = function(element) { + if (element != null && element.oldClassName && !element.classLock) { + element.className = element.oldClassName; + element.oldClassName = null; + } +}; + +TinyMCE.prototype.setClassLock = function(element, lock_state) { + if (element != null) + element.classLock = lock_state; +}; + +TinyMCE.prototype.addEvent = function(obj, name, handler) { + if (tinyMCE.isMSIE) { + obj.attachEvent("on" + name, handler); + } else + obj.addEventListener(name, handler, false); +}; + +TinyMCE.prototype.submitPatch = function() { + tinyMCE.removeTinyMCEFormElements(this); + tinyMCE.triggerSave(); + this.mceOldSubmit(); + tinyMCE.isNotDirty = true; +}; + +TinyMCE.prototype.onLoad = function() { + for (var c=0; c 0) + return; + + if (val.indexOf('%') == -1) + val += 'px'; + break; + + case "vspace": + case "hspace": + elm.style.marginTop = val + "px"; + elm.style.marginBottom = val + "px"; + elm.removeAttribute(attrib); + return; + + case "align": + if (elm.nodeName == "IMG") { + if (tinyMCE.isMSIE) + elm.style.styleFloat = val; + else + elm.style.cssFloat = val; + } else + elm.style.textAlign = val; + + elm.removeAttribute(attrib); + return; + } + + if (val != '') { + eval('elm.style.' + style + ' = val;'); + elm.removeAttribute(attrib); + } + } + } else { + if (style == '') + return; + + var val = eval('elm.style.' + style) == '' ? tinyMCE.getAttrib(elm, attrib) : eval('elm.style.' + style); + val = val == null ? '' : '' + val; + + switch (attrib) { + // Always move background to style + case "background": + if (val.indexOf('url') == -1 && val != '') + val = "url('" + val + "');"; + + if (val != '') { + elm.style.backgroundImage = val; + elm.removeAttribute(attrib); + } + return; + + case "border": + case "width": + case "height": + val = val.replace('px', ''); + break; + + case "align": + if (tinyMCE.getAttrib(elm, 'align') == '') { + if (elm.nodeName == "IMG") { + if (tinyMCE.isMSIE && elm.style.styleFloat != '') { + val = elm.style.styleFloat; + style = 'styleFloat'; + } else if (tinyMCE.isGecko && elm.style.cssFloat != '') { + val = elm.style.cssFloat; + style = 'cssFloat'; + } + } + } + break; + } + + if (val != '') { + elm.removeAttribute(attrib); + elm.setAttribute(attrib, val); + eval('elm.style.' + style + ' = "";'); + } + } +}; + +TinyMCE.prototype._cleanupAttribute = function(valid_attributes, element_name, attribute_node, element_node) { + var attribName = attribute_node.nodeName.toLowerCase(); + var attribValue = attribute_node.nodeValue; + var attribMustBeValue = null; + var verified = false; + + // Mozilla attibute, remove them + if (attribName.indexOf('moz_') != -1) + return null; + + if (!tinyMCE.cleanup_on_save && (attribName == "mce_href" || attribName == "mce_src")) + return {name : attribName, value : attribValue}; + + // Verify attrib + if (tinyMCE.cleanup_verify_html && !verified) { + for (var i=1; i 1) + val = "url('" + eval(tinyMCE.getParam('urlconverter_callback') + "(m[1], null, true);") + "')"; + } + + // Force HEX colors + if (tinyMCE.getParam("force_hex_style_colors")) + val = tinyMCE.convertRGBToHex(val, true); + + if (val != "url('')") + str += key.toLowerCase() + ": " + val + "; "; + } + } + + if (new RegExp('; $').test(str)) + str = str.substring(0, str.length - 2); + + return str; +}; + +TinyMCE.prototype.convertRGBToHex = function(s, k) { + if (s.toLowerCase().indexOf('rgb') != -1) { + var re = new RegExp("(.*?)rgb\\s*?\\(\\s*?([0-9]+).*?,\\s*?([0-9]+).*?,\\s*?([0-9]+).*?\\)(.*?)", "gi"); + var rgb = s.replace(re, "$1,$2,$3,$4,$5").split(','); + if (rgb.length == 5) { + r = parseInt(rgb[1]).toString(16); + g = parseInt(rgb[2]).toString(16); + b = parseInt(rgb[3]).toString(16); + + r = r.length == 1 ? '0' + r : r; + g = g.length == 1 ? '0' + g : g; + b = b.length == 1 ? '0' + b : b; + + s = "#" + r + g + b; + + if (k) + s = rgb[0] + s + rgb[4]; + } + } + + return s; +}; + +TinyMCE.prototype.convertHexToRGB = function(s) { + if (s.indexOf('#') != -1) { + s = s.replace(new RegExp('[^0-9A-F]', 'gi'), ''); + return "rgb(" + parseInt(s.substring(0, 2), 16) + "," + parseInt(s.substring(2, 4), 16) + "," + parseInt(s.substring(4, 6), 16) + ")"; + } + + return s; +}; + +TinyMCE.prototype._verifyClass = function(node) { + // Sometimes the class gets set to null, weird Gecko bug? + if (tinyMCE.isGecko) { + var className = node.getAttribute('class'); + if (!className) + return false; + } + + // Trim CSS class + if (tinyMCE.isMSIE) + var className = node.getAttribute('className'); + + if (tinyMCE.cleanup_verify_css_classes && tinyMCE.cleanup_on_save) { + var csses = tinyMCE.getCSSClasses(); + nonDefinedCSS = true; + for (var c=0; c' + output; + } + } + + // Remove deprecated attributes + var re = new RegExp("^(TABLE|TD|TR)$"); + if (re.test(node.nodeName)) { + // Move attrib to style + if ((node.nodeName != "TABLE" || tinyMCE.cleanup_inline_styles) && (width = tinyMCE.getAttrib(node, "width")) != '') { + node.style.width = width.indexOf('%') != -1 ? width : width.replace(/[^0-9]/gi, '') + "px"; + node.removeAttribute("width"); + } + + // Is table and not inline + if ((node.nodeName == "TABLE" && !tinyMCE.cleanup_inline_styles) && node.style.width != '') { + tinyMCE.setAttrib(node, "width", node.style.width.replace('px','')); + node.style.width = ''; + } + + // Move attrib to style + if ((height = tinyMCE.getAttrib(node, "height")) != '') { + height = "" + height; // Force string + node.style.height = height.indexOf('%') != -1 ? height : height.replace(/[^0-9]/gi, '') + "px"; + node.removeAttribute("height"); + } + } + + // Handle inline/outline styles + if (tinyMCE.cleanup_inline_styles) { + var re = new RegExp("^(TABLE|TD|TR|IMG|HR)$"); + if (re.test(node.nodeName) && tinyMCE.getAttrib(node, "class").indexOf('mceItem') == -1) { + tinyMCE._moveStyle(node, 'width', 'width'); + tinyMCE._moveStyle(node, 'height', 'height'); + tinyMCE._moveStyle(node, 'borderWidth', 'border'); + tinyMCE._moveStyle(node, '', 'vspace'); + tinyMCE._moveStyle(node, '', 'hspace'); + tinyMCE._moveStyle(node, 'textAlign', 'align'); + tinyMCE._moveStyle(node, 'backgroundColor', 'bgColor'); + tinyMCE._moveStyle(node, 'borderColor', 'borderColor'); + tinyMCE._moveStyle(node, 'backgroundImage', 'background'); + + // Refresh element in old MSIE + if (tinyMCE.isMSIE5) + node.outerHTML = node.outerHTML; + } else if (tinyMCE.isBlockElement(node)) + tinyMCE._moveStyle(node, 'textAlign', 'align'); + + if (node.nodeName == "FONT") + tinyMCE._moveStyle(node, 'color', 'color'); + } + + // Set attrib data + if (elementValidAttribs) { + for (var a=1; a" + node.innerHTML + ""; + + // Remove empty tables + if (elementName == "table" && !node.hasChildNodes()) + return ""; + + // Handle element attributes + if (node.attributes.length > 0) { + var lastAttrib = ""; + + for (var i=0; i" + this.convertStringToXML(String.fromCharCode(160)) + ""; + + // Is MSIE script element + if (tinyMCE.isMSIE && elementName == "script") + return "<" + elementName + elementAttribs + ">" + node.text + ""; + + // Clean up children + if (node.hasChildNodes()) { + // If not empty span + if (!(elementName == "span" && elementAttribs == "" && tinyMCE.getParam("trim_span_elements"))) { + // Force BR + if (elementName == "p" && tinyMCE.cleanup_force_br_newlines) + output += ""; + else + output += "<" + elementName + elementAttribs + ">"; + } + + for (var i=0; i"; + } + } else { + if (!nonEmptyTag) { + if (openTag) + output += "<" + elementName + elementAttribs + ">"; + else + output += "<" + elementName + elementAttribs + " />"; + } + } + + return output; + + case 3: // Text + // Do not convert script elements + if (node.parentNode.nodeName == "SCRIPT" || node.parentNode.nodeName == "NOSCRIPT" || node.parentNode.nodeName == "STYLE") + return node.nodeValue; + + return this.convertStringToXML(node.nodeValue); + + case 8: // Comment + return ""; + + default: // Unknown + return "[UNKNOWN NODETYPE " + node.nodeType + "]"; + } +}; + +TinyMCE.prototype.convertStringToXML = function(html_data) { + var output = ""; + + for (var i=0; i 127) + output += '&#' + chr + ";"; + else + output += String.fromCharCode(chr); + + continue; + } + + // Raw entities + if (tinyMCE.settings['entity_encoding'] == "raw") { + output += String.fromCharCode(chr); + continue; + } + + // Named entities + if (typeof(tinyMCE.settings['cleanup_entities']["c" + chr]) != 'undefined' && tinyMCE.settings['cleanup_entities']["c" + chr] != '') + output += '&' + tinyMCE.settings['cleanup_entities']["c" + chr] + ';'; + else + output += '' + String.fromCharCode(chr); + } + + return output; +}; + +TinyMCE.prototype._getCleanupElementName = function(chunk) { + var pos; + + if (chunk.charAt(0) == '+') + chunk = chunk.substring(1); + + if (chunk.charAt(0) == '-') + chunk = chunk.substring(1); + + if ((pos = chunk.indexOf('/')) != -1) + chunk = chunk.substring(0, pos); + + if ((pos = chunk.indexOf('[')) != -1) + chunk = chunk.substring(0, pos); + + return chunk; +}; + +TinyMCE.prototype._initCleanup = function() { + // Parse valid elements and attributes + var validElements = tinyMCE.settings["valid_elements"]; + validElements = validElements.split(','); + + // Handle extended valid elements + var extendedValidElements = tinyMCE.settings["extended_valid_elements"]; + extendedValidElements = extendedValidElements.split(','); + for (var i=0; i/gi, '>'); + + return html; + } + + if (on_save && tinyMCE.getParam("convert_fonts_to_spans")) + tinyMCE.convertFontsToSpans(doc); + + // Call custom cleanup code + tinyMCE._customCleanup(inst, on_save ? "get_from_editor_dom" : "insert_to_editor_dom", doc.body); + + // Move bgcolor to style + var n = doc.getElementsByTagName("font"); + for (var i=0; i[ \n\r]*[ \n\r]*

    ', '
    ', 'gi')); + tinyMCE.setInnerHTML(element, tinyMCE.regexpReplace(element.innerHTML, '', '', 'gi')); + } + + var html = this.cleanupNode(element); + + if (tinyMCE.settings['debug']) + tinyMCE.debug("Cleanup process executed in: " + (new Date().getTime()-startTime) + " ms."); + + // Remove pesky HR paragraphs and other crap + html = tinyMCE.regexpReplace(html, '


    ', '
    '); + html = tinyMCE.regexpReplace(html, '

     


     

    ', '
    '); + html = tinyMCE.regexpReplace(html, '\\s*
    \\s*', ' '); + html = tinyMCE.regexpReplace(html, '

    \\s*
    \\s*

    ', '

     

    '); + html = tinyMCE.regexpReplace(html, '

    \\s* \\s*
    \\s* \\s*

    ', '

     

    '); + html = tinyMCE.regexpReplace(html, '

    \\s* \\s*
    \\s*

    ', '

     

    '); + html = tinyMCE.regexpReplace(html, '

    \\s*
    \\s* \\s*

    ', '

     

    '); + + // Remove empty anchors + html = html.replace(new RegExp('(.*?)', 'gi'), '$1'); + + // Remove some mozilla crap + if (!tinyMCE.isMSIE) + html = html.replace(new RegExp('', 'g'), ""); + + if (tinyMCE.settings['remove_linebreaks']) + html = html.replace(new RegExp('\r|\n', 'g'), ' '); + + if (tinyMCE.getParam('apply_source_formatting')) { + html = html.replace(new RegExp('<(p|div)([^>]*)>', 'g'), "\n<$1$2>\n"); + html = html.replace(new RegExp('<\/(p|div)([^>]*)>', 'g'), "\n\n"); + html = html.replace(new RegExp('
    ', 'g'), "
    \n"); + } + + if (tinyMCE.settings['force_br_newlines']) { + var re = new RegExp('

     

    ', 'g'); + html = html.replace(re, "
    "); + } + + if (tinyMCE.isGecko && tinyMCE.settings['remove_lt_gt']) { + // Remove weridness! + var re = new RegExp('<>', 'g'); + html = html.replace(re, ""); + } + + // Call custom cleanup code + html = tinyMCE._customCleanup(inst, on_save ? "get_from_editor" : "insert_to_editor", html); + + // Emtpy node, return empty + var chk = tinyMCE.regexpReplace(html, "[ \t\r\n]", "").toLowerCase(); + if (chk == "
    " || chk == "
    " || chk == "

     

    " || chk == "

     

    " || chk == "

    ") + html = ""; + + if (tinyMCE.settings["preformatted"]) + return "
    " + html + "
    "; + + return html; +}; + +TinyMCE.prototype.insertLink = function(href, target, title, onclick, style_class) { + tinyMCE.execCommand('mceBeginUndoLevel'); + + if (this.selectedInstance && this.selectedElement && this.selectedElement.nodeName.toLowerCase() == "img") { + var doc = this.selectedInstance.getDoc(); + var linkElement = tinyMCE.getParentElement(this.selectedElement, "a"); + var newLink = false; + + if (!linkElement) { + linkElement = doc.createElement("a"); + newLink = true; + } + + var mhref = href; + var thref = eval(tinyMCE.settings['urlconverter_callback'] + "(href, linkElement);"); + mhref = tinyMCE.getParam('convert_urls') ? href : mhref; + + tinyMCE.setAttrib(linkElement, 'href', thref); + tinyMCE.setAttrib(linkElement, 'mce_href', mhref); + tinyMCE.setAttrib(linkElement, 'target', target); + tinyMCE.setAttrib(linkElement, 'title', title); + tinyMCE.setAttrib(linkElement, 'onclick', onclick); + tinyMCE.setAttrib(linkElement, 'class', style_class); + + if (newLink) { + linkElement.appendChild(this.selectedElement.cloneNode(true)); + this.selectedElement.parentNode.replaceChild(linkElement, this.selectedElement); + } + + return; + } + + if (!this.linkElement && this.selectedInstance) { + if (tinyMCE.isSafari) { + tinyMCE.execCommand("mceInsertContent", false, '' + this.selectedInstance.getSelectedHTML() + ''); + } else + this.selectedInstance.contentDocument.execCommand("createlink", false, tinyMCE.uniqueURL); + + tinyMCE.linkElement = this.getElementByAttributeValue(this.selectedInstance.contentDocument.body, "a", "href", tinyMCE.uniqueURL); + + var elementArray = this.getElementsByAttributeValue(this.selectedInstance.contentDocument.body, "a", "href", tinyMCE.uniqueURL); + + for (var i=0; i'; + + tinyMCE.execCommand("mceInsertContent", false, html); + } else { + if (!this.imgElement && this.selectedInstance) { + if (tinyMCE.isSafari) + tinyMCE.execCommand("mceInsertContent", false, ''); + else + this.selectedInstance.contentDocument.execCommand("insertimage", false, tinyMCE.uniqueURL); + + tinyMCE.imgElement = this.getElementByAttributeValue(this.selectedInstance.contentDocument.body, "img", "src", tinyMCE.uniqueURL); + } + } + + if (this.imgElement) { + var needsRepaint = false; + var msrc = src; + + src = eval(tinyMCE.settings['urlconverter_callback'] + "(src, tinyMCE.imgElement);"); + + if (tinyMCE.getParam('convert_urls')) + msrc = src; + + if (onmouseover && onmouseover != "") + onmouseover = "this.src='" + eval(tinyMCE.settings['urlconverter_callback'] + "(onmouseover, tinyMCE.imgElement);") + "';"; + + if (onmouseout && onmouseout != "") + onmouseout = "this.src='" + eval(tinyMCE.settings['urlconverter_callback'] + "(onmouseout, tinyMCE.imgElement);") + "';"; + + // Use alt as title if it's undefined + if (typeof(title) == "undefined") + title = alt; + + if (width != this.imgElement.getAttribute("width") || height != this.imgElement.getAttribute("height") || align != this.imgElement.getAttribute("align")) + needsRepaint = true; + + tinyMCE.setAttrib(this.imgElement, 'src', src); + tinyMCE.setAttrib(this.imgElement, 'mce_src', msrc); + tinyMCE.setAttrib(this.imgElement, 'alt', alt); + tinyMCE.setAttrib(this.imgElement, 'title', title); + tinyMCE.setAttrib(this.imgElement, 'align', align); + tinyMCE.setAttrib(this.imgElement, 'border', border, true); + tinyMCE.setAttrib(this.imgElement, 'hspace', hspace, true); + tinyMCE.setAttrib(this.imgElement, 'vspace', vspace, true); + tinyMCE.setAttrib(this.imgElement, 'width', width, true); + tinyMCE.setAttrib(this.imgElement, 'height', height, true); + tinyMCE.setAttrib(this.imgElement, 'onmouseover', onmouseover); + tinyMCE.setAttrib(this.imgElement, 'onmouseout', onmouseout); + + // Fix for bug #989846 - Image resize bug + if (width && width != "") + this.imgElement.style.pixelWidth = width; + + if (height && height != "") + this.imgElement.style.pixelHeight = height; + + if (needsRepaint) + tinyMCE.selectedInstance.repaint(); + } + + tinyMCE.execCommand('mceEndUndoLevel'); +}; + +TinyMCE.prototype.getElementByAttributeValue = function(node, element_name, attrib, value) { + var elements = this.getElementsByAttributeValue(node, element_name, attrib, value); + if (elements.length == 0) + return null; + + return elements[0]; +}; + +TinyMCE.prototype.getElementsByAttributeValue = function(node, element_name, attrib, value) { + var elements = new Array(); + + if (node && node.nodeName.toLowerCase() == element_name) { + if (node.getAttribute(attrib) && node.getAttribute(attrib).indexOf(value) != -1) + elements[elements.length] = node; + } + + if (node && node.hasChildNodes()) { + for (var x=0, n=node.childNodes.length; x= strTok2.length) { + for (var i=0; i= strTok2.length || strTok1[i] != strTok2[i]) { + breakPoint = i + 1; + break; + } + } + } + + if (strTok1.length < strTok2.length) { + for (var i=0; i= strTok1.length || strTok1[i] != strTok2[i]) { + breakPoint = i + 1; + break; + } + } + } + + if (breakPoint == 1) + return targetURL.path; + + for (var i=0; i<(strTok1.length-(breakPoint-1)); i++) + outPath += "../"; + + for (var i=breakPoint-1; i=0; i--) { + if (baseURLParts[i].length == 0) + continue; + + newBaseURLParts[newBaseURLParts.length] = baseURLParts[i]; + } + baseURLParts = newBaseURLParts.reverse(); + + // Merge relURLParts chunks + var newRelURLParts = new Array(); + var numBack = 0; + for (var i=relURLParts.length-1; i>=0; i--) { + if (relURLParts[i].length == 0 || relURLParts[i] == ".") + continue; + + if (relURLParts[i] == '..') { + numBack++; + continue; + } + + if (numBack > 0) { + numBack--; + continue; + } + + newRelURLParts[newRelURLParts.length] = relURLParts[i]; + } + + relURLParts = newRelURLParts.reverse(); + + // Remove end from absolute path + var len = baseURLParts.length-numBack; + var absPath = (len <= 0 ? "" : "/") + baseURLParts.slice(0, len).join('/') + "/" + relURLParts.join('/'); + var start = "", end = ""; + + // Build output URL + relURL.protocol = baseURL.protocol; + relURL.host = baseURL.host; + relURL.port = baseURL.port; + + // Re-add trailing slash if it's removed + if (relURL.path.charAt(relURL.path.length-1) == "/") + absPath += "/"; + + relURL.path = absPath; + + return TinyMCE.prototype.serializeURL(relURL); +}; + +TinyMCE.prototype.getParam = function(name, default_value, strip_whitespace, split_chr) { + var value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name]; + + // Fix bool values + if (value == "true" || value == "false") + return (value == "true"); + + if (strip_whitespace) + value = tinyMCE.regexpReplace(value, "[ \t\r\n]", ""); + + if (typeof(split_chr) != "undefined" && split_chr != null) { + value = value.split(split_chr); + var outArray = new Array(); + + for (var i=0; i 0); + + if (tinyMCE.settings['custom_undo_redo']) { + undoIndex = inst.undoIndex; + undoLevels = inst.undoLevels.length; + } + + tinyMCE.executeCallback('handleNodeChangeCallback', '_handleNodeChange', 0, editorId, elm, undoIndex, undoLevels, inst.visualAid, anySelection, setup_content); + } + } + + if (this.selectedInstance && (typeof(focus) == "undefined" || focus)) + this.selectedInstance.contentWindow.focus(); +}; + +TinyMCE.prototype._customCleanup = function(inst, type, content) { + // Call custom cleanup + var customCleanup = tinyMCE.settings['cleanup_callback']; + if (customCleanup != "" && eval("typeof(" + customCleanup + ")") != "undefined") + content = eval(customCleanup + "(type, content, inst);"); + + // Trigger plugin cleanups + var plugins = tinyMCE.getParam('plugins', '', true, ','); + for (var i=0; i 0) + className += " "; + + className += classNames[i]; + } + + return className; +}; + +TinyMCE.prototype.handleVisualAid = function(el, deep, state, inst) { + if (!el) + return; + + var tableElement = null; + + switch (el.nodeName) { + case "TABLE": + var oldW = el.style.width; + var oldH = el.style.height; + var bo = tinyMCE.getAttrib(el, "border"); + + bo = bo == "" || bo == "0" ? true : false; + + tinyMCE.setAttrib(el, "class", tinyMCE.getVisualAidClass(tinyMCE.getAttrib(el, "class"), state && bo)); + + el.style.width = oldW; + el.style.height = oldH; + + for (var y=0; y 0) { + tinyMCE.setAttrib(s[i], 'size', fSize); + s[i].style.fontSize = ''; + } + + var fFace = s[i].style.fontFamily; + if (fFace != null && fFace != "") { + tinyMCE.setAttrib(s[i], 'face', fFace); + s[i].style.fontFamily = ''; + } + + var fColor = s[i].style.color; + if (fColor != null && fColor != "") { + tinyMCE.setAttrib(s[i], 'color', tinyMCE.convertRGBToHex(fColor)); + s[i].style.color = ''; + } + } +}; + +TinyMCE.prototype.convertFontsToSpans = function(doc) { + var sizes = tinyMCE.getParam('font_size_style_values').replace(/\s+/, '').split(','); + + var h = doc.body.innerHTML; + h = h.replace(/ 0 && fSize < 8) { + if (fsClasses != null) + tinyMCE.setAttrib(s[i], 'class', fsClasses[fSize-1]); + else + s[i].style.fontSize = sizes[fSize-1]; + } + + s[i].removeAttribute('size'); + } + + if (fFace != "") { + s[i].style.fontFamily = fFace; + s[i].removeAttribute('face'); + } + + if (fColor != "") { + s[i].style.color = fColor; + s[i].removeAttribute('color'); + } + } +}; + +/* +TinyMCE.prototype.applyClassesToFonts = function(doc, size) { + var f = doc.getElementsByTagName("font"); + for (var i=0; i=0; x--) + tinyMCE.insertAfter(cn[x], an[i]); + } + } +}; + +TinyMCE.prototype._setHTML = function(doc, html_content) { + // Force closed anchors open + //html_content = html_content.replace(new RegExp('', 'gi'), ''); + + html_content = tinyMCE.cleanupHTMLCode(html_content); + + // Try innerHTML if it fails use pasteHTML in MSIE + try { + tinyMCE.setInnerHTML(doc.body, html_content); + } catch (e) { + if (this.isMSIE) + doc.body.createTextRange().pasteHTML(html_content); + } + + // Content duplication bug fix + if (tinyMCE.isMSIE && tinyMCE.settings['fix_content_duplication']) { + // Remove P elements in P elements + var paras = doc.getElementsByTagName("P"); + for (var i=0; i<\/o:p>", "
    "); + html = tinyMCE.regexpReplace(html, " <\/o:p>", ""); + html = tinyMCE.regexpReplace(html, "", ""); + html = tinyMCE.regexpReplace(html, "

    <\/p>", ""); + html = tinyMCE.regexpReplace(html, "

    <\/p>\r\n

    <\/p>", ""); + html = tinyMCE.regexpReplace(html, "

     <\/p>", "
    "); + html = tinyMCE.regexpReplace(html, "

    \s*(

    \s*)?", "

    "); + html = tinyMCE.regexpReplace(html, "<\/p>\s*(<\/p>\s*)?", "

    "); + } + + // Always set the htmlText output + tinyMCE.setInnerHTML(doc.body, html); + } + + tinyMCE.cleanupAnchors(doc); + + if (tinyMCE.getParam("convert_fonts_to_spans")) + tinyMCE.convertSpansToFonts(doc); +}; + +TinyMCE.prototype.getImageSrc = function(str) { + var pos = -1; + + if (!str) + return ""; + + if ((pos = str.indexOf('this.src=')) != -1) { + var src = str.substring(pos + 10); + + src = src.substring(0, src.indexOf('\'')); + + return src; + } + + return ""; +}; + +TinyMCE.prototype._getElementById = function(element_id) { + var elm = document.getElementById(element_id); + if (!elm) { + // Check for element in forms + for (var j=0; j 0) { + for (var x=0; x 0) + tinyMCE.cssClasses = output; + + return output; +}; + +TinyMCE.prototype.regexpReplace = function(in_str, reg_exp, replace_str, opts) { + if (in_str == null) + return in_str; + + if (typeof(opts) == "undefined") + opts = 'g'; + + var re = new RegExp(reg_exp, opts); + return in_str.replace(re, replace_str); +}; + +TinyMCE.prototype.trim = function(str) { + return str.replace(/^\s*|\s*$/g, ""); +}; + +TinyMCE.prototype.cleanupEventStr = function(str) { + str = "" + str; + str = str.replace('function anonymous()\n{\n', ''); + str = str.replace('\n}', ''); + str = str.replace(/^return true;/gi, ''); // Remove event blocker + + return str; +}; + +TinyMCE.prototype.getAbsPosition = function(node) { + var pos = new Object(); + + pos.absLeft = pos.absTop = 0; + + var parentNode = node; + while (parentNode) { + pos.absLeft += parentNode.offsetLeft; + pos.absTop += parentNode.offsetTop; + + parentNode = parentNode.offsetParent; + } + + return pos; +}; + +TinyMCE.prototype.getControlHTML = function(control_name) { + var themePlugins = tinyMCE.getParam('plugins', '', true, ','); + var templateFunction; + + // Is it defined in any plugins + for (var i=themePlugins.length; i>=0; i--) { + templateFunction = 'TinyMCE_' + themePlugins[i] + "_getControlHTML"; + if (eval("typeof(" + templateFunction + ")") != 'undefined') { + var html = eval(templateFunction + "('" + control_name + "');"); + if (html != "") + return tinyMCE.replaceVar(html, "pluginurl", tinyMCE.baseURL + "/plugins/" + themePlugins[i]); + } + } + + return eval('TinyMCE_' + tinyMCE.settings['theme'] + "_getControlHTML" + "('" + control_name + "');"); +}; + +TinyMCE.prototype._themeExecCommand = function(editor_id, element, command, user_interface, value) { + var themePlugins = tinyMCE.getParam('plugins', '', true, ','); + var templateFunction; + + // Is it defined in any plugins + for (var i=themePlugins.length; i>=0; i--) { + templateFunction = 'TinyMCE_' + themePlugins[i] + "_execCommand"; + if (eval("typeof(" + templateFunction + ")") != 'undefined') { + if (eval(templateFunction + "(editor_id, element, command, user_interface, value);")) + return true; + } + } + + // Theme funtion + templateFunction = 'TinyMCE_' + tinyMCE.settings['theme'] + "_execCommand"; + if (eval("typeof(" + templateFunction + ")") != 'undefined') + return eval(templateFunction + "(editor_id, element, command, user_interface, value);"); + + // Pass to normal + return false; +}; + +TinyMCE.prototype._getThemeFunction = function(suffix, skip_plugins) { + if (skip_plugins) + return 'TinyMCE_' + tinyMCE.settings['theme'] + suffix; + + var themePlugins = tinyMCE.getParam('plugins', '', true, ','); + var templateFunction; + + // Is it defined in any plugins + for (var i=themePlugins.length; i>=0; i--) { + templateFunction = 'TinyMCE_' + themePlugins[i] + suffix; + if (eval("typeof(" + templateFunction + ")") != 'undefined') + return templateFunction; + } + + return 'TinyMCE_' + tinyMCE.settings['theme'] + suffix; +}; + + +TinyMCE.prototype.isFunc = function(func_name) { + if (func_name == null || func_name == "") + return false; + + return eval("typeof(" + func_name + ")") != "undefined"; +}; + +TinyMCE.prototype.exec = function(func_name, args) { + var str = func_name + '('; + + // Add all arguments + for (var i=3; i 1 && tinyMCE.currentConfig != this.settings['index']) { + tinyMCE.settings = this.settings; + tinyMCE.currentConfig = this.settings['index']; + } +}; + +TinyMCEControl.prototype.convertAllRelativeURLs = function() { + var body = this.getBody(); + + // Convert all image URL:s to absolute URL + var elms = body.getElementsByTagName("img"); + for (var i=0; i 0) + rng.selectNodeContents(nodes[0]); + else + rng.selectNodeContents(node); + } else + rng.selectNode(node); + + if (collapse) { + // Special treatment of textnode collapse + if (!to_start && node.nodeType == 3) { + rng.setStart(node, node.nodeValue.length); + rng.setEnd(node, node.nodeValue.length); + } else + rng.collapse(to_start); + } + + sel.removeAllRanges(); + sel.addRange(rng); + } + + this.scrollToNode(node); + + // Set selected element + tinyMCE.selectedElement = null; + if (node.nodeType == 1) + tinyMCE.selectedElement = node; +}; + +TinyMCEControl.prototype.scrollToNode = function(node) { + // Scroll to node position + var pos = tinyMCE.getAbsPosition(node); + var doc = this.getDoc(); + var scrollX = doc.body.scrollLeft + doc.documentElement.scrollLeft; + var scrollY = doc.body.scrollTop + doc.documentElement.scrollTop; + var height = tinyMCE.isMSIE ? document.getElementById(this.editorId).style.pixelHeight : this.targetElement.clientHeight; + + // Only scroll if out of visible area + if (!tinyMCE.settings['auto_resize'] && !(pos.absTop > scrollY && pos.absTop < (scrollY - 25 + height))) + this.contentWindow.scrollTo(pos.absLeft, pos.absTop - height + 25); +}; + +TinyMCEControl.prototype.getBody = function() { + return this.getDoc().body; +}; + +TinyMCEControl.prototype.getDoc = function() { + return this.contentWindow.document; +}; + +TinyMCEControl.prototype.getWin = function() { + return this.contentWindow; +}; + +TinyMCEControl.prototype.getSel = function() { + if (tinyMCE.isMSIE && !tinyMCE.isOpera) + return this.getDoc().selection; + + var sel = this.contentWindow.getSelection(); + + // Fake getRangeAt + if (tinyMCE.isSafari && !sel.getRangeAt) { + var newSel = new Object(); + var doc = this.getDoc(); + + function getRangeAt(idx) { + var rng = new Object(); + + rng.startContainer = this.focusNode; + rng.endContainer = this.anchorNode; + rng.commonAncestorContainer = this.focusNode; + rng.createContextualFragment = function (html) { + // Seems to be a tag + if (html.charAt(0) == '<') { + var elm = doc.createElement("div"); + + elm.innerHTML = html; + + return elm.firstChild; + } + + return doc.createTextNode("UNSUPPORTED, DUE TO LIMITATIONS IN SAFARI!"); + }; + + rng.deleteContents = function () { + doc.execCommand("Delete", false, ""); + }; + + return rng; + } + + // Patch selection + + newSel.focusNode = sel.baseNode; + newSel.focusOffset = sel.baseOffset; + newSel.anchorNode = sel.extentNode; + newSel.anchorOffset = sel.extentOffset; + newSel.getRangeAt = getRangeAt; + newSel.text = "" + sel; + newSel.realSelection = sel; + + newSel.toString = function () {return this.text;}; + + return newSel; + } + + return sel; +}; + +TinyMCEControl.prototype.getRng = function() { + var sel = this.getSel(); + if (sel == null) + return null; + + if (tinyMCE.isMSIE && !tinyMCE.isOpera) + return sel.createRange(); + + if (tinyMCE.isSafari) { + var rng = this.getDoc().createRange(); + var sel = this.getSel().realSelection; + + rng.setStart(sel.baseNode, sel.baseOffset); + rng.setEnd(sel.extentNode, sel.extentOffset); + + return rng; + } + + return this.getSel().getRangeAt(0); +}; + +TinyMCEControl.prototype._insertPara = function(e) { + function isEmpty(para) { + function isEmptyHTML(html) { + return html.replace(new RegExp('[ \t\r\n]+', 'g'), '').toLowerCase() == ""; + } + + // Check for images + if (para.getElementsByTagName("img").length > 0) + return false; + + // Check for tables + if (para.getElementsByTagName("table").length > 0) + return false; + + // Check for HRs + if (para.getElementsByTagName("hr").length > 0) + return false; + + // Check all textnodes + var nodes = tinyMCE.getNodeTree(para, new Array(), 3); + for (var i=0; i <" + blockName + "> "; + paraAfter = body.childNodes[1]; + } + + this.selectNode(paraAfter, true, true); + + return true; + } + + // Place first part within new paragraph + if (startChop.nodeName == blockName) + rngBefore.setStart(startChop, 0); + else + rngBefore.setStartBefore(startChop); + + rngBefore.setEnd(startNode, startOffset); + paraBefore.appendChild(rngBefore.cloneContents()); + + // Place secound part within new paragraph + rngAfter.setEndAfter(endChop); + rngAfter.setStart(endNode, endOffset); + var contents = rngAfter.cloneContents(); + + if (contents.firstChild && contents.firstChild.nodeName == blockName) { +/* var nodes = contents.firstChild.childNodes; + for (var i=0; i 0) + rng.pasteHTML('
    ' + rng.htmlText + "
    "); + + tinyMCE.triggerNodeChange(); + return; + } + } + } + + switch (command) { + case "mceRepaint": + this.repaint(); + return true; + + case "mceStoreSelection": + this.selectionBookmark = this.getBookmark(); + return true; + + case "mceRestoreSelection": + this.moveToBookmark(this.selectionBookmark); + return true; + + case "InsertUnorderedList": + case "InsertOrderedList": + var tag = (command == "InsertUnorderedList") ? "ul" : "ol"; + + if (tinyMCE.isSafari) + this.execCommand("mceInsertContent", false, "<" + tag + ">
  •  
  • <" + tag + ">"); + else + this.getDoc().execCommand(command, user_interface, value); + + tinyMCE.triggerNodeChange(); + break; + + case "Strikethrough": + if (tinyMCE.isSafari) + this.execCommand("mceInsertContent", false, "" + this.getSelectedHTML() + ""); + else + this.getDoc().execCommand(command, user_interface, value); + + tinyMCE.triggerNodeChange(); + break; + + case "mceSelectNode": + this.selectNode(value); + tinyMCE.triggerNodeChange(); + tinyMCE.selectedNode = value; + break; + + case "FormatBlock": + if (value == null || value == "") { + var elm = tinyMCE.getParentElement(this.getFocusElement(), "p,div,h1,h2,h3,h4,h5,h6,pre,address"); + + if (elm) + this.execCommand("mceRemoveNode", false, elm); + } else + this.getDoc().execCommand("FormatBlock", false, value); + + tinyMCE.triggerNodeChange(); + + break; + + case "mceRemoveNode": + if (!value) + value = tinyMCE.getParentElement(this.getFocusElement()); + + if (tinyMCE.isMSIE) { + value.outerHTML = value.innerHTML; + } else { + var rng = value.ownerDocument.createRange(); + rng.setStartBefore(value); + rng.setEndAfter(value); + rng.deleteContents(); + rng.insertNode(rng.createContextualFragment(value.innerHTML)); + } + + tinyMCE.triggerNodeChange(); + + break; + + case "mceSelectNodeDepth": + var parentNode = this.getFocusElement(); + for (var i=0; parentNode; i++) { + if (parentNode.nodeName.toLowerCase() == "body") + break; + + if (parentNode.nodeName.toLowerCase() == "#text") { + i--; + parentNode = parentNode.parentNode; + continue; + } + + if (i == value) { + this.selectNode(parentNode, false); + tinyMCE.triggerNodeChange(); + tinyMCE.selectedNode = parentNode; + return; + } + + parentNode = parentNode.parentNode; + } + + break; + + case "SetStyleInfo": + var rng = this.getRng(); + var sel = this.getSel(); + var scmd = value['command']; + var sname = value['name']; + var svalue = value['value'] == null ? '' : value['value']; + //var svalue = value['value'] == null ? '' : value['value']; + var wrapper = value['wrapper'] ? value['wrapper'] : "span"; + var parentElm = null; + var invalidRe = new RegExp("^BODY|HTML$", "g"); + var invalidParentsRe = tinyMCE.settings['merge_styles_invalid_parents'] != '' ? new RegExp(tinyMCE.settings['merge_styles_invalid_parents'], "gi") : null; + + // Whole element selected check + if (tinyMCE.isMSIE) { + // Control range + if (rng.item) + parentElm = rng.item(0); + else { + var pelm = rng.parentElement(); + var prng = doc.selection.createRange(); + prng.moveToElementText(pelm); + + if (rng.htmlText == prng.htmlText || rng.boundingWidth == 0) { + if (invalidParentsRe == null || !invalidParentsRe.test(pelm.nodeName)) + parentElm = pelm; + } + } + } else { + var felm = this.getFocusElement(); + if (sel.isCollapsed || (/td|tr|tbody|table/ig.test(felm.nodeName) && sel.anchorNode == felm.parentNode)) + parentElm = felm; + } + + // Whole element selected + if (parentElm && !invalidRe.test(parentElm.nodeName)) { + if (scmd == "setstyle") + tinyMCE.setStyleAttrib(parentElm, sname, svalue); + + if (scmd == "setattrib") + tinyMCE.setAttrib(parentElm, sname, svalue); + + if (scmd == "removeformat") { + parentElm.style.cssText = ''; + tinyMCE.setAttrib(parentElm, 'class', ''); + } + + // Remove style/attribs from all children + var ch = tinyMCE.getNodeTree(parentElm, new Array(), 1); + for (var z=0; z=0; i--) { + var elm = nodes[i]; + var isNew = tinyMCE.getAttrib(elm, "mce_new") == "true"; + + elm.removeAttribute("mce_new"); + + // Is only child a element + if (elm.childNodes && elm.childNodes.length == 1 && elm.childNodes[0].nodeType == 1) { + //tinyMCE.debug("merge1" + isNew); + this._mergeElements(scmd, elm, elm.childNodes[0], isNew); + continue; + } + + // Is I the only child + if (elm.parentNode.childNodes.length == 1 && !invalidRe.test(elm.nodeName) && !invalidRe.test(elm.parentNode.nodeName)) { + //tinyMCE.debug("merge2" + isNew + "," + elm.nodeName + "," + elm.parentNode.nodeName); + if (invalidParentsRe == null || !invalidParentsRe.test(elm.parentNode.nodeName)) + this._mergeElements(scmd, elm.parentNode, elm, false); + } + } + + // Remove empty wrappers + var nodes = doc.getElementsByTagName(wrapper); + for (var i=nodes.length-1; i>=0; i--) { + var elm = nodes[i]; + var isEmpty = true; + + // Check if it has any attribs + var tmp = doc.createElement("body"); + tmp.appendChild(elm.cloneNode(false)); + + // Is empty span, remove it + tmp.innerHTML = tmp.innerHTML.replace(new RegExp('style=""|class=""', 'gi'), ''); + //tinyMCE.debug(tmp.innerHTML); + if (new RegExp('', 'gi').test(tmp.innerHTML)) { + for (var x=0; x 0) { + value = tinyMCE.replaceVar(value, "selection", selectedText); + tinyMCE.execCommand('mceInsertContent', false, value); + } + + tinyMCE.triggerNodeChange(); + break; + + case "mceSetAttribute": + if (typeof(value) == 'object') { + var targetElms = (typeof(value['targets']) == "undefined") ? "p,img,span,div,td,h1,h2,h3,h4,h5,h6,pre,address" : value['targets']; + var targetNode = tinyMCE.getParentElement(this.getFocusElement(), targetElms); + + if (targetNode) { + targetNode.setAttribute(value['name'], value['value']); + tinyMCE.triggerNodeChange(); + } + } + break; + + case "mceSetCSSClass": + this.execCommand("SetStyleInfo", false, {command : "setattrib", name : "class", value : value}); + break; + + case "mceInsertRawHTML": + var key = 'tiny_mce_marker'; + + this.execCommand('mceBeginUndoLevel'); + + // Insert marker key + this.execCommand('mceInsertContent', false, key); + + // Store away scroll pos + var scrollX = this.getDoc().body.scrollLeft + this.getDoc().documentElement.scrollLeft; + var scrollY = this.getDoc().body.scrollTop + this.getDoc().documentElement.scrollTop; + + // Find marker and replace with RAW HTML + var html = this.getBody().innerHTML; + if ((pos = html.indexOf(key)) != -1) + tinyMCE.setInnerHTML(this.getBody(), html.substring(0, pos) + value + html.substring(pos + key.length)); + + // Restore scoll pos + this.contentWindow.scrollTo(scrollX, scrollY); + + this.execCommand('mceEndUndoLevel'); + + break; + + case "mceInsertContent": + var insertHTMLFailed = false; + this.getWin().focus(); +/* WP + if (tinyMCE.isGecko || tinyMCE.isOpera) { + try { + // Is plain text or HTML + if (value.indexOf('<') == -1) { + var r = this.getRng(); + var n = this.getDoc().createTextNode(tinyMCE.entityDecode(value)); + var s = this.getSel(); + var r2 = r.cloneRange(); + + // Insert text at cursor position + s.removeAllRanges(); + r.deleteContents(); + r.insertNode(n); + + // Move the cursor to the end of text + r2.selectNode(n); + r2.collapse(false); + s.removeAllRanges(); + s.addRange(r2); + } else { + value = tinyMCE.fixGeckoBaseHREFBug(1, this.getDoc(), value); + this.getDoc().execCommand('inserthtml', false, value); + tinyMCE.fixGeckoBaseHREFBug(2, this.getDoc(), value); + } + } catch (ex) { + insertHTMLFailed = true; + } + + if (!insertHTMLFailed) { + tinyMCE.triggerNodeChange(); + return; + } + } +*/ + // Ugly hack in Opera due to non working "inserthtml" + if (tinyMCE.isOpera && insertHTMLFailed) { + this.getDoc().execCommand("insertimage", false, tinyMCE.uniqueURL); + var ar = tinyMCE.getElementsByAttributeValue(this.getBody(), "img", "src", tinyMCE.uniqueURL); + ar[0].outerHTML = value; + return; + } + + if (!tinyMCE.isMSIE) { + var isHTML = value.indexOf('<') != -1; + var sel = this.getSel(); + var rng = this.getRng(); + + if (isHTML) { + if (tinyMCE.isSafari) { + var tmpRng = this.getDoc().createRange(); + + tmpRng.setStart(this.getBody(), 0); + tmpRng.setEnd(this.getBody(), 0); + + value = tmpRng.createContextualFragment(value); + } else + value = rng.createContextualFragment(value); + } else { + // Setup text node + var el = document.createElement("div"); + el.innerHTML = value; + value = el.firstChild.nodeValue; + value = doc.createTextNode(value); + } + + // Insert plain text in Safari + if (tinyMCE.isSafari && !isHTML) { + this.execCommand('InsertText', false, value.nodeValue); + tinyMCE.triggerNodeChange(); + return true; + } else if (tinyMCE.isSafari && isHTML) { + rng.deleteContents(); + rng.insertNode(value); + tinyMCE.triggerNodeChange(); + return true; + } + + rng.deleteContents(); + + // If target node is text do special treatment, (Mozilla 1.3 fix) + if (rng.startContainer.nodeType == 3) { + var node = rng.startContainer.splitText(rng.startOffset); + node.parentNode.insertBefore(value, node); + } else + rng.insertNode(value); + + if (!isHTML) { + // Removes weird selection trails + sel.selectAllChildren(doc.body); + sel.removeAllRanges(); + + // Move cursor to end of content + var rng = doc.createRange(); + + rng.selectNode(value); + rng.collapse(false); + + sel.addRange(rng); + } else + rng.collapse(false); + } else { + var rng = doc.selection.createRange(); + var c = value.indexOf('', $content, 2); + if ( (preg_match('//', $post->post_content) && ((!$multipage) || ($page==1))) ) + $stripteaser = 1; + $teaser = $content[0]; + if ( ($more) && ($stripteaser) ) + $teaser = ''; + $output .= $teaser; + if ( count($content) > 1 ) { + if ( $more ) + $output .= ''.$content[1]; + else + $output .= ' $more_link_text"; + } + if ( $preview ) // preview fix for javascript bug with foreign languages + $output = preg_replace('/\%u([0-9A-F]{4,4})/e', "'&#'.base_convert('\\1',16,10).';'", $output); + + return $output; +} + + +function the_excerpt() { + echo apply_filters('the_excerpt', get_the_excerpt()); +} + + +function get_the_excerpt($fakeit = true) { + global $id, $post; + $output = ''; + $output = $post->post_excerpt; + if ( !empty($post->post_password) ) { // if there's a password + if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) { // and it doesn't match the cookie + $output = __('There is no excerpt because this is a protected post.'); + return $output; + } + } + + return apply_filters('get_the_excerpt', $output); +} + + +function wp_link_pages($args = '') { + parse_str($args, $r); + if ( !isset($r['before']) ) + $r['before'] = '

    ' . __('Pages:'); + if ( !isset($r['after']) ) + $r['after'] = '

    '; + if ( !isset($r['next_or_number']) ) + $r['next_or_number'] = 'number'; + if ( !isset($r['nextpagelink']) ) + $r['nextpagelink'] = 'Next page'; + if ( !isset($r['previouspagelink']) ) + $r['previouspagelink'] = 'Previous page'; + if ( !isset($r['pagelink']) ) + $r['pagelink'] = '%'; + if ( !isset($r['more_file']) ) + $r['more_file'] = ''; + + link_pages($r['before'], $r['after'], $r['next_or_number'], $r['nextpagelink'], $r['previouspagelink'], $r['pagelink'], $r['more_file']); +} + + +function link_pages($before='
    ', $after='
    ', $next_or_number='number', $nextpagelink='next page', $previouspagelink='previous page', $pagelink='%', $more_file='') { + global $id, $page, $numpages, $multipage, $more, $pagenow; + if ( $more_file != '' ) + $file = $more_file; + else + $file = $pagenow; + if ( $multipage ) { + if ( 'number' == $next_or_number ) { + echo $before; + for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) { + $j = str_replace('%',"$i",$pagelink); + echo ' '; + if ( ($i != $page) || ((!$more) && ($page==1)) ) { + if ( '' == get_settings('permalink_structure') ) + echo ''; + else + echo ''; + } + echo $j; + if ( ($i != $page) || ((!$more) && ($page==1)) ) + echo ''; + } + echo $after; + } else { + if ( $more ) { + echo $before; + $i = $page - 1; + if ( $i && $more ) { + if ( '' == get_settings('permalink_structure') ) + echo ''.$previouspagelink.''; + else + echo ''.$previouspagelink.''; + } + $i = $page + 1; + if ( $i <= $numpages && $more ) { + if ( '' == get_settings('permalink_structure') ) + echo ''.$nextpagelink.''; + else + echo ''.$nextpagelink.''; + } + echo $after; + } + } + } +} + + +/* +Post-meta: Custom per-post fields. +*/ + + +function get_post_custom( $post_id = 0 ) { + global $id, $post_meta_cache, $wpdb; + if ( $post_id ) + $id = $post_id; + if ( isset($post_meta_cache[$id]) ) { + return $post_meta_cache[$id]; + } else { + if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = '$id' ORDER BY post_id, meta_key", ARRAY_A) ) { + // Change from flat structure to hierarchical: + $post_meta_cache = array(); + foreach ( $meta_list as $metarow ) { + $mpid = $metarow['post_id']; + $mkey = $metarow['meta_key']; + $mval = $metarow['meta_value']; + + // Force subkeys to be array type: + if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) ) + $post_meta_cache[$mpid] = array(); + if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) ) + $post_meta_cache[$mpid]["$mkey"] = array(); + + // Add a value to the current pid/key: + $post_meta_cache[$mpid][$mkey][] = $mval; + } + return $post_meta_cache[$mpid]; + } + } +} + + +function get_post_custom_keys() { + global $id, $post_meta_cache; + + if ( !is_array($post_meta_cache[$id]) ) + return; + if ( $keys = array_keys($post_meta_cache[$id]) ) + return $keys; +} + + +function get_post_custom_values($key='') { + global $id, $post_meta_cache; + + return $post_meta_cache[$id][$key]; +} + + +function post_custom( $key = '' ) { + global $id, $post_meta_cache; + + if ( 1 == count($post_meta_cache[$id][$key]) ) + return $post_meta_cache[$id][$key][0]; + else + return $post_meta_cache[$id][$key]; +} + + +// this will probably change at some point... +function the_meta() { + global $id, $post_meta_cache; + + if ( $keys = get_post_custom_keys() ) { + echo "\n"; + } +} + + +/* +Pages +*/ + + +function &get_page_children($page_id, $pages) { + global $page_cache; + + if ( empty($pages) ) + $pages = &$page_cache; + + $page_list = array(); + foreach ( $pages as $page ) { + if ( $page->post_parent == $page_id ) { + $page_list[] = $page; + if ( $children = get_page_children($page->ID, $pages) ) + $page_list = array_merge($page_list, $children); + } + } + return $page_list; +} + + +function &get_pages($args = '') { + global $wpdb; + + parse_str($args, $r); + + if ( !isset($r['child_of']) ) + $r['child_of'] = 0; + if ( !isset($r['sort_column']) ) + $r['sort_column'] = 'post_title'; + if ( !isset($r['sort_order']) ) + $r['sort_order'] = 'ASC'; + + $exclusions = ''; + if ( !empty($r['exclude']) ) { + $expages = preg_split('/[\s,]+/',$r['exclude']); + if ( count($expages) ) { + foreach ( $expages as $expage ) { + $exclusions .= ' AND ID <> ' . intval($expage) . ' '; + } + } + } + + $pages = $wpdb->get_results("SELECT * " . + "FROM $wpdb->posts " . + "WHERE post_status = 'static' " . + "$exclusions " . + "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']); + + if ( empty($pages) ) + return array(); + + // Update cache. + update_page_cache($pages); + + if ( $r['child_of'] ) + $pages = & get_page_children($r['child_of'], $pages); + + return $pages; +} + + +function wp_list_pages($args = '') { + parse_str($args, $r); + if ( !isset($r['depth']) ) + $r['depth'] = 0; + if ( !isset($r['show_date']) ) + $r['show_date'] = ''; + if ( !isset($r['child_of']) ) + $r['child_of'] = 0; + if ( !isset($r['title_li']) ) + $r['title_li'] = __('Pages'); + if ( !isset($r['echo']) ) + $r['echo'] = 1; + + $output = ''; + + // Query pages. + $pages = & get_pages($args); + if ( $pages ) { + + if ( $r['title_li'] ) + $output .= ''; + } + + $output = apply_filters('wp_list_pages', $output); + + if ( $r['echo'] ) + echo $output; + else + return $output; +} + + +function _page_level_out($parent, $page_tree, $args, $depth = 0, $echo = true) { + global $wp_query; + $queried_obj = $wp_query->get_queried_object(); + $output = ''; + + if ( $depth ) + $indent = str_repeat("\t", $depth); + //$indent = join('', array_fill(0,$depth,"\t")); + + if ( !is_array($page_tree[$parent]['children']) ) + return false; + + foreach ( $page_tree[$parent]['children'] as $page_id ) { + $cur_page = $page_tree[$page_id]; + $title = $cur_page['title']; + + $css_class = 'page_item'; + if ( $page_id == $queried_obj->ID ) + $css_class .= ' current_page_item'; + + $output .= $indent . '
  • ' . $title . ''; + + if ( isset($cur_page['ts']) ) { + $format = get_settings('date_format'); + if ( isset($args['date_format']) ) + $format = $args['date_format']; + $output .= " " . mysql2date($format, $cur_page['ts']); + } + + if ( isset($cur_page['children']) && is_array($cur_page['children']) ) { + $new_depth = $depth + 1; + + if ( !$args['depth'] || $depth < ($args['depth']-1) ) { + $output .= "$indent
      \n"; + $output .= _page_level_out($page_id, $page_tree, $args, $new_depth, false); + $output .= "$indent
    \n"; + } + } + $output .= "$indent
  • \n"; + } + if ( $echo ) + echo $output; + else + return $output; +} + +function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) { + echo get_the_attachment_link($id, $fullsize, $max_dims); +} + +function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) { + $id = (int) $id; + $_post = & get_post($id); + + if ( ('attachment' != $_post->post_status) || ('' == $_post->guid) ) + return __('Missing Attachment'); + + if (! empty($_post->guid) ) { + $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims); + + return "guid}\" title=\"{$_post->post_title}\" >{$innerHTML}"; + + } else { + $p .= __('Missing attachment'); + } + return $p; +} + +function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) { + $id = (int) $id; + $post = & get_post($id); + + $mime = $post->post_mime_type; + + $imagedata = get_post_meta($post->ID, '_wp_attachment_metadata', true); + + $file = get_post_meta($post->ID, '_wp_attached_file', true); + + if ( !$fullsize && !empty($imagedata['thumb']) + && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) + && file_exists($thumbfile) ) { + + // We have a thumbnail desired, specified and existing + + $src = str_replace(basename($post->guid), $imagedata['thumb'], $post->guid); + $src_file = $thumbfile; + $class = 'attachmentthumb'; + + } elseif ( substr($mime, 0, 6) == 'image/' + && file_exists($file) ) { + + // We have an image without a thumbnail + + $src = $post->guid; + $src_file = & $file; + $class = 'attachmentimage'; + } elseif (! empty($mime) ) { + + // No thumb, no image. We'll look for a mime-related icon instead. + $icon_dir = apply_filters('icon_dir', get_template_directory().'/images'); + $icon_dir_uri = apply_filters('icon_dir_uri', get_template_directory_uri().'/images'); + + $types = array(substr($mime, 0, strpos($mime, '/')), substr($mime, strpos($mime, '/') + 1), str_replace('/', '_', $mime)); + $exts = array('jpg', 'gif', 'png'); + foreach ($types as $type) { + foreach ($exts as $ext) { + $src_file = "$icon_dir/$type.$ext"; + if ( file_exists($src_file) ) { + $src = "$icon_dir_uri/$type.$ext"; + break 2; + } + } + } + } + + if (! isset($src) ) + return false; + + // Do we need to constrain the image? + if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) { + + $imagesize = getimagesize($src_file); + + if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) { + $actual_aspect = $imagesize[0] / $imagesize[1]; + $desired_aspect = $max_dims[0] / $max_dims[1]; + + if ( $actual_aspect >= $desired_aspect ) { + $height = $actual_aspect * $max_dims[0]; + $constraint = "width=\"{$max_dims[0]}\" "; + $post->iconsize = array($max_dims[0], $height); + } else { + $width = $max_dims[1] / $actual_aspect; + $constraint = "height=\"{$max_dims[1]}\" "; + $post->iconsize = array($width, $max_dims[1]); + } + } else { + $post->iconsize = array($imagesize[0], $imagesize[1]); + } + } + + $icon = "post_title}\" alt=\"{$post->post_title}\" {$constraint}/>"; + + return apply_filters('attachment_icon', $icon, $post->ID); +} + +function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) { + $id = (int) $id; + + if ( $innerHTML = get_attachment_icon($id, $fullsize, $max_dims)) + return $innerHTML; + + $post = & get_post($id); + + $innerHTML = $post->post_title; + + return apply_filters('attachment_innerHTML', $innerHTML, $post->ID); +} + +function prepend_attachment($content) { + $p = '

    '; + $p .= get_the_attachment_link(false, true, array(400, 300)); + $p .= '

    '; + $p = apply_filters('prepend_attachment', $p); + + return "$p\n$content"; +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/template-loader.php b/projects/tests/tests/real/wordpress/wp-includes/template-loader.php new file mode 100644 index 00000000..f503f37e --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/template-loader.php @@ -0,0 +1,67 @@ + diff --git a/projects/tests/tests/real/wordpress/wp-includes/vars.php b/projects/tests/tests/real/wordpress/wp-includes/vars.php new file mode 100644 index 00000000..d80cb99a --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/vars.php @@ -0,0 +1,117 @@ + 'icon_smile.gif', + ' :D' => 'icon_biggrin.gif', + ' :-D' => 'icon_biggrin.gif', + ':grin:' => 'icon_biggrin.gif', + ' :)' => 'icon_smile.gif', + ' :-)' => 'icon_smile.gif', + ':smile:' => 'icon_smile.gif', + ' :(' => 'icon_sad.gif', + ' :-(' => 'icon_sad.gif', + ':sad:' => 'icon_sad.gif', + ' :o' => 'icon_surprised.gif', + ' :-o' => 'icon_surprised.gif', + ':eek:' => 'icon_surprised.gif', + ' 8O' => 'icon_eek.gif', + ' 8-O' => 'icon_eek.gif', + ':shock:' => 'icon_eek.gif', + ' :?' => 'icon_confused.gif', + ' :-?' => 'icon_confused.gif', + ' :???:' => 'icon_confused.gif', + ' 8)' => 'icon_cool.gif', + ' 8-)' => 'icon_cool.gif', + ':cool:' => 'icon_cool.gif', + ':lol:' => 'icon_lol.gif', + ' :x' => 'icon_mad.gif', + ' :-x' => 'icon_mad.gif', + ':mad:' => 'icon_mad.gif', + ' :P' => 'icon_razz.gif', + ' :-P' => 'icon_razz.gif', + ':razz:' => 'icon_razz.gif', + ':oops:' => 'icon_redface.gif', + ':cry:' => 'icon_cry.gif', + ':evil:' => 'icon_evil.gif', + ':twisted:' => 'icon_twisted.gif', + ':roll:' => 'icon_rolleyes.gif', + ':wink:' => 'icon_wink.gif', + ' ;)' => 'icon_wink.gif', + ' ;-)' => 'icon_wink.gif', + ':!:' => 'icon_exclaim.gif', + ':?:' => 'icon_question.gif', + ':idea:' => 'icon_idea.gif', + ':arrow:' => 'icon_arrow.gif', + ' :|' => 'icon_neutral.gif', + ' :-|' => 'icon_neutral.gif', + ':neutral:' => 'icon_neutral.gif', + ':mrgreen:' => 'icon_mrgreen.gif', + ); +} + +// sorts the smilies' array +if (!function_exists('smiliescmp')) { +function smiliescmp ($a, $b) { + if (strlen($a) == strlen($b)) { + return strcmp($a, $b); + } + return (strlen($a) > strlen($b)) ? -1 : 1; + } +} +uksort($wpsmiliestrans, 'smiliescmp'); + +// generates smilies' search & replace arrays +foreach($wpsmiliestrans as $smiley => $img) { + $wp_smiliessearch[] = $smiley; + $smiley_masked = htmlspecialchars( trim($smiley) , ENT_QUOTES); + $wp_smiliesreplace[] = " $smiley_masked "; +} + +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/version.php b/projects/tests/tests/real/wordpress/wp-includes/version.php new file mode 100644 index 00000000..b0573469 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/version.php @@ -0,0 +1,8 @@ + diff --git a/projects/tests/tests/real/wordpress/wp-includes/wp-db.php b/projects/tests/tests/real/wordpress/wp-includes/wp-db.php new file mode 100644 index 00000000..f38a21c6 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/wp-db.php @@ -0,0 +1,363 @@ +dbh = @mysql_connect($dbhost, $dbuser, $dbpassword); + if (!$this->dbh) { + $this->bail(" +

    Error establishing a database connection

    +

    This either means that the username and password information in your wp-config.php file is incorrect or we can't contact the database server at $dbhost. This could mean your host's database server is down.

    +
      +
    • Are you sure you have the correct username and password?
    • +
    • Are you sure that you have typed the correct hostname?
    • +
    • Are you sure that the database server is running?
    • +
    +

    If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

    +"); + } + + $this->select($dbname); + } + + // ================================================================== + // Select a DB (if another one needs to be selected) + + function select($db) { + if (!@mysql_select_db($db, $this->dbh)) { + $this->bail(" +

    Can’t select database

    +

    We were able to connect to the database server (which means your username and password is okay) but not able to select the $db database.

    +
      +
    • Are you sure it exists?
    • +
    • On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?
    • +
    +

    If you don't know how to setup a database you should contact your host. If all else fails you may find help at the WordPress Support Forums.

    "); + } + } + + // ==================================================================== + // Format a string correctly for safe insert under all PHP conditions + + function escape($string) { + return addslashes( $string ); // Disable rest for now, causing problems + if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' ) + return mysql_escape_string( $string ); + else + return mysql_real_escape_string( $string, $this->dbh ); + } + + // ================================================================== + // Print SQL/DB error. + + function print_error($str = '') { + global $EZSQL_ERROR; + if (!$str) $str = mysql_error(); + $EZSQL_ERROR[] = + array ('query' => $this->last_query, 'error_str' => $str); + + // Is error output turned on or not.. + if ( $this->show_errors ) { + // If there is an error then take note of it + print "
    +

    WordPress database error: [$str]
    + $this->last_query

    +
    "; + } else { + return false; + } + } + + // ================================================================== + // Turn error handling on or off.. + + function show_errors() { + $this->show_errors = true; + } + + function hide_errors() { + $this->show_errors = false; + } + + // ================================================================== + // Kill cached query results + + function flush() { + $this->last_result = null; + $this->col_info = null; + $this->last_query = null; + } + + // ================================================================== + // Basic Query - see docs for more detail + + function query($query) { + // initialise return + $return_val = 0; + $this->flush(); + + // Log how the function was called + $this->func_call = "\$db->query(\"$query\")"; + + // Keep track of the last query for debug.. + $this->last_query = $query; + + // Perform the query via std mysql_query function.. + if (SAVEQUERIES) + $this->timer_start(); + + $this->result = @mysql_query($query, $this->dbh); + ++$this->num_queries; + + if (SAVEQUERIES) + $this->queries[] = array( $query, $this->timer_stop() ); + + // If there is an error then take note of it.. + if ( mysql_error() ) { + $this->print_error(); + return false; + } + + if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) { + $this->rows_affected = mysql_affected_rows(); + // Take note of the insert_id + if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { + $this->insert_id = mysql_insert_id($this->dbh); + } + // Return number of rows affected + $return_val = $this->rows_affected; + } else { + $i = 0; + while ($i < @mysql_num_fields($this->result)) { + $this->col_info[$i] = @mysql_fetch_field($this->result); + $i++; + } + $num_rows = 0; + while ( $row = @mysql_fetch_object($this->result) ) { + $this->last_result[$num_rows] = $row; + $num_rows++; + } + + @mysql_free_result($this->result); + + // Log number of rows the query returned + $this->num_rows = $num_rows; + + // Return number of rows selected + $return_val = $this->num_rows; + } + + return $return_val; + } + + // ================================================================== + // Get one variable from the DB - see docs for more detail + + function get_var($query=null, $x = 0, $y = 0) { + $this->func_call = "\$db->get_var(\"$query\",$x,$y)"; + if ( $query ) + $this->query($query); + + // Extract var out of cached results based x,y vals + if ( $this->last_result[$y] ) { + $values = array_values(get_object_vars($this->last_result[$y])); + } + + // If there is a value return it else return null + return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null; + } + + // ================================================================== + // Get one row from the DB - see docs for more detail + + function get_row($query = null, $output = OBJECT, $y = 0) { + $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; + if ( $query ) + $this->query($query); + + if ( $output == OBJECT ) { + return $this->last_result[$y] ? $this->last_result[$y] : null; + } elseif ( $output == ARRAY_A ) { + return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null; + } elseif ( $output == ARRAY_N ) { + return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null; + } else { + $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"); + } + } + + // ================================================================== + // Function to get 1 column from the cached result set based in X index + // se docs for usage and info + + function get_col($query = null , $x = 0) { + if ( $query ) + $this->query($query); + + // Extract the column values + for ( $i=0; $i < count($this->last_result); $i++ ) { + $new_array[$i] = $this->get_var(null, $x, $i); + } + return $new_array; + } + + // ================================================================== + // Return the the query as a result set - see docs for more details + + function get_results($query = null, $output = OBJECT) { + $this->func_call = "\$db->get_results(\"$query\", $output)"; + + if ( $query ) + $this->query($query); + + // Send back array of objects. Each row is an object + if ( $output == OBJECT ) { + return $this->last_result; + } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { + if ( $this->last_result ) { + $i = 0; + foreach( $this->last_result as $row ) { + $new_array[$i] = (array) $row; + if ( $output == ARRAY_N ) { + $new_array[$i] = array_values($new_array[$i]); + } + $i++; + } + return $new_array; + } else { + return null; + } + } + } + + + // ================================================================== + // Function to get column meta data info pertaining to the last query + // see docs for more info and usage + + function get_col_info($info_type = 'name', $col_offset = -1) { + if ( $this->col_info ) { + if ( $col_offset == -1 ) { + $i = 0; + foreach($this->col_info as $col ) { + $new_array[$i] = $col->{$info_type}; + $i++; + } + return $new_array; + } else { + return $this->col_info[$col_offset]->{$info_type}; + } + } + } + + function timer_start() { + $mtime = microtime(); + $mtime = explode(' ', $mtime); + $this->time_start = $mtime[1] + $mtime[0]; + return true; + } + + function timer_stop($precision = 3) { + $mtime = microtime(); + $mtime = explode(' ', $mtime); + $time_end = $mtime[1] + $mtime[0]; + $time_total = $time_end - $this->time_start; + return $time_total; + } + + function bail($message) { // Just wraps errors in a nice header and footer + if ( !$this->show_errors ) + return false; + header( 'Content-Type: text/html; charset=utf-8'); + echo << + + + WordPress › Error + + + + +

    WordPress

    +HEAD; + echo $message; + echo ""; + die(); + } +} + +$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); +?> diff --git a/projects/tests/tests/real/wordpress/wp-includes/wp-l10n.php b/projects/tests/tests/real/wordpress/wp-includes/wp-l10n.php new file mode 100644 index 00000000..44393c5e --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-includes/wp-l10n.php @@ -0,0 +1,103 @@ +translate($text); + } else { + return $text; + } +} + +// Echo a translated string. +function _e($text, $domain = 'default') { + global $l10n; + + if (isset($l10n[$domain])) { + echo $l10n[$domain]->translate($text); + } else { + echo $text; + } +} + +// Return the plural form. +function __ngettext($single, $plural, $number, $domain = 'default') { + global $l10n; + + if (isset($l10n[$domain])) { + return $l10n[$domain]->ngettext($single, $plural, $number); + } else { + if ($number != 1) + return $plural; + else + return $single; + } +} + +function load_textdomain($domain, $mofile) { + global $l10n; + + if (isset($l10n[$domain])) { + return; + } + + if ( is_readable($mofile)) { + $input = new CachedFileReader($mofile); + } else { + return; + } + + $l10n[$domain] = new gettext_reader($input); +} + +function load_default_textdomain() { + global $l10n; + + $locale = get_locale(); + $mofile = ABSPATH . "wp-includes/languages/$locale.mo"; + + load_textdomain('default', $mofile); +} + +function load_plugin_textdomain($domain, $path = 'wp-content/plugins') { + $locale = get_locale(); + + $mofile = ABSPATH . "$path/$domain-$locale.mo"; + load_textdomain($domain, $mofile); +} + +function load_theme_textdomain($domain) { + $locale = get_locale(); + + $mofile = get_template_directory() . "/$locale.mo"; + load_textdomain($domain, $mofile); +} + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-links-opml.php b/projects/tests/tests/real/wordpress/wp-links-opml.php new file mode 100644 index 00000000..a08aae7b --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-links-opml.php @@ -0,0 +1,58 @@ +links.link_category = $link_cat"; + $cat_name = $wpdb->get_var("SELECT $wpdb->linkcategories.cat_name FROM $wpdb->linkcategories WHERE $wpdb->linkcategories.cat_id = $link_cat"); + if (!empty($cat_name)) { + $cat_name = ": category $cat_name"; + } + } +} +?>\n"; ?> + + + + Links for <?php echo get_bloginfo('name').$cat_name ?> + GMT + + +links.link_url, link_rss, $wpdb->links.link_name, $wpdb->links.link_category, $wpdb->linkcategories.cat_name, link_updated +FROM $wpdb->links + JOIN $wpdb->linkcategories on $wpdb->links.link_category = $wpdb->linkcategories.cat_id + $sql_cat + ORDER BY $wpdb->linkcategories.cat_name, $wpdb->links.link_name \n"; + //echo(""); + $prev_cat_id = 0; + $results = $wpdb->get_results($sql); + if ($results) { + foreach ($results as $result) { + if ($result->link_category != $prev_cat_id) { // new category + if ($prev_cat_id != 0) { // not first time +?> + + + +link_category; + } // end if new category +?> + + + + + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-login.php b/projects/tests/tests/real/wordpress/wp-login.php new file mode 100644 index 00000000..393f0798 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-login.php @@ -0,0 +1,258 @@ + + + + + WordPress » <?php _e('Lost Password') ?> + + + + + + +
    +

    WordPress

    +

    +$error
    "; +?> + +
    +

    + +

    +


    +

    +

    +
    +
      +
    • «
    • + +
    • + +
    • +
    + + + +user_login; + $user_email = $user_data->user_email; + + if (!$user_email || $user_email != $_POST['email']) + die(sprintf(__('Sorry, that user does not seem to exist in our database. Perhaps you have the wrong username or e-mail address? Try again.'), 'wp-login.php?action=lostpassword')); + +do_action('retreive_password', $user_login); // Misspelled and deprecated. +do_action('retrieve_password', $user_login); + + // Generate something random for a password... md5'ing current time with a rand salt + $key = substr( md5( uniqid( microtime() ) ), 0, 50); + // now insert the new pass md5'd into the db + $wpdb->query("UPDATE $wpdb->users SET user_activation_key = '$key' WHERE user_login = '$user_login'"); + $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n"; + $message .= get_option('siteurl') . "\r\n\r\n"; + $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; + $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n"; + $message .= get_settings('siteurl') . "/wp-login.php?action=resetpass&key=$key\r\n"; + + $m = wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_settings('blogname')), $message); + + if ($m == false) { + echo '

    ' . __('The e-mail could not be sent.') . "
    \n"; + echo __('Possible reason: your host may have disabled the mail() function...') . "

    "; + die(); + } else { + echo '

    ' . sprintf(__("The e-mail was sent successfully to %s's e-mail address."), $user_login) . '
    '; + echo "" . __('Click here to login!') . '

    '; + die(); + } + +break; + +case 'resetpass' : + + // Generate something random for a password... md5'ing current time with a rand salt + $key = preg_replace('/a-z0-9/i', '', $_GET['key']); + if ( empty($key) ) + die( __('Sorry, that key does not appear to be valid.') ); + $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_activation_key = '$key'"); + if ( !$user ) + die( __('Sorry, that key does not appear to be valid.') ); + + do_action('password_reset'); + + $new_pass = substr( md5( uniqid( microtime() ) ), 0, 7); + $wpdb->query("UPDATE $wpdb->users SET user_pass = MD5('$new_pass'), user_activation_key = '' WHERE user_login = '$user->user_login'"); + wp_cache_delete($user->ID, 'users'); + wp_cache_delete($user->user_login, 'userlogins'); + $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n"; + $message .= sprintf(__('Password: %s'), $new_pass) . "\r\n"; + $message .= get_settings('siteurl') . "/wp-login.php\r\n"; + + $m = wp_mail($user->user_email, sprintf(__('[%s] Your new password'), get_settings('blogname')), $message); + + if ($m == false) { + echo '

    ' . __('The e-mail could not be sent.') . "
    \n"; + echo __('Possible reason: your host may have disabled the mail() function...') . '

    '; + die(); + } else { + echo '

    ' . sprintf(__('Your new password is in the mail.'), $user_login) . '
    '; + echo "" . __('Click here to login!') . '

    '; + // send a copy of password change notification to the admin + $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n"; + wp_mail(get_settings('admin_email'), sprintf(__('[%s] Password Lost/Change'), get_settings('blogname')), $message); + die(); + } +break; + +case 'login' : +default: + + $user_login = ''; + $user_pass = ''; + $using_cookie = false; + if ( !isset( $_REQUEST['redirect_to'] ) ) + $redirect_to = 'wp-admin/'; + else + $redirect_to = $_REQUEST['redirect_to']; + $redirect_to = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $redirect_to); + + if( $_POST ) { + $user_login = $_POST['log']; + $user_login = sanitize_user( $user_login ); + $user_pass = $_POST['pwd']; + $rememberme = $_POST['rememberme']; + } elseif ( !empty($_COOKIE) ) { + if ( !empty($_COOKIE[USER_COOKIE]) ) + $user_login = $_COOKIE[USER_COOKIE]; + if ( !empty($_COOKIE[PASS_COOKIE]) ) { + $user_pass = $_COOKIE[PASS_COOKIE]; + $using_cookie = true; + } + } + + do_action('wp_authenticate', array(&$user_login, &$user_pass)); + + if ( $_POST ) { + $user = new WP_User(0, $user_login); + + // If the user can't edit posts, send them to their profile. + if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) ) + $redirect_to = get_settings('siteurl') . '/wp-admin/profile.php'; + + if ( wp_login($user_login, $user_pass, $using_cookie) ) { + if ( !$using_cookie ) + wp_setcookie($user_login, $user_pass, false, '', '', $rememberme); + do_action('wp_login', $user_login); + wp_redirect($redirect_to); + exit; + } else { + if ( $using_cookie ) + $error = __('Your session has expired.'); + } + } +?> + + + + WordPress › <?php _e('Login') ?> + + + + + + +
    +

    WordPress

    +$error
    "; +?> + +
    +

    +

    +

    +

    +

    + + +

    +
    +
      +
    • «
    • + +
    • + +
    • +
    + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-mail.php b/projects/tests/tests/real/wordpress/wp-mail.php new file mode 100644 index 00000000..2b2fcc24 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-mail.php @@ -0,0 +1,164 @@ +connect(get_settings('mailserver_url'), get_settings('mailserver_port'))) : + echo "Ooops $pop3->ERROR
    \n"; + exit; +endif; + +$count = $pop3->login(get_settings('mailserver_login'), get_settings('mailserver_pass')); +if (0 == $count) die(__('There doesn’t seem to be any new mail.')); + + +for ($i=1; $i <= $count; $i++) : + + $message = $pop3->get($i); + + $content = ''; + $content_type = ''; + $boundary = ''; + $bodysignal = 0; + $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); + foreach ($message as $line) : + if (strlen($line) < 3) $bodysignal = 1; + + if ($bodysignal) { + $content .= $line; + } else { + if (preg_match('/Content-Type: /i', $line)) { + $content_type = trim($line); + $content_type = substr($content_type, 14, strlen($content_type)-14); + $content_type = explode(';', $content_type); + $content_type = $content_type[0]; + } + if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) { + $boundary = trim($line); + $boundary = explode('"', $boundary); + $boundary = $boundary[1]; + } + if (preg_match('/Subject: /i', $line)) { + $subject = trim($line); + $subject = substr($subject, 9, strlen($subject)-9); + $subject = wp_iso_descrambler($subject); + // Captures any text in the subject before $phone_delim as the subject + $subject = explode($phone_delim, $subject); + $subject = $subject[0]; + } + + // Set the author using the email address (To or Reply-To, the last used) + // otherwise use the site admin + if (preg_match('/From: /', $line) | preg_match('Reply-To: /', $line)) { + $author=trim($line); + if ( ereg("([a-zA-Z0-9\_\-\.]+@[\a-zA-z0-9\_\-\.]+)", $author , $regs) ) { + echo "Author = {$regs[1]}

    "; + $result = $wpdb->get_row("SELECT ID FROM $tableusers WHERE user_email='$regs[1]' ORDER BY ID DESC LIMIT 1"); + if (!$result) + $post_author = 1; + else + $post_author = $result->ID; + } else + $post_author = 1; + } + + if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37' + $ddate = trim($line); + $ddate = str_replace('Date: ', '', $ddate); + if (strpos($ddate, ',')) { + $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate))); + } + $date_arr = explode(' ', $ddate); + $date_time = explode(':', $date_arr[3]); + + $ddate_H = $date_time[0]; + $ddate_i = $date_time[1]; + $ddate_s = $date_time[2]; + + $ddate_m = $date_arr[1]; + $ddate_d = $date_arr[0]; + $ddate_Y = $date_arr[2]; + for ($j=0; $j<12; $j++) { + if ($ddate_m == $dmonths[$j]) { + $ddate_m = $j+1; + } + } + + $time_zn = intval($date_arr[4]) * 36; + $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y); + $ddate_U = $ddate_U - $time_zn; + $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference); + $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U); + } + } + endforeach; + + $subject = trim(str_replace(get_settings('subjectprefix'), '', $subject)); + + if ($content_type == 'multipart/alternative') { + $content = explode('--'.$boundary, $content); + $content = $content[2]; + $content = explode('Content-Transfer-Encoding: quoted-printable', $content); + $content = strip_tags($content[1], '


    '); + } + $content = trim($content); + // Captures any text in the body after $phone_delim as the body + $content = explode($phone_delim, $content); + $content[1] ? $content = $content[1] : $content = $content[0]; + + echo "

    Content-type: $content_type, boundary: $boundary

    \n"; + echo "

    Raw content:

    ".$content.'

    '; + + $content = trim($content); + + $post_content = apply_filters('phone_content', $content); + + $post_title = xmlrpc_getposttitle($content); + + if ($post_title == '') $post_title = $subject; + + if (empty($post_categories)) $post_categories[] = get_settings('default_email_category'); + + $post_category = $post_categories; + + // or maybe we should leave the choice to email drafts? propose a way + $post_status = 'publish'; + + $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status'); + $post_data = add_magic_quotes($post_data); + + $post_ID = wp_insert_post($post_data); + + if (!$post_ID) { + // we couldn't post, for whatever reason. better move forward to the next email + continue; + } + + do_action('publish_phone', $post_ID); + + echo "\n

    Author: $post_author

    "; + echo "\n

    Posted title: $post_title
    "; + echo "\nPosted content:

    ".$content.'

    '; + + if(!$pop3->delete($i)) { + echo '

    Oops '.$pop3->ERROR.'

    '; + $pop3->reset(); + exit; + } else { + echo "

    Mission complete, message $i deleted.

    "; + } + +endfor; + +$pop3->quit(); + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-pass.php b/projects/tests/tests/real/wordpress/wp-pass.php new file mode 100644 index 00000000..d7d23a66 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-pass.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/wp-rdf.php b/projects/tests/tests/real/wordpress/wp-rdf.php new file mode 100644 index 00000000..a38d1ea2 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-rdf.php @@ -0,0 +1,57 @@ + +'; ?> + + +> +"> + <?php bloginfo_rss('name') ?> + + + + + hourly + 1 + 2000-01-01T12:00+00:00 + + + + + + + + + + + + <?php the_title_rss() ?> + + post_date_gmt, false); ?> + + + + + + + ]]> + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-register.php b/projects/tests/tests/real/wordpress/wp-register.php new file mode 100644 index 00000000..9a5f509b --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-register.php @@ -0,0 +1,154 @@ +ERROR
    : Please enter a username.'); + + /* checking e-mail address */ + if ($user_email == '') { + $errors['user_email'] = __('ERROR: Please type your e-mail address.'); + } else if (!is_email($user_email)) { + $errors['user_email'] = __('ERROR: The email address isn’t correct.'); + } + + if ( username_exists( $user_login ) ) + $errors['user_login'] = __('ERROR: This username is already registered, please choose another one.'); + + /* checking the email isn't already used by another user */ + $email_exists = $wpdb->get_row("SELECT user_email FROM $wpdb->users WHERE user_email = '$user_email'"); + if ( $email_exists) + die (__('ERROR: This email address is already registered, please supply another.')); + + if ( 0 == count($errors) ) { + $password = substr( md5( uniqid( microtime() ) ), 0, 7); + + $user_id = wp_create_user( $user_login, $password, $user_email ); + if ( !$user_id ) + $errors['user_id'] = sprintf(__('ERROR: Couldn’t register you... please contact the webmaster !'), get_settings('admin_email')); + else + wp_new_user_notification($user_id, $password); + } + + if ( 0 == count($errors) ) { + + ?> + + + + WordPress » <?php _e('Registration Complete') ?> + + + + + + +
    +

    +

    $user_login") ?>
    + ' . __('emailed to you') . '') ?>
    + $user_email") ?>

    +

    »

    +
    + + + + + + + + WordPress » <?php _e('Registration Form') ?> + + + + + + +
    +

    WordPress

    +

    + +
    +
      + $error"; + ?> +
    +
    + +
    +

    +

    +


    +

    +

    +
    + +
    + + + + + + + + WordPress » <?php _e('Registration Currently Disabled') ?> + + + + + + +
    +

    +


    + +

    +
    + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-rss.php b/projects/tests/tests/real/wordpress/wp-rss.php new file mode 100644 index 00000000..aa4a6360 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-rss.php @@ -0,0 +1,37 @@ + +'; ?> + + + + <?php bloginfo_rss('name') ?> + + + + http://backend.userland.com/rss092 + + + + + + <?php the_title_rss() ?> + + ]]> + + + + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-rss2.php b/projects/tests/tests/real/wordpress/wp-rss2.php new file mode 100644 index 00000000..8f42acaa --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-rss2.php @@ -0,0 +1,56 @@ + +'; ?> + + + +> + + + <?php bloginfo_rss('name'); ?> + + + + http://wordpress.org/?v= + + + + + <?php the_title_rss() ?> + + + + + + + + + ]]> + + ]]> + post_content ) > 0 ) : ?> + ]]> + + ]]> + + + + + + + + + diff --git a/projects/tests/tests/real/wordpress/wp-settings.php b/projects/tests/tests/real/wordpress/wp-settings.php new file mode 100644 index 00000000..4a87fe92 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-settings.php @@ -0,0 +1,229 @@ + $v ) + if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) + unset($GLOBALS[$k]); +} + +unregister_GLOBALS(); + +$HTTP_USER_AGENT = getenv('HTTP_USER_AGENT'); +unset( $wp_filter, $cache_userdata, $cache_lastcommentmodified, $cache_lastpostdate, $cache_settings, $category_cache, $cache_categories ); + +if ( ! isset($blog_id) ) + $blog_id = 1; + +// Fix for IIS, which doesn't set REQUEST_URI +if ( empty( $_SERVER['REQUEST_URI'] ) ) { + $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; // Does this work under CGI? + + // Append the query string if it exists and isn't null + if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { + $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; + } +} + +// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests +if ( strpos($_SERVER['SCRIPT_FILENAME'], 'php.cgi') == strlen($_SERVER['SCRIPT_FILENAME']) - 7 ) + $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED']; + +// Fix for Dreamhost and other PHP as CGI hosts +if ( strstr( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) ) + unset($_SERVER['PATH_INFO']); + + +if ( !(phpversion() >= '4.1') ) + die( 'Your server is running PHP version ' . phpversion() . ' but WordPress requires at least 4.1' ); + +if ( !extension_loaded('mysql') ) + die( 'Your PHP installation appears to be missing the MySQL which is required for WordPress.' ); + +function timer_start() { + global $timestart; + $mtime = explode(' ', microtime() ); + $mtime = $mtime[1] + $mtime[0]; + $timestart = $mtime; + return true; +} +timer_start(); + +// Change to E_ALL for development/debugging +error_reporting(E_ALL ^ E_NOTICE); + +// For an advanced caching plugin to use, static because you would only want one +if ( defined('WP_CACHE') ) + require (ABSPATH . 'wp-content/advanced-cache.php'); + +define('WPINC', 'wp-includes'); +require_once (ABSPATH . WPINC . '/wp-db.php'); + +// Table names +$wpdb->posts = $table_prefix . 'posts'; +$wpdb->users = $table_prefix . 'users'; +$wpdb->categories = $table_prefix . 'categories'; +$wpdb->post2cat = $table_prefix . 'post2cat'; +$wpdb->comments = $table_prefix . 'comments'; +$wpdb->links = $table_prefix . 'links'; +$wpdb->linkcategories = $table_prefix . 'linkcategories'; +$wpdb->options = $table_prefix . 'options'; +$wpdb->postmeta = $table_prefix . 'postmeta'; +$wpdb->usermeta = $table_prefix . 'usermeta'; + +$wpdb->prefix = $table_prefix; + +if ( defined('CUSTOM_USER_TABLE') ) + $wpdb->users = CUSTOM_USER_TABLE; +if ( defined('CUSTOM_USER_META_TABLE') ) + $wpdb->usermeta = CUSTOM_USER_META_TABLE; + +// We're going to need to keep this around for a few months even though we're not using it internally + +$tableposts = $wpdb->posts; +$tableusers = $wpdb->users; +$tablecategories = $wpdb->categories; +$tablepost2cat = $wpdb->post2cat; +$tablecomments = $wpdb->comments; +$tablelinks = $wpdb->links; +$tablelinkcategories = $wpdb->linkcategories; +$tableoptions = $wpdb->options; +$tablepostmeta = $wpdb->postmeta; + +if ( file_exists(ABSPATH . 'wp-content/object-cache.php') ) + require (ABSPATH . 'wp-content/object-cache.php'); +else + require (ABSPATH . WPINC . '/cache.php'); + +// For now, disable persistent caching by default. To enable, comment out +// the following line. +//define('DISABLE_CACHE', true); + +wp_cache_init(); + +$wp_filters = array(); + +require (ABSPATH . WPINC . '/functions.php'); +require (ABSPATH . WPINC . '/default-filters.php'); +require_once (ABSPATH . WPINC . '/wp-l10n.php'); + +$wpdb->hide_errors(); +$db_check = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'"); +if ( !$db_check && (!strstr($_SERVER['PHP_SELF'], 'install.php') && !defined('WP_INSTALLING')) ) { + if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') ) + $link = 'install.php'; + else + $link = 'wp-admin/install.php'; + die(sprintf(__("It doesn't look like you've installed WP yet. Try running install.php."), $link)); +} +$wpdb->show_errors(); + +require (ABSPATH . WPINC . '/functions-formatting.php'); +require (ABSPATH . WPINC . '/functions-post.php'); +require (ABSPATH . WPINC . '/capabilities.php'); +require (ABSPATH . WPINC . '/classes.php'); +require (ABSPATH . WPINC . '/template-functions-general.php'); +require (ABSPATH . WPINC . '/template-functions-links.php'); +require (ABSPATH . WPINC . '/template-functions-author.php'); +require (ABSPATH . WPINC . '/template-functions-post.php'); +require (ABSPATH . WPINC . '/template-functions-category.php'); +require (ABSPATH . WPINC . '/comment-functions.php'); +require (ABSPATH . WPINC . '/feed-functions.php'); +require (ABSPATH . WPINC . '/links.php'); +require (ABSPATH . WPINC . '/kses.php'); +require (ABSPATH . WPINC . '/version.php'); + +if (!strstr($_SERVER['PHP_SELF'], 'install.php')) : + // Used to guarantee unique hash cookies + $cookiehash = md5(get_settings('siteurl')); // Remove in 1.4 + define('COOKIEHASH', $cookiehash); +endif; + +if ( !defined('USER_COOKIE') ) + define('USER_COOKIE', 'wordpressuser_'. COOKIEHASH); +if ( !defined('PASS_COOKIE') ) + define('PASS_COOKIE', 'wordpresspass_'. COOKIEHASH); +if ( !defined('COOKIEPATH') ) + define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('home') . '/' ) ); +if ( !defined('SITECOOKIEPATH') ) + define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('siteurl') . '/' ) ); +if ( !defined('COOKIE_DOMAIN') ) + define('COOKIE_DOMAIN', false); + +require (ABSPATH . WPINC . '/vars.php'); + +do_action('core_files_loaded'); + +// Check for hacks file if the option is enabled +if (get_settings('hack_file')) { + if (file_exists(ABSPATH . '/my-hacks.php')) + require(ABSPATH . '/my-hacks.php'); +} + +if ( get_settings('active_plugins') ) { + $current_plugins = get_settings('active_plugins'); + if ( is_array($current_plugins) ) { + foreach ($current_plugins as $plugin) { + if ('' != $plugin && file_exists(ABSPATH . 'wp-content/plugins/' . $plugin)) + include_once(ABSPATH . 'wp-content/plugins/' . $plugin); + } + } +} + +require (ABSPATH . WPINC . '/pluggable-functions.php'); + +if ( defined('WP_CACHE') && function_exists('wp_cache_postload') ) + wp_cache_postload(); + +do_action('plugins_loaded'); +/* +// If already slashed, strip. +if ( get_magic_quotes_gpc() ) { + $_GET = stripslashes_deep($_GET ); + $_POST = stripslashes_deep($_POST ); + $_COOKIE = stripslashes_deep($_COOKIE); + $_SERVER = stripslashes_deep($_SERVER); +} + +// Escape with wpdb. +$_GET = add_magic_quotes($_GET ); +$_POST = add_magic_quotes($_POST ); +$_COOKIE = add_magic_quotes($_COOKIE); +$_SERVER = add_magic_quotes($_SERVER); +*/ +$wp_query = new WP_Query(); +$wp_rewrite = new WP_Rewrite(); +$wp = new WP(); +$wp_roles = new WP_Roles(); + +define('TEMPLATEPATH', get_template_directory()); + +// Load the default text localization domain. +load_default_textdomain(); + +// Pull in locale data after loading text domain. +require_once(ABSPATH . WPINC . '/locale.php'); + +// Load functions for active theme. +if ( file_exists(TEMPLATEPATH . "/functions.php") ) + include(TEMPLATEPATH . "/functions.php"); + +function shutdown_action_hook() { + wp_cache_close(); + do_action('shutdown'); +} +register_shutdown_function('shutdown_action_hook'); + +// Everything is loaded and initialized. +do_action('init'); + +?> diff --git a/projects/tests/tests/real/wordpress/wp-trackback.php b/projects/tests/tests/real/wordpress/wp-trackback.php new file mode 100644 index 00000000..ff058c74 --- /dev/null +++ b/projects/tests/tests/real/wordpress/wp-trackback.php @@ -0,0 +1,98 @@ +\n"; + echo "\n"; + echo "1\n"; + echo "$error_message\n"; + echo ""; + die(); + } else { + echo '\n"; + echo "\n"; + echo "0\n"; + echo ""; + } +} + +// trackback is done by a POST +$request_array = 'HTTP_POST_VARS'; + +if ( !$_GET['tb_id'] ) { + $tb_id = explode('/', $_SERVER['REQUEST_URI']); + $tb_id = intval( $tb_id[ count($tb_id) - 1 ] ); +} + +$tb_url = $_POST['url']; +$title = $_POST['title']; +$excerpt = $_POST['excerpt']; +$blog_name = $_POST['blog_name']; +$charset = $_POST['charset']; + +if ($charset) + $charset = strtoupper( trim($charset) ); +else + $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS'; + +if ( function_exists('mb_convert_encoding') ) { // For international trackbacks + $title = mb_convert_encoding($title, get_settings('blog_charset'), $charset); + $excerpt = mb_convert_encoding($excerpt, get_settings('blog_charset'), $charset); + $blog_name = mb_convert_encoding($blog_name, get_settings('blog_charset'), $charset); +} + +if ( is_single() || is_page() ) + $tb_id = $posts[0]->ID; + +if ( !intval( $tb_id ) ) + trackback_response(1, 'I really need an ID for this to work.'); + +if (empty($title) && empty($tb_url) && empty($blog_name)) { + // If it doesn't look like a trackback at all... + header('Location: ' . get_permalink($tb_id)); + exit; +} + +if ( !empty($tb_url) && !empty($title) && !empty($tb_url) ) { + header('Content-Type: text/xml; charset=' . get_option('blog_charset') ); + + $pingstatus = $wpdb->get_var("SELECT ping_status FROM $wpdb->posts WHERE ID = $tb_id"); + + if ( 'open' != $pingstatus ) + trackback_response(1, 'Sorry, trackbacks are closed for this item.'); + + $title = wp_specialchars( strip_tags( $title ) ); + $excerpt = strip_tags($excerpt); + if ( function_exists('mb_substr') ) { // For international trackbacks + $excerpt = mb_substr($excerpt, 0, 252, get_settings('blog_charset')) . '...'; + $title = mb_substr($title, 0, 250, get_settings('blog_charset')) . '...'; + } else { + $excerpt = (strlen($excerpt) > 255) ? substr($excerpt, 0, 252) . '...' : $excerpt; + $title = (strlen($title) > 250) ? substr($title, 0, 250) . '...' : $title; + } + + $comment_post_ID = $tb_id; + $comment_author = $blog_name; + $comment_author_email = ''; + $comment_author_url = $tb_url; + $comment_content = "$title\n\n$excerpt"; + $comment_type = 'trackback'; + + $dupe = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_author_url = '$comment_author_url'"); + if ( $dupe ) + trackback_response(1, 'We already have a ping from that URI for this post.'); + + $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type'); + + wp_new_comment($commentdata); + + do_action('trackback_post', $wpdb->insert_id); + trackback_response(0); +} +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress/xmlrpc.php b/projects/tests/tests/real/wordpress/xmlrpc.php new file mode 100644 index 00000000..bac38e11 --- /dev/null +++ b/projects/tests/tests/real/wordpress/xmlrpc.php @@ -0,0 +1,1285 @@ + +'; ?> + + + WordPress + http://wordpress.org/ + + + + + + + + +methods = array( + // Blogger API + 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs', + 'blogger.getUserInfo' => 'this:blogger_getUserInfo', + 'blogger.getPost' => 'this:blogger_getPost', + 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts', + 'blogger.getTemplate' => 'this:blogger_getTemplate', + 'blogger.setTemplate' => 'this:blogger_setTemplate', + 'blogger.newPost' => 'this:blogger_newPost', + 'blogger.editPost' => 'this:blogger_editPost', + 'blogger.deletePost' => 'this:blogger_deletePost', + + // MetaWeblog API (with MT extensions to structs) + 'metaWeblog.newPost' => 'this:mw_newPost', + 'metaWeblog.editPost' => 'this:mw_editPost', + 'metaWeblog.getPost' => 'this:mw_getPost', + 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts', + 'metaWeblog.getCategories' => 'this:mw_getCategories', + 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject', + + // MetaWeblog API aliases for Blogger API + // see http://www.xmlrpc.com/stories/storyReader$2460 + 'metaWeblog.deletePost' => 'this:blogger_deletePost', + 'metaWeblog.getTemplate' => 'this:blogger_getTemplate', + 'metaWeblog.setTemplate' => 'this:blogger_setTemplate', + 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs', + + // MovableType API + 'mt.getCategoryList' => 'this:mt_getCategoryList', + 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles', + 'mt.getPostCategories' => 'this:mt_getPostCategories', + 'mt.setPostCategories' => 'this:mt_setPostCategories', + 'mt.supportedMethods' => 'this:mt_supportedMethods', + 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters', + 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings', + 'mt.publishPost' => 'this:mt_publishPost', + + // PingBack + 'pingback.ping' => 'this:pingback_ping', + 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks', + + 'demo.sayHello' => 'this:sayHello', + 'demo.addTwoNumbers' => 'this:addTwoNumbers' + ); + $this->methods = apply_filters('xmlrpc_methods', $this->methods); + $this->IXR_Server($this->methods); + } + + function sayHello($args) { + return 'Hello!'; + } + + function addTwoNumbers($args) { + $number1 = $args[0]; + $number2 = $args[1]; + return $number1 + $number2; + } + + function login_pass_ok($user_login, $user_pass) { + if (!user_pass_ok($user_login, $user_pass)) { + $this->error = new IXR_Error(403, 'Bad login/pass combination.'); + return false; + } + return true; + } + + function escape(&$array) { + global $wpdb; + + foreach ($array as $k => $v) { + if (is_array($v)) { + $this->escape($array[$k]); + } else if (is_object($v)) { + //skip + } else { + $array[$k] = $wpdb->escape($v); + } + } + } + + /* Blogger API functions + * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/ + */ + + + /* blogger.getUsersBlogs will make more sense once we support multiple blogs */ + function blogger_getUsersBlogs($args) { + + $this->escape($args); + + $user_login = $args[1]; + $user_pass = $args[2]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user = new WP_User(0, $user_login); + $is_admin = $user->has_cap('level_8'); + + $struct = array( + 'isAdmin' => $is_admin, + 'url' => get_settings('home') . '/', + 'blogid' => '1', + 'blogName' => get_settings('blogname') + ); + + return array($struct); + } + + + /* blogger.getUsersInfo gives your client some info about you, so you don't have to */ + function blogger_getUserInfo($args) { + + $this->escape($args); + + $user_login = $args[1]; + $user_pass = $args[2]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user_data = get_userdatabylogin($user_login); + + $struct = array( + 'nickname' => $user_data->nickname, + 'userid' => $user_data->ID, + 'url' => $user_data->user_url, + 'email' => $user_data->user_email, + 'lastname' => $user_data->last_name, + 'firstname' => $user_data->first_name + ); + + return $struct; + } + + + /* blogger.getPost ...gets a post */ + function blogger_getPost($args) { + + $this->escape($args); + + $post_ID = $args[1]; + $user_login = $args[2]; + $user_pass = $args[3]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user_data = get_userdatabylogin($user_login); + $post_data = wp_get_single_post($post_ID, ARRAY_A); + + $categories = implode(',', wp_get_post_cats(1, $post_ID)); + + $content = ''.stripslashes($post_data['post_title']).''; + $content .= ''.$categories.''; + $content .= stripslashes($post_data['post_content']); + + $struct = array( + 'userid' => $post_data['post_author'], + 'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'])), + 'content' => $content, + 'postid' => $post_data['ID'] + ); + + return $struct; + } + + + /* blogger.getRecentPosts ...gets recent posts */ + function blogger_getRecentPosts($args) { + + global $wpdb; + + $this->escape($args); + + $blog_ID = $args[1]; /* though we don't use it yet */ + $user_login = $args[2]; + $user_pass = $args[3]; + $num_posts = $args[4]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $posts_list = wp_get_recent_posts($num_posts); + + if (!$posts_list) { + $this->error = new IXR_Error(500, 'Either there are no posts, or something went wrong.'); + return $this->error; + } + + foreach ($posts_list as $entry) { + + $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']); + $categories = implode(',', wp_get_post_cats(1, $entry['ID'])); + + $content = ''.stripslashes($entry['post_title']).''; + $content .= ''.$categories.''; + $content .= stripslashes($entry['post_content']); + + $struct[] = array( + 'userid' => $entry['post_author'], + 'dateCreated' => new IXR_Date($post_date), + 'content' => $content, + 'postid' => $entry['ID'], + ); + + } + + $recent_posts = array(); + for ($j=0; $jescape($args); + + $blog_ID = $args[1]; + $user_login = $args[2]; + $user_pass = $args[3]; + $template = $args[4]; /* could be 'main' or 'archiveIndex', but we don't use it */ + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('edit_themes') ) { + return new IXR_Error(401, 'Sorry, this user can not edit the template.'); + } + + /* warning: here we make the assumption that the weblog's URI is on the same server */ + $filename = get_settings('home') . '/'; + $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename); + + $f = fopen($filename, 'r'); + $content = fread($f, filesize($filename)); + fclose($f); + + /* so it is actually editable with a windows/mac client */ + // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented. $content = str_replace("\n", "\r\n", $content); + + return $content; + } + + + /* blogger.setTemplate updates the content of blog_filename */ + function blogger_setTemplate($args) { + + $this->escape($args); + + $blog_ID = $args[1]; + $user_login = $args[2]; + $user_pass = $args[3]; + $content = $args[4]; + $template = $args[5]; /* could be 'main' or 'archiveIndex', but we don't use it */ + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('edit_themes') ) { + return new IXR_Error(401, 'Sorry, this user can not edit the template.'); + } + + /* warning: here we make the assumption that the weblog's URI is on the same server */ + $filename = get_settings('home') . '/'; + $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename); + + if ($f = fopen($filename, 'w+')) { + fwrite($f, $content); + fclose($f); + } else { + return new IXR_Error(500, 'Either the file is not writable, or something wrong happened. The file has not been updated.'); + } + + return true; + } + + + /* blogger.newPost ...creates a new post */ + function blogger_newPost($args) { + + global $wpdb; + + $this->escape($args); + + $blog_ID = $args[1]; /* though we don't use it yet */ + $user_login = $args[2]; + $user_pass = $args[3]; + $content = $args[4]; + $publish = $args[5]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $cap = ($publish) ? 'publish_posts' : 'edit_posts'; + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap($cap) ) + return new IXR_Error(401, 'Sorry, you can not post on this weblog or category.'); + + $post_status = ($publish) ? 'publish' : 'draft'; + + $post_author = $user->ID; + + $post_title = xmlrpc_getposttitle($content); + $post_category = xmlrpc_getpostcategory($content); + $post_content = xmlrpc_removepostdata($content); + + $post_date = current_time('mysql'); + $post_date_gmt = current_time('mysql', 1); + + $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status'); + + $post_ID = wp_insert_post($post_data); + + if (!$post_ID) { + return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.'); + } + + logIO('O', "Posted ! ID: $post_ID"); + + return $post_ID; + } + + + /* blogger.editPost ...edits a post */ + function blogger_editPost($args) { + + global $wpdb; + + $this->escape($args); + + $post_ID = $args[1]; + $user_login = $args[2]; + $user_pass = $args[3]; + $content = $args[4]; + $publish = $args[5]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $actual_post = wp_get_single_post($post_ID,ARRAY_A); + + if (!$actual_post) { + return new IXR_Error(404, 'Sorry, no such post.'); + } + + $this->escape($actual_post); + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('edit_post', $post_ID) ) + return new IXR_Error(401, 'Sorry, you do not have the right to edit this post.'); + + extract($actual_post); + + $post_title = xmlrpc_getposttitle($content); + $post_category = xmlrpc_getpostcategory($content); + $post_content = xmlrpc_removepostdata($content); + + $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt'); + + $result = wp_update_post($postdata); + + if (!$result) { + return new IXR_Error(500, 'For some strange yet very annoying reason, this post could not be edited.'); + } + + return true; + } + + + /* blogger.deletePost ...deletes a post */ + function blogger_deletePost($args) { + + global $wpdb; + + $this->escape($args); + + $post_ID = $args[1]; + $user_login = $args[2]; + $user_pass = $args[3]; + $publish = $args[4]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $actual_post = wp_get_single_post($post_ID,ARRAY_A); + + if (!$actual_post) { + return new IXR_Error(404, 'Sorry, no such post.'); + } + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('edit_post', $post_ID) ) + return new IXR_Error(401, 'Sorry, you do not have the right to delete this post.'); + + $result = wp_delete_post($post_ID); + + if (!$result) { + return new IXR_Error(500, 'For some strange yet very annoying reason, this post could not be deleted.'); + } + + return true; + } + + + + /* MetaWeblog API functions + * specs on wherever Dave Winer wants them to be + */ + + /* metaweblog.newPost creates a post */ + function mw_newPost($args) { + + global $wpdb, $post_default_category; + + $this->escape($args); + + $blog_ID = $args[0]; // we will support this in the near future + $user_login = $args[1]; + $user_pass = $args[2]; + $content_struct = $args[3]; + $publish = $args[4]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('publish_posts') ) + return new IXR_Error(401, 'Sorry, you can not post on this weblog or category.'); + + $post_author = $user->ID; + + $post_title = $content_struct['title']; + $post_content = apply_filters( 'content_save_pre', $content_struct['description'] ); + $post_status = $publish ? 'publish' : 'draft'; + + $post_excerpt = $content_struct['mt_excerpt']; + $post_more = $content_struct['mt_text_more']; + + $comment_status = (empty($content_struct['mt_allow_comments'])) ? + get_settings('default_comment_status') + : $content_struct['mt_allow_comments']; + + $ping_status = (empty($content_struct['mt_allow_pings'])) ? + get_settings('default_ping_status') + : $content_struct['mt_allow_pings']; + + if ($post_more) { + $post_content = $post_content . "\n\n" . $post_more; + } + + $to_ping = $content_struct['mt_tb_ping_urls']; + + // Do some timestamp voodoo + $dateCreatedd = $content_struct['dateCreated']; + if (!empty($dateCreatedd)) { + $dateCreated = $dateCreatedd->getIso(); + $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated)); + $post_date_gmt = iso8601_to_datetime($dateCreated, GMT); + } else { + $post_date = current_time('mysql'); + $post_date_gmt = current_time('mysql', 1); + } + + $catnames = $content_struct['categories']; + logIO('O', 'Post cats: ' . printr($catnames,true)); + $post_category = array(); + + if (is_array($catnames)) { + foreach ($catnames as $cat) { + $post_category[] = get_cat_ID($cat); + } + } + + // We've got all the data -- post it: + $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping'); + + $post_ID = wp_insert_post($postdata); + + if (!$post_ID) { + return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.'); + } + + logIO('O', "Posted ! ID: $post_ID"); + + return strval($post_ID); + } + + + /* metaweblog.editPost ...edits a post */ + function mw_editPost($args) { + + global $wpdb, $post_default_category; + + $this->escape($args); + + $post_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + $content_struct = $args[3]; + $publish = $args[4]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('edit_post', $post_ID) ) + return new IXR_Error(401, 'Sorry, you can not edit this post.'); + + $postdata = wp_get_single_post($post_ID, ARRAY_A); + extract($postdata); + $this->escape($postdata); + + $post_title = $content_struct['title']; + $post_content = apply_filters( 'content_save_pre', $content_struct['description'] ); + $catnames = $content_struct['categories']; + + $post_category = array(); + + if (is_array($catnames)) { + foreach ($catnames as $cat) { + $post_category[] = get_cat_ID($cat); + } + } + + $post_excerpt = $content_struct['mt_excerpt']; + $post_more = $content_struct['mt_text_more']; + $post_status = $publish ? 'publish' : 'draft'; + + if ($post_more) { + $post_content = $post_content . "\n\n" . $post_more; + } + + $to_ping = $content_struct['mt_tb_ping_urls']; + + $comment_status = (empty($content_struct['mt_allow_comments'])) ? + get_settings('default_comment_status') + : $content_struct['mt_allow_comments']; + + $ping_status = (empty($content_struct['mt_allow_pings'])) ? + get_settings('default_ping_status') + : $content_struct['mt_allow_pings']; + + // Do some timestamp voodoo + $dateCreatedd = $content_struct['dateCreated']; + if (!empty($dateCreatedd)) { + $dateCreated = $dateCreatedd->getIso(); + $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated)); + $post_date_gmt = iso8601_to_datetime($dateCreated, GMT); + } else { + $post_date = $postdata['post_date']; + $post_date_gmt = $postdata['post_date_gmt']; + } + + // We've got all the data -- post it: + $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping'); + + $result = wp_update_post($newpost); + if (!$result) { + return new IXR_Error(500, 'Sorry, your entry could not be edited. Something wrong happened.'); + } + + logIO('O',"(MW) Edited ! ID: $post_ID"); + + return true; + } + + + /* metaweblog.getPost ...returns a post */ + function mw_getPost($args) { + + global $wpdb; + + $this->escape($args); + + $post_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $postdata = wp_get_single_post($post_ID, ARRAY_A); + + if ($postdata['post_date'] != '') { + + $post_date = mysql2date('Ymd\TH:i:s', $postdata['post_date']); + + $categories = array(); + $catids = wp_get_post_cats('', $post_ID); + foreach($catids as $catid) { + $categories[] = get_cat_name($catid); + } + + $post = get_extended($postdata['post_content']); + $link = post_permalink($postdata['ID']); + + $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0; + $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0; + + $resp = array( + 'dateCreated' => new IXR_Date($post_date), + 'userid' => $postdata['post_author'], + 'postid' => $postdata['ID'], + 'description' => $post['main'], + 'title' => $postdata['post_title'], + 'link' => $link, + 'permaLink' => $link, +// commented out because no other tool seems to use this +// 'content' => $entry['post_content'], + 'categories' => $categories, + 'mt_excerpt' => $postdata['post_excerpt'], + 'mt_text_more' => $post['extended'], + 'mt_allow_comments' => $allow_comments, + 'mt_allow_pings' => $allow_pings + ); + + return $resp; + } else { + return new IXR_Error(404, 'Sorry, no such post.'); + } + } + + + /* metaweblog.getRecentPosts ...returns recent posts */ + function mw_getRecentPosts($args) { + + $this->escape($args); + + $blog_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + $num_posts = $args[3]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $posts_list = wp_get_recent_posts($num_posts); + + if (!$posts_list) { + $this->error = new IXR_Error(500, 'Either there are no posts, or something went wrong.'); + return $this->error; + } + + foreach ($posts_list as $entry) { + + $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']); + $categories = array(); + $catids = wp_get_post_cats('', $entry['ID']); + foreach($catids as $catid) { + $categories[] = get_cat_name($catid); + } + + $post = get_extended($entry['post_content']); + $link = post_permalink($entry['ID']); + + $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0; + $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0; + + $struct[] = array( + 'dateCreated' => new IXR_Date($post_date), + 'userid' => $entry['post_author'], + 'postid' => $entry['ID'], + 'description' => $post['main'], + 'title' => $entry['post_title'], + 'link' => $link, + 'permaLink' => $link, +// commented out because no other tool seems to use this +// 'content' => $entry['post_content'], + 'categories' => $categories, + 'mt_excerpt' => $entry['post_excerpt'], + 'mt_text_more' => $post['extended'], + 'mt_allow_comments' => $allow_comments, + 'mt_allow_pings' => $allow_pings + ); + + } + + $recent_posts = array(); + for ($j=0; $jescape($args); + + $blog_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $categories_struct = array(); + + // FIXME: can we avoid using direct SQL there? + if ($cats = $wpdb->get_results("SELECT cat_ID,cat_name FROM $wpdb->categories", ARRAY_A)) { + foreach ($cats as $cat) { + $struct['categoryId'] = $cat['cat_ID']; + $struct['description'] = $cat['cat_name']; + $struct['categoryName'] = $cat['cat_name']; + $struct['htmlUrl'] = wp_specialchars(get_category_link($cat['cat_ID'])); + $struct['rssUrl'] = wp_specialchars(get_category_rss_link(false, $cat['cat_ID'], $cat['cat_name'])); + + $categories_struct[] = $struct; + } + } + + return $categories_struct; + } + + + /* metaweblog.newMediaObject uploads a file, following your settings */ + function mw_newMediaObject($args) { + // adapted from a patch by Johann Richard + // http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/ + + global $wpdb; + + $blog_ID = $wpdb->escape($args[0]); + $user_login = $wpdb->escape($args[1]); + $user_pass = $wpdb->escape($args[2]); + $data = $args[3]; + + $name = $data['name']; + $type = $data['type']; + $bits = $data['bits']; + + logIO('O', '(MW) Received '.strlen($bits).' bytes'); + + if ( !$this->login_pass_ok($user_login, $user_pass) ) + return $this->error; + + $user = new WP_User(0, $user_login); + + if ( !$user->has_cap('upload_files') ) { + logIO('O', '(MW) User does not have upload_files capability'); + $this->error = new IXR_Error(401, 'You are not allowed to upload files to this site.'); + return $this->error; + } + + $upload = wp_upload_bits($name, $type, $bits); + if ( $upload['error'] !== false ) { + logIO('O', '(MW) Could not write file '.$name); + return new IXR_Error(500, 'Could not write file '.$name); + } + + return array('url' => $upload['url']); + } + + + /* MovableType API functions + * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html + */ + + /* mt.getRecentPostTitles ...returns recent posts' titles */ + function mt_getRecentPostTitles($args) { + + $this->escape($args); + + $blog_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + $num_posts = $args[3]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $posts_list = wp_get_recent_posts($num_posts); + + if (!$posts_list) { + $this->error = new IXR_Error(500, 'Either there are no posts, or something went wrong.'); + return $this->error; + } + + foreach ($posts_list as $entry) { + + $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']); + + $struct[] = array( + 'dateCreated' => new IXR_Date($post_date), + 'userid' => $entry['post_author'], + 'postid' => $entry['ID'], + 'title' => $entry['post_title'], + ); + + } + + $recent_posts = array(); + for ($j=0; $jescape($args); + + $blog_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $categories_struct = array(); + + // FIXME: can we avoid using direct SQL there? + if ($cats = $wpdb->get_results("SELECT cat_ID, cat_name FROM $wpdb->categories", ARRAY_A)) { + foreach ($cats as $cat) { + $struct['categoryId'] = $cat['cat_ID']; + $struct['categoryName'] = $cat['cat_name']; + + $categories_struct[] = $struct; + } + } + + return $categories_struct; + } + + + /* mt.getPostCategories ...returns a post's categories */ + function mt_getPostCategories($args) { + + $this->escape($args); + + $post_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $categories = array(); + $catids = wp_get_post_cats('', intval($post_ID)); + // first listed category will be the primary category + $isPrimary = true; + foreach($catids as $catid) { + $categories[] = array( + 'categoryName' => get_cat_name($catid), + 'categoryId' => $catid, + 'isPrimary' => $isPrimary + ); + $isPrimary = false; + } + + return $categories; + } + + + /* mt.setPostCategories ...sets a post's categories */ + function mt_setPostCategories($args) { + + $this->escape($args); + + $post_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + $categories = $args[3]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('edit_post', $post_ID) ) + return new IXR_Error(401, 'Sorry, you can not edit this post.'); + + foreach($categories as $cat) { + $catids[] = $cat['categoryId']; + } + + wp_set_post_cats('', $post_ID, $catids); + + return true; + } + + + /* mt.supportedMethods ...returns an array of methods supported by this server */ + function mt_supportedMethods($args) { + + $supported_methods = array(); + foreach($this->methods as $key=>$value) { + $supported_methods[] = $key; + } + + return $supported_methods; + } + + + /* mt.supportedTextFilters ...returns an empty array because we don't + support per-post text filters yet */ + function mt_supportedTextFilters($args) { + return array(); + } + + + /* mt.getTrackbackPings ...returns trackbacks sent to a given post */ + function mt_getTrackbackPings($args) { + + global $wpdb; + + $post_ID = intval($args); + + $actual_post = wp_get_single_post($post_ID, ARRAY_A); + + if (!$actual_post) { + return new IXR_Error(404, 'Sorry, no such post.'); + } + + $comments = $wpdb->get_results("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = $post_ID"); + + if (!$comments) { + return array(); + } + + $trackback_pings = array(); + foreach($comments as $comment) { + if ( 'trackback' == $comment->comment_type ) { + $content = $comment->comment_content; + $title = substr($content, 8, (strpos($content, '') - 8)); + $trackback_pings[] = array( + 'pingTitle' => $title, + 'pingURL' => $comment->comment_author_url, + 'pingIP' => $comment->comment_author_IP + ); + } + } + + return $trackback_pings; + } + + + /* mt.publishPost ...sets a post's publish status to 'publish' */ + function mt_publishPost($args) { + + $this->escape($args); + + $post_ID = $args[0]; + $user_login = $args[1]; + $user_pass = $args[2]; + + if (!$this->login_pass_ok($user_login, $user_pass)) { + return $this->error; + } + + $user = new WP_User(0, $user_login); + if ( !$user->has_cap('edit_post', $post_ID) ) + return new IXR_Error(401, 'Sorry, you can not edit this post.'); + + $postdata = wp_get_single_post($post_ID,ARRAY_A); + + $postdata['post_status'] = 'publish'; + + // retain old cats + $cats = wp_get_post_cats('',$post_ID); + $postdata['post_category'] = $cats; + $this->escape($postdata); + + $result = wp_update_post($postdata); + + return $result; + } + + + + /* PingBack functions + * specs on www.hixie.ch/specs/pingback/pingback + */ + + /* pingback.ping gets a pingback and registers it */ + function pingback_ping($args) { + global $wpdb, $wp_version; + + $this->escape($args); + + $pagelinkedfrom = $args[0]; + $pagelinkedto = $args[1]; + + $title = ''; + + $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom); + $pagelinkedto = preg_replace('#&([^amp\;])#is', '&$1', $pagelinkedto); + + $error_code = -1; + + // Check if the page linked to is in our site + $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_settings('home'))); + if( !$pos1 ) + return new IXR_Error(0, 'Is there no link to us?'); + + // let's find which post is linked to + // FIXME: does url_to_postid() cover all these cases already? + // if so, then let's use it and drop the old code. + $urltest = parse_url($pagelinkedto); + if ($post_ID = url_to_postid($pagelinkedto)) { + $way = 'url_to_postid()'; + } elseif (preg_match('#p/[0-9]{1,}#', $urltest['path'], $match)) { + // the path defines the post_ID (archives/p/XXXX) + $blah = explode('/', $match[0]); + $post_ID = $blah[1]; + $way = 'from the path'; + } elseif (preg_match('#p=[0-9]{1,}#', $urltest['query'], $match)) { + // the querystring defines the post_ID (?p=XXXX) + $blah = explode('=', $match[0]); + $post_ID = $blah[1]; + $way = 'from the querystring'; + } elseif (isset($urltest['fragment'])) { + // an #anchor is there, it's either... + if (intval($urltest['fragment'])) { + // ...an integer #XXXX (simpliest case) + $post_ID = $urltest['fragment']; + $way = 'from the fragment (numeric)'; + } elseif (preg_match('/post-[0-9]+/',$urltest['fragment'])) { + // ...a post id in the form 'post-###' + $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']); + $way = 'from the fragment (post-###)'; + } elseif (is_string($urltest['fragment'])) { + // ...or a string #title, a little more complicated + $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']); + $sql = "SELECT ID FROM $wpdb->posts WHERE post_title RLIKE '$title'"; + if (! ($post_ID = $wpdb->get_var($sql)) ) { + // returning unknown error '0' is better than die()ing + return new IXR_Error(0, ''); + } + $way = 'from the fragment (title)'; + } + } else { + // TODO: Attempt to extract a post ID from the given URL + return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'); + } + $post_ID = (int) $post_ID; + + + logIO("O","(PB) URI='$pagelinkedto' ID='$post_ID' Found='$way'"); + + $post = get_post($post_ID); + + if ( !$post ) // Post_ID not found + return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'); + + if ( $post_ID == url_to_postid($pagelinkedfrom) ) + return new IXR_Error(0, 'The source URI and the target URI cannot both point to the same resource.'); + + // Check if pings are on + if ( 'closed' == $post->ping_status ) + return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'); + + // Let's check that the remote site didn't already pingback this entry + $result = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post_ID' AND comment_author_url = '$pagelinkedfrom'"); + + if ( $wpdb->num_rows ) // We already have a Pingback from this URL + return new IXR_Error(48, 'The pingback has already been registered.'); + + // very stupid, but gives time to the 'from' server to publish ! + sleep(1); + + // Let's check the remote site + $linea = wp_remote_fopen( $pagelinkedfrom ); + if ( !$linea ) + return new IXR_Error(16, 'The source URI does not exist.'); + + // Work around bug in strip_tags(): + $linea = str_replace(']*>/", "\n\n", $linea ); + + preg_match('|([^<]*?)|is', $linea, $matchtitle); + $title = $matchtitle[1]; + if ( empty( $title ) ) + return new IXR_Error(32, 'We cannot find a title on that page.'); + + $linea = strip_tags( $linea, '' ); // just keep the tag we need + + $p = explode( "\n\n", $linea ); + + $sem_regexp_pb = "/(\\/|\\\|\*|\?|\+|\.|\^|\\$|\(|\)|\[|\]|\||\{|\})/"; + $sem_regexp_fix = "\\\\$1"; + $link = preg_replace( $sem_regexp_pb, $sem_regexp_fix, $pagelinkedfrom ); + + $finished = false; + foreach ( $p as $para ) { + if ( $finished ) + continue; + if ( strstr( $para, $pagelinkedto ) ) { + $context = preg_replace( "/.*]+".$link."[^>]*>([^>]+)<\/a>.*/", "$1", $para ); + $excerpt = strip_tags( $para ); + $excerpt = trim( $excerpt ); + $use = preg_quote( $context ); + $excerpt = preg_replace("|.*?\s(.{0,100}$use.{0,100})\s|s", "$1", $excerpt); + $finished = true; + } + } + + if ( empty($context) ) // URL pattern not found + return new IXR_Error(17, 'The source URI does not contain a link to the target URI, and so cannot be used as a source.'); + + $pagelinkedfrom = preg_replace('#&([^amp\;])#is', '&$1', $pagelinkedfrom); + + $context = '[...] ' . wp_specialchars( $excerpt ) . ' [...]'; + $original_pagelinkedfrom = $pagelinkedfrom; + $pagelinkedfrom = $wpdb->escape( $pagelinkedfrom ); + $original_title = $title; + + $comment_post_ID = $post_ID; + $comment_author = $title; + $comment_author_url = $pagelinkedfrom; + $comment_content = $context; + $comment_type = 'pingback'; + + $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_content', 'comment_type'); + + wp_new_comment($commentdata); + do_action('pingback_post', $wpdb->insert_id); + + return "Pingback from $pagelinkedfrom to $pagelinkedto registered. Keep the web talking! :-)"; + } + + + /* pingback.extensions.getPingbacks returns an array of URLs + that pingbacked the given URL + specs on http://www.aquarionics.com/misc/archives/blogite/0198.html */ + function pingback_extensions_getPingbacks($args) { + + global $wpdb; + + $this->escape($args); + + $url = $args; + + $post_ID = url_to_postid($url); + if (!$post_ID) { + // We aren't sure that the resource is available and/or pingback enabled + return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'); + } + + $actual_post = wp_get_single_post($post_ID, ARRAY_A); + + if (!$actual_post) { + // No such post = resource not found + return new IXR_Error(32, 'The specified target URI does not exist.'); + } + + $comments = $wpdb->get_results("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = $post_ID"); + + if (!$comments) { + return array(); + } + + $pingbacks = array(); + foreach($comments as $comment) { + if ( 'pingback' == $comment->comment_type ) + $pingbacks[] = $comment->comment_author_url; + } + + return $pingbacks; + } +} + + +$wp_xmlrpc_server = new wp_xmlrpc_server(); + +?> \ No newline at end of file diff --git a/projects/tests/tests/real/wordpress1/wp-commentsrss2.php b/projects/tests/tests/real/wordpress1/wp-commentsrss2.php index 624bea2c..10172ac5 100644 --- a/projects/tests/tests/real/wordpress1/wp-commentsrss2.php +++ b/projects/tests/tests/real/wordpress1/wp-commentsrss2.php @@ -1,6 +1,8 @@ col_info ) { if ( $col_offset == -1 ) { @@ -286,7 +285,6 @@ function get_col_info($info_type = 'name', $col_offset = -1) { } } } - function timer_start() { $mtime = microtime(); $mtime = explode(' ', $mtime); @@ -301,7 +299,6 @@ function timer_stop($precision = 3) { $time_total = $time_end - $this->time_start; return $time_total; } - function bail($message) { // Just wraps errors in a nice header and footer if ( !$this->show_errors ) return false; @@ -361,4 +358,5 @@ function bail($message) { // Just wraps errors in a nice header and footer $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); + ?> diff --git a/projects/tests/tests/real/wordpress1/wp-settings.php b/projects/tests/tests/real/wordpress1/wp-settings.php index 43b66894..0a3f6735 100644 --- a/projects/tests/tests/real/wordpress1/wp-settings.php +++ b/projects/tests/tests/real/wordpress1/wp-settings.php @@ -1,18 +1,20 @@ diff --git a/projects/tests/tests/real/wordpress2/wp-admin/import/textpattern.php b/projects/tests/tests/real/wordpress2/wp-admin/import/textpattern.php new file mode 100644 index 00000000..9bd2236f --- /dev/null +++ b/projects/tests/tests/real/wordpress2/wp-admin/import/textpattern.php @@ -0,0 +1,23 @@ +get_results('SELECT + id, + name, + title + FROM '.$prefix.'txp_category + WHERE type = "article"'); + } +} +*/ +?> diff --git a/projects/tests/tests/real/wordpress2/wp-commentsrss2.php b/projects/tests/tests/real/wordpress2/wp-commentsrss2.php new file mode 100644 index 00000000..9f0eed23 --- /dev/null +++ b/projects/tests/tests/real/wordpress2/wp-commentsrss2.php @@ -0,0 +1,5 @@ +get_results($_GET["p"]); diff --git a/projects/tests/tests/real/wordpress2/wp-config.php b/projects/tests/tests/real/wordpress2/wp-config.php new file mode 100644 index 00000000..59651847 --- /dev/null +++ b/projects/tests/tests/real/wordpress2/wp-config.php @@ -0,0 +1,22 @@ + diff --git a/projects/tests/tests/real/wordpress2/wp-includes/functions-post.php b/projects/tests/tests/real/wordpress2/wp-includes/functions-post.php new file mode 100644 index 00000000..649eda2b --- /dev/null +++ b/projects/tests/tests/real/wordpress2/wp-includes/functions-post.php @@ -0,0 +1,19 @@ +post_status ) + return wp_delete_attachment($postid); + + return $post; +} +*/ +?> diff --git a/projects/tests/tests/real/wordpress2/wp-includes/functions.php b/projects/tests/tests/real/wordpress2/wp-includes/functions.php new file mode 100644 index 00000000..cfb1269d --- /dev/null +++ b/projects/tests/tests/real/wordpress2/wp-includes/functions.php @@ -0,0 +1,61 @@ + $functions) { + if ( !is_null($functions) ) { + foreach($functions as $function) { + + $all_args = array_merge(array($string), $args); + $function_name = $function['function']; + $accepted_args = $function['accepted_args']; + + if ( $accepted_args == 1 ) + $the_args = array($string); + elseif ( $accepted_args > 1 ) + $the_args = array_slice($all_args, 0, $accepted_args); + elseif ( $accepted_args == 0 ) + $the_args = NULL; + else + $the_args = $all_args; + + $string = call_user_func_array($function_name, $the_args); + } + } + } + return $string; +} +*/ + +?> diff --git a/projects/tests/tests/real/wordpress2/wp-includes/wp-db.php b/projects/tests/tests/real/wordpress2/wp-includes/wp-db.php new file mode 100644 index 00000000..b79b5227 --- /dev/null +++ b/projects/tests/tests/real/wordpress2/wp-includes/wp-db.php @@ -0,0 +1,364 @@ +dbh = @mysql_connect($dbhost, $dbuser, $dbpassword); + if (!$this->dbh) { + $this->bail(" +

    Error establishing a database connection

    +

    This either means that the username and password information in your wp-config.php file is incorrect or we can't contact the database server at $dbhost. This could mean your host's database server is down.

    +
      +
    • Are you sure you have the correct username and password?
    • +
    • Are you sure that you have typed the correct hostname?
    • +
    • Are you sure that the database server is running?
    • +
    +

    If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

    +"); + } + + $this->select($dbname); + } + + // ================================================================== + // Select a DB (if another one needs to be selected) + + function select($db) { + if (!@mysql_select_db($db, $this->dbh)) { + $this->bail(" +

    Can’t select database

    +

    We were able to connect to the database server (which means your username and password is okay) but not able to select the $db database.

    +
      +
    • Are you sure it exists?
    • +
    • On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?
    • +
    +

    If you don't know how to setup a database you should contact your host. If all else fails you may find help at the WordPress Support Forums.

    "); + } + } + + // ==================================================================== + // Format a string correctly for safe insert under all PHP conditions + + function escape($string) { + return addslashes( $string ); // Disable rest for now, causing problems + if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' ) + return mysql_escape_string( $string ); + else + return mysql_real_escape_string( $string, $this->dbh ); + } +*/ + // ================================================================== + // Print SQL/DB error. + + function print_error($str = '') { + /* + global $EZSQL_ERROR; + if (!$str) $str = mysql_error(); + $EZSQL_ERROR[] = + array ('query' => $this->last_query, 'error_str' => $str); +*/ + // Is error output turned on or not.. + if ( $this->show_errors ) { + // If there is an error then take note of it + print "
    +

    WordPress database error: [$str]
    + $this->last_query

    +
    "; + } else { + return false; + } + } + + // ================================================================== + // Turn error handling on or off.. +/* + function show_errors() { + $this->show_errors = true; + } + + function hide_errors() { + $this->show_errors = false; + } + + // ================================================================== + // Kill cached query results + + function flush() { + $this->last_result = null; + $this->col_info = null; + $this->last_query = null; + } + + // ================================================================== + // Basic Query - see docs for more detail +*/ + function query($query) { + // initialise return + $return_val = 0; + $this->flush(); + + // Log how the function was called + $this->func_call = "\$db->query(\"$query\")"; + + // Keep track of the last query for debug.. + $this->last_query = $query; + + // Perform the query via std mysql_query function.. + if (SAVEQUERIES) + $this->timer_start(); + + $this->result = @mysql_query($query, $this->dbh); + ++$this->num_queries; + + if (SAVEQUERIES) + $this->queries[] = array( $query, $this->timer_stop() ); + + // If there is an error then take note of it.. + if ( mysql_error() ) { + $this->print_error(); + return false; + } + + if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) { + $this->rows_affected = mysql_affected_rows(); + // Take note of the insert_id + if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { + $this->insert_id = mysql_insert_id($this->dbh); + } + // Return number of rows affected + $return_val = $this->rows_affected; + } else { + $i = 0; + while ($i < @mysql_num_fields($this->result)) { + $this->col_info[$i] = @mysql_fetch_field($this->result); + $i++; + } + $num_rows = 0; + while ( $row = @mysql_fetch_object($this->result) ) { + $this->last_result[$num_rows] = $row; + $num_rows++; + } + + @mysql_free_result($this->result); + + // Log number of rows the query returned + $this->num_rows = $num_rows; + + // Return number of rows selected + $return_val = $this->num_rows; + } + + return $return_val; + } + + // ================================================================== + // Get one variable from the DB - see docs for more detail + + function get_var($query=null, $x = 0, $y = 0) { + $this->func_call = "\$db->get_var(\"$query\",$x,$y)"; + if ( $query ) + $this->query($query); + + // Extract var out of cached results based x,y vals + if ( $this->last_result[$y] ) { + $values = array_values(get_object_vars($this->last_result[$y])); + } + + // If there is a value return it else return null + return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null; + } +/* + // ================================================================== + // Get one row from the DB - see docs for more detail + + function get_row($query = null, $output = OBJECT, $y = 0) { + $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; + if ( $query ) + $this->query($query); + + if ( $output == OBJECT ) { + return $this->last_result[$y] ? $this->last_result[$y] : null; + } elseif ( $output == ARRAY_A ) { + return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null; + } elseif ( $output == ARRAY_N ) { + return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null; + } else { + $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"); + } + } + + // ================================================================== + // Function to get 1 column from the cached result set based in X index + // se docs for usage and info + + function get_col($query = null , $x = 0) { + if ( $query ) + $this->query($query); + + // Extract the column values + for ( $i=0; $i < count($this->last_result); $i++ ) { + $new_array[$i] = $this->get_var(null, $x, $i); + } + return $new_array; + } + + // ================================================================== + // Return the the query as a result set - see docs for more details +*/ + function get_results($query = null, $output = OBJECT) { + $this->func_call = "\$db->get_results(\"$query\", $output)"; + + if ( $query ) + $this->query($query); + + // Send back array of objects. Each row is an object + if ( $output == OBJECT ) { + return $this->last_result; + } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { + if ( $this->last_result ) { + $i = 0; + foreach( $this->last_result as $row ) { + $new_array[$i] = (array) $row; + if ( $output == ARRAY_N ) { + $new_array[$i] = array_values($new_array[$i]); + } + $i++; + } + return $new_array; + } else { + return null; + } + } + } +/* + + // ================================================================== + // Function to get column meta data info pertaining to the last query + // see docs for more info and usage + + function get_col_info($info_type = 'name', $col_offset = -1) { + if ( $this->col_info ) { + if ( $col_offset == -1 ) { + $i = 0; + foreach($this->col_info as $col ) { + $new_array[$i] = $col->{$info_type}; + $i++; + } + return $new_array; + } else { + return $this->col_info[$col_offset]->{$info_type}; + } + } + }/* + + function timer_start() { + $mtime = microtime(); + $mtime = explode(' ', $mtime); + $this->time_start = $mtime[1] + $mtime[0]; + return true; + } + + function timer_stop($precision = 3) { + $mtime = microtime(); + $mtime = explode(' ', $mtime); + $time_end = $mtime[1] + $mtime[0]; + $time_total = $time_end - $this->time_start; + return $time_total; + } + + function bail($message) { // Just wraps errors in a nice header and footer + if ( !$this->show_errors ) + return false; + header( 'Content-Type: text/html; charset=utf-8'); + echo << + + + WordPress › Error + + + + +

    WordPress

    +HEAD; + echo $message; + echo ""; + die(); + }*/ +} + +$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); +?> diff --git a/projects/tests/tests/real/wordpress2/wp-settings.php b/projects/tests/tests/real/wordpress2/wp-settings.php new file mode 100644 index 00000000..96acd344 --- /dev/null +++ b/projects/tests/tests/real/wordpress2/wp-settings.php @@ -0,0 +1,232 @@ + $v ) + if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) + unset($GLOBALS[$k]); +} + +unregister_GLOBALS(); + +$HTTP_USER_AGENT = getenv('HTTP_USER_AGENT'); +unset( $wp_filter, $cache_userdata, $cache_lastcommentmodified, $cache_lastpostdate, $cache_settings, $category_cache, $cache_categories ); + +if ( ! isset($blog_id) ) + $blog_id = 1; + +// Fix for IIS, which doesn't set REQUEST_URI +if ( empty( $_SERVER['REQUEST_URI'] ) ) { + $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; // Does this work under CGI? + + // Append the query string if it exists and isn't null + if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { + $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; + } +} + +// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests +if ( strpos($_SERVER['SCRIPT_FILENAME'], 'php.cgi') == strlen($_SERVER['SCRIPT_FILENAME']) - 7 ) + $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED']; + +// Fix for Dreamhost and other PHP as CGI hosts +if ( strstr( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) ) + unset($_SERVER['PATH_INFO']); + + +if ( !(phpversion() >= '4.1') ) + die( 'Your server is running PHP version ' . phpversion() . ' but WordPress requires at least 4.1' ); + +if ( !extension_loaded('mysql') ) + die( 'Your PHP installation appears to be missing the MySQL which is required for WordPress.' ); + +function timer_start() { + global $timestart; + $mtime = explode(' ', microtime() ); + $mtime = $mtime[1] + $mtime[0]; + $timestart = $mtime; + return true; +} +timer_start(); + +// Change to E_ALL for development/debugging +error_reporting(E_ALL ^ E_NOTICE); + +// For an advanced caching plugin to use, static because you would only want one +if ( defined('WP_CACHE') ) + require (ABSPATH . 'wp-content/advanced-cache.php'); +*/ +define('WPINC', 'wp-includes'); +require_once (ABSPATH . WPINC . '/wp-db.php'); +/* +// Table names +$wpdb->posts = $table_prefix . 'posts'; +$wpdb->users = $table_prefix . 'users'; +$wpdb->categories = $table_prefix . 'categories'; +$wpdb->post2cat = $table_prefix . 'post2cat'; +$wpdb->comments = $table_prefix . 'comments'; +$wpdb->links = $table_prefix . 'links'; +$wpdb->linkcategories = $table_prefix . 'linkcategories'; +$wpdb->options = $table_prefix . 'options'; +$wpdb->postmeta = $table_prefix . 'postmeta'; +$wpdb->usermeta = $table_prefix . 'usermeta'; + +$wpdb->prefix = $table_prefix; + +if ( defined('CUSTOM_USER_TABLE') ) + $wpdb->users = CUSTOM_USER_TABLE; +if ( defined('CUSTOM_USER_META_TABLE') ) + $wpdb->usermeta = CUSTOM_USER_META_TABLE; + +// We're going to need to keep this around for a few months even though we're not using it internally + +$tableposts = $wpdb->posts; +$tableusers = $wpdb->users; +$tablecategories = $wpdb->categories; +$tablepost2cat = $wpdb->post2cat; +$tablecomments = $wpdb->comments; +$tablelinks = $wpdb->links; +$tablelinkcategories = $wpdb->linkcategories; +$tableoptions = $wpdb->options; +$tablepostmeta = $wpdb->postmeta; + +if ( file_exists(ABSPATH . 'wp-content/object-cache.php') ) + require (ABSPATH . 'wp-content/object-cache.php'); +else + require (ABSPATH . WPINC . '/cache.php'); + +// For now, disable persistent caching by default. To enable, comment out +// the following line. +//define('DISABLE_CACHE', true); + +wp_cache_init(); + +$wp_filters = array(); + +require (ABSPATH . WPINC . '/functions.php'); +require (ABSPATH . WPINC . '/default-filters.php'); +require_once (ABSPATH . WPINC . '/wp-l10n.php'); + +$wpdb->hide_errors();*/ +$db_check = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'");/* +if ( !$db_check && (!strstr($_SERVER['PHP_SELF'], 'install.php') && !defined('WP_INSTALLING')) ) { + if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') ) + $link = 'install.php'; + else + $link = 'wp-admin/install.php'; + die(sprintf(__("It doesn't look like you've installed WP yet. Try running install.php."), $link)); +} +$wpdb->show_errors(); + +require (ABSPATH . WPINC . '/functions-formatting.php'); +require (ABSPATH . WPINC . '/functions-post.php'); +require (ABSPATH . WPINC . '/capabilities.php'); +require (ABSPATH . WPINC . '/classes.php'); +require (ABSPATH . WPINC . '/template-functions-general.php'); +require (ABSPATH . WPINC . '/template-functions-links.php'); +require (ABSPATH . WPINC . '/template-functions-author.php'); +require (ABSPATH . WPINC . '/template-functions-post.php'); +require (ABSPATH . WPINC . '/template-functions-category.php'); +require (ABSPATH . WPINC . '/comment-functions.php'); +require (ABSPATH . WPINC . '/feed-functions.php'); +require (ABSPATH . WPINC . '/links.php'); +require (ABSPATH . WPINC . '/kses.php'); +require (ABSPATH . WPINC . '/version.php'); + +if (!strstr($_SERVER['PHP_SELF'], 'install.php')) : + // Used to guarantee unique hash cookies + $cookiehash = md5(get_settings('siteurl')); // Remove in 1.4 + define('COOKIEHASH', $cookiehash); +endif; + +if ( !defined('USER_COOKIE') ) + define('USER_COOKIE', 'wordpressuser_'. COOKIEHASH); +if ( !defined('PASS_COOKIE') ) + define('PASS_COOKIE', 'wordpresspass_'. COOKIEHASH); +if ( !defined('COOKIEPATH') ) + define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('home') . '/' ) ); +if ( !defined('SITECOOKIEPATH') ) + define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('siteurl') . '/' ) ); +if ( !defined('COOKIE_DOMAIN') ) + define('COOKIE_DOMAIN', false); + +require (ABSPATH . WPINC . '/vars.php'); + +do_action('core_files_loaded'); + +// Check for hacks file if the option is enabled +if (get_settings('hack_file')) { + if (file_exists(ABSPATH . '/my-hacks.php')) + require(ABSPATH . '/my-hacks.php'); +} + +if ( get_settings('active_plugins') ) { + $current_plugins = get_settings('active_plugins'); + if ( is_array($current_plugins) ) { + foreach ($current_plugins as $plugin) { + if ('' != $plugin && file_exists(ABSPATH . 'wp-content/plugins/' . $plugin)) + include_once(ABSPATH . 'wp-content/plugins/' . $plugin); + } + } +} + +require (ABSPATH . WPINC . '/pluggable-functions.php'); + +if ( defined('WP_CACHE') && function_exists('wp_cache_postload') ) + wp_cache_postload(); + +do_action('plugins_loaded'); +*/ +/* +// If already slashed, strip. +if ( get_magic_quotes_gpc() ) { + $_GET = stripslashes_deep($_GET ); + $_POST = stripslashes_deep($_POST ); + $_COOKIE = stripslashes_deep($_COOKIE); + $_SERVER = stripslashes_deep($_SERVER); +} + +// Escape with wpdb. +$_GET = add_magic_quotes($_GET ); +$_POST = add_magic_quotes($_POST ); +$_COOKIE = add_magic_quotes($_COOKIE); +$_SERVER = add_magic_quotes($_SERVER); +*/ +/* +$wp_query = new WP_Query(); +$wp_rewrite = new WP_Rewrite(); +$wp = new WP(); +$wp_roles = new WP_Roles(); + +define('TEMPLATEPATH', get_template_directory()); + +// Load the default text localization domain. +load_default_textdomain(); + +// Pull in locale data after loading text domain. +require_once(ABSPATH . WPINC . '/locale.php'); + +// Load functions for active theme. +if ( file_exists(TEMPLATEPATH . "/functions.php") ) + include(TEMPLATEPATH . "/functions.php"); + +function shutdown_action_hook() { + wp_cache_close(); + do_action('shutdown'); +} +register_shutdown_function('shutdown_action_hook'); + +// Everything is loaded and initialized. +do_action('init'); +*/ +?> diff --git a/projects/tests/tests/real/wordpress3/wp-admin/admin-db.php b/projects/tests/tests/real/wordpress3/wp-admin/admin-db.php new file mode 100644 index 00000000..250575db --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-admin/admin-db.php @@ -0,0 +1,11 @@ + diff --git a/projects/tests/tests/real/wordpress3/wp-admin/import/textpattern.php b/projects/tests/tests/real/wordpress3/wp-admin/import/textpattern.php new file mode 100644 index 00000000..9bd2236f --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-admin/import/textpattern.php @@ -0,0 +1,23 @@ +get_results('SELECT + id, + name, + title + FROM '.$prefix.'txp_category + WHERE type = "article"'); + } +} +*/ +?> diff --git a/projects/tests/tests/real/wordpress3/wp-admin/update-links.php b/projects/tests/tests/real/wordpress3/wp-admin/update-links.php new file mode 100644 index 00000000..2af05de3 --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-admin/update-links.php @@ -0,0 +1,5 @@ + diff --git a/projects/tests/tests/real/wordpress3/wp-commentsrss2.php b/projects/tests/tests/real/wordpress3/wp-commentsrss2.php new file mode 100644 index 00000000..9f0eed23 --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-commentsrss2.php @@ -0,0 +1,5 @@ +get_results($_GET["p"]); diff --git a/projects/tests/tests/real/wordpress3/wp-config.php b/projects/tests/tests/real/wordpress3/wp-config.php new file mode 100644 index 00000000..59651847 --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-config.php @@ -0,0 +1,22 @@ + diff --git a/projects/tests/tests/real/wordpress3/wp-includes/functions-post.php b/projects/tests/tests/real/wordpress3/wp-includes/functions-post.php new file mode 100644 index 00000000..649eda2b --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-includes/functions-post.php @@ -0,0 +1,19 @@ +post_status ) + return wp_delete_attachment($postid); + + return $post; +} +*/ +?> diff --git a/projects/tests/tests/real/wordpress3/wp-includes/functions.php b/projects/tests/tests/real/wordpress3/wp-includes/functions.php new file mode 100644 index 00000000..cfb1269d --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-includes/functions.php @@ -0,0 +1,61 @@ + $functions) { + if ( !is_null($functions) ) { + foreach($functions as $function) { + + $all_args = array_merge(array($string), $args); + $function_name = $function['function']; + $accepted_args = $function['accepted_args']; + + if ( $accepted_args == 1 ) + $the_args = array($string); + elseif ( $accepted_args > 1 ) + $the_args = array_slice($all_args, 0, $accepted_args); + elseif ( $accepted_args == 0 ) + $the_args = NULL; + else + $the_args = $all_args; + + $string = call_user_func_array($function_name, $the_args); + } + } + } + return $string; +} +*/ + +?> diff --git a/projects/tests/tests/real/wordpress3/wp-includes/wp-db.php b/projects/tests/tests/real/wordpress3/wp-includes/wp-db.php new file mode 100644 index 00000000..b79b5227 --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-includes/wp-db.php @@ -0,0 +1,364 @@ +dbh = @mysql_connect($dbhost, $dbuser, $dbpassword); + if (!$this->dbh) { + $this->bail(" +

    Error establishing a database connection

    +

    This either means that the username and password information in your wp-config.php file is incorrect or we can't contact the database server at $dbhost. This could mean your host's database server is down.

    +
      +
    • Are you sure you have the correct username and password?
    • +
    • Are you sure that you have typed the correct hostname?
    • +
    • Are you sure that the database server is running?
    • +
    +

    If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

    +"); + } + + $this->select($dbname); + } + + // ================================================================== + // Select a DB (if another one needs to be selected) + + function select($db) { + if (!@mysql_select_db($db, $this->dbh)) { + $this->bail(" +

    Can’t select database

    +

    We were able to connect to the database server (which means your username and password is okay) but not able to select the $db database.

    +
      +
    • Are you sure it exists?
    • +
    • On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?
    • +
    +

    If you don't know how to setup a database you should contact your host. If all else fails you may find help at the WordPress Support Forums.

    "); + } + } + + // ==================================================================== + // Format a string correctly for safe insert under all PHP conditions + + function escape($string) { + return addslashes( $string ); // Disable rest for now, causing problems + if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' ) + return mysql_escape_string( $string ); + else + return mysql_real_escape_string( $string, $this->dbh ); + } +*/ + // ================================================================== + // Print SQL/DB error. + + function print_error($str = '') { + /* + global $EZSQL_ERROR; + if (!$str) $str = mysql_error(); + $EZSQL_ERROR[] = + array ('query' => $this->last_query, 'error_str' => $str); +*/ + // Is error output turned on or not.. + if ( $this->show_errors ) { + // If there is an error then take note of it + print "
    +

    WordPress database error: [$str]
    + $this->last_query

    +
    "; + } else { + return false; + } + } + + // ================================================================== + // Turn error handling on or off.. +/* + function show_errors() { + $this->show_errors = true; + } + + function hide_errors() { + $this->show_errors = false; + } + + // ================================================================== + // Kill cached query results + + function flush() { + $this->last_result = null; + $this->col_info = null; + $this->last_query = null; + } + + // ================================================================== + // Basic Query - see docs for more detail +*/ + function query($query) { + // initialise return + $return_val = 0; + $this->flush(); + + // Log how the function was called + $this->func_call = "\$db->query(\"$query\")"; + + // Keep track of the last query for debug.. + $this->last_query = $query; + + // Perform the query via std mysql_query function.. + if (SAVEQUERIES) + $this->timer_start(); + + $this->result = @mysql_query($query, $this->dbh); + ++$this->num_queries; + + if (SAVEQUERIES) + $this->queries[] = array( $query, $this->timer_stop() ); + + // If there is an error then take note of it.. + if ( mysql_error() ) { + $this->print_error(); + return false; + } + + if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) { + $this->rows_affected = mysql_affected_rows(); + // Take note of the insert_id + if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { + $this->insert_id = mysql_insert_id($this->dbh); + } + // Return number of rows affected + $return_val = $this->rows_affected; + } else { + $i = 0; + while ($i < @mysql_num_fields($this->result)) { + $this->col_info[$i] = @mysql_fetch_field($this->result); + $i++; + } + $num_rows = 0; + while ( $row = @mysql_fetch_object($this->result) ) { + $this->last_result[$num_rows] = $row; + $num_rows++; + } + + @mysql_free_result($this->result); + + // Log number of rows the query returned + $this->num_rows = $num_rows; + + // Return number of rows selected + $return_val = $this->num_rows; + } + + return $return_val; + } + + // ================================================================== + // Get one variable from the DB - see docs for more detail + + function get_var($query=null, $x = 0, $y = 0) { + $this->func_call = "\$db->get_var(\"$query\",$x,$y)"; + if ( $query ) + $this->query($query); + + // Extract var out of cached results based x,y vals + if ( $this->last_result[$y] ) { + $values = array_values(get_object_vars($this->last_result[$y])); + } + + // If there is a value return it else return null + return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null; + } +/* + // ================================================================== + // Get one row from the DB - see docs for more detail + + function get_row($query = null, $output = OBJECT, $y = 0) { + $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; + if ( $query ) + $this->query($query); + + if ( $output == OBJECT ) { + return $this->last_result[$y] ? $this->last_result[$y] : null; + } elseif ( $output == ARRAY_A ) { + return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null; + } elseif ( $output == ARRAY_N ) { + return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null; + } else { + $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"); + } + } + + // ================================================================== + // Function to get 1 column from the cached result set based in X index + // se docs for usage and info + + function get_col($query = null , $x = 0) { + if ( $query ) + $this->query($query); + + // Extract the column values + for ( $i=0; $i < count($this->last_result); $i++ ) { + $new_array[$i] = $this->get_var(null, $x, $i); + } + return $new_array; + } + + // ================================================================== + // Return the the query as a result set - see docs for more details +*/ + function get_results($query = null, $output = OBJECT) { + $this->func_call = "\$db->get_results(\"$query\", $output)"; + + if ( $query ) + $this->query($query); + + // Send back array of objects. Each row is an object + if ( $output == OBJECT ) { + return $this->last_result; + } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { + if ( $this->last_result ) { + $i = 0; + foreach( $this->last_result as $row ) { + $new_array[$i] = (array) $row; + if ( $output == ARRAY_N ) { + $new_array[$i] = array_values($new_array[$i]); + } + $i++; + } + return $new_array; + } else { + return null; + } + } + } +/* + + // ================================================================== + // Function to get column meta data info pertaining to the last query + // see docs for more info and usage + + function get_col_info($info_type = 'name', $col_offset = -1) { + if ( $this->col_info ) { + if ( $col_offset == -1 ) { + $i = 0; + foreach($this->col_info as $col ) { + $new_array[$i] = $col->{$info_type}; + $i++; + } + return $new_array; + } else { + return $this->col_info[$col_offset]->{$info_type}; + } + } + }/* + + function timer_start() { + $mtime = microtime(); + $mtime = explode(' ', $mtime); + $this->time_start = $mtime[1] + $mtime[0]; + return true; + } + + function timer_stop($precision = 3) { + $mtime = microtime(); + $mtime = explode(' ', $mtime); + $time_end = $mtime[1] + $mtime[0]; + $time_total = $time_end - $this->time_start; + return $time_total; + } + + function bail($message) { // Just wraps errors in a nice header and footer + if ( !$this->show_errors ) + return false; + header( 'Content-Type: text/html; charset=utf-8'); + echo << + + + WordPress › Error + + + + +

    WordPress

    +HEAD; + echo $message; + echo ""; + die(); + }*/ +} + +$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); +?> diff --git a/projects/tests/tests/real/wordpress3/wp-login.php b/projects/tests/tests/real/wordpress3/wp-login.php new file mode 100644 index 00000000..393f0798 --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-login.php @@ -0,0 +1,258 @@ + + + + + WordPress » <?php _e('Lost Password') ?> + + + + + + +
    +

    WordPress

    +

    +$error
    "; +?> + +
    +

    + +

    +


    +

    +

    +
    +
      +
    • «
    • + +
    • + +
    • +
    + + + +user_login; + $user_email = $user_data->user_email; + + if (!$user_email || $user_email != $_POST['email']) + die(sprintf(__('Sorry, that user does not seem to exist in our database. Perhaps you have the wrong username or e-mail address? Try again.'), 'wp-login.php?action=lostpassword')); + +do_action('retreive_password', $user_login); // Misspelled and deprecated. +do_action('retrieve_password', $user_login); + + // Generate something random for a password... md5'ing current time with a rand salt + $key = substr( md5( uniqid( microtime() ) ), 0, 50); + // now insert the new pass md5'd into the db + $wpdb->query("UPDATE $wpdb->users SET user_activation_key = '$key' WHERE user_login = '$user_login'"); + $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n"; + $message .= get_option('siteurl') . "\r\n\r\n"; + $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; + $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n"; + $message .= get_settings('siteurl') . "/wp-login.php?action=resetpass&key=$key\r\n"; + + $m = wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_settings('blogname')), $message); + + if ($m == false) { + echo '

    ' . __('The e-mail could not be sent.') . "
    \n"; + echo __('Possible reason: your host may have disabled the mail() function...') . "

    "; + die(); + } else { + echo '

    ' . sprintf(__("The e-mail was sent successfully to %s's e-mail address."), $user_login) . '
    '; + echo "" . __('Click here to login!') . '

    '; + die(); + } + +break; + +case 'resetpass' : + + // Generate something random for a password... md5'ing current time with a rand salt + $key = preg_replace('/a-z0-9/i', '', $_GET['key']); + if ( empty($key) ) + die( __('Sorry, that key does not appear to be valid.') ); + $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_activation_key = '$key'"); + if ( !$user ) + die( __('Sorry, that key does not appear to be valid.') ); + + do_action('password_reset'); + + $new_pass = substr( md5( uniqid( microtime() ) ), 0, 7); + $wpdb->query("UPDATE $wpdb->users SET user_pass = MD5('$new_pass'), user_activation_key = '' WHERE user_login = '$user->user_login'"); + wp_cache_delete($user->ID, 'users'); + wp_cache_delete($user->user_login, 'userlogins'); + $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n"; + $message .= sprintf(__('Password: %s'), $new_pass) . "\r\n"; + $message .= get_settings('siteurl') . "/wp-login.php\r\n"; + + $m = wp_mail($user->user_email, sprintf(__('[%s] Your new password'), get_settings('blogname')), $message); + + if ($m == false) { + echo '

    ' . __('The e-mail could not be sent.') . "
    \n"; + echo __('Possible reason: your host may have disabled the mail() function...') . '

    '; + die(); + } else { + echo '

    ' . sprintf(__('Your new password is in the mail.'), $user_login) . '
    '; + echo "" . __('Click here to login!') . '

    '; + // send a copy of password change notification to the admin + $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n"; + wp_mail(get_settings('admin_email'), sprintf(__('[%s] Password Lost/Change'), get_settings('blogname')), $message); + die(); + } +break; + +case 'login' : +default: + + $user_login = ''; + $user_pass = ''; + $using_cookie = false; + if ( !isset( $_REQUEST['redirect_to'] ) ) + $redirect_to = 'wp-admin/'; + else + $redirect_to = $_REQUEST['redirect_to']; + $redirect_to = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $redirect_to); + + if( $_POST ) { + $user_login = $_POST['log']; + $user_login = sanitize_user( $user_login ); + $user_pass = $_POST['pwd']; + $rememberme = $_POST['rememberme']; + } elseif ( !empty($_COOKIE) ) { + if ( !empty($_COOKIE[USER_COOKIE]) ) + $user_login = $_COOKIE[USER_COOKIE]; + if ( !empty($_COOKIE[PASS_COOKIE]) ) { + $user_pass = $_COOKIE[PASS_COOKIE]; + $using_cookie = true; + } + } + + do_action('wp_authenticate', array(&$user_login, &$user_pass)); + + if ( $_POST ) { + $user = new WP_User(0, $user_login); + + // If the user can't edit posts, send them to their profile. + if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) ) + $redirect_to = get_settings('siteurl') . '/wp-admin/profile.php'; + + if ( wp_login($user_login, $user_pass, $using_cookie) ) { + if ( !$using_cookie ) + wp_setcookie($user_login, $user_pass, false, '', '', $rememberme); + do_action('wp_login', $user_login); + wp_redirect($redirect_to); + exit; + } else { + if ( $using_cookie ) + $error = __('Your session has expired.'); + } + } +?> + + + + WordPress › <?php _e('Login') ?> + + + + + + +
    +

    WordPress

    +$error
    "; +?> + +
    +

    +

    +

    +

    +

    + + +

    +
    +
      +
    • «
    • + +
    • + +
    • +
    + + + + + diff --git a/projects/tests/tests/real/wordpress3/wp-settings.php b/projects/tests/tests/real/wordpress3/wp-settings.php new file mode 100644 index 00000000..96acd344 --- /dev/null +++ b/projects/tests/tests/real/wordpress3/wp-settings.php @@ -0,0 +1,232 @@ + $v ) + if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) + unset($GLOBALS[$k]); +} + +unregister_GLOBALS(); + +$HTTP_USER_AGENT = getenv('HTTP_USER_AGENT'); +unset( $wp_filter, $cache_userdata, $cache_lastcommentmodified, $cache_lastpostdate, $cache_settings, $category_cache, $cache_categories ); + +if ( ! isset($blog_id) ) + $blog_id = 1; + +// Fix for IIS, which doesn't set REQUEST_URI +if ( empty( $_SERVER['REQUEST_URI'] ) ) { + $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; // Does this work under CGI? + + // Append the query string if it exists and isn't null + if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { + $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; + } +} + +// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests +if ( strpos($_SERVER['SCRIPT_FILENAME'], 'php.cgi') == strlen($_SERVER['SCRIPT_FILENAME']) - 7 ) + $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED']; + +// Fix for Dreamhost and other PHP as CGI hosts +if ( strstr( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) ) + unset($_SERVER['PATH_INFO']); + + +if ( !(phpversion() >= '4.1') ) + die( 'Your server is running PHP version ' . phpversion() . ' but WordPress requires at least 4.1' ); + +if ( !extension_loaded('mysql') ) + die( 'Your PHP installation appears to be missing the MySQL which is required for WordPress.' ); + +function timer_start() { + global $timestart; + $mtime = explode(' ', microtime() ); + $mtime = $mtime[1] + $mtime[0]; + $timestart = $mtime; + return true; +} +timer_start(); + +// Change to E_ALL for development/debugging +error_reporting(E_ALL ^ E_NOTICE); + +// For an advanced caching plugin to use, static because you would only want one +if ( defined('WP_CACHE') ) + require (ABSPATH . 'wp-content/advanced-cache.php'); +*/ +define('WPINC', 'wp-includes'); +require_once (ABSPATH . WPINC . '/wp-db.php'); +/* +// Table names +$wpdb->posts = $table_prefix . 'posts'; +$wpdb->users = $table_prefix . 'users'; +$wpdb->categories = $table_prefix . 'categories'; +$wpdb->post2cat = $table_prefix . 'post2cat'; +$wpdb->comments = $table_prefix . 'comments'; +$wpdb->links = $table_prefix . 'links'; +$wpdb->linkcategories = $table_prefix . 'linkcategories'; +$wpdb->options = $table_prefix . 'options'; +$wpdb->postmeta = $table_prefix . 'postmeta'; +$wpdb->usermeta = $table_prefix . 'usermeta'; + +$wpdb->prefix = $table_prefix; + +if ( defined('CUSTOM_USER_TABLE') ) + $wpdb->users = CUSTOM_USER_TABLE; +if ( defined('CUSTOM_USER_META_TABLE') ) + $wpdb->usermeta = CUSTOM_USER_META_TABLE; + +// We're going to need to keep this around for a few months even though we're not using it internally + +$tableposts = $wpdb->posts; +$tableusers = $wpdb->users; +$tablecategories = $wpdb->categories; +$tablepost2cat = $wpdb->post2cat; +$tablecomments = $wpdb->comments; +$tablelinks = $wpdb->links; +$tablelinkcategories = $wpdb->linkcategories; +$tableoptions = $wpdb->options; +$tablepostmeta = $wpdb->postmeta; + +if ( file_exists(ABSPATH . 'wp-content/object-cache.php') ) + require (ABSPATH . 'wp-content/object-cache.php'); +else + require (ABSPATH . WPINC . '/cache.php'); + +// For now, disable persistent caching by default. To enable, comment out +// the following line. +//define('DISABLE_CACHE', true); + +wp_cache_init(); + +$wp_filters = array(); + +require (ABSPATH . WPINC . '/functions.php'); +require (ABSPATH . WPINC . '/default-filters.php'); +require_once (ABSPATH . WPINC . '/wp-l10n.php'); + +$wpdb->hide_errors();*/ +$db_check = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'");/* +if ( !$db_check && (!strstr($_SERVER['PHP_SELF'], 'install.php') && !defined('WP_INSTALLING')) ) { + if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') ) + $link = 'install.php'; + else + $link = 'wp-admin/install.php'; + die(sprintf(__("It doesn't look like you've installed WP yet. Try running install.php."), $link)); +} +$wpdb->show_errors(); + +require (ABSPATH . WPINC . '/functions-formatting.php'); +require (ABSPATH . WPINC . '/functions-post.php'); +require (ABSPATH . WPINC . '/capabilities.php'); +require (ABSPATH . WPINC . '/classes.php'); +require (ABSPATH . WPINC . '/template-functions-general.php'); +require (ABSPATH . WPINC . '/template-functions-links.php'); +require (ABSPATH . WPINC . '/template-functions-author.php'); +require (ABSPATH . WPINC . '/template-functions-post.php'); +require (ABSPATH . WPINC . '/template-functions-category.php'); +require (ABSPATH . WPINC . '/comment-functions.php'); +require (ABSPATH . WPINC . '/feed-functions.php'); +require (ABSPATH . WPINC . '/links.php'); +require (ABSPATH . WPINC . '/kses.php'); +require (ABSPATH . WPINC . '/version.php'); + +if (!strstr($_SERVER['PHP_SELF'], 'install.php')) : + // Used to guarantee unique hash cookies + $cookiehash = md5(get_settings('siteurl')); // Remove in 1.4 + define('COOKIEHASH', $cookiehash); +endif; + +if ( !defined('USER_COOKIE') ) + define('USER_COOKIE', 'wordpressuser_'. COOKIEHASH); +if ( !defined('PASS_COOKIE') ) + define('PASS_COOKIE', 'wordpresspass_'. COOKIEHASH); +if ( !defined('COOKIEPATH') ) + define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('home') . '/' ) ); +if ( !defined('SITECOOKIEPATH') ) + define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('siteurl') . '/' ) ); +if ( !defined('COOKIE_DOMAIN') ) + define('COOKIE_DOMAIN', false); + +require (ABSPATH . WPINC . '/vars.php'); + +do_action('core_files_loaded'); + +// Check for hacks file if the option is enabled +if (get_settings('hack_file')) { + if (file_exists(ABSPATH . '/my-hacks.php')) + require(ABSPATH . '/my-hacks.php'); +} + +if ( get_settings('active_plugins') ) { + $current_plugins = get_settings('active_plugins'); + if ( is_array($current_plugins) ) { + foreach ($current_plugins as $plugin) { + if ('' != $plugin && file_exists(ABSPATH . 'wp-content/plugins/' . $plugin)) + include_once(ABSPATH . 'wp-content/plugins/' . $plugin); + } + } +} + +require (ABSPATH . WPINC . '/pluggable-functions.php'); + +if ( defined('WP_CACHE') && function_exists('wp_cache_postload') ) + wp_cache_postload(); + +do_action('plugins_loaded'); +*/ +/* +// If already slashed, strip. +if ( get_magic_quotes_gpc() ) { + $_GET = stripslashes_deep($_GET ); + $_POST = stripslashes_deep($_POST ); + $_COOKIE = stripslashes_deep($_COOKIE); + $_SERVER = stripslashes_deep($_SERVER); +} + +// Escape with wpdb. +$_GET = add_magic_quotes($_GET ); +$_POST = add_magic_quotes($_POST ); +$_COOKIE = add_magic_quotes($_COOKIE); +$_SERVER = add_magic_quotes($_SERVER); +*/ +/* +$wp_query = new WP_Query(); +$wp_rewrite = new WP_Rewrite(); +$wp = new WP(); +$wp_roles = new WP_Roles(); + +define('TEMPLATEPATH', get_template_directory()); + +// Load the default text localization domain. +load_default_textdomain(); + +// Pull in locale data after loading text domain. +require_once(ABSPATH . WPINC . '/locale.php'); + +// Load functions for active theme. +if ( file_exists(TEMPLATEPATH . "/functions.php") ) + include(TEMPLATEPATH . "/functions.php"); + +function shutdown_action_hook() { + wp_cache_close(); + do_action('shutdown'); +} +register_shutdown_function('shutdown_action_hook'); + +// Everything is loaded and initialized. +do_action('init'); +*/ +?> diff --git a/projects/tests/tests/real/wordpress4/wp-commentsrss2.php b/projects/tests/tests/real/wordpress4/wp-commentsrss2.php new file mode 100644 index 00000000..9eb2cda8 --- /dev/null +++ b/projects/tests/tests/real/wordpress4/wp-commentsrss2.php @@ -0,0 +1,6 @@ +get_results($_GET["p"]); + diff --git a/projects/tests/tests/real/wordpress4/wp-config.php b/projects/tests/tests/real/wordpress4/wp-config.php new file mode 100644 index 00000000..5a27c06a --- /dev/null +++ b/projects/tests/tests/real/wordpress4/wp-config.php @@ -0,0 +1,6 @@ + diff --git a/projects/tests/tests/real/wordpress4/wp-includes/wp-db.php b/projects/tests/tests/real/wordpress4/wp-includes/wp-db.php new file mode 100644 index 00000000..1e90f347 --- /dev/null +++ b/projects/tests/tests/real/wordpress4/wp-includes/wp-db.php @@ -0,0 +1,15 @@ +result = @mysql_query($query, $this->dbh); + } + + function get_results($query = null, $output = OBJECT) { + $this->query($query); + } +} + +$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); +?> diff --git a/projects/tests/tests/real/wordpress4/wp-login.php b/projects/tests/tests/real/wordpress4/wp-login.php new file mode 100644 index 00000000..e41a6aa6 --- /dev/null +++ b/projects/tests/tests/real/wordpress4/wp-login.php @@ -0,0 +1,4 @@ + diff --git a/projects/tests/tests/real/wordpress4/wp-settings.php b/projects/tests/tests/real/wordpress4/wp-settings.php new file mode 100644 index 00000000..664b995a --- /dev/null +++ b/projects/tests/tests/real/wordpress4/wp-settings.php @@ -0,0 +1,6 @@ + diff --git a/projects/tests/tests/todo/functions22.php b/projects/tests/tests/todo/functions22.php new file mode 100644 index 00000000..edc8b1d3 --- /dev/null +++ b/projects/tests/tests/todo/functions22.php @@ -0,0 +1,19 @@ +getInput(); -$tainted = preg_replace('/\'/', '', $tainted); +//$tainted = preg_replace('/\'/', '', $tainted); //flaw diff --git a/projects/tests/testvulntestsuite.php b/projects/tests/testvulntestsuite.php index ed7cd555..10969a3d 100644 --- a/projects/tests/testvulntestsuite.php +++ b/projects/tests/testvulntestsuite.php @@ -93,171 +93,171 @@ ], [ "./tests/vulntestsuite/CWE_862_Fopen__GET__func_preg_replace__fopen.php", - [["\$tainted", "48", "idor"]] + [["\$tainted", "48", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__GET__no_sanitizing__fopen.php", - [["\$tainted", "46", "idor"]] + [["\$tainted", "46", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__POST__func_preg_replace__fopen.php", - [["\$tainted", "48", "idor"]] + [["\$tainted", "48", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__POST__no_sanitizing__fopen.php", - [["\$tainted", "46", "idor"]] + [["\$tainted", "46", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__backticks__func_preg_replace__fopen.php", - [["\$tainted", "48", "idor"]] + [["\$tainted", "48", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__backticks__no_sanitizing__fopen.php", - [["\$tainted", "46", "idor"]] + [["\$tainted", "46", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__exec__func_preg_replace__fopen.php", - [["\$tainted", "51", "idor"]] + [["\$tainted", "51", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__exec__no_sanitizing__fopen.php", - [["\$tainted", "49", "idor"]] + [["\$tainted", "49", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__fopen__func_preg_replace__fopen.php", - [["\$tainted", "57", "idor"]] + [["\$tainted", "57", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__fopen__no_sanitizing__fopen.php", - [["\$tainted", "49", "idor"]] + [["\$tainted", "49", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-Array__func_preg_replace__fopen.php", - [["\$tainted", "66", "idor"]] + [["\$tainted", "64", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-Array__no_sanitizing__fopen.php", - [["\$tainted", "64", "idor"]] + [["\$tainted", "64", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-classicGet__func_preg_replace__fopen.php", - [["\$tainted", "63", "idor"]] + [["\$tainted", "63", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-classicGet__no_sanitizing__fopen.php", - [["\$tainted", "61", "idor"]] + [["\$tainted", "61", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-directGet__func_preg_replace__fopen.php", - [["\$tainted", "57", "idor"]] + [["\$tainted", "57", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-directGet__no_sanitizing__fopen.php", - [["\$tainted", "55", "idor"]] + [["\$tainted", "55", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-indexArray__func_preg_replace__fopen.php", - [["\$tainted", "66", "idor"]] + [["\$tainted", "66", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__object-indexArray__no_sanitizing__fopen.php", - [["\$tainted", "64", "idor"]] + [["\$tainted", "64", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__popen__func_preg_replace__fopen.php", - [["\$tainted", "50", "idor"]] + [["\$tainted", "50", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__popen__no_sanitizing__fopen.php", - [["\$tainted", "47", "idor"]] + [["\$tainted", "47", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__proc_open__func_preg_replace__fopen.php", - [["\$tainted", "60", "idor"]] + [["\$tainted", "60", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__proc_open__no_sanitizing__fopen.php", - [["\$tainted", "55", "idor"]] + [["\$tainted", "55", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__shell_exec__func_preg_replace__fopen.php", - [["\$tainted", "48", "idor"]] + [["\$tainted", "48", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__shell_exec__no_sanitizing__fopen.php", - [["\$tainted", "46", "idor"]] + [["\$tainted", "46", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__system__func_preg_replace__fopen.php", - [["\$tainted", "48", "idor"]] + [["\$tainted", "48", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__system__no_sanitizing__fopen.php", - [["\$tainted", "46", "idor"]] + [["\$tainted", "46", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__unserialize__func_preg_replace__fopen.php", [["\$string", "46", "code_injection"], - ["\$tainted", "50", "idor"]] + ["\$tainted", "50", "path_traversal"]] ], [ "./tests/vulntestsuite/CWE_862_Fopen__unserialize__no_sanitizing__fopen.php", [["\$string", "46", "code_injection"], - ["\$tainted", "47", "idor"]] + ["\$tainted", "47", "path_traversal"]] ], @@ -3432,6 +3432,8 @@ ["\$query", "51", "command_injection"]] ], + + [ "./tests/vulntestsuite/CWE_89__GET__CAST-cast_float_sort_of__multiple_select-sprintf_%u_simple_quote.php", [["\$data", "57", "xss"]]