"format",
"ls:bitrate" => "bit_rate",
"ls:samplerate" => "sample_rate",
"dcterms:extent" => "length",
"dc:title" => "track_title",
"dc:description" => "comments",
"dc:type" => "genre",
"dc:creator" => "artist_name",
"dc:source" => "album_title",
"ls:channels" => "channels",
"ls:filename" => "name",
"ls:year" => "year",
"ls:url" => "url",
"ls:track_num" => "track_number");
/**
* Core of Campcaster file storage module
*
* @package Campcaster
* @subpackage StorageServer
* @copyright 2010 Sourcefabric O.P.S.
* @license http://www.gnu.org/licenses/gpl.txt
* @see Alib
*/
class BasicStor {
public $storId;
public function __construct()
{
}
/**
* Store new file in the storage
*
* @param int $p_parentId
* Parent id
* @param array $p_values
* See StoredFile::Insert() for details.
* @param boolean $copyMedia
* copy the media file if true, make symlink if false
* @return int|PEAR_Error
* ID of the StoredFile that was created.
*/
public function bsPutFile($p_values, $p_copyMedia=TRUE)
{
if (!isset($p_values['filetype']) || !isset($p_values['filename'])) {
return NULL;
}
$ftype = strtolower($p_values['filetype']);
$storedFile = StoredFile::Insert($p_values, $p_copyMedia);
if (PEAR::isError($storedFile)) {
$res = BasicStor::RemoveObj($id);
// catch constraint violations
switch ($storedFile->getCode()) {
case -3:
return PEAR::raiseError(
"BasicStor::bsPutFile: gunid duplication",
GBERR_GUNID);
default:
return $storedFile;
}
}
return $storedFile;
} // fn bsPutFile
/**
* Rename file
*
* @param int $id
* Virtual file's local id
* @param string $newName
* @return boolean|PEAR_Error
*/
public function bsRenameFile($id, $newName)
{
switch (BasicStor::GetObjType($id)) {
case "audioclip":
case "playlist":
case "webstream":
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
// catch nonerror exception:
//if($storedFile->getCode() != GBERR_FOBJNEX)
return $storedFile;
}
$res = $storedFile->setName($newName);
if (PEAR::isError($res)) {
return $res;
}
break;
case "File":
default:
}
return TRUE;
}
/**
* Replace file. Doesn't change filetype!
*
* @param int $id
* Virtual file's local id
* @param string $localFilePath
* Local path of media file
* @param string $metadataFilePath
* Local path of metadata file
* @param string $mdataLoc
* 'file'|'string'
* @return true|PEAR_Error
* @exception PEAR::error
*/
public function bsReplaceFile($id, $localFilePath, $metadataFilePath, $mdataLoc='file')
{
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
if (!empty($metadataFilePath) &&
($mdataLoc!='file' || file_exists($metadataFilePath))) {
$r = $storedFile->setMetadata($metadataFilePath, $mdataLoc);
if (PEAR::isError($r)) {
return $r;
}
}
if (!empty($localFilePath) && file_exists($localFilePath)) {
$r = $storedFile->setRawMediaData($localFilePath);
if (PEAR::isError($r)) {
return $r;
}
}
return TRUE;
}
/**
* Delete file
*
* @param int $id
* Virtual file's local id
* @param boolean $forced
* If true don't use trash
* @return true|PEAR_Error
*/
public function bsDeleteFile($id, $forced=FALSE)
{
global $CC_CONFIG;
// full delete:
if (!$CC_CONFIG['useTrash'] || $forced) {
$res = BasicStor::RemoveObj($id, $forced);
return $res;
}
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
if ($storedFile->isAccessed()) {
return PEAR::raiseError(
'Cannot delete an object that is currently accessed.'
);
}
// move to trash:
switch (BasicStor::GetObjType($id)) {
case "audioclip":
$playLists = $storedFile->getPlaylists();
$item_gunid = $storedFile->getGunid();
if( $playLists != NULL) {
foreach($playLists as $key=>$val) {
$playList_id = BasicStor::IdFromGunidBigInt($val["gunid"]);
$playList_titles[] = BasicStor::bsGetMetadataValue($playList_id, "dc:title");
}
return PEAR::raiseError(
'Please remove this song from all playlists: ' . join(",", $playList_titles)
);
}
break;
case "playlist":
if($storedFile->isScheduled()) {
return PEAR::raiseError(
'Cannot delete an object that is scheduled to play.'
);
}
break;
case "webstream":
break;
default:
}
$res = $storedFile->setState('deleted');
if (PEAR::isError($res)) {
return $res;
}
return TRUE;
}
/* ----------------------------------------------------- put, access etc. */
/**
* Check validity of access/put token
*
* @param string $token
* Access/put token
* @param string $type
* 'put'|'access'|'download'
* @return boolean
*/
public static function bsCheckToken($token, $type='put')
{
global $CC_CONFIG, $CC_DBC;
$cnt = $CC_DBC->getOne("
SELECT count(token) FROM ".$CC_CONFIG['accessTable']."
WHERE token=x'{$token}'::bigint AND type='$type'
");
if (PEAR::isError($cnt)) {
return FALSE;
}
return ($cnt == 1);
}
/**
* Create and return access link to real file
*
* @param string $realFname
* Local filepath to accessed file
* (NULL for only increase access counter, no symlink)
* @param string $ext
* Useful filename extension for accessed file
* @param int $gunid
* Global unique id
* (NULL for special files such exported playlists)
* @param string $type
* 'access'|'download'
* @param int $parent
* parent token (recursive access/release)
* @param int $owner
* Local user id - owner of token
* @return array
* array with: seekable filehandle, access token
*/
public static function bsAccess($realFname, $ext, $gunid, $type='access',
$parent='0', $owner=NULL)
{
global $CC_CONFIG, $CC_DBC;
if (!is_null($gunid)) {
$gunid = StoredFile::NormalizeGunid($gunid);
}
$token = StoredFile::CreateGunid();
if (!is_null($realFname)) {
$linkFname = $CC_CONFIG['accessDir']."/$token.$ext";
//broken links are ignored by the player, do not worry about it here
/* if (!is_file($realFname) && !is_link($realFname)) {
return PEAR::raiseError(
"BasicStor::bsAccess: real file not found ($realFname)",
GBERR_FILEIO);
}
*/
if (! @symlink($realFname, $linkFname)) {
return PEAR::raiseError(
"BasicStor::bsAccess: symlink create failed ($linkFname)",
GBERR_FILEIO);
}
} else {
$linkFname = NULL;
}
$escapedExt = pg_escape_string($ext);
$escapedType = pg_escape_string($type);
$CC_DBC->query("BEGIN");
$gunidSql = (is_null($gunid) ? "NULL" : "x'{$gunid}'::bigint" );
$ownerSql = (is_null($owner) ? "NULL" : "$owner" );
$res = $CC_DBC->query("
INSERT INTO ".$CC_CONFIG['accessTable']."
(gunid, token, ext, type, parent, owner, ts)
VALUES
($gunidSql, x'$token'::bigint,
'$escapedExt', '$escapedType', x'{$parent}'::bigint, $ownerSql, now())
");
if (PEAR::isError($res)) {
$CC_DBC->query("ROLLBACK");
return $res;
}
if (!is_null($gunid)) {
$res = $CC_DBC->query("
UPDATE ".$CC_CONFIG['filesTable']."
SET currentlyAccessing=currentlyAccessing+1, mtime=now()
WHERE gunid=x'{$gunid}'::bigint
");
}
if (PEAR::isError($res)) {
$CC_DBC->query("ROLLBACK");
return $res;
}
$res = $CC_DBC->query("COMMIT");
if (PEAR::isError($res)) {
return $res;
}
return array('fname'=>$linkFname, 'token'=>$token);
}
/**
* Release access link to real file
*
* @param string $token
* Access token
* @param string $type
* 'access'|'download'
* @return array
* gunid: string, global unique ID or real pathname of special file
* owner: int, local subject id of token owner
* realFname: string, real local pathname of accessed file
*/
public static function bsRelease($token, $type='access')
{
global $CC_CONFIG, $CC_DBC;
if (!BasicStor::bsCheckToken($token, $type)) {
return PEAR::raiseError(
"BasicStor::bsRelease: invalid token ($token)"
);
}
$acc = $CC_DBC->getRow("
SELECT to_hex(gunid)as gunid, ext, owner FROM ".$CC_CONFIG['accessTable']."
WHERE token=x'{$token}'::bigint AND type='$type'
");
if (PEAR::isError($acc)) {
return $acc;
}
$ext = $acc['ext'];
$owner = $acc['owner'];
$linkFname = $CC_CONFIG['accessDir']."/$token.$ext";
$realFname = readlink($linkFname);
if (file_exists($linkFname)) {
if(! @unlink($linkFname)){
return PEAR::raiseError(
"BasicStor::bsRelease: unlink failed ($linkFname)",
GBERR_FILEIO);
}
}
$CC_DBC->query("BEGIN");
if (!is_null($acc['gunid'])) {
$gunid = StoredFile::NormalizeGunid($acc['gunid']);
$res = $CC_DBC->query("
UPDATE ".$CC_CONFIG['filesTable']."
SET currentlyAccessing=currentlyAccessing-1, mtime=now()
WHERE gunid=x'{$gunid}'::bigint AND currentlyAccessing>0
");
if (PEAR::isError($res)) {
$CC_DBC->query("ROLLBACK");
return $res;
}
}
$res = $CC_DBC->query("
DELETE FROM ".$CC_CONFIG['accessTable']." WHERE token=x'$token'::bigint
");
if (PEAR::isError($res)) {
$CC_DBC->query("ROLLBACK");
return $res;
}
$res = $CC_DBC->query("COMMIT");
if (PEAR::isError($res)) {
return $res;
}
$res = array(
'gunid' => (isset($gunid) ? $gunid : NULL ),
'realFname' => $realFname,
'owner' => $owner,
);
return $res;
}
/**
* Create and return downloadable URL for file
*
* @param int $id
* Virtual file's local id
* @param string $part
* 'media'|'metadata'
* @param int $parent
* parent token (recursive access/release)
* @return array
* array with strings:
* downloadable URL, download token, chsum, size, filename
*/
public function bsOpenDownload($id, $part='media')
{
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
$gunid = $storedFile->gunid;
switch ($part) {
case "media":
$realfile = $storedFile->getRealFileName();
$ext = $storedFile->getFileExtension();
$filename = $storedFile->getName();
break;
case "metadata":
$realfile = $storedFile->getRealMetadataFileName();
$ext = "xml";
$filename = $storedFile->getName();
break;
default:
return PEAR::raiseError(
"BasicStor::bsOpenDownload: unknown part ($part)"
);
}
$acc = BasicStor::bsAccess($realfile, $ext, $gunid, 'download');
if (PEAR::isError($acc)) {
return $acc;
}
$url = BasicStor::GetUrlPart()."access/".basename($acc['fname']);
$chsum = md5_file($realfile);
$size = filesize($realfile);
return array(
'url'=>$url, 'token'=>$acc['token'],
'chsum'=>$chsum, 'size'=>$size,
'filename'=>$filename
);
}
/**
* Discard downloadable URL
*
* @param string $token
* Download token
* @param string $part
* 'media'|'metadata'
* @return string
* gunid
*/
public function bsCloseDownload($token, $part='media')
{
if (!BasicStor::bsCheckToken($token, 'download')) {
return PEAR::raiseError(
"BasicStor::bsCloseDownload: invalid token ($token)"
);
}
$r = BasicStor::bsRelease($token, 'download');
if (PEAR::isError($r)){
return $r;
}
return (is_null($r['gunid']) ? $r['realFname'] : $r['gunid']);
}
/**
* Create writable URL for HTTP PUT method file insert
*
* @param string $chsum
* md5sum of the file having been put
* @param string $gunid
* global unique id
* (NULL for special files such imported playlists)
* @param int $owner
* local user id - owner of token
* @return array
* array with:
* url string: writable URL
* fname string: writable local filename
* token string: PUT token
*/
public function bsOpenPut($chsum, $gunid, $owner=NULL)
{
global $CC_CONFIG, $CC_DBC;
if (!is_null($gunid)) {
$gunid = StoredFile::NormalizeGunid($gunid);
}
$escapedChsum = pg_escape_string($chsum);
$token = StoredFile::CreateGunid();
$res = $CC_DBC->query("DELETE FROM ".$CC_CONFIG['accessTable']
." WHERE token=x'$token'::bigint");
if (PEAR::isError($res)) {
return $res;
}
$gunidSql = (is_null($gunid) ? "NULL" : "x'{$gunid}'::bigint" );
$ownerSql = (is_null($owner) ? "NULL" : "$owner" );
$res = $CC_DBC->query("
INSERT INTO ".$CC_CONFIG['accessTable']."
(gunid, token, ext, chsum, type, owner, ts)
VALUES
($gunidSql, x'$token'::bigint,
'', '$escapedChsum', 'put', $ownerSql, now())");
if (PEAR::isError($res)) {
return $res;
}
$fname = $CC_CONFIG['accessDir']."/$token";
touch($fname); // is it needed?
$url = BasicStor::GetUrlPart()."xmlrpc/put.php?token=$token";
return array('url'=>$url, 'fname'=>$fname, 'token'=>$token);
}
/**
* Get file from writable URL and return local filename.
* Caller should move or unlink this file.
*
* @param string $token
* PUT token
* @return array
* hash with fields:
* fname string, local path of the file having been put
* owner int, local subject id - owner of token
*/
public function bsClosePut($token)
{
global $CC_CONFIG, $CC_DBC;
$token = StoredFile::NormalizeGunid($token);
if (!BasicStor::bsCheckToken($token, 'put')) {
return PEAR::raiseError(
"BasicStor::bsClosePut: invalid token ($token)",
GBERR_TOKEN);
}
$row = $CC_DBC->getRow(
"SELECT chsum, owner FROM ".$CC_CONFIG['accessTable']
." WHERE token=x'{$token}'::bigint");
if (PEAR::isError($row)) {
return $row;
}
$fname = $CC_CONFIG['accessDir']."/$token";
$md5sum = md5_file($fname);
$chsum = $row['chsum'];
$owner = $row['owner'];
$error = null;
if ( (trim($chsum) != '') && ($chsum != $md5sum) ) {
// Delete the file if the checksums do not match.
if (file_exists($fname)) {
@unlink($fname);
}
$error = new PEAR_Error(
"BasicStor::bsClosePut: md5sum does not match (token=$token)".
" [$chsum/$md5sum]",
GBERR_PUT);
} else {
// Remember the MD5 sum
$storedFile = StoredFile::RecallByToken($token);
if (!is_null($storedFile) && !PEAR::isError($storedFile)) {
$storedFile->setMd5($md5sum);
} else {
# $error = $storedFile;
}
}
// Delete entry from access table.
$res = $CC_DBC->query("DELETE FROM ".$CC_CONFIG['accessTable']
." WHERE token=x'$token'::bigint");
if (PEAR::isError($error)) {
return $error;
} elseif (PEAR::isError($res)) {
return $res;
}
return array('fname'=>$fname, 'owner'=>$owner);
}
/**
* Check uploaded file
*
* @param string $token
* "Put" token
* @return array
* hash, (
* status: boolean,
* size: int - filesize
* expectedsum: string - expected checksum
* realsum: string - checksum of uploaded file
* )
*/
public function bsCheckPut($token)
{
global $CC_CONFIG, $CC_DBC;
if (!BasicStor::bsCheckToken($token, 'put')) {
return PEAR::raiseError(
"BasicStor::bsCheckPut: invalid token ($token)"
);
}
$chsum = $CC_DBC->getOne("
SELECT chsum FROM ".$CC_CONFIG['accessTable']."
WHERE token=x'{$token}'::bigint
");
if (PEAR::isError($chsum)) {
return $chsum;
}
$fname = $CC_CONFIG['accessDir']."/$token";
$md5sum = md5_file($fname);
$size = filesize($fname);
$status = ($chsum == $md5sum);
return array(
'status'=>$status, 'size'=>$size,
'expectedsum'=>$chsum,
'realsum'=>$md5sum,
);
}
/**
* Return starting part of storageServer URL
*
* @return string
* URL
*/
public static function GetUrlPart()
{
global $CC_CONFIG;
$host = $CC_CONFIG['storageUrlHost'];
$port = $CC_CONFIG['storageUrlPort'];
$path = $CC_CONFIG['storageUrlPath'];
return "http://$host:$port$path/";
}
/**
* Get tokens by type
*
* @param string $type
* access|put|render etc.
* @return array
* array of tokens
*/
public static function GetTokensByType($type)
{
global $CC_CONFIG, $CC_DBC;
$res = $CC_DBC->query(
"SELECT TO_HEX(token) AS token FROM ".$CC_CONFIG['accessTable']." WHERE type=?",
array($type));
while ($row = $res->fetchRow()) {
$r[] = $row['token'];
}
return $r;
}
/* ----------------------------------------------------- metadata methods */
/**
* Replace metadata with new XML file or string
*
* @param int $id
* Virtual file's local id
* @param string $mdata
* Local path of metadata XML file
* @param string $mdataLoc
* 'file'|'string'
* @return boolean|PEAR_Error
*/
public function bsReplaceMetadata($id, $mdata, $mdataLoc='file')
{
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
return $storedFile->setMetadata($mdata, $mdataLoc);
}
/**
* Get metadata as XML string
*
* @param int $id
* Virtual file's local id
* @return string|PEAR_Error
*/
public function bsGetMetadata($id)
{
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
return $storedFile->getMetadata();
}
/**
* Get dc:title (if exists)
*
* @param int $id
* Virtual file's local id
* @param string $gunid
* Virtual file's gunid, optional, used only if not
* null, id is then ignored
* @param string $lang
* xml:lang value for select language version
* @param string $deflang
* xml:lang for default language
* @return string|PEAR_Error
*/
public function bsGetTitle($id, $gunid=NULL, $lang=NULL, $deflang=NULL)
{
if (is_null($gunid)) {
$storedFile = StoredFile::Recall($id);
} else {
$storedFile = StoredFile::RecallByGunid($gunid);
}
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
// $r = $storedFile->md->getMetadataValue('dc:title', $lang, $deflang);
// if (PEAR::isError($r)) {
// return $r;
// }
$r = $storedFile->md["title"];
$title = (isset($r[0]['value']) ? $r[0]['value'] : 'unknown');
return $title;
}
/**
* Get metadata element value
*
* @param int $id
* Virtual file's local id
* @param string $category
* metadata element name
* @param string $lang
* xml:lang value for select language version
* @param string $deflang
* xml:lang for default language
* @return array
* array of matching records (as hash {id, value, attrs})
* @see Metadata::getMetadataValue
*/
// public function bsGetMetadataValue($id, $category, $lang=NULL, $deflang=NULL)
// {
// $storedFile = StoredFile::Recall($id);
// if (PEAR::isError($storedFile)) {
// return $storedFile;
// }
// return $storedFile->md->getMetadataValue($category, $lang, $deflang);
// }
/**
* Get metadata element value
*
* @param int $id
* Virtual file's local id
* @param string|array|null $category
* metadata element name, or array of metadata element names,
* if null is passed, all metadata values for the given ID will
* be fetched.
* @return string|array
* If a string is passed in for $category, a string is returned,
* if an array is passed, an array is returned.
* @see Metadata::getMetadataValue
*/
public function bsGetMetadataValue($id, $category = null)
{
if (!is_numeric($id)) {
return null;
}
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
if (is_null($category)) {
return $storedFile->md;
} elseif (is_array($category)) {
$values = array();
foreach ($category as $tmpCat) {
// $values[$tmpCat] = $storedFile->md->getMetadataValue($tmpCat);
$values[$tmpCat] = $storedFile->md[$tmpCat];
}
return $values;
} else {
// return $storedFile->md->getMetadataValue($category);
return $storedFile->md[$category];
}
}
public static function xmlCategoryToDbColumn($p_category)
{
global $g_metadata_xml_to_db_mapping;
if (array_key_exists($p_category, $g_metadata_xml_to_db_mapping)) {
return $g_metadata_xml_to_db_mapping[$p_category];
}
return null;
}
public static function dbColumnToXmlCatagory($p_dbColumn)
{
global $g_metadata_xml_to_db_mapping;
return array_search($p_dbColumn, $g_metadata_xml_to_db_mapping);
}
/**
* Set metadata element value
*
* @param int|StoredFile $id
* Database ID of file
* @param string $category
* Metadata element identification (e.g. dc:title)
* @param string $value
* value to store, if NULL then delete record
* @param string $lang
* xml:lang value for select language version
* @param int $mid
* (optional on unique elements) metadata record id
* @param string $container
* container element name for insert
* @param boolean $regen
* flag, if true, regenerate XML file
* @return boolean
*/
public static function bsSetMetadataValue($p_id, $p_category, $p_value)/*,
$lang=NULL, $mid=NULL, $container='metadata', $regen=TRUE)*/
{
global $CC_CONFIG, $CC_DBC;
if (!is_string($p_category) || is_array($p_value)) {
return FALSE;
}
if (is_a($p_id, "StoredFile")) {
$storedFile =& $p_id;
} else {
$storedFile = StoredFile::Recall($p_id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
}
if ($p_category == 'dcterms:extent') {
$p_value = BasicStor::NormalizeExtent($p_value);
}
$columnName = BasicStor::xmlCategoryToDbColumn($p_category); // Get column name
if (!is_null($columnName)) {
$escapedValue = pg_escape_string($p_value);
$sql = "UPDATE ".$CC_CONFIG["filesTable"]
." SET $columnName='$escapedValue'"
." WHERE id=".$storedFile->getId();
//var_dump($sql);
//$res = $storedFile->md->setMetadataValue($category, $value, $lang, $mid, $container);
$res = $CC_DBC->query($sql);
if (PEAR::isError($res)) {
return $res;
}
}
// if ($regen) {
// $r = $storedFile->md->regenerateXmlFile();
// if (PEAR::isError($r)) {
// return $r;
// }
// }
// return $res;
}
/**
* Normalize time value to hh:mm:ss:dddddd format
*
* @param mixed $v
* value to normalize
* @return string
*/
private static function NormalizeExtent($v)
{
if (!preg_match("|^\d{2}:\d{2}:\d{2}.\d{6}$|", $v)) {
require_once("Playlist.php");
$s = Playlist::playlistTimeToSeconds($v);
$t = Playlist::secondsToPlaylistTime($s);
return $t;
}
return $v;
}
/**
* Set metadata values in 'batch' mode
*
* @param int|StoredFile $id
* Database ID of file or StoredFile object
* @param array $values
* array of key/value pairs
* (e.g. 'dc:title'=>'New title')
* @param string $lang
* xml:lang value for select language version
* @param string $container
* Container element name for insert
* @param boolean $regen
* flag, if true, regenerate XML file
* @return boolean
*/
public static function bsSetMetadataBatch(
$id, $values, $lang=NULL, $container='metadata', $regen=TRUE)
{
if (!is_array($values)) {
$values = array($values);
}
if (is_a($id, "StoredFile")) {
$storedFile =& $id;
} else {
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
}
foreach ($values as $category => $oneValue) {
$res = BasicStor::bsSetMetadataValue($storedFile, $category,
$oneValue/*, $lang, NULL, $container, FALSE*/);
if (PEAR::isError($res)) {
return $res;
}
}
// if ($regen) {
// $storedFile = StoredFile::Recall($id);
// if (is_null($storedFile) || PEAR::isError($storedFile)) {
// return $storedFile;
// }
// $r = $storedFile->md->regenerateXmlFile();
// if (PEAR::isError($r)) {
// return $r;
// }
// }
return TRUE;
}
/**
* Search in local metadata database.
*
* @param array $criteria
* has the following structure:
*
\n";
print_r($va);
}
/**
* deleteFiles
*
* @return void
*/
private function deleteFiles()
{
global $CC_CONFIG, $CC_DBC;
$ids = $CC_DBC->getAll("SELECT id FROM ".$CC_CONFIG['filesTable']);
if (is_array($ids)) {
foreach ($ids as $i => $item) {
$this->bsDeleteFile($item['id'], TRUE);
}
}
}
/**
* deleteData
*
* @return void
*/
public function deleteData()
{
$this->deleteFiles();
Alib::DeleteData();
$this->initData();
}
/**
* initData - initialize
*
*/
public function initData($p_verbose = false)
{
global $CC_CONFIG;
// Create the Admin group
// if (!empty($CC_CONFIG['AdminsGr'])) {
// if (!Subjects::GetSubjId($CC_CONFIG['AdminsGr'])) {
// echo " * Creating group '".$CC_CONFIG['AdminsGr']."'...";
// // Add the admin group
// $admid = Subjects::AddSubj($CC_CONFIG['AdminsGr']);
// if (PEAR::isError($admid)) {
// return $admid;
// }
//
// // Add the "all" permission to the "admin" group
// $res = Alib::AddPerm($admid, '_all', $this->storId, 'A');
// if (PEAR::isError($res)) {
// return $res;
// }
// echo "done.\n";
// } else {
// echo " * Skipping: group already exists: '".$CC_CONFIG['AdminsGr']."'\n";
// }
// }
// Add the "all" group
// if (!empty($CC_CONFIG['AllGr'])) {
// if (!Subjects::GetSubjId($CC_CONFIG['AllGr'])) {
// echo " * Creating group '".$CC_CONFIG['AllGr']."'...";
// $allid = Subjects::AddSubj($CC_CONFIG['AllGr']);
// if (PEAR::isError($allid)) {
// return $allid;
// }
//
// // Add the "read" permission to the "all" group.
// Alib::AddPerm($allid, 'read', $this->storId, 'A');
// echo "done.\n";
// } else {
// echo " * Skipping: group already exists: '".$CC_CONFIG['AllGr']."'\n";
// }
// }
// Add the "Station Preferences" group
if (!empty($CC_CONFIG['StationPrefsGr'])) {
if (!Subjects::GetSubjId('scheduler')) {
echo " * Creating group '".$CC_CONFIG['StationPrefsGr']."'...";
$stPrefGr = Subjects::AddSubj($CC_CONFIG['StationPrefsGr']);
if (PEAR::isError($stPrefGr)) {
return $stPrefGr;
}
Subjects::AddSubjectToGroup('root', $CC_CONFIG['StationPrefsGr']);
echo "done.\n";
} else {
echo " * Skipping: group already exists: '".$CC_CONFIG['StationPrefsGr']."'\n";
}
}
// Add the root user if it doesnt exist yet.
$rootUid = Subjects::GetSubjId('root');
if (!$rootUid) {
echo " * Creating user 'root'...";
$rootUid = $this->addSubj("root", $CC_CONFIG['tmpRootPass']);
if (PEAR::isError($rootUid)) {
return $rootUid;
}
// Add root user to the admin group
// $r = Subjects::AddSubjectToGroup('root', $CC_CONFIG['AdminsGr']);
// if (PEAR::isError($r)) {
// return $r;
// }
echo "done.\n";
} else {
echo " * Skipping: user already exists: 'root'\n";
}
// Create the user named 'scheduler'.
if (!Subjects::GetSubjId('scheduler')) {
echo " * Creating user 'scheduler'...";
$subid = Subjects::AddSubj('scheduler', $CC_CONFIG['schedulerPass']);
$res = Alib::AddPerm($subid, 'read', '0', 'A');
if (PEAR::isError($res)) {
return $res;
}
//$r = Subjects::AddSubjectToGroup('scheduler', $CC_CONFIG['AllGr']);
echo "done.\n";
} else {
echo " * Skipping: user already exists: 'scheduler'\n";
}
// Need to add 'scheduler' to group StationPrefs
Subjects::AddSubjectToGroup('scheduler', $CC_CONFIG['StationPrefsGr']);
}
/**
* Aux logging for debug
*
* @param string $msg - log message
*/
public function debugLog($msg)
{
global $CC_CONFIG, $CC_DBC;
$fp = fopen($CC_CONFIG['storageDir']."/log", "a") or die("Can't write to log\n");
fputs($fp, date("H:i:s").">$msg<\n");
fclose($fp);
}
} // class BasicStor
?>