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