function move_files_to_folder($from_folder_id, $to_folder_id, $files)
{
    global $site;
    $return = array('error' => 0, 'error_message' => '', 'moved_files' => array());
    if (count($files) && $to_folder_id && $to_folder_id != $from_folder_id) {
        $to_folder_obj = new Objekt(array('objekt_id' => (int) $to_folder_id, 'on_sisu' => 1));
        $from_folder_obj = new Objekt(array('objekt_id' => (int) $from_folder_id, 'on_sisu' => 1));
        if ($to_folder_obj->objekt_id == $to_folder_id && $to_folder_obj->all['klass'] == 'folder' && $from_folder_obj->objekt_id == $from_folder_id && $from_folder_obj->all['klass'] == 'folder') {
            if ($to_folder_obj->permission['C'] == 1) {
                $to_folder_obj->all['fullpath'] = preg_replace('#/$#', '', $site->absolute_path) . $to_folder_obj->all['relative_path'];
                $from_folder_obj->all['fullpath'] = preg_replace('#/$#', '', $site->absolute_path) . $from_folder_obj->all['relative_path'];
                foreach ($files as $object_id) {
                    $file_obj = new Objekt(array('objekt_id' => (int) $object_id, 'on_sisu' => 1));
                    if ($file_obj->objekt_id && $file_obj->parent_id == $from_folder_obj->objekt_id && $file_obj->all['klass'] == 'file' && $file_obj->permission['D'] == 1) {
                        //if file exists and there is not a file with the same name in the destination folder
                        $file_obj->all['fullpath'] = preg_replace('#/$#', '', $site->absolute_path) . $file_obj->all['relative_path'];
                        if (file_exists($file_obj->all['fullpath']) && !file_exists($to_folder_obj->all['fullpath'] . '/' . $file_obj->all['filename'])) {
                            $relative_path = $to_folder_obj->all['relative_path'] . '/' . $file_obj->all['filename'];
                            if (rename($file_obj->all['fullpath'], $to_folder_obj->all['fullpath'] . '/' . $file_obj->all['filename'])) {
                                // file successfully moved, update db object
                                $sql = "update obj_file set relative_path = '" . $relative_path . "' where objekt_id = " . $file_obj->objekt_id;
                                //printr($sql);
                                new SQL($sql);
                                // update parent -> object relation
                                $sql = "update objekt_objekt set parent_id = " . $to_folder_obj->objekt_id . " where objekt_id = " . $file_obj->objekt_id . " and parent_id = " . $from_folder_obj->objekt_id;
                                //printr($sql);
                                new SQL($sql);
                                ########## write log
                                new Log(array('action' => 'update', 'component' => 'Files', 'objekt_id' => $file_obj->objekt_id, 'message' => "File '" . $file_obj->all['relative_path'] . "' (ID = " . $file_obj->objekt_id . ") moved to '" . $relative_path . "'"));
                                $return['moved_files'][] = $file_obj->objekt_id;
                                // also move thumbnails, keep quiet about success?
                                if (file_exists($from_folder_obj->all['fullpath'] . '/.thumbnails/') . $file_obj->all['filename']) {
                                    if (!file_exists($to_folder_obj->all['fullpath'] . '/.thumbnails')) {
                                        $mask = umask(0);
                                        $thumbnails_folder = mkdir($to_folder_obj->all['fullpath'] . '/.thumbnails', 0777);
                                        umask($mask);
                                    } else {
                                        $thumbnails_folder = is_dir($to_folder_obj->all['fullpath'] . '/.thumbnails/');
                                    }
                                    if ($thumbnails_folder) {
                                        rename($from_folder_obj->all['fullpath'] . '/.thumbnails/' . $file_obj->all['filename'], $to_folder_obj->all['fullpath'] . '/.thumbnails/' . $file_obj->all['filename']);
                                    }
                                }
                            } else {
                                // file move failed
                                new Log(array('action' => 'update', 'component' => 'Files', 'type' => 'ERROR', 'objekt_id' => $file_obj->objekt_id, 'message' => "File '" . $file_obj->all['relative_path'] . "' (ID = " . $file_obj->objekt_id . ") move to '" . $to_folder_obj->all['relative_path'] . "' failed, file system error."));
                                $return['error'] = 5;
                                $return['error_message'] = 'item_error';
                            }
                        } elseif (file_exists($to_folder_obj->all['fullpath'] . '/' . $file_obj->all['filename'])) {
                            // no overwriting
                            new Log(array('action' => 'update', 'component' => 'Files', 'type' => 'NOTICE', 'objekt_id' => $file_obj->objekt_id, 'message' => "File '" . $file_obj->all['relative_path'] . "' (ID = " . $file_obj->objekt_id . ") could not be moved to '" . $to_folder_obj->all['relative_path'] . "'. File already exists."));
                            $return['error'] = 4;
                            $return['error_message'] = 'item_error';
                        } else {
                            // no such file, del from db
                            $file_obj->del();
                            // file is moved in a sense, to nothing
                            $return['moved_files'][] = $file_obj->objekt_id;
                        }
                    } else {
                        //no file to move or no cms permissions
                        new Log(array('action' => 'update', 'component' => 'Files', 'type' => 'ERROR', 'objekt_id' => $file_obj->objekt_id, 'message' => "File (ID = " . $object_id . ") move to '" . $to_folder_obj->all['relative_path'] . "' failed, access denied."));
                        $return['error'] = 3;
                        $return['error_message'] = 'no_permissions_to_move_files';
                    }
                }
            } else {
                //no file to move or no cms permissions
                new Log(array('action' => 'update', 'component' => 'Files', 'type' => 'ERROR', 'objekt_id' => $file_obj->objekt_id, 'message' => "File (ID = " . $object_id . ") move to '" . $to_folder_obj->all['relative_path'] . "' failed, access denied."));
                $return['error'] = 4;
                $return['error_message'] = 'no_permissions_to_move_files';
            }
        } else {
            $return['error'] = 2;
            $return['error_message'] = 'no_such_folder_object';
        }
    } else {
        $return['error'] = 1;
        $return['error_message'] = 'parameters_missing';
    }
    return $return;
}
	# debug info
	if (!$site->fdat['run']){
		echo "
		<tr bgcolor=\"FFFFFF\"> 	
		<td nowrap>".$tmp[objekt_id]."&nbsp;&nbsp;&nbsp;".$tmp['pealkiri'].($objekt->objekt_id ? "": ": delete skipped, cant  create objekt")."</td>
		<td nowrap>Faulty objects, having parent_id=0</td>
		<td nowrap>".$alam_obj."&nbsp;</td>
		<td nowrap>&nbsp;</td>
		</tr>
		";
	}
	#####################
	# delete
	else {
		if($objekt->objekt_id) {
			$objekt->del();
		}
	}
}

