path = $path; $this->id = $id; if ($validate && !$this->isValid()) { if ($this->invalidPluginError) { throw new PluginException(elgg_echo('PluginException:InvalidPlugin:Details', array($plugin, $this->invalidPluginError))); } else { throw new PluginException(elgg_echo('PluginException:InvalidPlugin', array($plugin))); } } return true; } /******************************** * Validation and sanity checks * ********************************/ /** * Checks if this is a valid Elgg plugin. * * Checks for requires files as defined at the start of this * class. Will check require manifest fields via ElggPluginManifest * for Elgg 1.8 plugins. * * @note This doesn't check dependencies or conflicts. * Use {@link ElggPluginPackage::canActivate()} or * {@link ElggPluginPackage::checkDependencies()} for that. * * @return bool */ public function isValid() { if (isset($this->valid)) { return $this->valid; } $valid = true; // check required files. $have_req_files = true; foreach ($this->requiredFiles as $file) { if (!is_readable($this->path . $file)) { $have_req_files = false; $this->invalidPluginError = elgg_echo('ElggPluginPackage:InvalidPlugin:MissingFile', array($file)); break; } } // check required files if (!$have_req_files) { $valid = false; } // check for valid manifest. if (!$this->loadManifest()) { $valid = false; } // can't require or conflict with yourself or something you provide. // make sure provides are all valid. if (!$this->isSaneDeps()) { $valid = false; } $this->valid = $valid; return $valid; } /** * Check the plugin doesn't require or conflict with itself * or something provides. Also check that it only list * valid provides. Deps are checked in checkDependencies() * * @note Plugins always provide themselves. * * @todo Don't let them require and conflict the same thing * * @return bool */ private function isSaneDeps() { $conflicts = $this->getManifest()->getConflicts(); $requires = $this->getManifest()->getRequires(); $provides = $this->getManifest()->getProvides(); foreach ($provides as $provide) { // only valid provide types if (!in_array($provide['type'], $this->providesSupportedTypes)) { $this->invalidPluginError = elgg_echo('ElggPluginPackage:InvalidPlugin:InvalidProvides', array($provide['type'])); return false; } // doesn't conflict or require any of its provides $name = $provide['name']; foreach (array('conflicts', 'requires') as $dep_type) { foreach (${$dep_type} as $dep) { if (!in_array($dep['type'], $this->depsSupportedTypes)) { $this->invalidPluginError = elgg_echo('ElggPluginPackage:InvalidPlugin:InvalidDependency', array($dep['type'])); return false; } // make sure nothing is providing something it conflicts or requires. if ($dep['name'] == $name) { $version_compare = version_compare($provide['version'], $dep['version'], $dep['comparison']); if ($version_compare) { $this->invalidPluginError = elgg_echo('ElggPluginPackage:InvalidPlugin:CircularDep', array($dep['type'], $dep['name'], $this->id)); return false; } } } } } return true; } /************ * Manifest * ************/ /** * Returns a parsed manifest file. * * @return ElggPluginManifest */ public function getManifest() { if (!$this->manifest) { if (!$this->loadManifest()) { return false; } } return $this->manifest; } /** * Loads the manifest into this->manifest as an * ElggPluginManifest object. * * @return bool */ private function loadManifest() { $file = $this->path . 'manifest.xml'; try { $this->manifest = new ElggPluginManifest($file, $this->id); } catch (Exception $e) { return false; } if ($this->manifest instanceof ElggPluginManifest) { return true; } return false; } /*********************** * Dependencies system * ***********************/ /** * Returns if the Elgg system meets the plugin's dependency * requirements. This includes both requires and conflicts. * * Full reports can be requested. The results are returned * as an array of arrays in the form array( * 'type' => requires|conflicts, * 'dep' => array( dependency array ), * 'status' => bool if depedency is met, * 'comment' => optional comment to display to the user. * ) * * @param bool $full_report Return a full report. * @return bool|array */ public function checkDependencies($full_report = false) { $requires = $this->getManifest()->getRequires(); $conflicts = $this->getManifest()->getConflicts(); $enabled_plugins = elgg_get_plugins('active'); $report = array(); foreach (array('requires', 'conflicts') as $dep_type) { $inverse = ($dep_type == 'conflicts') ? true : false; foreach (${$dep_type} as $dep) { switch ($dep['type']) { case 'elgg_version': $result = $this->checkDepElgg($dep, get_version()); break; case 'elgg_release': $result = $this->checkDepElgg($dep, get_version(true)); break; case 'plugin': $result = $this->checkDepPlugin($dep, $enabled_plugins, $inverse); break; case 'php_extension': $result = $this->checkDepPhpExtension($dep); break; case 'php_ini': $result = $this->checkDepPhpIni($dep); break; } // unless we're doing a full report, break as soon as we fail. if (!$full_report && !$result['status']) { return $result['status']; } else { // build report element and comment $report[] = array( 'type' => $dep_type, 'dep' => $dep, 'status' => $result['status'], 'value' => $result['value'] ); } } } if ($full_report) { // add provides to full report $provides = $this->getManifest()->getProvides(); foreach ($provides as $provide) { $report[] = array( 'type' => 'provides', 'dep' => $provide, 'status' => true, 'value' => '' ); } return $report; } return true; } /** * Checks if $plugins meets the requirement by $dep. * * @param array $dep An Elgg manifest.xml deps array * @param array $plugins A list of plugins as returned by get_installed_plugins(); * @param bool $inverse Inverse the results to use as a conflicts. * @return bool */ private function checkDepPlugin(array $dep, array $plugins, $inverse = false) { $r = elgg_check_plugins_provides('plugin', $dep['name'], $dep['version'], $dep['comparison']); if ($inverse) { $r['status'] = !$r['status']; } return $r; } /** * Checks if $elgg_version meets the requirement by $dep. * * @param array $dep An Elgg manifest.xml deps array * @param array $elgg_version An Elgg version (either YYYYMMDDXX or X.Y.Z) * @param bool $inverse Inverse the result to use as a conflicts. * @return bool */ private function checkDepElgg(array $dep, $elgg_version, $inverse = false) { $status = version_compare($elgg_version, $dep['version'], $dep['comparison']); if ($inverse) { $status = !$status; } return array( 'status' => $status, 'value' => $elgg_version ); } /** * Checks if the PHP extension in $dep is loaded. * * @todo Can this be merged with the plugin checker? * * @param array $dep An Elgg manifest.xml deps array * @return array An array in the form array( * 'status' => bool * 'value' => string The version provided * ) */ private function checkDepPhpExtension(array $dep) { $name = $dep['name']; $version = $dep['version']; $comparison = $dep['comparison']; // not enabled. $status = extension_loaded($name); // enabled. check version. $ext_version = phpversion($name); if ($status) { // some extensions (like gd) don't provide versions. neat. // don't check version info and return a lie. if ($ext_version && $version) { $status = version_compare($ext_version, $version, $comparison); } if (!$ext_version) { $ext_version = '???'; } } // some php extensions can be emulated, so check provides. if ($status == false) { $provides = elgg_check_plugins_provides('php_extension', $name, $version, $comparison); $status = $provides['status']; $ext_version = $provides['value']; } return array( 'status' => $status, 'value' => $ext_version ); } /** * Check if the PHP ini setting satisfies $dep. * * @param array $dep An Elgg manifest.xml deps array * @return bool */ private function checkDepPhpIni($dep) { $name = $dep['name']; $value = $dep['value']; $comparison = $dep['comparison']; // ini_get() normalizes truthy values to 1 but falsey values to 0 or ''. // version_compare() considers '' < 0, so normalize '' to 0. // ElggPluginManifest normalizes all bool values and '' to 1 or 0. $setting = ini_get($name); if ($setting === '') { $setting = 0; } $status = version_compare($setting, $value, $comparison); return array( 'status' => $status, 'value' => $setting ); } /** * Activate the plugin. * * @note This method is activate() to avoid clashing with ElggEntity::enable() * * @return bool */ public function activate() { return enable_plugin($this->getID()); } /** * Deactivate the plugin. * * @note This method is deactivate() to avoid clashing with ElggEntity::disable() * * @return bool */ public function deactivate() { return disable_plugin($this->getID()); } /** * Returns the Plugin ID * * @return string */ public function getID() { return $this->id; } }