]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
aed6ea0dd2892ec3702c3d09117c6c34bc267f8c
[quix0rs-gnu-social.git] / classes / Avatar.php
1 <?php
2 /**
3  * Table Definition for avatar
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Avatar extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'avatar';                          // table name
13     public $profile_id;                      // int(4)  primary_key not_null
14     public $original;                        // tinyint(1)
15     public $width;                           // int(4)  primary_key not_null
16     public $height;                          // int(4)  primary_key not_null
17     public $mediatype;                       // varchar(32)   not_null
18     public $filename;                        // varchar(255)
19     public $url;                             // varchar(255)  unique_key
20     public $created;                         // datetime()   not_null
21     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
22
23     /* the code above is auto generated do not remove the tag below */
24     ###END_AUTOCODE
25         
26     public static function schemaDef()
27     {
28         return array(
29             'fields' => array(
30                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
31                 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
32                 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
33                 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
34                 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
35                 'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'local filename, if local'),
36                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
37                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
38                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
39             ),
40             'primary key' => array('profile_id', 'width', 'height'),
41             'unique keys' => array(
42                 'avatar_url_key' => array('url'),
43             ),
44             'foreign keys' => array(
45                 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
46             ),
47             'indexes' => array(
48                 'avatar_profile_id_idx' => array('profile_id'),
49             ),
50         );
51     }
52     // We clean up the file, too
53
54     function delete()
55     {
56         $filename = $this->filename;
57         if (parent::delete()) {
58             @unlink(Avatar::path($filename));
59         }
60     }
61
62     /**
63      * Where should the avatar go for this user?
64      */
65     static function filename($id, $extension, $size=null, $extra=null)
66     {
67         if ($size) {
68             return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
69         } else {
70             return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
71         }
72     }
73
74     static function path($filename)
75     {
76         $dir = common_config('avatar', 'dir');
77
78         if ($dir[strlen($dir)-1] != '/') {
79             $dir .= '/';
80         }
81
82         return $dir . $filename;
83     }
84
85     static function url($filename)
86     {
87         $path = common_config('avatar', 'path');
88
89         if ($path[strlen($path)-1] != '/') {
90             $path .= '/';
91         }
92
93         if ($path[0] != '/') {
94             $path = '/'.$path;
95         }
96
97         $server = common_config('avatar', 'server');
98
99         if (empty($server)) {
100             $server = common_config('site', 'server');
101         }
102
103         $ssl = common_config('avatar', 'ssl');
104
105         if (is_null($ssl)) { // null -> guess
106             if (common_config('site', 'ssl') == 'always' &&
107                 !common_config('avatar', 'server')) {
108                 $ssl = true;
109             } else {
110                 $ssl = false;
111             }
112         }
113
114         $protocol = ($ssl) ? 'https' : 'http';
115
116         return $protocol.'://'.$server.$path.$filename;
117     }
118
119     function displayUrl()
120     {
121         $server = common_config('avatar', 'server');
122         if ($server && !empty($this->filename)) {
123             return Avatar::url($this->filename);
124         } else {
125             return $this->url;
126         }
127     }
128
129     static function defaultImage($size)
130     {
131         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
132                                   AVATAR_STREAM_SIZE => 'stream',
133                                   AVATAR_MINI_SIZE => 'mini');
134         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
135     }
136 }