Exemplo n.º 1
0
 /**
  * Returns a new PluginQuery object.
  *
  * @param     string $modelAlias The alias of a model in the query
  * @param     Criteria $criteria Optional Criteria to build the query from
  *
  * @return    PluginQuery
  */
 public static function create($modelAlias = null, $criteria = null)
 {
     if ($criteria instanceof PluginQuery) {
         return $criteria;
     }
     $query = new PluginQuery();
     if (null !== $modelAlias) {
         $query->setModelAlias($modelAlias);
     }
     if ($criteria instanceof Criteria) {
         $query->mergeWith($criteria);
     }
     return $query;
 }
Exemplo n.º 2
0
 public function loadMessages($locale)
 {
     global $memcache;
     $locale = str_replace('-', '_', $locale);
     $mkey = 'l10n-messages-' . $locale;
     if (isset($_GLOBALS['dw-config']['memcache'])) {
         // pull translation from memcache
         $msg = $memcache->get($mkey);
         if (!empty($msg)) {
             return $msg;
         }
     }
     // core
     $messages = array();
     $messages['core'] = $this->parse(ROOT_PATH . 'locale/' . $locale . '.json');
     $plugins = PluginQuery::create()->filterByEnabled(true)->find();
     foreach ($plugins as $plugin) {
         $messages[$plugin->getName()] = $this->parse($plugin->getPath() . 'locale/' . $locale . '.json');
     }
     if (isset($_GLOBALS['dw-config']['memcache'])) {
         // store translation in memcache for one minute to prevent
         // us from loading the JSON for every request
         $memcache->set($mkey, $messages, 60);
     }
     $this->__messages = $messages;
 }
Exemplo n.º 3
0
 public function getAutoPriority(PropelPDO $con = null)
 {
     if ($this->isNew()) {
         $priority = null;
     } else {
         $priority = PluginQuery::create()->select('Priority')->filterById($this->getId())->findOne($con);
     }
     if ($priority === null) {
         $priority = PluginQuery::create()->withColumn('MAX(Priority)+1', 'NextPriority')->select('NextPriority')->filterByAccountId($this->getAccountId())->filterByEntity($this->getEntity())->filterByEvent($this->getEvent())->findOne($con);
     }
     return $priority === null ? 1 : $priority;
 }
Exemplo n.º 4
0
function load_plugins()
{
    // build plugin dependency tree
    $data = array();
    $index = array();
    $roots = array();
    // load plugins
    $plugins = PluginQuery::create()->filterByEnabled(true)->find();
    foreach ($plugins as $plugin) {
        $id = $plugin->getId();
        $data[$id] = $plugin;
        $deps = $plugin->getDependencies();
        if (!empty($deps)) {
            foreach ($deps as $parent_id => $parent_version) {
                $index[$parent_id][] = $id;
            }
        } else {
            $roots[] = $id;
        }
    }
    function load_child_plugins($data, $index, $parent_id, $level)
    {
        $parent_id = $parent_id === NULL ? "NULL" : $parent_id;
        // load this plugin
        $plugin = $data[$parent_id];
        // require plugin class
        $plugin_path = ROOT_PATH . 'plugins/' . $plugin->getName() . '/plugin.php';
        if (file_exists($plugin_path)) {
            require_once $plugin_path;
            // init plugin class
            $className = $plugin->getClassName();
            $pluginClass = new $className();
        } else {
            $pluginClass = new DatawrapperPlugin($plugin->getName());
        }
        // but before we load the required libraries
        foreach ($pluginClass->getRequiredLibraries() as $lib) {
            require_once ROOT_PATH . 'plugins/' . $plugin->getName() . '/' . $lib;
        }
        $pluginClass->init();
        if (isset($index[$parent_id])) {
            foreach ($index[$parent_id] as $id) {
                load_child_plugins($data, $index, $id, $level + 1);
            }
        }
    }
    foreach ($roots as $id) {
        load_child_plugins($data, $index, $id, 0);
    }
}
Exemplo n.º 5
0
 public function disable()
 {
     $plugin = PluginQuery::create()->findPK($this->getName());
     if ($plugin) {
         $plugin->setEnabled(false);
         $plugin->save();
     }
 }
