protected function _getBodyData() { try { return Zend_Json::decode($this->getRequest()->getRawBody()); } catch (Zend_Json_Exception $e) { Bbx_Log::write('JSON exception: ' . $e->getMessage()); } return $this->getRequest()->getRawBody(); }
private static function _clean($tags) { $san_tags = array(); foreach ($tags as $k => $v) { $san_tags[$k] = preg_replace('/[^A-Za-z0-9_]/', '', $v); } Bbx_Log::write('Cleaning tags ' . print_r($san_tags, true)); self::_cache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $san_tags); }
public function sync($start) { $root = realpath(APPLICATION_PATH . '/..'); $root_length = strlen($root); $remote_path = $this->_streamPath(); Bbx_Log::write("Starting sync at " . $root . $start, null, self::LOG); Bbx_Log::write("Syncing to " . $remote_path, null, self::LOG); $stack = array(); array_push($stack, $root . $start); while (!empty($stack)) { $node = array_pop($stack); if (is_dir($node)) { $rel_path = substr($node, $root_length); Bbx_Log::write("found dir " . $node, null, self::LOG); Bbx_Log::write("checking remote dir " . $remote_path . $rel_path, null, self::LOG); if (!is_resource(opendir($remote_path . ${$rel_path}))) { Bbx_Log::write($remote_path . $rel_path . ' does not exist, creating', null, self::LOG); if (!mkdir($remote_path . $rel_path)) { Bbx_Log::write('Unable to create directory, skipping', null, self::LOG); continue; } } Bbx_Log::write("dir OK", null, self::LOG); $dir_res = opendir($node); while (false !== ($dir_contents = readdir($dir_res))) { if (substr($dir_contents, 0, 1) == '.') { continue; } $path = $node . '/' . $dir_contents; Bbx_Log::write("adding " . $path . " to stack", null, self::LOG); array_push($stack, $path); } } else { if (is_file($node)) { $rel_path = substr($node, $root_length); Bbx_Log::write("copying " . $node . " to " . $remote_path . $rel_path, null, self::LOG); if (!copy($node, $remote_path . $rel_path)) { Bbx_Log::write("unable to copy " . $node . " to " . $remote_path . $rel_path, null, self::LOG); } else { Bbx_Log::write("copy OK", null, self::LOG); if (!unlink($node)) { Bbx_Log::write("unable to delete " . $node, null, self::LOG); } else { Bbx_Log::write("deleted " . $node, null, self::LOG); } } } } } $this->_syncing = false; }
protected function _doCdnSync() { $cdnType = @Bbx_Config::get()->site->cdn->type; if (APPLICATION_ENV == 'production' && $cdnType != null) { Bbx_Log::write('Doing CDN sync'); $pid = exec('nice php ' . APPLICATION_PATH . '/../library/Bbx/bin/cdn-sync.php /www/media ' . $cdnType . ' 2>&1 &', $out, $result); if ($result !== 0) { Bbx_Log::write("CDN command failed with the following output: "); Bbx_Log::write(print_r($out, true)); } } else { Bbx_Log::write('App env is ' . APPLICATION_ENV . '; cdnType is ' . $cdnType); } }
protected function _httpAuth() { $config = array('accept_schemes' => 'digest', 'realm' => Bbx_Config::get()->site->location, 'digest_domains' => '/', 'nonce_timeout' => 3600); $adaptor = new Zend_Auth_Adapter_Http($config); $this->_resolver = new Bbx_Auth_Resolver_Db(); $adaptor->setDigestResolver($this->_resolver)->setRequest($this->getRequest())->setResponse($this->getResponse()); try { $auth = $adaptor->authenticate(); if (!$auth->isValid()) { if (isset($this->_redirect)) { $body = $this->_getRedirectBody(); } else { $body = $this->_get403Body(); } $this->getResponse()->setHttpResponseCode(401)->setBody($body)->sendResponse(); exit; } return $auth->isValid(); } catch (Exception $e) { Bbx_Log::write('Exception during authentication: ' . $e->getMessage()); throw new Bbx_Controller_Rest_Exception(null, 401); } }
<?php require 'init.php'; $mail = Bbx_Mail::instance(); $mail->setFrom((string) $argv[1])->setBodyText((string) $argv[3])->setSubject((string) $argv[4]); foreach (explode(',', (string) $argv[2]) as $to) { $mail->addTo($to); } try { $mail->send(); } catch (Exception $e) { Bbx_Log::write("Couldn't send mail: " . $e->getMessage()); }
protected function _spider($url) { $queue = array(); $visited = array(); array_push($queue, $url); while (!empty($queue)) { $doc = null; $url = array_shift($queue); if ($url = $this->_sanitizeUrl($url)) { if (!in_array($url, $visited)) { $visited[] = $url; Bbx_Log::write('Spidering url ' . $url, null, Bbx_Search::LOG); $cachePath = APPLICATION_PATH . '/../www/cached' . $url . '.html'; if (file_exists($cachePath)) { Bbx_Log::write('Found file in cache', null, Bbx_Search::LOG); try { $doc = Zend_Search_Lucene_Document_Html::loadHTMLFile($cachePath, false, 'utf-8'); } catch (Exception $e) { Bbx_Log::write('Unable to open file: ' . $cachePath, null, Bbx_Search::LOG); } } else { $this->_client->setUri($this->_getAbsoluteUrl($url)); try { $response = $this->_client->request(); $status = $response->getStatus(); Bbx_Log::write('Client response code ' . $status, null, Bbx_Search::LOG); if ($status == '200') { $data = $response->getBody(); $doc = Zend_Search_Lucene_Document_Html::loadHTML($data, false, 'utf-8'); } } catch (Exception $e) { Bbx_Log::write('Request failed: ' . $e->getMessage(), null, Bbx_Search::LOG); } } if ($doc !== null) { $this->_search()->indexDoc($doc, $url); $this->_indexed++; $links = array_diff($doc->getLinks(), $this->_visited); if (count($visited) < $this->_maxLinks) { $queue = array_merge($queue, $links); } else { Bbx_Log::write('Reached max number of links (' . $this->_maxLinks . '), exiting', null, Bbx_Search::LOG); exit; } } } } } }
<?php require 'init.php'; Bbx_Log::write('Starting CDN Sync'); $sentinel_file = APPLICATION_PATH . '/../www/media/.cdn_syncing'; if (file_exists($sentinel_file)) { if (time() - filemtime($sentinel_file) < 86400) { Bbx_Log::write('CDN is already syncing'); exit; } } if (!touch($sentinel_file)) { Bbx_Log::write('Unable to create sentinel file for CDN sync'); } else { set_time_limit(7200); $start = (string) $argv[1]; $cdnType = (string) $argv[2]; Bbx_Log::write('CDN syncing at ' . $start . ', CDN type is ' . $cdnType); switch ($cdnType) { case 's3': $cdn = Bbx_Model_Default_Media_Cdn_S3::init(); } $cdn->sync($start); if (!unlink($sentinel_file)) { Bbx_Log::write('Unable to delete sentinel file for CDN sync'); } }
public function migrateAction() { $users = Bbx_Model::load('User')->findAll(); if (count($users) > 0) { // $this->_helper->authenticate(); } $dirname = APPLICATION_PATH . '/scripts/migrations'; if ($handle = opendir($dirname)) { $db = Zend_Registry::get('db')->getConnection(); $mignames = array(); while (false !== ($entry = readdir($handle))) { if (!in_array($entry, array('.', '..'))) { array_push($mignames, $entry); } } sort($mignames); foreach ($mignames as $name) { Bbx_Log::write('Running migration ' . $name); $sql = file_get_contents($dirname . '/' . $name); if (!$db->multi_query($sql)) { throw new Zend_Db_Exception('Can\'t complete migrations: ' . $db->error); } do { $db->use_result(); } while ($db->next_result()); } closedir($handle); } else { Bbx_Log::write('Unable to find migrations directory ' . $dirname); } }
protected function _documentExists($docId) { @($hits = $this->_index->find('docId:' . $docId)); foreach ($hits as $hit) { if ($hit->docId == $docId) { Bbx_Log::write('Found existing document: ' . urldecode($hit->url), null, self::LOG); return $hit; } } return false; }
protected function _put($data = null) { $this->_helper->authenticate(); try { $model = $this->_helper->Model->getModel(); $method = $model->isEmpty() ? 'create' : 'update'; if ($data === null) { $data = $this->_getBodyData(); } $model->{$method}($data); Bbx_CacheManager::clean($this->getRequest(), 'put'); } catch (Exception $e) { Bbx_Log::write('Unable to _put to model: ' . $e->getMessage()); throw new Bbx_Controller_Rest_Exception(null, 405, array('allowed_methods' => 'GET,POST')); } }
public function attachMedia($filePath, $overwrite = true) { Bbx_Log::debug("trying to attach media to Image " . $this->id); $this->setSize('original'); try { $img = Bbx_Media_Image::load($filePath); } catch (Exception $e) { throw new Bbx_Model_Exception("Couldn't load image " . $filePath); Bbx_Log::write(print_r($e, true)); } try { if (file_exists($this->getMediaPath('original')) && !$overwrite) { Bbx_Log::debug('original exists (' . $this->getMediaPath('original') . ')'); return $this->_createSizedMedia(null, $overwrite); } $img->setResolution(300)->save($this->getMediaPath(null, true)); $this->width = $img->width(); $this->height = $img->height(); if ($this->width == 0 || $this->height == 0) { throw new Bbx_Model_Exception('Unable to create sized media for image ' . $filePath); } $this->save(); $this->_createSizedMedia(); } catch (Exception $e) { Bbx_Log::debug($e->getMessage()); throw new Bbx_Model_Exception('Unable to create sized media for image ' . $filePath); } }
public static function debug($str, $type = 'info', $log = 'main_log') { return Bbx_Log::write('DEBUG: ' . $str, $type, $log); }
<?php require 'init.php'; $sentinel_file = APPLICATION_PATH . '/modules/' . MODULE_NAME . '/search/.indexing'; if (file_exists($sentinel_file)) { Bbx_Log::write('Spider is already indexing'); exit; } touch($sentinel_file); set_time_limit(21600); $host = (string) $argv[1]; $spider = new Bbx_Search_Spider(); $spider->start('/', $host); unlink($sentinel_file);
public function hasLinkable($key) { $method = 'hasLinkable' . Inflector::camelize($key); if (method_exists($this, $method)) { return $this->{$method}(); } try { return $this->{$key}->isLinkable(); } catch (Exception $e) { Bbx_Log::write('Exception when checking isLinkable (' . $key . '): ' . $e->getMessage()); return false; } }
protected function _set500() { Bbx_Log::write(print_r($this->_error, true)); $this->getResponse()->setHttpResponseCode(500); $this->view->errorType = 'Server Error'; $this->view->error = $this->_error['exception']->getMessage(); $this->view->responseCode = '500'; $url = $this->getRequest()->getRequestUri(); $this->view->errorVars = array('url' => $url); $this->_notify(); }