function delete() { global $db; $db->begin(); sotf_NodeObject::nodeLeavingNetwork($this->get('node_id')); parent::delete(); $db->commit(); }
/** may be static, if all paramters filled -- When you have to send forward stats data to the home node */ function createForwardObject($type, $data, $objId = 0, $nodeId = 0) { global $db; if (!$objId) { $objId = $this->id; } // the id of the target object if (!$nodeId) { $nodeId = $this->getNodeId(); } // the id of the node to send this to $obj = new sotf_Object("sotf_to_forward"); $obj->setAll(array('prog_id' => $objId, 'node_id' => $nodeId, 'type' => $type, 'entered' => $db->getTimestampTz(), 'data' => serialize($data))); $obj->create(); }
function startStreaming() { global $config, $page, $db; if ($config['httpStreaming']) { $this->makeLocalPlaylist(); $this->url = $config['tmpUrl'] . '/pl_' . $this->getTmpId() . '.m3u'; return; } // check if the stream has started already (Win+IE+Media player) $urls = $db->getCol("SELECT url FROM sotf_streams WHERE host='" . getHostName() . "' AND started < CURRENT_TIMESTAMP AND started > CURRENT_TIMESTAMP - interval '15 seconds'"); if (count($urls) == 1) { debug("found url for Win Explorer", $urls[0]); $this->url = $urls[0]; // found stream so we can return return; } $this->stopMyStream(); if ($config['tamburineURL']) { // streaming with tamburine + XML-RPC // playlist reset($this->audioFiles); while (list(, $audioFile) = each($this->audioFiles)) { $songs[] = $audioFile['path']; } $rpc = new rpc_Utils(); //$rpc->debug = true; $response = $rpc->callTamburine('setpls', $songs); if (is_null($response)) { raiseError("no reply from tamburine server"); } else { $this->url = $response[2]; $this->streamId = $response[1]; } if (!$this->url) { raiseError("Could not find mount point for stream!"); } } elseif ($config['tamburineCMD']) { // streaming with tbrcmd if (!$this->localPlaylist) { $this->makeLocalPlaylist(); } $cmd = $config['tamburineCMD'] . " setpls " . $this->localPlaylist . " 2>&1"; exec($cmd, $output, $retval); debug("cmd", $cmd); //debug("output", $output); //debug("retval", $retval); $lines = array_values(preg_grep('/Fatal Error:/', $output)); if (count($lines) > 0) { raiseError(join(", ", $lines)); // TODO: restart tamburine (??) } //$lines = preg_grep('/Stream\[(\d+)\]\s+spawned on (\S+)/', $output); foreach ($output as $line) { if (preg_match('/Stream\\[(\\d+)\\]\\s+spawned on (\\S+)/', $line, $mm)) { $this->streamId = $mm[1]; $this->url = $mm[2]; break; } } if (!$this->url) { raiseError("Could not find mount point for stream!"); } } else { // command-line streaming if (!$this->localPlaylist) { $this->makeLocalPlaylist(); } $this->url = 'http://' . $config['iceServer'] . ':' . $config['icePort'] . '/' . $this->getMountPoint() . "\n"; //$url = preg_replace('/^.*\/repository/', 'http://sotf2.dsd.sztaki.hu/node/repository', $filepath); // TODO: calculate bitrate from all files... $bitrate = $this->audioFiles[0]['bitrate']; if (!$bitrate) { $bitrate = 24; } $this->cmdStart($this->localPlaylist, $this->getMountPoint(), $bitrate); //$this->cmdStart2($bitrate); } if ($this->url) { $streamData = array('pid' => $this->streamId, 'url' => $this->url, 'started' => $db->getTimestamp(), 'length' => round($this->totalLength), 'will_end_at' => $db->getTimestamp(time() + round($this->totalLength)), 'host' => getHostName()); debug("streamData", $streamData); $_SESSION['stream'] = $streamData; $obj = new sotf_Object('sotf_streams'); $obj->setAll($streamData); $obj->create(); // TODO wait until stream really starts sleep(3); } }
function setGroup($uid, $gid, $member, $rid = '') { if ($rid) { if (!$member) { $o = new sotf_Object('sotf_user_groups', $rid); $o->delete(); } return; } $o = new sotf_Object('sotf_user_groups'); $o->set('user_id', $uid); $o->set('group_id', $gid); $o->find(); debug("EXISTS", $o->exists()); debug("MEM", $member); if ($member) { if (!$o->exists()) { $o->create(); } } else { if ($o->exists()) { $o->delete(); } } }
function recordStat($data, $update = false) { global $db, $repository, $sotfVars; $type = $data['type']; if ($type != 'listens' && $type != 'downloads' && $type != 'visits') { raiseError("addStat: type should be 'listens' or 'downloads' or 'visits'"); } // update periodic stat $date = $data['date']; //debug("date", $db->getTimestampTz($date)); $now = getdate($date); //debug("now", $now); $year = $now['year']; $month = $now['mon']; $day = $now['mday']; $week = date('W', $date); $prgId = $data['prog_id']; $fileId = $data['file']; $where = " WHERE prog_id='{$prgId}' AND year='{$year}' AND month='{$month}' AND day='{$day}' AND week='{$week}'"; $prg = $repository->getObject($prgId); if (!$prg) { // don't raiseError("addStat: no such programme: $prgId"); return null; } $db->begin(); // to avoid deadlocks I try this: $db->query("LOCK TABLE sotf_stats, sotf_unique_access, sotf_to_update IN ROW EXCLUSIVE MODE"); $id = $db->getOne("SELECT id FROM sotf_stats {$where}"); if ($id) { $obj = new sotf_Statistics($id); $obj->set($type, $obj->get($type) + 1); // station may change!! $obj->set('station_id', $prg->get('station_id')); } else { $obj = new sotf_Statistics(); $obj->setAll(array('prog_id' => $prgId, 'station_id' => $prg->get('station_id'), 'year' => $year, 'month' => $month, 'week' => $week, 'day' => $day, $type => 1)); } if ($obj->exists()) { $obj->update(); } else { $obj->create(); //debug("obj1", $obj); $obj->find(); // to get the id //debug("obj2", $obj); } // update uniqueness memory sotf_Statistics::addUniqueAccess($data['ip'], $prgId, $fileId, $type); // would be too often: if ($update) { $obj->updateStats(false); } else { sotf_Object::addToUpdate('sotf_stats', $obj->id); } $db->commit(); return $obj; }
$x->set('supertopic', $parent); $x->set('name', $name); $x->create(); $id = $x->getID(); } if ($topic_name != "") { $x = new sotf_NodeObject("sotf_topics"); $x->set('topic_id', $topic_id); $x->set('language', "en"); $x->set('topic_name', $topic_name); $x->create(); $id = $x->getID(); print $id; } if ($topic_counter != "") { $x = new sotf_Object("sotf_topics_counter"); $x->set('topic_id', $topic_id); $x->set('number', $topic_counter); $x->create(); $id = $x->getID(); print $id; } $query = "SELECT sotf_topic_tree_defs.*, sotf_topics.topic_name, sotf_topics_counter.number from sotf_topic_tree_defs" . " LEFT JOIN sotf_topics ON sotf_topics.topic_id = sotf_topic_tree_defs.id" . " LEFT JOIN sotf_topics_counter ON sotf_topics_counter.topic_id = sotf_topic_tree_defs.id"; $result = $db->getAll($query); usort($result, "SORTIT"); $parentid; $counter = 0; $max = count($result); for ($i = 0; $i < $max; $i++) { if ($result[$i]["supertopic"] == 0) { if ($i != 0) {
} elseif ($name == "addtree") { $vocabularies->addToTopic($id, $value); // doesnt work: print("<script type=\"text/javascript\">window.opener.opener.reload();</script>"); print "success"; } elseif ($name == "editorpub") { $x = new sotf_Programme($id); if ($value == 'true') { $x->publish(); } elseif ($value == 'false') { $x->withdraw(); } else { print "<script type=\"text/javascript\" language=\"javascript1.1\">error();</script>"; } print "success"; } elseif ($name == "editorflag") { $x = new sotf_Object("sotf_user_progs"); $x->set('user_id', $user->id); $x->set('prog_id', $id); $x->find(); $x->set('flags', $value); if ($x->id) { if ($value == "none") { $x->delete(); } else { $x->update(); } } else { $x->create(); } // else print("<script type=\"text/javascript\" language=\"javascript1.1\">error();</script>"); print "success";
function addStat($fileId, $type) { global $db, $user; sotf_Statistics::addStat($this, $fileId, $type); if ($user) { $hist = new sotf_Object('sotf_user_history'); $hist->set('user_id', $user->id); $hist->set('object_id', $this->id); $hist->set('action', $type); $hist->set('action_date', $db->getTimestampTz()); $hist->create(); } }
function setRemoteRating($data) { global $repository; $obj =& $repository->getObject($data['prog_id']); if ($obj) { $this->recordRating($data); sotf_Object::addToUpdate('ratingUpdate', $data['prog_id']); //$this->updateInstant($data['prog_id']); } else { debug("Rating for non-existent prog", $data['prog_id']); } }
function processPortalEvent($event) { debug("processing event", $event); $progId = $event['prog_id']; if ($progId) { if ($this->looksLikeId($progId)) { $prg =& $this->getObject($progId); } if (!$prg) { debug("Invalid prog_id arrived in portal event", $progId); return -1; } } switch ($event['name']) { case 'programme_added': $obj = new sotf_NodeObject('sotf_prog_refs'); $obj->set('prog_id', $event['value']); $obj->set('url', $event['url']); $obj->find(); $obj->set('station_id', $prg->get('station_id')); $obj->set('start_date', $event['timestamp']); $obj->set('portal_name', $event['portal_name']); $obj->save(); break; case 'programme_deleted': $obj = new sotf_NodeObject('sotf_prog_refs'); $obj->set('prog_id', $event['value']); $obj->set('url', $event['url']); $obj->find(); if (!$obj->exists()) { debug("unknown prog ref arrives: " . $event['value'] . ' - ' . $event['url']); $obj->set('portal_name', $event['portal_name']); } $obj->set('station_id', $prg->get('station_id')); $obj->set('end_date', $event['timestamp']); //$obj->set('portal_name', $event['portal_name']); $obj->save(); break; case 'visit': $obj = new sotf_NodeObject('sotf_prog_refs'); $obj->set('prog_id', $event['value']['prog_id']); $obj->set('url', $event['url']); $obj->find(); if (!$obj->exists()) { // TODO: how can this happen? It happens too many times! debug("unknown prog ref arrives: " . $event['value']['prog_id'] . ' - ' . $event['url']); $obj->set('start_date', $event['timestamp']); $obj->set('portal_name', $event['portal_name']); } $obj->set('station_id', $prg->get('station_id')); $obj->set('visits', (int) $obj->get('visits') + 1); // TODO: count unique accesses $obj->save(); break; case 'page_impression': $obj = new sotf_NodeObject('sotf_portals'); $obj->set('url', $event['url']); $obj->find(); $obj->set('name', $event['portal_name']); $obj->set('page_impression', $event['value']); $obj->set('last_access', $event['timestamp']); $obj->save(); break; case 'portal_updated': $obj = new sotf_NodeObject('sotf_portals'); $obj->set('url', $event['url']); $obj->find(); $obj->set('name', $event['portal_name']); $obj->set('last_update', $event['timestamp']); $obj->save(); break; case 'users': $obj = new sotf_NodeObject('sotf_portals'); $obj->set('url', $event['url']); $obj->find(); if (!$obj->exists()) { $obj->set('name', $event['portal_name']); } $obj->set('last_update', $event['timestamp']); $obj->set('reg_users', $event['value']); if (!$obj->get('name') || !$obj->get('url')) { logError("Bad portal even teceived", implode(" | ", $event)); } else { $obj->save(); } break; case 'rating': // first save in prog_refs $obj = new sotf_NodeObject('sotf_prog_refs'); $obj->set('prog_id', $event['value']['prog_id']); $obj->set('url', $event['url']); $obj->find(); if (!$obj->exists()) { debug("unknown prog ref arrives: " . $event['url']); $obj->set('start_date', $event['timestamp']); $obj->set('portal_name', $event['portal_name']); } $obj->set('station_id', $prg->get('station_id')); $obj->set('rating', $event['value']['RATING_VALUE']); $obj->set('raters', $event['value']['RATING_COUNT']); $obj->save(); // TODO second, put into global rating database /* $rating = new sotf_Rating(); $id = $event['value']['prog_id']; $obj = & $this->getObject($id); if($obj->isLocal()) { $data = $event['value']; $rating->setRemoteRating($data); } else { logError("received rating for non-local object!"); } */ break; case 'comment': // first save in prog_refs $obj = new sotf_NodeObject('sotf_prog_refs'); $obj->set('prog_id', $event['value']['prog_id']); $obj->set('url', $event['url']); $obj->find(); if (!$obj->exists()) { logError("unknown prog ref arrives: " . $event['value']['prog_id'] . ' - ' . $event['url']); $obj->set('start_date', $event['timestamp']); $obj->set('portal_name', $event['portal_name']); } $obj->set('station_id', $prg->get('station_id')); $obj->set('comments', (int) $obj->get('comments') + 1); $obj->save(); // save comment $obj = new sotf_Object('sotf_comments'); $obj->set('prog_id', $event['value']['prog_id']); $obj->set('portal', $event['url']); $obj->set('entered', $event['timestamp']); $obj->set('comment_title', $event['value']['title']); $obj->set('comment_text', $event['value']['comment']); $obj->set('from_name', $event['value']['user_name']); $obj->set('from_email', $event['value']['email']); $obj->create(); // TODO forward to authors break; case 'query_added': //debug("query from portal", $event); //debug("query from portal", $event); case 'query_deleted': case 'file_uploaded': // silently ignored break; default: logError("unknown portal event: " . $event['name']); } }
?> CRON</title></head> <body> <?php while (@ob_end_flush()) { } debug("--------------- CRON STARTED -----------------------------------"); line("CRON STARTED"); // this can be long duty! set_time_limit(18000); // don't garble reply message with warnings in HTML //ini_set("display_errors", 0); //******** cascading deletes may have problems. $repository->cleanTables(); //******** Perform expensive updates on objects sotf_Object::doUpdates(); line("UPDATES FINISHED"); //******** Synchronize with network: send new local data and forward new remote data // sync with all neighbours $rpc = new rpc_Utils(); $neighbours = sotf_Neighbour::listAll(); //debug("neighbours", $neighbours); if (count($neighbours) > 0) { while (list(, $neighbour) = each($neighbours)) { $neighbour->sync(); } } line("SYNC FINISHED"); //******** Forward messages to remote nodes // for all nodes $nodes = sotf_Node::listAll();
/** Private! Compares a replicated object to the local one and saves it if it's newer than the local. */ function saveReplica() { global $sotfVars; $oldData = $this->db->getRow("SELECT * FROM sotf_node_objects WHERE id='{$this->id}' "); //debug("changed", $changed); //debug("lch", $this->lastChange); if (count($oldData) > 0) { if ($this->internalData['change_stamp'] && $this->internalData['change_stamp'] > $oldData['change_stamp']) { // this is newer, save it sotf_Object::update(); // save internal data $this->internalData['arrived_stamp'] = $sotfVars->get('sync_stamp', 0); $this->internalData['arrived'] = $this->db->getTimestampTz(); $internalObj = new sotf_Object('sotf_node_objects', $this->internalData['id'], $this->internalData); $internalObj->update(); // save binary fields /* reset($this->binaryFields); while(list(,$field)=each($this->binaryFields)) { sotf_Object::setBlob($field, $this->db->unescape_bytea($this->data[$field])); } */ debug("updated ", $this->id); return true; } elseif ($this->internalData['change_stamp'] && $this->internalData['change_stamp'] == $oldData['change_stamp']) { debug("arrived same version of", $this->id); return true; } else { debug("arrived older version of", $this->id); return false; } } else { $this->internalData['arrived_stamp'] = $sotfVars->get('sync_stamp', 0); $this->internalData['arrived'] = $this->db->getTimestampTz(); $internalObj = new sotf_Object('sotf_node_objects', $this->id, $this->internalData); $internalObj->create(); $success = sotf_Object::create(); if (!$success) { $internalObj->delete(); } debug("created ", $this->id); return $success; } }
function setBlob($prop_name, $prop_value) { parent::setBlob($prop_name, $prop_value); $this->updateInternalData(); }