Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.Vault.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Utilities functions for manipulate files from VAULT
8  *
9  * @author Anakeen
10  * @version $Id: Lib.Vault.php,v 1.23 2008/07/24 16:03:15 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("VAULT/Class.VaultFile.php");
18 include_once ("VAULT/Class.VaultEngine.php");
19 include_once ("VAULT/Class.VaultDiskStorage.php");
20 
21 function initVaultAccess()
22 {
23  static $FREEDOM_VAULT = false;;
24  if (!$FREEDOM_VAULT) {
25  include_once ("VAULT/Class.VaultFile.php");
27  $FREEDOM_VAULT = new VaultFile($dbaccess, "FREEDOM");
28  }
29  return $FREEDOM_VAULT;
30 }
31 /**
32  * get url with open id to use with open authentiication
33  */
34 function getOpenTeUrl($context = array())
35 {
36  global $action;
37  $urlindex = getParam("TE_URLINDEX");
38  if ($urlindex == "") { //case DAV
39  $au = getParam("CORE_URLINDEX");
40  if ($au != "") $urlindex = getParam("CORE_URLINDEX");
41  else {
42  $scheme = getParam("CORE_ABSURL");
43  if ($scheme == "") $urlindex = '/freedom/';
44  else $urlindex = getParam("CORE_ABSURL");
45  }
46  }
47  $token = $action->user->getUserToken(3600 * 24, true, $context);
48  if (strstr($urlindex, '?')) $beg = '&';
49  else $beg = '?';
50  $openurl = $urlindex . $beg . "authtype=open&privateid=$token";
51  return $openurl;
52 }
53 /**
54  * Generate a conversion of a file
55  * The result is store in vault itself
56  * @param string $engine the convert engine identifier (from VaultEngine Class)
57  * @param int $vidin vault file identifier (original file)
58  * @param int $vidout vault identifier of new stored file
59  * @param boolean $isimage true is it is a image (jpng, png, ...)
60  * @param int $docid original document where the file is inserted
61  * @return string error message (empty if OK)
62  */
63 function vault_generate($dbaccess, $engine, $vidin, $vidout, $isimage = false, $docid = '')
64 {
65  $err = '';
66  if (($vidin > 0) && ($vidout > 0)) {
67  $tea = getParam("TE_ACTIVATE");
68  if ($tea != "yes" || !\Dcp\Autoloader::classExists('Dcp\TransformationEngine\Client')) return '';
69  global $action;
70  include_once ("FDL/Class.TaskRequest.php");
71  $of = new VaultDiskStorage($dbaccess, $vidin);
72  $filename = $of->getPath();
73  if (!$of->isAffected()) return "no file $vidin";
74  $ofout = new VaultDiskStorage($dbaccess, $vidout);
75  $ofout->teng_state = \Dcp\TransformationEngine\Client::status_waiting; // in progress
76  $ofout->modify();
77 
78  $urlindex = getOpenTeUrl();
79  $callback = $urlindex . "&sole=Y&app=FDL&action=INSERTFILE&engine=$engine&vidin=$vidin&vidout=$vidout&isimage=$isimage&docid=$docid";
80  $ot = new \Dcp\TransformationEngine\Client(getParam("TE_HOST") , getParam("TE_PORT"));
81  $err = $ot->sendTransformation($engine, $vidout, $filename, $callback, $info);
82  if ($err == "") {
83  $tr = new TaskRequest($dbaccess);
84  $tr->tid = $info["tid"];
85  $tr->fkey = $vidout;
86  $tr->status = $info["status"];
87  $tr->comment = $info["comment"];
88  $tr->uid = $action->user->id;
89  $tr->uname = $action->user->firstname . " " . $action->user->lastname;
90  $err = $tr->Add();
91  } else {
92  $vf = initVaultAccess();
93  $filename = uniqid(getTmpDir() . "/txt-" . $vidout . '-');
94  file_put_contents($filename, $err);
95  //$vf->rename($vidout,"toto.txt");
96  $infofile = null;
97  $vf->Retrieve($vidout, $infofile);
98  $err.= $vf->Save($filename, false, $vidout);
99  @unlink($filename);
100  $vf->rename($vidout, _("impossible conversion") . ".txt");
101  if ($info["status"]) $vf->storage->teng_state = $info["status"];
102  else $vf->storage->teng_state = \Dcp\TransformationEngine\Client::status_inprogress;
103  $vf->storage->modify();
104  }
105  }
106  return $err;
107 }
108 /**
109  * return various informations for a file stored in VAULT
110  * @param int $idfile vault file identifier
111  * @param string $teng_name transformation engine name
112  * @return vaultFileInfo
113  */
114 function vault_properties($idfile, $teng_name = "")
115 {
116 
117  $FREEDOM_VAULT = initVaultAccess();
118  $FREEDOM_VAULT->Show($idfile, $info, $teng_name);
119  return $info;
120 }
121 /**
122  * return unique name with for a vault file
123  * @param int $idfile vault file identifier
124  * @param string $teng_name transformation engine name
125  * @return string the unique name
126  */
127 function vault_uniqname($idfile, $teng_name = "")
128 {
129  $FREEDOM_VAULT = initVaultAccess();
130  $FREEDOM_VAULT->Show($idfile, $info, $teng_name);
131  if ($info->name) {
132 
133  $m2009 = iso8601DateToUnixTs("2009-01-01");
134  $mdate = stringDateToUnixTs($info->mdate);
135  $check = base_convert($mdate - $m2009, 10, 34);
136  $pos = strrpos($info->name, '.');
137  // $check= md5_file($info->path);
138  if ($pos) {
139  $bpath = substr($info->name, 0, $pos);
140  $extpath = substr($info->name, $pos);
141  $othername = sprintf("%s{%s-%s}%s", $bpath, $check, $info->id_file, $extpath);
142  return $othername;
143  }
144  }
145  return 0;
146 }
147 /**
148  * return various informations for a file stored in VAULT
149  * @param string $filename
150  * @param int &$vid return vaul identifier
151  * @return string error message
152  */
153 function vault_store($filename, &$vid, $ftitle = "")
154 {
155 
156  $FREEDOM_VAULT = initVaultAccess();
157  $err = $FREEDOM_VAULT->store($filename, false, $vid);
158  if (($err == "") && ($ftitle != "")) $FREEDOM_VAULT->rename($vid, $ftitle);
159  return $err;
160 }
161 /**
162  * return context of a file
163  * @param int $idfile vault file identifier
164  * @return array|false
165  */
166 function vault_get_content($idfile)
167 {
168  $FREEDOM_VAULT = initVaultAccess();
169  $v = new VaultDiskStorage($FREEDOM_VAULT->dbaccess, $idfile);
170 
171  if ($v->isAffected()) {
172  $path = $v->getPath();
173  if (file_exists($path)) return file_get_contents($path);
174  }
175  return false;
176 }
177 /**
178  * send request to have text conversion of file
179  */
180 function sendTextTransformation($dbaccess, $docid, $attrid, $index, $vid)
181 {
182  $err = '';
183  if (($docid > 0) && ($vid > 0)) {
184 
185  $tea = getParam("TE_ACTIVATE");
186  if ($tea != "yes" || !\Dcp\Autoloader::classExists('Dcp\TransformationEngine\Client')) return '';
187  $tea = getParam("TE_FULLTEXT");
188  if ($tea != "yes") return '';
189 
190  global $action;
191  include_once ("FDL/Class.TaskRequest.php");
192  $of = new VaultDiskStorage($dbaccess, $vid);
193  $filename = $of->getPath();
194  $urlindex = getOpenTeUrl();
195  $callback = $urlindex . "&sole=Y&app=FDL&action=SETTXTFILE&docid=$docid&attrid=" . $attrid . "&index=$index";
196  $ot = new \Dcp\TransformationEngine\Client(getParam("TE_HOST") , getParam("TE_PORT"));
197  $err = $ot->sendTransformation('utf8', $vid, $filename, $callback, $info);
198  if ($err == "") {
199  $tr = new TaskRequest($dbaccess);
200  $tr->tid = $info["tid"];
201  $tr->fkey = $vid;
202  $tr->status = $info["status"];
203  $tr->comment = $info["comment"];
204  $tr->uid = $action->user->id;
205  $tr->uname = $action->user->firstname . " " . $action->user->lastname;
206  $err = $tr->Add();
207  }
208  }
209  return $err;
210 }
211 /**
212  * send request to convert and waiting
213  * @param string $infile path to file to convert
214  * @param string $engine engine name to use
215  * @param string $outfile path where to store new file
216  * @param array &$info various informations for convertion process
217  * @return string error message
218  */
219 function convertFile($infile, $engine, $outfile, &$info)
220 {
221  global $action;
222  $err = '';
223  if (file_exists($infile) && ($engine != "")) {
224  $tea = getParam("TE_ACTIVATE");
225  if ($tea != "yes" || !\Dcp\Autoloader::classExists('Dcp\TransformationEngine\Client')) return _("TE not activated");
226  $callback = "";
227  $ot = new \Dcp\TransformationEngine\Client(getParam("TE_HOST") , getParam("TE_PORT"));
228  $vid = '';
229  $err = $ot->sendTransformation($engine, $vid, $infile, $callback, $info);
230  if ($err == "") {
231  include_once ("FDL/Class.TaskRequest.php");
233  $tr = new TaskRequest($dbaccess);
234  $tr->tid = $info["tid"];
235  $tr->fkey = $vid;
236  $tr->status = $info["status"];
237  $tr->comment = $info["comment"];
238  $tr->uid = $action->user->id;
239  $tr->uname = $action->user->firstname . " " . $action->user->lastname;
240  $err = $tr->Add();
241  }
242  $tid = 0;
243  if ($err == "") {
244  $tid = $info["tid"];
245  if ($tid == 0) $err = _("no task identificator");
246  }
247  // waiting response
248  if ($err == "") {
249  $status = "";
250  setMaxExecutionTimeTo(3600);
251  while (($status != 'K') && ($status != 'D') && ($err == "")) {
252  $err = $ot->getInfo($tid, $info);
253  $status = $info["status"];
254  if ($err == "") {
255  switch ($info["status"]) {
256  case 'P':
257  $statusmsg = _("File:: Processing");
258  break;
259 
260  case 'W':
261  $statusmsg = _("File:: Waiting");
262  break;
263 
264  case 'D':
265  $statusmsg = _("File:: converted");
266  break;
267 
268  case 'K':
269  $statusmsg = _("File:: failed");
270  break;
271 
272  default:
273  $statusmsg = $info["status"];
274  }
275  }
276 
277  sleep(2);
278  }
279  if (($err == "") && ($status == 'D')) {
280  include_once ("FDL/insertfile.php");
282  }
283  }
284  } else {
285  $err = sprintf(_("file %s not found") , $infile);
286  }
287  return $err;
288 }
initVaultAccess()
Definition: Lib.Vault.php:21
convertFile($infile, $engine, $outfile, &$info)
Definition: Lib.Vault.php:219
vault_uniqname($idfile, $teng_name="")
Definition: Lib.Vault.php:127
$status
Definition: index.php:30
vault_properties($idfile, $teng_name="")
Definition: Lib.Vault.php:114
global $action
$filename
iso8601DateToUnixTs($isodate, $utc=false)
Definition: Lib.Util.php:198
sendTextTransformation($dbaccess, $docid, $attrid, $index, $vid)
Definition: Lib.Vault.php:180
$docid
Definition: cleanFamily.php:13
$outfile
getOpenTeUrl($context=array())
Definition: Lib.Vault.php:34
$path
Definition: dav.php:39
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
vault_generate($dbaccess, $engine, $vidin, $vidout, $isimage=false, $docid= '')
Definition: Lib.Vault.php:63
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
getDbAccess()
Definition: Lib.Common.php:368
vault_store($filename, &$vid, $ftitle="")
Definition: Lib.Vault.php:153
stringDateToUnixTs($isodate, $utc=false)
Definition: Lib.Util.php:217
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
$vf
Definition: geticon.php:28
$dbaccess
Definition: checkVault.php:17
$info
Definition: geticon.php:30
getTEFile($tid, $filename, &$info)
Definition: insertfile.php:117
if($file) if($subject==""&&$file) if($subject=="") $err
vault_get_content($idfile)
Definition: Lib.Vault.php:166
← centre documentaire © anakeen