PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/3.0/modules/g1_import/helpers/Gallery1DataParser.php

http://github.com/gallery/gallery3-contrib
PHP | 419 lines | 234 code | 34 blank | 151 comment | 53 complexity | 5042163a97cd0e792f175504ea204245 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2013 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /**
  21. * This class provides an API for parsing Gallery 1 data files
  22. * Updated to Gallery 3 by Thomas E. Horner
  23. */
  24. class Gallery1DataParser {
  25. /**
  26. * Verify that the given path holds an albumdb and that the albumdb is readable
  27. *
  28. * @param string $path Path to albums
  29. * @return boolean True if the path is valid, otherwise false
  30. */
  31. function isValidAlbumsPath($path) {
  32. if (file_exists($path . 'albumdb.dat')
  33. && is_readable($path . 'albumdb.dat')) {
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. }
  39. /**
  40. * Load and return user metadata from given file
  41. *
  42. * @param string $fileName Path to user file to unserialize
  43. * @return array GalleryStatus a status code,
  44. * object Unserialized user metadata
  45. */
  46. function loadFile($fileName) {
  47. $fileName = str_replace('//','/',$fileName);
  48. if (!file_exists($fileName) || !is_readable($fileName)) {
  49. if (file_exists($fileName . '.bak') &&
  50. is_readable($fileName . '.bak')) {
  51. $fileName .= '.bak';
  52. } else {
  53. message::warning(
  54. t('Gallery1 inconsistency: Missing or not readable file %file',
  55. array('file' => $fileName)));
  56. return array('ERROR_BAD_PARAMETER', null);
  57. }
  58. }
  59. $tmp = file($fileName);
  60. if (empty($tmp)) {
  61. message::warning(
  62. t('Gallery1 inconsistency: Empty file %file',
  63. array('file' => $fileName)));
  64. return array('ERROR_MISSING_VALUE', null);
  65. }
  66. $tmp = join('', $tmp);
  67. /*
  68. * We renamed User.php to Gallery_User.php in v1.2, so port forward
  69. * any saved user objects.
  70. */
  71. if (stripos($tmp, 'O:4:"user"')!==false) {
  72. $tmp = str_ireplace('O:4:"user"', 'O:12:"gallery_user"', $tmp);
  73. }
  74. /*
  75. * Gallery3 already contains a class named Image so
  76. * we need to rename the G1 Image class to G1Img here
  77. */
  78. if (stripos($tmp, 'O:5:"image"')!==false) {
  79. $tmp = str_ireplace('O:5:"image"', 'O:5:"G1Img"', $tmp);
  80. }
  81. $object = unserialize($tmp);
  82. return array(null, $object);
  83. }
  84. /**
  85. * Fetch an array of albums from a given path
  86. *
  87. * @param string $path Path to albums directory
  88. * @return array GalleryStatus a status code,
  89. * array of objects
  90. */
  91. function getAlbumList($path) {
  92. list ($ret, $albumOrder) = Gallery1DataParser::loadFile($path . 'albumdb.dat');
  93. if ($ret) {
  94. return array($ret, null);
  95. }
  96. /* TODO: check that there is an $albumOrder */
  97. foreach ($albumOrder as $albumName) {
  98. list ($ret, $albumFields) =
  99. Gallery1DataParser::loadAlbumFields($path . $albumName . DIRECTORY_SEPARATOR);
  100. if ($ret) {
  101. return array($ret,'');
  102. }
  103. $albumList[$albumName] = $albumFields;
  104. }
  105. return array(null, $albumList);
  106. }
  107. /**
  108. * Fetch an associative array of parentalbum names from a given path to gallery1 albums
  109. *
  110. * @param string $path Path to albums directory
  111. * @return array GalleryStatus a status code,
  112. * array of albums and their parents
  113. */
  114. function getParentAlbumList($path) {
  115. list ($ret, $albumOrder) = Gallery1DataParser::loadFile($path . 'albumdb.dat');
  116. if ($ret) {
  117. return array($ret, null);
  118. }
  119. foreach ($albumOrder as $albumName) {
  120. list ($ret, $albumFields) =
  121. Gallery1DataParser::loadAlbumFields($path . $albumName . DIRECTORY_SEPARATOR);
  122. if ($ret) {
  123. return array($ret,'');
  124. }
  125. $parentAlbumList[$albumName] = $albumFields['parentAlbumName'];
  126. }
  127. return array(null, $parentAlbumList);
  128. }
  129. /**
  130. * Build a data tree of albums
  131. *
  132. * @param string $path Path to albums directory
  133. * @return array GalleryStatus a status code,
  134. * array of albumnames and their children, and their children...
  135. */
  136. function getAlbumHierarchy($path) {
  137. list ($ret, $parentAlbumList) = Gallery1DataParser::getParentAlbumList($path);
  138. if ($ret) {
  139. return array($ret, null);
  140. }
  141. foreach ($parentAlbumList as $myName => $parentName) {
  142. if (!isset($tempAlbums[$myName])) {
  143. $tempAlbums[$myName] = array();
  144. }
  145. if (empty($parentName) || $parentName == '.root') {
  146. $hierarchy[$myName] = &$tempAlbums[$myName];
  147. } else {
  148. if (!isset($tempAlbums[$parentName])) {
  149. $tempAlbums[$parentName] = array();
  150. }
  151. $tempAlbums[$parentName][$myName] = &$tempAlbums[$myName];
  152. }
  153. }
  154. return array(null, $hierarchy);
  155. }
  156. /**
  157. * Fetch an array of albums with no parents
  158. *
  159. * @param string $path Path to albums directory
  160. * @return array GalleryStatus a status code,
  161. * array of albumnames
  162. */
  163. function getRootAlbums($path) {
  164. list ($ret, $albumOrder) = Gallery1DataParser::loadFile($path . 'albumdb.dat');
  165. if ($ret) {
  166. return array($ret, null);
  167. }
  168. foreach ($albumOrder as $albumName) {
  169. list ($ret, $albumFields) =
  170. Gallery1DataParser::loadAlbumFields($path . $albumName . DIRECTORY_SEPARATOR);
  171. if ($ret) {
  172. return array($ret,'');
  173. }
  174. if ($albumFields['parentAlbumName'] == '.root') {
  175. $rootAlbums[] = $albumName;
  176. }
  177. }
  178. return array(null, $rootAlbums);
  179. }
  180. /**
  181. * Load and return album metadata from given directory
  182. *
  183. * @param string $path Path to album directory
  184. * @return array GalleryStatus a status code,
  185. * object Unserialized album metadata
  186. */
  187. function loadAlbumFields($path) {
  188. $tmp = trim($path);
  189. if ($tmp[strlen($tmp)-1] != DIRECTORY_SEPARATOR) {
  190. $tmp .= DIRECTORY_SEPARATOR;
  191. }
  192. $path = trim($tmp);
  193. $albumPath = explode(DIRECTORY_SEPARATOR, $path);
  194. $albumName = $albumPath[count($albumPath)-2];
  195. list ($ret, $album) = Gallery1DataParser::loadFile($path . 'album.dat');
  196. if ($ret) {
  197. return array($ret, null);
  198. }
  199. $album->fields['name'] = $albumName;
  200. if (!$album->fields['parentAlbumName']) {
  201. $album->fields['parentAlbumName'] = '.root';
  202. }
  203. return array(null, $album->fields);
  204. }
  205. /**
  206. * Count the number of photos in an album dir
  207. *
  208. * @param string $path Path to album directory
  209. * @return array GalleryStatus a status code,
  210. * integer Count of photos
  211. */
  212. function getPhotoCount($path) {
  213. list ($ret, $photos) = Gallery1DataParser::loadFile($path . 'photos.dat');
  214. if ($ret) {
  215. return array($ret, null);
  216. }
  217. $photoCount = count($photos);
  218. return array(null, $photoCount);
  219. }
  220. /**
  221. * Count the photo data from an album dir
  222. *
  223. * @param string $path Path to album directory
  224. * @return array GalleryStatus a status code,
  225. * array Galleryphotos
  226. */
  227. function getPhotos($path) {
  228. list ($ret, $photos) = Gallery1DataParser::loadFile($path . DIRECTORY_SEPARATOR . 'photos.dat');
  229. if ($ret) {
  230. return array($ret, null);
  231. }
  232. return array(null, $photos);
  233. }
  234. /**
  235. * Load user uids from path
  236. *
  237. * @param string $path Path to album directory
  238. * @return array GalleryStatus a status code,
  239. * array Associative array of uids and usernames
  240. */
  241. function getUserUids($path) {
  242. static $uids;
  243. if (!isset($uids[$path])) {
  244. if (!isset($uids)) {
  245. $uids = array();
  246. }
  247. list ($ret, $userDB) =
  248. Gallery1DataParser::loadFile($path . '.users' . DIRECTORY_SEPARATOR . 'userdb.dat');
  249. if ($ret) {
  250. return array($ret, null);
  251. }
  252. $uids[$path] = array();
  253. foreach ($userDB->userMap as $username => $uid) {
  254. if (Gallery1DataParser::isValidUid($path, $uid)
  255. && !Gallery1DataParser::isValidUid($path, $username)
  256. && !preg_match('/nobody|everybody|loggedin/i', $username)) {
  257. $uids[$path][$uid] = $username;
  258. }
  259. }
  260. }
  261. return array(null, $uids[$path]);
  262. }
  263. /**
  264. * Validate user id string from gallery v1.x.x
  265. *
  266. * @param string $uid Uid to be tested
  267. * @return boolean
  268. */
  269. function isValidUidString($uid) {
  270. if (preg_match('/^\d{9,}_\d+$/', $uid)) {
  271. return TRUE;
  272. } else {
  273. return FALSE;
  274. }
  275. }
  276. /**
  277. * Validate user id from gallery v1.x.x
  278. *
  279. * @param string $path
  280. * @param string $uid Uid to be tested
  281. * @return boolean
  282. */
  283. function isValidUid($path, $uid) {
  284. static $valid;
  285. if (!isset($valid[$path][$uid])) {
  286. if (!isset($valid)) {
  287. $valid = array();
  288. }
  289. if (!isset($valid[$path])) {
  290. $valid[$path] = array();
  291. }
  292. if (Gallery1DataParser::isValidUidString($uid)) {
  293. list ($ret, $fields) = Gallery1DataParser::getUserFieldsByUid($path, $uid);
  294. if (!$ret) {
  295. $valid[$path][$uid] = TRUE;
  296. } else {
  297. $valid[$path][$uid] = FALSE;
  298. }
  299. } else {
  300. $valid[$path][$uid] = FALSE;
  301. }
  302. }
  303. return $valid[$path][$uid];
  304. }
  305. /**
  306. * Load user metadata given a path and uid
  307. *
  308. * @param string $path Path to album directory
  309. * @param string $uid Uid to import
  310. * @return array GalleryStatus a status code,
  311. * array User metadata
  312. */
  313. function getUserFieldsByUid($path, $uid) {
  314. static $fields;
  315. if (!isset($fields[$path][$uid])) {
  316. if (!isset($fields)) {
  317. $fields = array();
  318. }
  319. if (!isset($fields[$path])) {
  320. $fields[$path] = array();
  321. }
  322. $fields[$path][$uid] = array();
  323. if (Gallery1DataParser::isValidUidString($uid)) {
  324. list ($ret, $user) = Gallery1DataParser::loadFile($path . '.users' . DIRECTORY_SEPARATOR . $uid);
  325. if ($ret) {
  326. return array($ret, null);
  327. }
  328. foreach ($user as $key => $value) {
  329. $fields[$path][$uid][$key] = $value;
  330. }
  331. }
  332. }
  333. return array(null, $fields[$path][$uid]);
  334. }
  335. /**
  336. * Load user metadata given a path and username
  337. *
  338. * @param string $path Path to album directory
  339. * @param string $username Username to import
  340. * @return array GalleryStatus a status code,
  341. * array User metadata
  342. */
  343. function getUserFieldsByUsername($path, $username) {
  344. list ($ret, $uids) = Gallery1DataParser::getUserUids($path);
  345. if ($ret) {
  346. return array($ret, null);
  347. }
  348. $usernames = array_flip($uids);
  349. $uid = $usernames[$username];
  350. list ($ret, $fields) = Gallery1DataParser::getUserFieldsByUid($path, $uid);
  351. if ($ret) {
  352. return array($ret, null);
  353. }
  354. return array(null, $fields);
  355. }
  356. }
  357. /* Define these classes so that unserialize can use them */
  358. /**
  359. * A stub class into which various G1 objects can be unserialized.
  360. */
  361. class G1AlbumDB{ }
  362. /**
  363. * A stub class into which various G1 objects can be unserialized.
  364. */
  365. class Album { }
  366. /**
  367. * A stub class into which various G1 objects can be unserialized.
  368. */
  369. class Gallery_UserDB { }
  370. /**
  371. * A stub class into which various G1 objects can be unserialized.
  372. */
  373. class Gallery_User { }
  374. /**
  375. * A stub class into which various G1 objects can be unserialized.
  376. */
  377. class AlbumItem { }
  378. /**
  379. * A stub class into which various G1 objects can be unserialized.
  380. */
  381. class G1Img { }
  382. /**
  383. * A stub class into which various G1 objects can be unserialized.
  384. */
  385. class Comment { }