function indexText($text, $type, $id, &$trigramValues, &$suggestionValues)
{
    if (!trim($text)) {
        return;
    }
    static $stopWords, $db;
    if (!$stopWords) {
        $stopWords = Lms_Application::getConfig('indexing', 'stop_words');
    }
    if (!$db) {
        $db = Lms_Db::get('main');
    }
    $trigrams = array();
    $textLength = Lms_Text::length($text);
    if ($textLength >= 3) {
        for ($i = 0; $i <= $textLength - 3; $i++) {
            $trigram = substr($text, $i, 3);
            $trigramValues[] = sprintf("('%s','%s',%d)", mysql_real_escape_string(strtolower($trigram)), $type, $id);
        }
    }
    preg_match_all('{\\w{2,}}', strtolower($text), $words, PREG_PATTERN_ORDER);
    $wordsFiltered = array();
    foreach (array_diff($words[0], $stopWords) as $word) {
        if (!preg_match('{^\\d+$}', $word)) {
            $wordsFiltered[] = $word;
        }
    }
    array_unshift($wordsFiltered, strtolower($text));
    //print_r($wordsFiltered);
    foreach ($wordsFiltered as $word) {
        $suggestionValues[] = sprintf("('%s','%s',%d)", mysql_real_escape_string(trim($word, ' .\'"')), $type, $id);
    }
}
 public static function logon($params)
 {
     $config = Lms_Application::getConfig('auth');
     $authProviderKey = $config['module'];
     $authClassName = 'Lms_Auth_Adapter_' . ucfirst($config['module']);
     $authAdapter = new $authClassName(Lms_Db::get($config['db']));
     $authAdapter->setIdentity($params['username'])->setCredential($params['password']);
     $auth = Lms_MultiAuth::getInstance();
     if (!$params['remember']) {
         $storage = $auth->getStorage();
         $storage->setCookieExpire(0);
     }
     $result = $auth->authenticate($authAdapter, $config['module']);
     if (!$result->isValid()) {
         $auth->clearIdentity();
         $errors = $result->getMessages();
         $translate = Lms_Application::getTranslate();
         foreach ($errors as &$error) {
             $error = $translate->translate($error);
         }
         return new Lms_Api_Response(401, 'Authorization failed', $errors);
     } else {
         return new Lms_Api_Response(200, 'OK');
     }
 }
 public static function updateLocalRating($filmId)
 {
     $db = Lms_Db::get('main');
     $ratings = $db->selectCol("SELECT Rating FROM userfilmratings WHERE FilmID=?d", $filmId);
     $rating = self::getBayes($ratings, Lms_Application::getConfig('minratingcount'));
     $db->query("UPDATE films SET LocalRating=?d, CountLocalRating=?d, LocalRatingDetail=? WHERE ID=?d", round(10 * $rating['bayes']), $rating['count'], serialize($rating['detail']), $filmId);
     return $rating;
 }
 function delete($dbAlias, $table, $conditions)
 {
     $db = Lms_Db::get($dbAlias);
     $sqlBuilder = new Lms_SqlBuilder();
     $sqlBuilder->setSafeMode(false);
     $whereStatement = $sqlBuilder->parse($conditions);
     $affected = $db->query("DELETE FROM ?_{$table} WHERE {$whereStatement}");
     return $affected;
 }
 public function loadFromDb($userName, $password = false)
 {
     $db = Lms_Db::get('main');
     $row = $db->selectRow('SELECT * FROM users WHERE `Login`=? {AND `Password`=?}', $userName, $password !== false ? md5($password) : DBSIMPLE_SKIP);
     if ($row) {
         $this->setData($row);
     }
     return $this;
 }
function genreExist($genreName, $filmId)
{
    static $filmgenres;
    if (!$filmgenres) {
        $db = Lms_Db::get('main');
        $rows = $db->select('SELECT FilmID as film_id, GenreID as genre_id FROM filmgenres');
        $filmgenres = array();
        foreach ($rows as $row) {
            $filmgenres[$row['film_id']][] = $row['genre_id'];
        }
    }
    if (!isset($filmgenres[$filmId])) {
        return false;
    }
    return in_array(genre($genreName), $filmgenres[$filmId]);
}
 public static function hitFilm($params)
 {
     try {
         $db = Lms_Db::get('main');
         $filmId = (int) $params['film_id'];
         Lms_Application::hitFilm($filmId);
         return new Lms_Api_Response(200);
     } catch (Exception $e) {
         return new Lms_Api_Response(500, $e->getMessage());
     }
 }
#!/usr/local/bin/php -q
<?php 
require_once dirname(__FILE__) . '/include/init.php';
$db = Lms_Db::get('main');
$db->query('TRUNCATE `suggestion_cache`');
echo "\nIndexing...";
$n = 1;
$limit = 100;
while (true) {
    $rows = $db->select('SELECT (LEFT(`word`,?d)) as `query`, COUNT(*) as c FROM `suggestion` WHERE LENGTH(`word`)>=?d GROUP BY (LEFT(`word`, ?d)) HAVING c>?d', $n, $n, $n, $limit);
    if (!count($rows)) {
        break;
    }
    echo "\n{$n}: " . count($rows) . " ({$rows[0]['query']}) ";
    foreach ($rows as $num => $row) {
        if (!($num % 10)) {
            echo '.';
        }
        $suggestion = Lms_Application::getSuggestion($row['query']);
        $db->query('INSERT IGNORE INTO `suggestion_cache` SET `query`=? ,`result`=?', $row['query'], Zend_Json::encode($suggestion));
    }
    echo ' OK';
    $n += 1;
}
echo "\nDone\n";
require_once dirname(__FILE__) . '/include/end.php';
 public static function hitFilm($filmId)
 {
     if ($filmId) {
         $db = Lms_Db::get('main');
         $method = self::getConfig('hitmethod');
         switch ($method) {
             case 1:
                 $db->query('UPDATE films SET Hit=Hit+1 WHERE films.ID=?', $filmId);
                 break;
             case 2:
                 $ip = ip2long(Lms_Ip::getIp());
                 $c = $db->selectCell("SELECT count(*) FROM hits WHERE FilmID=?d AND UserID=?d", $filmId, $ip);
                 if ($c == 0) {
                     $db->query("INSERT INTO hits(FilmID,UserID,DateHit) VALUES(?d, ?d, NOW())", $filmId, $ip);
                     $db->query("UPDATE films SET Hit=Hit+1 WHERE films.ID=?d", $filmId);
                 }
                 break;
             case 3:
                 if (!isset($_SESSION['films'][$filmId])) {
                     $db->query("UPDATE films SET Hit=Hit+1 WHERE films.ID=?d", $filmId);
                     $_SESSION['films'][$filmId] = 1;
                 }
                 break;
             default:
                 $userId = Lms_User::getUser()->getId();
                 $db->query("UPDATE users SET PlayActivity=PlayActivity+1 WHERE ID=?d", $userId);
                 $c = $db->selectCell("SELECT count(*) FROM hits WHERE FilmID=?d AND UserID=?d", $filmId, $userId);
                 if ($c == 0) {
                     $db->query("INSERT INTO hits(FilmID,UserID,DateHit) VALUES(?d, ?d, NOW())", $filmId, $userId);
                     $db->query("UPDATE films SET Hit=Hit+1 WHERE films.ID=?d", $filmId);
                 }
         }
     }
 }