Exemplo n.º 1
0
 protected function deltaCheck()
 {
     $cache = $this->metaCacheArr;
     $cursor = '';
     if (!$cache) {
         $cache = array('cursor' => '', 'data' => array());
         $cache['data']['/'] = array('path' => '/', 'is_dir' => 1, 'mime_type' => '', 'bytes' => 0);
     } else {
         if (isset($cache['cursor'])) {
             $cursor = $cache['cursor'];
         }
     }
     try {
         $more = true;
         $info = array('cursor' => $cursor, 'entries' => array());
         do {
             $_info = $this->dropbox->delta($cursor);
             $info['entries'] += $_info['entries'];
             $cursor = $_info['cursor'];
         } while (!empty($_info['has_more']));
     } catch (Dropbox_Exception $e) {
         $info = $e->getMessage();
     }
     $info['cursor'] = $cursor;
     $entries = $info['entries'];
     $cache['cursor'] = $info['cursor'];
     foreach ($entries as $entry) {
         $key = strtolower($entry[0]);
         $pkey = strtolower(dirname($key));
         if (empty($entry[1])) {
             if (isset($cache['data'][$key]) && !empty($cache['data'][$key]['is_dir'])) {
                 $cache['data'][$pkey]['dirs']--;
             }
             unset($cache['data'][$pkey]['contents'][$key], $cache['data'][$key]);
             continue;
         }
         if (!isset($cache['data'][$pkey])) {
             $cache['data'][$pkey] = array();
         } else {
             unset($cache['data'][$key]['width'], $cache['data'][$key]['height']);
         }
         if (!empty($entry[1]['is_dir'])) {
             if (!isset($cache['data'][$pkey]['dirs'])) {
                 $cache['data'][$pkey]['dirs'] = 1;
             } else {
                 $cache['data'][$pkey]['dirs']++;
             }
         }
         $cache['data'][$pkey]['contents'][$key] = true;
         $cache['data'][$key] = $entry[1];
     }
     $cache['mtime'] = $_SERVER['REQUEST_TIME'];
     $this->metaCacheArr = $cache;
     $this->mataCacheSave();
 }
 /**
  * Get delta data and DB update
  * 
  * @param boolean $refresh force refresh
  * @return true|string error message
  */
 protected function deltaCheck($refresh = true)
 {
     $chk = false;
     if (!$refresh && ($chk = $this->query('select dat from ' . $this->DB_TableName . ' where path=\'\' and fname=\'\' limit 1'))) {
         $chk = unserialize($chk[0]);
     }
     if ($chk && $chk['mtime'] + $this->options['metaCacheTime'] > $_SERVER['REQUEST_TIME']) {
         return true;
     }
     try {
         $more = true;
         $this->DB->beginTransaction();
         if ($res = $this->query('select dat from ' . $this->DB_TableName . ' where path=\'\' and fname=\'\' limit 1')) {
             $res = unserialize($res[0]);
             $cursor = $res['cursor'];
         } else {
             $cursor = '';
         }
         $delete = false;
         $reset = false;
         $ptimes = array();
         $now = time();
         do {
             ini_set('max_execution_time', 120);
             $_info = $this->dropbox->delta($cursor);
             if (!empty($_info['reset'])) {
                 $this->DB->exec('TRUNCATE table ' . $this->DB_TableName);
                 $this->DB->exec('insert into ' . $this->DB_TableName . ' values(\'\', \'\', \'' . serialize(array('cursor' => '', 'mtime' => 0)) . '\', 0);');
                 $this->DB->exec('insert into ' . $this->DB_TableName . ' values(\'/\', \'\', \'' . serialize(array('path' => '/', 'is_dir' => 1, 'mime_type' => '', 'bytes' => 0)) . '\', 0);');
                 $reset = true;
             }
             $cursor = $_info['cursor'];
             foreach ($_info['entries'] as $entry) {
                 $key = strtolower($entry[0]);
                 $pkey = strtolower($this->_dirname($key));
                 $path = $this->DB->quote($pkey);
                 $fname = $this->DB->quote(strtolower($this->_basename($key)));
                 $where = 'where path=' . $path . ' and fname=' . $fname;
                 if (empty($entry[1])) {
                     $ptimes[$pkey] = isset($ptimes[$pkey]) ? max(array($now, $ptimes[$pkey])) : $now;
                     $this->DB->exec('delete from ' . $this->DB_TableName . ' ' . $where);
                     !$delete && ($delete = true);
                     continue;
                 }
                 $_itemTime = strtotime(isset($entry[1]['client_mtime']) ? $entry[1]['client_mtime'] : $entry[1]['modified']);
                 $ptimes[$pkey] = isset($ptimes[$pkey]) ? max(array($_itemTime, $ptimes[$pkey])) : $_itemTime;
                 $sql = 'select path from ' . $this->DB_TableName . ' ' . $where . ' limit 1';
                 if (!$reset && $this->query($sql)) {
                     $this->DB->exec('update ' . $this->DB_TableName . ' set dat=' . $this->DB->quote(serialize($entry[1])) . ', isdir=' . ($entry[1]['is_dir'] ? 1 : 0) . ' ' . $where);
                 } else {
                     $this->DB->exec('insert into ' . $this->DB_TableName . ' values (' . $path . ', ' . $fname . ', ' . $this->DB->quote(serialize($entry[1])) . ', ' . (int) $entry[1]['is_dir'] . ')');
                 }
             }
         } while (!empty($_info['has_more']));
         // update time stamp of parent holder
         foreach ($ptimes as $_p => $_t) {
             if ($praw = $this->getDBdat($_p)) {
                 $_update = false;
                 if (isset($praw['client_mtime']) && $_t > strtotime($praw['client_mtime'])) {
                     $praw['client_mtime'] = date('r', $_t);
                     $_update = true;
                 }
                 if (isset($praw['modified']) && $_t > strtotime($praw['modified'])) {
                     $praw['modified'] = date('r', $_t);
                     $_update = true;
                 }
                 if ($_update) {
                     $pwhere = 'where path=' . $this->DB->quote(strtolower($this->_dirname($_p))) . ' and fname=' . $this->DB->quote(strtolower($this->_basename($_p)));
                     $this->DB->exec('update ' . $this->DB_TableName . ' set dat=' . $this->DB->quote(serialize($praw)) . ' ' . $pwhere);
                 }
             }
         }
         $this->DB->exec('update ' . $this->DB_TableName . ' set dat=' . $this->DB->quote(serialize(array('cursor' => $cursor, 'mtime' => $_SERVER['REQUEST_TIME']))) . ' where path=\'\' and fname=\'\'');
         if (!$this->DB->commit()) {
             $e = $this->DB->errorInfo();
             return $e[2];
         }
         if ($delete) {
             $this->DB->exec('vacuum');
         }
     } catch (Dropbox_Exception $e) {
         return $e->getMessage();
     }
     return true;
 }