Exemplo n.º 6
0
 /**
  * Get the associated Plugin object
  *
  * @param PropelPDO $con Optional Connection object.
  * @param $doQuery Executes a query to get the object if required
  * @return Plugin The associated Plugin object.
  * @throws PropelException
  */
 public function getPlugin(PropelPDO $con = null, $doQuery = true)
 {
     if ($this->aPlugin === null && ($this->plugin_id !== "" && $this->plugin_id !== null) && $doQuery) {
         $this->aPlugin = PluginQuery::create()->findPk($this->plugin_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aPlugin->addPluginDatas($this);
            */
     }
     return $this->aPlugin;
 }
Exemplo n.º 7
0
 public static function getUserPlugins($user_id, $include_public = true)
 {
     $plugins = PluginQuery::create()->distinct()->filterByEnabled(true);
     if ($include_public) {
         $plugins->filterByIsPrivate(false)->_or();
     }
     if (!empty($user_id)) {
         $plugins->useProductPluginQuery(null, Criteria::LEFT_JOIN)->useProductQuery(null, Criteria::LEFT_JOIN)->useOrganizationProductQuery(null, Criteria::LEFT_JOIN)->useOrganizationQuery(null, Criteria::LEFT_JOIN)->useUserOrganizationQuery(null, Criteria::LEFT_JOIN)->endUse()->endUse()->endUse()->useUserProductQuery(null, Criteria::LEFT_JOIN)->endUse()->where('((product.deleted=? AND user_product.user_id=? AND user_product.expires >= NOW())
                         OR (product.deleted=? AND user_organization.user_id=? AND user_organization.invite_token = "" AND organization_product.expires >= NOW()))', array(false, $user_id, false, $user_id))->endUse()->endUse();
     }
     return $plugins->find();
 }
Exemplo n.º 8
0
function disable($pattern)
{
    _apply($pattern, function ($id) {
        $plugin = PluginQuery::create()->findPk($id);
        if (!$plugin) {
            print "Plugin {$id} is not installed. Skipping.\n";
            return false;
        }
        if ($plugin->getEnabled()) {
            $plugin->setEnabled(false);
            $plugin->save();
            print "Disabled plugin {$id}.\n";
        } else {
            print "Plugin {$id} is already disabled. Skipping.\n";
        }
    });
    exit;
}
Exemplo n.º 9
0
<?php

/*
 * this API endpoint allows plugins to provide custom
 * API actions
 */
// change plugin status
$app->put('/plugins/:id/:action', function ($plugin_id, $action) use($app) {
    if_is_admin(function () use($plugin_id, $action) {
        $plugin = PluginQuery::create()->findPk($plugin_id);
        if ($plugin) {
            switch ($action) {
                case 'enable':
                    $plugin->setEnabled(true);
                    break;
                case 'disable':
                    $plugin->setEnabled(false);
                    break;
                case 'publish':
                    $plugin->setIsPrivate(false);
                    break;
                case 'unpublish':
                    $plugin->setIsPrivate(true);
                    break;
            }
            $plugin->save();
            ok();
        } else {
            error('plugin-not-found', 'No plugin found with that ID');
        }
    });
Exemplo n.º 10
0
 protected function getFileContents($fileName)
 {
     $plugin = PluginQuery::create()->findOneByIdentifier($fileName);
     if ($plugin === null) {
         throw new \Zeyon\iXmlException('Could not find plugin "' . $fileName . '".');
     }
     return $plugin->getCode();
 }
Exemplo n.º 11
0
function health_check()
{
    $plugins = PluginQuery::create()->find();
    $core_info = json_decode(file_get_contents(ROOT_PATH . 'package.json'), true);
    $installed = array('core' => $core_info['version']);
    $dependencies = array();
    $WARN = "WARNING:";
    ob_start();
    foreach ($plugins as $plugin) {
        if (file_exists($plugin->getPath() . 'package.json')) {
            $info = json_decode(file_get_contents($plugin->getPath() . 'package.json'), true);
            if (empty($info)) {
                print $WARN . ' package.json could not be read: ' . $plugin->getId() . "\n";
            } else {
                if (empty($info['version'])) {
                    print $WARN . ' plugin has no version: ' . $plugin->getId() . "\n";
                    $info['version'] = true;
                } else {
                    $installed[$plugin->getId()] = $info['version'];
                }
                if (!empty($info['dependencies'])) {
                    $dependencies[$plugin->getId()] = $info['dependencies'];
                }
            }
        }
    }
    foreach ($dependencies as $id => $deps) {
        foreach ($deps as $dep_id => $dep_ver) {
            if (empty($installed[$dep_id]) && $dep_id != 'core') {
                print $WARN . ' ' . $id . ' depends on a missing plugin: ' . $dep_id . "\n";
            } else {
                if (version_compare($installed[$dep_id], $dep_ver) < 0) {
                    print $WARN . ' we need at least version ' . $dep_ver . ' of ' . $dep_id . "\n";
                }
            }
        }
    }
    $out = ob_get_contents();
    ob_end_clean();
    if (!empty($out)) {
        print $out;
        print "";
    }
}
Exemplo n.º 12
0
 /**
  * Loads a single plugin and checks the user's permissions.
  *
  * @param int|string $idOrCode
  * @throws Exception
  * @return Plugin
  */
 private function getPluginById($idOrCode, PropelPDO $con = null)
 {
     $user = $this->requireUser();
     if (!$user->isAdmin()) {
         throw new Exception('Non-administrative user "' . $user->getFQN() . '" cannot access plugins directly.');
     }
     $idOrCode = trim($idOrCode);
     if (substr($idOrCode, 0, 1) === '<') {
         // Protect against CSRF attacks
         if (!Form::verifyPersist('plugin.execute')) {
             throw new Exception('Plugin execution authentication failed.');
         }
         $plugin = new Plugin();
         $plugin->setCode($idOrCode);
         return $plugin;
     } elseif (is_numeric($idOrCode) and preg_match('`^\\d+$`', $idOrCode)) {
         $plugin = PluginQuery::create()->findOneById($idOrCode, $con);
     } else {
         $plugin = PluginQuery::create()->findOneByIdentifier($idOrCode, $con);
     }
     if ($plugin === null) {
         throw new Exception('Plugin with ID ' . $idOrCode . ' not found!');
     }
     // Check if the vacation belongs to the user's account
     if ($plugin->getAccountId() != $user->getAccount($con)->getId()) {
         throw new Exception('The selected plugin belongs to a different account!');
     }
     return $plugin;
 }
Exemplo n.º 13
0
 /**
  * Gets the number of Plugin objects related by a many-to-many relationship
  * to the current object by way of the product_plugin cross-reference table.
  *
  * @param Criteria $criteria Optional query object to filter the query
  * @param boolean $distinct Set to true to force count distinct
  * @param PropelPDO $con Optional connection object
  *
  * @return int the number of related Plugin objects
  */
 public function countPlugins($criteria = null, $distinct = false, PropelPDO $con = null)
 {
     if (null === $this->collPlugins || null !== $criteria) {
         if ($this->isNew() && null === $this->collPlugins) {
             return 0;
         } else {
             $query = PluginQuery::create(null, $criteria);
             if ($distinct) {
                 $query->distinct();
             }
             return $query->filterByProduct($this)->count($con);
         }
     } else {
         return count($this->collPlugins);
     }
 }
Exemplo n.º 14
0
<?php

/*
 * render templates provided by plugins
 */
$app->get('/plugins/:plugin/:template', function ($plugin_id, $template) use($app) {
    if (PluginQuery::create()->isInstalled($plugin_id)) {
        if (file_exists(ROOT_PATH . 'templates/plugins/' . $plugin_id . '/' . $template)) {
            $app->render('plugins/' . $plugin_id . '/' . $template, array('l10n__domain' => '/plugins/' . $plugin_id . '/...'));
            return;
        }
    }
    $app->notFound();
});
Exemplo n.º 15
0
<?php

require_once dirname(__FILE__) . '/../lib/tymio/common.php';
// Run timed plugins if either their last execution time is further in the past
// than their specified execution interval or where the time of day minus the
// start time offset is a divisor of the interval.
$now = time();
$timeOfDay = $now - strtotime('0:00', $now);
$query = new PluginQuery();
$plugins = $query->filterByActive(0, Criteria::NOT_EQUAL)->filterByEntity(PluginPeer::ENTITY_SYSTEM)->filterByEvent(PluginPeer::EVENT_TIMED)->add($query->getNewCriterion(PluginPeer::LAST_EXECUTION_TIME, '(' . $now . ' - ' . PluginPeer::LAST_EXECUTION_TIME . ' > ' . PluginPeer::INTERVAL . ')', Criteria::CUSTOM)->addOr($query->getNewCriterion(PluginPeer::START, '((86400 + ' . $timeOfDay . ' - ' . PluginPeer::START . ') % 86400 % ' . PluginPeer::INTERVAL . ' BETWEEN 0 AND 59)', Criteria::CUSTOM)->addAnd($query->getNewCriterion(PluginPeer::LAST_EXECUTION_TIME, '(' . $now . ' - ' . PluginPeer::LAST_EXECUTION_TIME . ' > 60)', Criteria::CUSTOM))))->addAscendingOrderByColumn(PluginPeer::LAST_EXECUTION_TIME)->addAscendingOrderByColumn(PluginPeer::PRIORITY)->find();
$parameters = PluginPeer::buildParameters(PluginPeer::ENTITY_SYSTEM, PluginPeer::EVENT_TIMED);
foreach ($plugins as $plugin) {
    try {
        error_log('Plugin #' . $plugin->getId() . ' ' . $plugin->getIdentifier() . ', last exec time ' . ($now - $plugin->getLastExecutionTime()) . ', time of day ' . $timeOfDay . ', interval ' . $plugin->getInterval() . ' => ' . (86400 + $timeOfDay - $plugin->getStart()) % 86400 % $plugin->getInterval());
        $sandbox = $plugin->execute(null, $parameters);
        $plugin->setLastExecutionTime($now)->save();
        $exception = $sandbox->getException();
        if ($exception !== null) {
            throw $exception;
        }
    } catch (Exception $e) {
        echo 'Plugin #' . $plugin->getId() . ' ' . $plugin->getIdentifier() . ': ' . $e->getMessage() . "\n";
    }
}
Exemplo n.º 16
0
 /**
  * Returns the number of related Plugin objects.
  *
  * @param Criteria $criteria
  * @param boolean $distinct
  * @param PropelPDO $con
  * @return int             Count of related Plugin objects.
  * @throws PropelException
  */
 public function countPlugins(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
 {
     $partial = $this->collPluginsPartial && !$this->isNew();
     if (null === $this->collPlugins || null !== $criteria || $partial) {
         if ($this->isNew() && null === $this->collPlugins) {
             return 0;
         }
         if ($partial && !$criteria) {
             return count($this->getPlugins());
         }
         $query = PluginQuery::create(null, $criteria);
         if ($distinct) {
             $query->distinct();
         }
         return $query->filterByAccount($this)->count($con);
     }
     return count($this->collPlugins);
 }
Exemplo n.º 17
0
 public static function load()
 {
     $plugins = PluginQuery::create()->filterByEnabled(true);
     if (!defined('NO_SESSION')) {
         $user_id = DatawrapperSession::getUser()->getId();
         if (!empty($user_id)) {
             $plugins->where('Plugin.Id IN (SELECT plugin_id FROM plugin_organization WHERE organization_id IN (SELECT organization_id FROM user_organization WHERE user_id = ?))', $user_id)->_or();
         }
         $plugins = $plugins->where('Plugin.IsPrivate = FALSE');
     }
     $plugins = $plugins->find();
     $not_loaded_yet = array();
     foreach ($plugins as $plugin) {
         if (!isset(self::$loaded[$plugin->getId()])) {
             $not_loaded_yet[] = $plugin;
         }
     }
     $could_not_install = array();
     if (!function_exists('load_plugin')) {
         function load_plugin($plugin)
         {
             $plugin_path = ROOT_PATH . 'plugins/' . $plugin->getName() . '/plugin.php';
             if (file_exists($plugin_path)) {
                 require $plugin_path;
                 // init plugin class
                 $className = $plugin->getClassName();
                 $pluginClass = new $className();
             } else {
                 $pluginClass = new DatawrapperPlugin($plugin->getName());
             }
             // but before we load the libraries required by this lib
             foreach ($pluginClass->getRequiredLibraries() as $lib) {
                 require_once ROOT_PATH . 'plugins/' . $plugin->getName() . '/' . $lib;
             }
             $pluginClass->init();
             return $pluginClass;
         }
     }
     while (count($not_loaded_yet) > 0) {
         $try = $not_loaded_yet;
         $not_loaded_yet = array();
         while (count($try) > 0) {
             $plugin = array_shift($try);
             $id = $plugin->getId();
             $deps = $plugin->getDependencies();
             unset($deps['core']);
             // ignore core dependency
             $can_load = true;
             if (is_array($deps)) {
                 foreach ($deps as $dep => $version) {
                     if (!isset(self::$loaded[$dep])) {
                         // dependency not loaded
                         $can_load = false;
                         if (!file_exists(ROOT_PATH . 'plugins/' . $dep) || isset($could_not_install[$dep])) {
                             // dependency does not exists, not good
                             $could_not_install[$id] = true;
                         }
                         break;
                     }
                 }
             }
             if (isset(self::$loaded[$id]) && self::$loaded[$id]) {
                 // plugin already loaded by now
                 continue;
             }
             if ($can_load) {
                 // load plugin
                 self::$loaded[$id] = true;
                 self::$instances[$id] = load_plugin($plugin);
             } else {
                 if (!isset($could_not_install[$id])) {
                     $not_loaded_yet[] = $plugin;
                     // so try next time
                 }
             }
         }
     }
 }
Exemplo n.º 18
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param PropelPDO $con
  * @return void
  * @throws PropelException
  * @throws Exception
  * @see        BaseObject::setDeleted()
  * @see        BaseObject::isDeleted()
  */
 public function delete(PropelPDO $con = null)
 {
     if ($this->isDeleted()) {
         throw new PropelException("This object has already been deleted.");
     }
     if ($con === null) {
         $con = Propel::getConnection(PluginPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = PluginQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             $con->commit();
             $this->setDeleted(true);
         } else {
             $con->commit();
         }
     } catch (Exception $e) {
         $con->rollBack();
         throw $e;
     }
 }
Exemplo n.º 19
0
 /**
  * Returns a list of plugins.
  *
  * @return array|PropelObjectCollection
  */
 public static function getPlugins(User $user, $entityName, $eventName, PropelPDO $con = null)
 {
     return PluginQuery::create()->filterByAccount($user->getAccount($con))->filterByEntity($entityName)->filterByActive(0, Criteria::NOT_EQUAL)->addAscendingOrderByColumn(PluginPeer::PRIORITY)->addAscendingOrderByColumn(PluginPeer::IDENTIFIER)->findByEvent($eventName, $con);
 }