This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpnvfs_dbapi.php
152 lines (114 loc) · 4.79 KB
/
pnvfs_dbapi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
// $Id$
//
// Mediashare by Jorn Lind-Nielsen (C)
//
require_once 'modules/mediashare/common.php';
class mediashare_vfsHandlerDB
{
function mediashare_vfsHandlerDB()
{
}
function createFile($filename, $args)
{
$dom = ZLanguage::getModuleDomain('mediashare');
$fileReference = "vfsdb/$args[baseFileRef]-$args[fileMode].$args[fileType]";
$data = file_get_contents($filename);
$bytes = count($data);
$record = array(
'fileref' => $fileReference,
'mode' => $args['fileMode'],
'type' => $args['fileType'],
'bytes' => $bytes,
'data' => $data
);
$result = DBUtil::insertObject($record, 'mediashare_mediadb', 'id');
if ($result === false) {
return LogUtil::registerError(__f('Error in %1$s: %2$s.', array('vfsHandlerDB.createFile', 'Could not retrieve insert the file information.'), $dom));
}
return $fileReference;
}
function deleteFile($fileReference)
{
$dom = ZLanguage::getModuleDomain('mediashare');
$pntable = pnDBGetTables();
$mediadbColumn = $pntable['mediashare_mediadb_column'];
$fileReference = DataUtil::formatForStore($fileReference);
$where = "$mediadbColumn[fileref] = '$fileReference'";
$result = DBUtil::deleteWhere('mediashare_mediadb', $where);
if ($result === false) {
return LogUtil::registerError(__f('Error in %1$s: %2$s.', array('vfsHandlerDB.deleteFile', 'Could not delete the file information.'), $dom));
}
return true;
}
function updateFile($orgFileReference, $newFilename)
{
$dom = ZLanguage::getModuleDomain('mediashare');
$pntable = pnDBGetTables();
$mediadbTable = $pntable['mediashare_mediadb'];
$mediadbColumn = $pntable['mediashare_mediadb_column'];
$data = file_get_contents($newFilename);
$bytes = count($data);
$orgFileReference = DataUtil::formatForStore($orgFileReference);
$sql = "UPDATE $mediadbTable
SET $mediadbColumn[data] = '" . DataUtil::formatForStore($data) . "',
$mediadbColumn[bytes] = '$bytes'
WHERE $mediadbColumn[fileref] = '$orgFileReference'";
$result = DBUtil::executeSQL($sql);
if ($result === false) {
return LogUtil::registerError(__f('Error in %1$s: %2$s.', array('vfsHandlerDB.updateFile', 'Could not update the file information.'), $dom));
}
return true;
}
function getNewFileReference()
{
$chars = "0123456789abcdefghijklmnopqrstuvwxyz";
$charLen = strlen($chars);
$id = '';
for ($i = 0; $i < 30; ++$i) {
$id .= $chars[mt_rand(0, $charLen - 1)];
}
return $id;
}
}
function mediashare_vfs_dbapi_buildHandler()
{
return new mediashare_vfsHandlerDB();
}
function mediashare_vfs_dbapi_getMedia($args)
{
$dom = ZLanguage::getModuleDomain('mediashare');
$fileref = DataUtil::formatForStore($args['fileref']);
$pntable = pnDBGetTables();
$mediaTable = $pntable['mediashare_media'];
$mediaColumn = $pntable['mediashare_media_column'];
$storageTable = $pntable['mediashare_mediastore'];
$storageColumn = $pntable['mediashare_mediastore_column'];
$mediadbTable = $pntable['mediashare_mediadb'];
$mediadbColumn = &$pntable['mediashare_mediadb_column'];
$sql = "SELECT db.$mediadbColumn[data],
store.$storageColumn[mimeType],
store.$storageColumn[bytes],
media.$mediaColumn[id],
media.$mediaColumn[parentAlbumId],
media.$mediaColumn[title],
media.$mediaColumn[modifiedDate]
FROM $mediadbTable db
LEFT JOIN $storageTable store
ON store.$storageColumn[fileRef] = db.$mediadbColumn[fileref]
LEFT JOIN $mediaTable media
ON (media.$mediaColumn[thumbnailId] = store.$storageColumn[id]
OR media.$mediaColumn[previewId] = store.$storageColumn[id]
OR media.$mediaColumn[originalId] = store.$storageColumn[id])
AND media.$mediaColumn[title] IS NOT NULL
WHERE db.$mediadbColumn[fileref] = '$fileref'";
$result = DBUtil::executeSQL($sql);
if ($result === false) {
return LogUtil::registerError(__f('Error in %1$s: %2$s.', array('vfsHandlerDB.getMedia', 'Could not retrieve the file information.'), $dom));
}
if (!$result) {
return LogUtil::registerError(__('Unknown media item.', $dom));
}
$info = DBUtil::marshallObjects($result, array('data', 'mimeType', 'bytes', 'mediaId', 'albumId', 'title', 'modifiedDate'));
return $info;
}