瀏覽代碼

rev.19 Change for avatars

Add random names.
Save names to the database.
Visman 4 年之前
父節點
當前提交
29d9db3727

+ 1 - 0
app/Models/Pages/Admin/Install.php

@@ -884,6 +884,7 @@ class Install extends Admin
                 'email_normal'     => ['VARCHAR(190)', false, ''],
                 'email_normal'     => ['VARCHAR(190)', false, ''],
                 'email_confirmed'  => ['TINYINT(1)', false, 0],
                 'email_confirmed'  => ['TINYINT(1)', false, 0],
                 'title'            => ['VARCHAR(50)', false, ''],
                 'title'            => ['VARCHAR(50)', false, ''],
+                'avatar'           => ['VARCHAR(30)', false, ''],
                 'realname'         => ['VARCHAR(40)', false, ''],
                 'realname'         => ['VARCHAR(40)', false, ''],
                 'url'              => ['VARCHAR(100)', false, ''],
                 'url'              => ['VARCHAR(100)', false, ''],
                 'jabber'           => ['VARCHAR(80)', false, ''],
                 'jabber'           => ['VARCHAR(80)', false, ''],

+ 42 - 1
app/Models/Pages/Admin/Update.php

@@ -17,7 +17,7 @@ class Update extends Admin
 {
 {
     const PHP_MIN = '7.3.0';
     const PHP_MIN = '7.3.0';
 
 
-    const LATEST_REV_WITH_DB_CHANGES = 16;
+    const LATEST_REV_WITH_DB_CHANGES = 19;
 
 
     const LOCK_NAME = 'lock_update';
     const LOCK_NAME = 'lock_update';
     const LOCk_TTL  = 1800;
     const LOCk_TTL  = 1800;
@@ -842,4 +842,45 @@ class Update extends Admin
 
 
         return null;
         return null;
     }
     }
+
+    /**
+     * rev.18 to rev.19
+     */
+    protected function stageNumber18(array $args): ?int
+    {
+        $this->c->DB->addField('users', 'avatar', 'VARCHAR(30)', false, '', 'title');
+
+        $dir     = $this->c->DIR_PUBLIC . $this->c->config->o_avatars_dir . '/';
+        $avatars = [];
+
+        if (
+            \is_dir($dir)
+            && false !== ($dh = \opendir($dir))
+        ) {
+            while (false !== ($entry = \readdir($dh))) {
+                if (
+                    \preg_match('%^([1-9]\d*)\.(jpg|gif|png)$%D', $entry, $matches)
+                    && \is_file($dir . $entry)
+                ) {
+                    $avatars[$matches[2]][] = (int) $matches[1];
+                }
+            }
+            \closedir($dh);
+        }
+
+        $query = 'UPDATE ::users
+            SET avatar=CONCAT(id, \'.\', ?s:ext)
+            WHERE id IN (?ai:ids)';
+
+        foreach ($avatars as $ext => $ids) {
+            $vars = [
+                ':ext' => $ext,
+                ':ids' => $ids,
+            ];
+
+            $this->c->DB->exec($query, $vars);
+        }
+
+        return null;
+    }
 }
 }

+ 10 - 4
app/Models/Pages/Profile/Edit.php

@@ -119,11 +119,17 @@ class Edit extends Profile
                 }
                 }
 
 
                 if ($v->upload_avatar instanceof Image) {
                 if ($v->upload_avatar instanceof Image) {
-                    $v->upload_avatar
-                        ->rename(false)
-                        ->rewrite(true)
+                    $name   = $this->c->Secury->randomPass(8);
+                    $path   = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$name}.(jpg|png|gif)";
+                    $result = $v->upload_avatar
+                        ->rename(true)
+                        ->rewrite(false)
                         ->resize((int) $this->c->config->o_avatars_width, (int) $this->c->config->o_avatars_height)
                         ->resize((int) $this->c->config->o_avatars_width, (int) $this->c->config->o_avatars_height)
-                        ->toFile($this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$this->curUser->id}.(jpg|png|gif)");
+                        ->toFile($path);
+
+                    if (true === $result) {
+                        $this->curUser->avatar = $v->upload_avatar->name() . '.' . $v->upload_avatar->ext();
+                    }
                 }
                 }
 
 
                 $this->curUser->replAttrs($data, true);
                 $this->curUser->replAttrs($data, true);

+ 14 - 14
app/Models/User/Model.php

@@ -24,7 +24,7 @@ class Model extends DataModel
 
 
         $this->zDepend = [
         $this->zDepend = [
             'group_id'     => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'link', 'viewUsers', 'showPostCount', 'searchUsers'],
             'group_id'     => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'link', 'viewUsers', 'showPostCount', 'searchUsers'],
-            'id'           => ['isGuest', 'link', 'avatar', 'online'],
+            'id'           => ['isGuest', 'link', 'online'],
             'last_visit'   => ['currentVisit'],
             'last_visit'   => ['currentVisit'],
             'show_sig'     => ['showSignature'],
             'show_sig'     => ['showSignature'],
             'show_avatars' => ['showAvatar'],
             'show_avatars' => ['showAvatar'],
@@ -162,14 +162,13 @@ class Model extends DataModel
      */
      */
     protected function getavatar(): ?string
     protected function getavatar(): ?string
     {
     {
-        foreach ($this->avatarTypes as $type) {
-            $path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$this->id}.{$type}";
-
-            if (
-                \is_file($path)
-                && \getimagesize($path)
-            ) {
-                return $this->c->PUBLIC_URL . "{$this->c->config->o_avatars_dir}/{$this->id}.{$type}";
+        $file = $this->getAttr('avatar');
+
+        if (! empty($file)) {
+            $path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$file}";
+
+            if (\is_file($path)) {
+                return $this->c->PUBLIC_URL . "{$this->c->config->o_avatars_dir}/{$file}";
             }
             }
         }
         }
 
 
@@ -181,13 +180,14 @@ class Model extends DataModel
      */
      */
     public function deleteAvatar(): void
     public function deleteAvatar(): void
     {
     {
-        foreach ($this->avatarTypes as $type) {
-            $path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$this->id}.{$type}";
+        $file = $this->getAttr('avatar');
+        $path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$file}";
 
 
-            if (\is_file($path)) {
-                @\unlink($path);
-            }
+        if (\is_file($path)) {
+            @\unlink($path);
         }
         }
+
+        $this->avatar = '';
     }
     }
 
 
     /**
     /**

+ 1 - 1
app/bootstrap.php

@@ -42,7 +42,7 @@ if (
 }
 }
 $c->PUBLIC_URL = $c->BASE_URL . $forkPublicPrefix;
 $c->PUBLIC_URL = $c->BASE_URL . $forkPublicPrefix;
 
 
-$c->FORK_REVISION = 18;
+$c->FORK_REVISION = 19;
 $c->START         = $forkStart;
 $c->START         = $forkStart;
 $c->DIR_APP       = __DIR__;
 $c->DIR_APP       = __DIR__;
 $c->DIR_PUBLIC    = $forkPublic;
 $c->DIR_PUBLIC    = $forkPublic;