/*----------------------------------------------------
# Faulty object content: k�ia l�bi k�ik obj_*
# tabelid+objekt_objekt ja vaadata kas tabelis objekt
# on vastavad kirjed olemas
# TABEL obj_artikkel
-----------------------------------------------------*/

repair_obj_table("obj_artikkel");
repair_obj_table("obj_asset");
repair_obj_table("obj_dokument");
repair_obj_table("document_parts");
 function Leht()
 {
     $args = func_get_arg(0);
     $this->BaasObjekt();
     $this->id = $args[id];
     $fdat = array();
     # Get full path to the class folder. added by Dima 19.03.2004
     $path_parts = pathinfo($_SERVER["SCRIPT_FILENAME"]);
     $class_path = $path_parts["dirname"];
     # bugfix #1393, by merle 15.10.2004
     # if path ends with "/editor", cut it off
     if (substr($path_parts["dirname"], -7) == '/editor') {
         $class_path = substr($path_parts["dirname"], 0, -7);
     }
     # if path ends with "/admin", cut it off
     if (substr($path_parts["dirname"], -6) == '/admin') {
         $class_path = substr($path_parts["dirname"], 0, -6);
     }
     $class_path .= "/classes/";
     //testing:
     //require_once($class_path."auto.inc.php");
     //auto_error_notifications(1);
     //auto_maillist(0, 0, 0 , 1);
     #################################################
     # run mailinglist in CONF[maillist_interval] hour
     if ($this->site->CONF['next_mailinglist'] < time() && $this->site->CONF['maillist_interval'] && $this->site->CONF['enable_mailing_list']) {
         # set next run
         $sql = $this->site->db->prepare("\r\n\t\t\t\tupdate config set sisu = ? where nimi='next_mailinglist'", time() + intval($this->site->CONF['maillist_interval']) * 3600);
         $sth = new SQL($sql);
         $this->site->debug->msg($sth->debug->get_msgs());
         require_once $class_path . "auto.inc.php";
         auto_maillist(0, 0, 0, 1);
     }
     ########################
     # run in every 10 minutes
     if ($this->site->CONF['next_10min'] < time()) {
         # set next run
         $sql = $this->site->db->prepare("update config set sisu = ? where nimi='next_10min'", time() + 600);
         $sth = new SQL($sql);
         $this->site->debug->msg($sth->debug->get_msgs());
         require_once $class_path . "auto.inc.php";
         auto_publishing(1);
     }
     ########################
     # run in every hour
     if ($this->site->CONF['next_hour'] < time()) {
         # set next run
         $sql = $this->site->db->prepare("update config set sisu = ? where nimi='next_hour'", time() + 3600);
         $sth = new SQL($sql);
         $this->site->debug->msg($sth->debug->get_msgs());
         require_once $class_path . "auto.inc.php";
         # delete from cache old content:
         if (is_numeric($this->site->CONF['cache_expired'])) {
             ## delete cache by interval
             $cache_expired = time() + $this->site->CONF['cache_expired'] * 60 * 60;
             # now + interval in seconds
             $sql = $this->site->db->prepare("DELETE FROM cache WHERE aeg < " . $this->site->db->unix2db_datetime($cache_expired) . " AND objekt_id != ?", 0);
             $sth = new SQL($sql);
             $this->site->debug->msg($sth->debug->get_msgs());
         }
         // error notifications, only if setting is marked as pageload
         if ($this->site->CONF['send_error_notifiations_setting'] == 1) {
             auto_error_notifications(1);
         }
     }
     #################################################
     # We don't want to dublicate code, so will write it here:
     if ($this->site->admin && $this->site->fdat['empty_recycle_bin']) {
         $do_empty = 1;
     }
     #################################################
     # run every day
     if ($this->site->CONF['next_day'] < time() || $do_empty) {
         if (!$do_empty) {
             # set next run
             $sql = $this->site->db->prepare("update config set sisu = ? where nimi='next_day'", time() + 86400);
             $sth = new SQL($sql);
             $this->site->debug->msg($sth->debug->get_msgs());
         }
         #################################################
         # empty Recycle Bin
         if ($this->site->CONF['trash_expires'] || $do_empty) {
             $sql = "SELECT keel_id FROM keel WHERE on_kasutusel = '1'";
             $sth503 = new SQL($sql);
             $this->site->debug->msg($sth503->debug->get_msgs());
             while ($tmp_keel = $sth503->fetch()) {
                 $trash_id = $this->site->alias(array('key' => 'trash', 'keel' => $tmp_keel['keel_id']));
                 if ($trash_id) {
                     # find objects which changed_time + trash.expires.in.days < NOW (Bug #2602)
                     $sql502 = $this->site->db->prepare("SELECT objekt_objekt.* FROM objekt_objekt LEFT JOIN objekt ON objekt.objekt_id=objekt_objekt.objekt_id WHERE objekt_objekt.parent_id=? AND  DATE_ADD(objekt.changed_time,INTERVAL ? DAY) < NOW() ", $trash_id, $this->site->CONF['trash_expires']);
                     $sth502 = new SQL($sql502);
                     $this->site->debug->msg($sth502->debug->get_msgs());
                     while ($ttmp = $sth502->fetch()) {
                         $this->site->debug->msg('leht.class.php : Trying to remove object ' . $ttmp['objekt_id'] . ' from Recycle Bin...');
                         $del_objekt = new Objekt(array('objekt_id' => $ttmp['objekt_id'], 'superuser' => 1));
                         if ($del_objekt->objekt_id && $del_objekt->parent_id == $trash_id) {
                             $del_objekt->del();
                             new Log(array('action' => 'delete', 'component' => 'Recycle bin', 'objekt_id' => $del_objekt->objekt_id, 'user_id' => 0, 'message' => sprintf("%s '%s' (ID = %s) %s", ucfirst(translate_en($del_objekt->all['klass'])), $del_objekt->pealkiri(), $del_objekt->objekt_id, " removed from Recycle Bin ")));
                         } else {
                             new Log(array('action' => 'delete', 'component' => 'Recycle bin', 'objekt_id' => $del_objekt->objekt_id, 'user_id' => 0, 'type' => 'ERROR', 'message' => "Couldn't remove object ID = '" . $ttmp['objekt_id'] . "' from Recycle Bin (Parent ID of this object ='" . $del_objekt->parent_id . "')"));
                         }
                     }
                 }
             }
         }
         # / empty Recycle Bin
         #################################################
         #################################################
         # lock inactive users
         $this->site->CONF['lock_inactive_user_after_x_days'] = (int) $this->site->CONF['lock_inactive_user_after_x_days'];
         if ($this->site->CONF['lock_inactive_user_after_x_days']) {
             $sql = 'select user_id, username, last_access_time, is_predefined, firstname, lastname, username from users where is_locked = 0 and date_sub(curdate(), interval ' . $this->site->CONF['lock_inactive_user_after_x_days'] . ' day) > last_access_time';
             $result = new SQL($sql);
             while ($row = $result->fetch('ASSOC')) {
                 // dont lock the last supersuser
                 if ($row['is_predefined'] == 1) {
                     $sql = 'select user_id from users where user_id <> ' . $row['user_id'] . ' and is_predefined = 1 and is_locked = 0 limit 1';
                     $_result = new SQL($sql);
                     if ($_result->rows) {
                         $lockuser = new user(array('user_id' => $row['user_id'], 'skip_last_access_time_update' => 1));
                         $lockuser->lock('Superuser ' . htmlspecialchars(xss_clean($row['firstname'])) . ' ' . htmlspecialchars(xss_clean($row['lastname'])) . ' (' . htmlspecialchars(xss_clean($row['username'])) . ') locked due to inactivity. Last access time: ' . ($row['last_access_time'] != '0000-00-00 00:00:00' ? date('d.m.Y h:i', strtotime($row['last_access_time'])) : 'never'), 0);
                     }
                 } else {
                     $lockuser = new user(array('user_id' => $row['user_id'], 'skip_last_access_time_update' => 1));
                     $lockuser->lock('User ' . htmlspecialchars(xss_clean($row['firstname'])) . ' ' . htmlspecialchars(xss_clean($row['lastname'])) . ' (' . htmlspecialchars(xss_clean($row['username'])) . ') locked due to inactivity. Last access time: ' . ($row['last_access_time'] != '0000-00-00 00:00:00' ? date('d.m.Y h:i', strtotime($row['last_access_time'])) : 'never'), 0);
                 }
             }
         }
         # / lock inactive users
         #################################################
     }
     # / run every day
     #################################################
     ########################
     # run every week - for alive site statistics (can be turned off from config.php by defining: disable_site_polling = 1)
     if ($this->site->CONF['next_week'] < time() && !$this->site->CONF['disable_site_polling']) {
         # set next week run
         $sql = $this->site->db->prepare("update config set sisu = ? where nimi='next_week'", time() + 604800);
         $sth = new SQL($sql);
         $this->site->debug->msg($sth->debug->get_msgs());
         $accessed_by = 1;
         # "CMS weekly"
         $latest_ver = $this->site->site_polling($accessed_by);
     }
     ########################
     # eriobjekt: op=...
     $eriobjekt = array("objekt_id" => $this->site->alias("rub_home_id"), "parent_id" => $this->site->alias("rub_home_id"), "on_avaldatud" => 1);
     if (isset($this->site->fdat['otsi']) && !$this->site->fdat['op']) {
         # Bug #1828: even if empty parameter "otsi" set in URL => go to search results page
         $sql = "select * from templ_tyyp where op='search'";
         $sth = new SQL($sql);
         $this->debug->msg($sth->debug->get_msgs());
         if ($this->template = $sth->fetch()) {
             $this->eritemplate = $this->template['templ_fail'];
         }
         /*
         $this->eritemplate = "templ_searchres.php";
         $eriobjekt[pealkiri] = $this->site->sys_sona(array("sona" => 'Otsing', "tyyp"=>"kujundus"));
         */
     } elseif ($this->site->fdat['op']) {
         #  op v�ib olla ka mitme v��rtusega - nt "cart,saveorder"
         $sql = "SELECT * FROM templ_tyyp WHERE op IN('" . str_replace(",", "','", mysql_real_escape_string($this->site->fdat[op])) . "')";
         $sth = new SQL($sql);
         $this->debug->msg($sth->debug->get_msgs());
         if ($this->template = $sth->fetch()) {
             $this->eritemplate = $this->template[templ_fail];
             # eriobjekt ehk vana fiks.op-mall (millel pole �ldse aimu, mis tema parent on)
             # on siis kui URL-il pole id-d antud (fixing Bug #1962,#1924)
             if (!$this->site->fdat['id']) {
                 $eriobjekt[pealkiri] = $this->site->sys_sona(array("sona" => $this->template[nimi], "tyyp" => "kujundus"));
             }
         }
         # found op-template
     }
     # op
     ########################
     # kui id = home id-ga, siis h�pata alla
     # condition "!$this->eritemplate" removed in 3.1.24 by bugfix #486
     #	if (!$this->eritemplate && $this->id == $this->site->alias("rub_home_id")) {
     if ($this->id == $this->site->alias("rub_home_id")) {
         $this->on_esileht = 1;
     }
     if (preg_match("/^\\d+\$/", $this->id)) {
         ####### PARENTS
         $this->parents = new vParents(array("parent" => $this->id, "on_esileht" => $this->on_esileht, "lisa_objekt" => $eriobjekt[pealkiri] && !$this->on_esileht ? new Objekt(array("ary" => $eriobjekt)) : 0, "on_custom" => 0));
         if ($this->parents->denied) {
             #				$this->parents ;
         }
         $this->id = $this->parents->aktiivne_id;
         $this->objekt = $this->parents->get(0);
         $this->meta =& $this->parents->meta;
         $this->debug->msg($this->parents->debug->get_msgs());
     } else {
         # if id
         $this->debug->msg("Vale ID {$args['id']}");
     }
     # if id
 }