/** * Custom validation function to check user name duplicate * This is just for the example admin panel * * @param $value (string) Name to check * @param $id (string) uid if any * @return boolean TRUE for no duplicate; FALSE for duplicate */ function validate_checkDuplicateUsername($value, $id = 0) { $value = strtolower($value); if (empty($value)) { return true; } $sql = 'SELECT uid FROM ' . db_prefix() . 'user WHERE LOWER(username) = ":value"'; if ($id) { $sql .= ' AND uid <> :id'; } $sql .= ' LIMIT 1'; $args = array(':value' => strtolower($value), ':id' => $id); if ($result = db_query($sql, $args)) { return db_numRows($result) ? false : true; } return false; }
function i18n_setLanguage($lang) { global $common_language; if ($lang == 'C') { setcookie('synchrotronLanguage', '', 0, $auth_path); unset($GLOBALS['common_language']); unset($common_language); unset($_COOKIE['synchrotronLanguage']); return; } $db = db_connection(); sql_addToWhereClause($where, 'WHERE', 'code', '=', $lang); $query = db_query($db, "select id from languages {$where};"); if (db_numRows($query) > 0) { list($common_language) = db_row($query, 0); $common_language = intval($common_language); } }
function processProviderAssets($assets, $packageBasePath, $provider, $providerId, $config) { global $verbose; $metadataPath = $config['metadata']; if (empty($metadataPath)) { $metadataPath = 'metadata.desktop'; } $recreateCategoriesFile = false; $categories = array(); $db = db_connection('write'); foreach ($assets as $asset => $path) { if ($verbose) { print "Processing {$providerId} {$asset} at {$path}\n"; } if (!is_file("{$path}/{$metadataPath}")) { if ($verbose) { print "No such thing as {$path}/{$metadataPath}, perhaps it was deleted?\n"; } deleteAsset($providerId, $asset); continue; } $metadata = new INIFile("{$path}/{$metadataPath}"); $plugin = $metadata->getValue('X-KDE-PluginInfo-Name', 'Desktop Entry'); if (empty($plugin)) { print "No X-KDE-PluginInfo-Name entry in {$path}/{$metadataPath}\n"; continue; } $packageFile = $metadata->getValue('X-Synchrotron-ContentUrl', 'Desktop Entry'); $externalPackage = !empty($packageFile); if (!$externalPackage) { $packageFile = createPackage($plugin, $path, $packageBasePath, $config); } if (!$packageFile) { deleteAsset($providerId, $asset); continue; } $category = $metadata->getValue('X-KDE-PluginInfo-Category', 'Desktop Entry'); if (empty($category)) { $category = 'Miscelaneous'; } if (isset($categories[$category])) { $categoryId = $categories[$category]; } else { unset($where); sql_addToWhereClause($where, '', 'provider', '=', $providerId); global $db_type; if ($db_type == 'postgres') { sql_addToWhereClause($where, 'and', 'name', 'ILIKE', $category); } else { sql_addToWhereClause($where, 'and', 'name', 'LIKE', $category); } $query = db_query($db, "SELECT id FROM categories WHERE {$where}"); if (db_numRows($query) < 1) { unset($fields, $values); sql_addIntToInsert($fields, $values, 'provider', $providerId); sql_addScalarToInsert($fields, $values, 'name', $category); db_insert($db, 'categories', $fields, $values); $query = db_query($db, "SELECT id FROM categories WHERE {$where}"); $recreateCategoriesFile = true; } list($categoryId) = db_row($query, 0); $categories[$category] = $categoryId; } unset($where); sql_addToWhereClause($where, '', 'provider', '=', $providerId); sql_addToWhereClause($where, 'and', 'id', '=', $plugin); $query = db_query($db, "select * from content where {$where};"); if (db_numRows($query) > 0) { // just update the field unset($fields); sql_addScalarToUpdate($fields, 'version', $metadata->getValue('X-KDE-PluginInfo-Version', 'Desktop Entry')); sql_addScalarToUpdate($fields, 'author', $metadata->getValue('X-KDE-PluginInfo-Author', 'Desktop Entry')); sql_addScalarToUpdate($fields, 'homepage', $metadata->getValue('X-KDE-PluginInfo-Website', 'Desktop Entry')); //FIXME: get preview image from asset dir! sql_addScalarToUpdate($fields, 'preview', <image path>); sql_addScalarToUpdate($fields, 'name', $metadata->getValue('Name', 'Desktop Entry')); // FIXME: i18n sql_addScalarToUpdate($fields, 'description', $metadata->getValue('Comment', 'Desktop Entry')); sql_addIntToUpdate($fields, 'category', $categoryId); sql_addRawToUpdate($fields, 'updated', 'current_timestamp'); sql_addScalarToUpdate($fields, 'package', $packageFile); sql_addBoolToUpdate($fields, 'externalPackage', $externalPackage); db_update($db, 'content', $fields, $where); } else { // new asset! unset($fields, $values); sql_addIntToInsert($fields, $values, 'provider', $providerId); sql_addScalarToInsert($fields, $values, 'id', $plugin); sql_addScalarToInsert($fields, $values, 'version', $metadata->getValue('X-KDE-PluginInfo-Version', 'Desktop Entry')); sql_addScalarToInsert($fields, $values, 'author', $metadata->getValue('X-KDE-PluginInfo-Author', 'Desktop Entry')); sql_addScalarToInsert($fields, $values, 'homepage', $metadata->getValue('X-KDE-PluginInfo-Website', 'Desktop Entry')); //FIXME: get preview image from asset dir! sql_addScalarToInsert($fields, $values, 'preview', <image path>); sql_addScalarToInsert($fields, $values, 'name', $metadata->getValue('Name', 'Desktop Entry')); // FIXME: i18n sql_addScalarToInsert($fields, $values, 'description', $metadata->getValue('Comment', 'Desktop Entry')); sql_addIntToInsert($fields, $values, 'category', $categoryId); sql_addScalarToInsert($fields, $values, 'package', $packageFile); sql_addBoolToInsert($fields, $values, 'externalPackage', $externalPackage); db_insert($db, 'content', $fields, $values); } } if ($recreateCategoriesFile) { createCategoriesFile($provider); } }
/** * Get the number of rows in the query result * @return int Returns the number of rows in the result set. */ public function getNumRows() { if ($this->result === null) { $this->execute(); } if ($this->result) { return db_numRows($this->result); } return 0; }
function db_quickQuery($db, $query, $noResultsError = false, $debug = 0, $profile = 0) { $results = db_query($db, $query, $debug, $profile); if (db_numRows($results) > 0) { return db_row($results, 0); } else { if ($noResultsError) { print_msg('ERROR', "db_quickQuery(...)", "0 rows returned on the following query:<BR><PRE>" . $query . "</PRE>"); } } return false; }
if ($pagesize > 0) { $limit = "LIMIT {$pagesize}"; } unset($offset); if ($page > 0) { $offset = 'OFFSET ' . $page * $pagesize; } unset($orderBy); if (empty($sortMode) || $sortMode == 'new') { $orderBy = 'ORDER BY c.updated DESC'; } else { if ($sortMode == 'alpha') { $orderBy = 'ORDER BY c.name'; // FIXME: i18n } else { if ($sortMode == 'down') { $orderBy = 'ORDER BY c.downloads DESC'; } } } /* else if ($sortMode == 'high') { ratings are not supported } */ $items = db_query($db, "SELECT c.id, c.name, c.version, c.updated, c.created, c.author, c.homepage, c.downloads, c.preview FROM content c LEFT JOIN providers p ON (c.provider = p.id) WHERE {$where} {$orderBy} {$limit} {$offset};"); printHeader($totalItemCount, $pagesize, $page); $itemCount = db_numRows($items); for ($i = 0; $i < $itemCount; ++$i) { list($id, $name, $version, $updated, $created, $author, $homepage, $downloads, $preview) = db_row($items, $i); printItem($id, $name, $version, $updated, $created, '', $author, $homepage, $downloads, $preview); } printFooter();
function vertColumnsFromQuery($theQuery, $callback, $columns = 3) { $max = db_numRows($theQuery); $rows = ceil($max / $columns); for ($currentRow = 0; $currentRow < $rows; ++$currentRow) { for ($i = 0; $i < $columns; ++$i) { unset($key, $value); if ($currentRow + $rows * $i < $max) { list($key, $value) = db_row($theQuery, $currentRow + $rows * $i); } $callback($key, $value, $i + 1, $columns); } } }
function db_canAccessApi($addr) { $slashed_ip = addslashes($addr); $old_time = time() - 60 * 15; db_query("INSERT INTO accesses (address) VALUES ({$slashed_ip})"); $results = db_query("SELECT COUNT({$slashed_ip}) < 60 FROM accesses WHERE address = p_addr AND ts > {$old_time}"); return !db_numRows($results); }
/** * Generate a slug of human-readable keywords * * @param string $string Text to slug * @param string $table Table name to check in. If it is empty, no check in the table * @param string|array $condition Condition to append table check-in, e.g, * `fieldName != value` or `array('fieldName !=' => value)` * * @return string The generated slug */ function _slug($string, $table = '', $condition = null) { $specChars = array('`', '~', '!', '@', '#', '$', '%', '\\^', '&', '*', '(', ')', '=', '+', '{', '}', '[', ']', ':', ';', "'", '"', '<', '>', '\\', '|', '?', '/', ','); $table = ltrim($table, db_prefix()); $slug = strtolower(trim($string)); $slug = trim($slug, '-'); # clear special characters $slug = preg_replace('/(&|"|'|<|>)/i', '', $slug); $slug = str_replace($specChars, '-', $slug); $slug = str_replace(array(' ', '.'), '-', $slug); if (is_array($condition)) { $condition = db_condition($condition); } while (1 && $table) { $sql = 'SELECT slug FROM ' . $table . ' WHERE slug = ":alias"'; if ($condition) { $sql .= ' AND ' . $condition; } if ($result = db_query($sql, array(':alias' => $slug))) { if (db_numRows($result) == 0) { break; } $segments = explode('-', $slug); if (sizeof($segments) > 1 && is_numeric($segments[sizeof($segments) - 1])) { $index = array_pop($segments); $index++; } else { $index = 1; } $segments[] = $index; $slug = implode('-', $segments); } } $slug = preg_replace('/[\\-]+/', '-', $slug); return trim($slug, '-'); }