Example #1
0
 function routematch($url = NULL)
 {
     // Match a URL against against one of the routes contained.
     if ($this->activeroute) {
         return;
     }
     $return = false;
     trigger_before('routematch', $this, $this->activeroute);
     if ($url === NULL) {
         $url = $this->uri;
     }
     foreach ($this->routes as $route) {
         if ($this->match($url, $route)) {
             break;
             $return = true;
         }
     }
     if (isset($this->params['method']) && !is_array($this->params['method'])) {
         $this->action = $this->method;
     }
     if (isset($this->params['forward_to'])) {
         $this->controller = $this->forward_to;
     }
     if (isset($this->action)) {
         if (!(strpos($this->action, ".") === false)) {
             // check for period
             $actionsplit = split("\\.", $this->action);
             $this->set_param('action', $actionsplit[0]);
             $this->set('client_wants', $actionsplit[1]);
         }
     }
     if (isset($this->resource)) {
         if (!(strpos($this->resource, ".") === false)) {
             // check for period
             $actionsplit = split("\\.", $this->resource);
             $this->set_param('resource', $actionsplit[0]);
             $this->set('client_wants', $actionsplit[1]);
         }
     }
     trigger_after('routematch', $this, $this->activeroute);
     return $return;
 }
Example #2
0
 function pick_template_extension(&$request, $template = null)
 {
     trigger_before('pick_template_extension', $this, $this);
     if (!empty($request->client_wants)) {
         return $request->client_wants;
     }
     $ext = $this->negotiate_content($request, $template);
     $this->extension = $ext;
     if (!$ext) {
         // if ( content-negotiation fails ) go to html
         $variants = array(array('id' => 'html', 'qs' => 1.0, 'type' => 'text/html', 'encoding' => null, 'charset' => 'utf-8', 'language' => 'en', 'size' => 3000));
         $this->negotiator = $variants;
         $ext = 'html';
     }
     return $ext;
 }
Example #3
0
 function get_query($id = NULL, $find_by = NULL, &$model)
 {
     if (isset($model->query)) {
         $q = $model->query;
         unset($model->query);
         return $q;
     }
     $model->set_param('id', $id);
     $model->set_param('find_by', $find_by);
     trigger_before('get_query', $model, $this);
     $pkfield = $model->primary_key;
     if ($model->find_by == NULL) {
         $model->set_param('find_by', $model->primary_key);
     }
     $relfields = array();
     $relfields = $model->relations;
     $table = $this->prefix . $model->table;
     $fieldstring = '';
     $sql = "SELECT " . "\n";
     if (!array_key_exists($pkfield, $model->field_array)) {
         $sql .= "{$table}.{$pkfield} as \"{$table}.{$pkfield}\", " . "\n";
     }
     foreach ($model->field_array as $fieldname => $datatypename) {
         if (strpos($fieldname, ".") === false) {
             $fieldname = $table . "." . $fieldname;
         }
         $fieldstring .= "{$fieldname} as \"{$fieldname}\", " . "\n";
     }
     $leftsql = "";
     $first = true;
     if (count($relfields) > 0) {
         foreach ($relfields as $key => $val) {
             $spl = split("\\.", $val["fkey"]);
             if (!$this->models[$spl[0]]->exists) {
                 ${$spl}[0] =& $this->get_table($spl[0]);
             }
             $leftsql .= "(";
         }
         foreach ($relfields as $key => $val) {
             $spl = split("\\.", $val["fkey"]);
             if ($val["type"] == 'child-many') {
                 $join =& $this->get_table($model->join_table_for($table, $val['tab']));
                 $spl[0] = $this->prefix . $join->table;
                 $val["fkey"] = $this->prefix . $join->table . '.' . strtolower(classify($table)) . "_" . $model->foreign_key_for($table);
             } else {
                 foreach ($this->models[$spl[0]]->field_array as $fieldname => $datatypename) {
                     $fieldstring .= $this->prefix . $spl[0] . "." . $fieldname . " as \"" . $this->prefix . $spl[0] . "." . $fieldname . "\", " . "\n";
                 }
             }
             if ($first) {
                 $leftsql .= $table;
             }
             $leftsql .= " left join " . $this->prefix . $spl[0] . " on " . $table . "." . $val["col"] . " = " . $val["fkey"];
             $leftsql .= ")";
             $first = false;
         }
     }
     $fieldstring = substr($fieldstring, 0, -3) . " " . "\n";
     $sql .= $fieldstring;
     $sql .= "FROM ";
     $sql .= $leftsql;
     if (!(strlen($leftsql) > 1)) {
         $sql .= $table;
     }
     if (is_array($model->find_by)) {
         $findfirst = true;
         $op = "AND";
         $eq = '=';
         foreach ($model->find_by as $col => $val) {
             if (is_array($val)) {
                 list($col, $val) = each($val);
             }
             if ($col == 'op') {
                 $op = $val;
             } elseif ($col == 'eq') {
                 $eq = $val;
             } else {
                 if (strpos($col, ".") === false) {
                     $field = "{$table}.{$col}";
                 } else {
                     $field = $this->prefix . $col;
                 }
                 if ($findfirst) {
                     $sql .= " WHERE {$field} {$eq} '{$val}' ";
                 } else {
                     $sql .= " {$op} {$field} {$eq} '{$val}' ";
                 }
                 $findfirst = false;
             }
         }
     } elseif ($model->id != NULL) {
         if (strpos($model->find_by, ".") === false) {
             $field = $table . "." . $model->find_by;
         } else {
             $field = $model->find_by;
         }
         $sql .= " WHERE {$field} = '" . $model->id . "' ";
     }
     if (!isset($model->orderby)) {
         $model->orderby = $table . "." . $pkfield;
     }
     if (!isset($model->order)) {
         $model->order = "DESC";
     }
     if (!isset($model->offset)) {
         $model->offset = 0;
     }
     if (!isset($model->limit)) {
         $model->limit = 20;
     }
     if (isset($model->groupby)) {
         $sql .= " GROUP BY " . $model->groupby . " ";
     }
     $sql .= " ORDER BY " . $model->orderby . " ";
     $sql .= $model->order . $this->query_limit($model->limit, $model->offset);
     trigger_after('get_query', $model, $this);
     return $sql;
 }
Example #4
0
function _admin(&$vars)
{
    include 'wp-content/language/lang_chooser.php';
    //Loads the language-file
    extract($vars);
    global $submenu, $current_user;
    trigger_before('admin_menu', $current_user, $current_user);
    $menuitems = array();
    $apps_list = array();
    global $env;
    if (is_array($env['apps'])) {
        $apps_list = $env['apps'];
    }
    $i = $Identity->find(get_profile_id());
    while ($s = $i->NextChild('settings')) {
        $s = $Setting->find($s->id);
        $e = $s->FirstChild('entries');
        $apps_list[] = $s->value;
    }
    $menuitems[$request->url_for(array('resource' => 'identities', 'id' => get_profile_id(), 'action' => 'edit')) . '/partial'] = $txt['identities_settings'];
    $menuitems[$request->url_for(array('resource' => 'identities', 'id' => get_profile_id(), 'action' => 'subs')) . '/partial'] = $txt['identities_friends'];
    //$menuitems[$request->url_for(array(
    //  'resource'=>'identities',
    //  'id'=>get_profile_id(),
    //  'action'=>'apps'
    //  )).'/partial'] = 'Apps';
    foreach ($submenu as $arr) {
        if (in_array($arr[0][0], $apps_list)) {
            $menuitems[$arr[0][4]] = $arr[0][3];
        }
    }
    return vars(array(&$menuitems), get_defined_vars());
}
Example #5
0
 /**
  * Set Value
  * 
  * change a Record attribute value, and
  * register the change in the database
  * 
  * @author Brian Hendrickson <*****@*****.**>
  * @access public
  * @param string field_name
  * @param string value
  */
 function set_value($field, $value)
 {
     global $db;
     trigger_before('set_value', $this, $db);
     if (!isset($this->attributes[$this->primary_key])) {
         $pkfield = $this->primary_key;
         $this->attributes[$pkfield] = "";
         $this->{$pkfield} =& $this->attributes[$pkfield];
     }
     if ($this->validate_field($field, $value)) {
         if ($db->models[$this->table]->is_blob($field) && is_array($value)) {
             $value = $value['tmp_name'];
         }
         $this->attributes[$field] = $value;
         if (!isset($this->{$field})) {
             $this->{$field} =& $this->attributes[$field];
         }
         $this->modified_fields[] = $field;
     } else {
         trigger_error("the new value for {$field} is invalid", E_USER_ERROR);
     }
     trigger_after('set_value', $this, $db);
 }
Example #6
0
function handle_posted_file($filename = "", $att, $profile)
{
    global $db, $request, $response;
    $response->set_var('profile', $profile);
    load_apps();
    if (isset($_FILES['media']['tmp_name'])) {
        $table = 'uploads';
    } else {
        $table = 'posts';
    }
    $modelvar = classify($table);
    $_FILES = array(strtolower($modelvar) => array('name' => array('attachment' => $filename), 'tmp_name' => array('attachment' => $att)));
    $Post =& $db->model('Post');
    $Upload =& $db->model('Upload');
    $field = 'attachment';
    $request->set_param('resource', $table);
    $request->set_param(array(strtolower(classify($table)), $field), $att);
    trigger_before('insert_from_post', ${$modelvar}, $request);
    $content_type = 'text/html';
    $rec = ${$modelvar}->base();
    $content_type = type_of($filename);
    $rec->set_value('profile_id', get_profile_id());
    $rec->set_value('parent_id', 0);
    if (isset($request->params['message'])) {
        $rec->set_value('title', $request->params['message']);
    } else {
        $rec->set_value('title', '');
    }
    if ($table == 'uploads') {
        $rec->set_value('tmp_name', 'new');
    }
    $upload_types = environment('upload_types');
    if (!$upload_types) {
        $upload_types = array('jpg', 'jpeg', 'png', 'gif');
    }
    $ext = extension_for(type_of($filename));
    if (!in_array($ext, $upload_types)) {
        trigger_error('Sorry, this site only allows the following file types: ' . implode(',', $upload_types), E_USER_ERROR);
    }
    $rec->set_value($field, $att);
    $rec->save_changes();
    $tmp = $att;
    if (is_jpg($tmp)) {
        $thumbsize = environment('max_pixels');
        $Thumbnail =& $db->model('Thumbnail');
        $t = $Thumbnail->base();
        $newthumb = tempnam("/tmp", "new" . $rec->id . ".jpg");
        resize_jpeg($tmp, $newthumb, $thumbsize);
        $t->set_value('target_id', $atomentry->id);
        $t->save_changes();
        update_uploadsfile('thumbnails', $t->id, $newthumb);
        $t->set_etag();
    }
    $atomentry = ${$modelvar}->set_metadata($rec, $content_type, $table, 'id');
    ${$modelvar}->set_categories($rec, $request, $atomentry);
    $url = $request->url_for(array('resource' => $table, 'id' => $rec->id));
    //	$title = substr($rec->title,0,140);
    //	$over = ((strlen($title) + strlen($url) + 1) - 140);
    //	if ($over > 0)
    //	  $rec->set_value('title',substr($title,0,-$over)." ".$url);
    //	else
    //	  $rec->set_value('title',$title." ".$url);
    //	$rec->save_changes();
    trigger_after('insert_from_post', ${$modelvar}, $rec);
    return true;
}
Example #7
0
/**
 * render_static
 * 
 * filter to intercept calls to static views
 * 
 * @author Brian Hendrickson <*****@*****.**>
 * @access public
 * @param object $req
 * @param object $route
 */
function render_static(&$req, &$route)
{
    trigger_before('render_static', $req, $route);
    if (!session_started()) {
        session_start();
    }
    ob_start();
    /*
     * List of known content types based on file extension.
     * Note: These must be built-in somewhere...
     */
    $known_content_types = array("htm" => "text/html", "html" => "text/html", "js" => "text/javascript", "css" => "text/css", "xml" => "text/xml", "gif" => "image/gif", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" => "image/png", "txt" => "text/plain");
    /*
     * Get the path of the target file.
     */
    if (in_array('static', $req->params, true)) {
        $resource = "";
        $slash = "";
        for ($i = 4; $i < count($req->params); $i++) {
            $resource .= $slash . $req->params[$i];
            $slash = "/";
        }
        /*
         * Verify the existence of the target file.
         * Return HTTP 404 if needed.
         */
        if (($src_uri = realpath($resource)) === false) {
            /* The file does not exist */
            header("HTTP/1.1 404 Not Found");
            echo "<html><body><h1>HTTP 404 - Not Found</h1></body></html>";
            exit;
        }
        /*
         * Verify the requested file is under the doc root for security reasons.
         */
        $doc_root = realpath(".");
        if (strpos($src_uri, $doc_root) !== 0) {
            header("HTTP/1.1 403 Forbidden");
            echo "<html><body><h1>HTTP 403 - Forbidden</h1></body></html>";
            exit;
        }
        /*
         * Set the HTTP response headers that will
         * tell the client to cache the resource.
         */
        $file_last_modified = filemtime($src_uri);
        header("Last-Modified: " . date("r", $file_last_modified));
        $max_age = 300 * 24 * 60 * 60;
        // 300 days
        $expires = $file_last_modified + $max_age;
        header("Expires: " . date("r", $expires));
        $etag = dechex($file_last_modified);
        header("ETag: " . $etag);
        $cache_control = "must-revalidate, proxy-revalidate, max-age=" . $max_age . ", s-maxage=" . $max_age;
        header("Cache-Control: " . $cache_control);
        /*
         * Check if the client should use the cached version.
         * Return HTTP 304 if needed.
         */
        if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $file_last_modified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
            //error_log('using cached static file');
            header("HTTP/1.1 304 Not Modified");
            exit;
        }
        if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) === $etag) {
            //error_log('using cached static file');
            header("HTTP/1.1 304 Not Modified");
            exit;
        }
        /*
         * Extract the directory, file name and file
         * extension from the "uri" parameter.
         */
        $uri_dir = "";
        $file_name = "";
        $content_type = "";
        $uri_parts = explode("/", $src_uri);
        for ($i = 0; $i < count($uri_parts) - 1; $i++) {
            $uri_dir .= $uri_parts[$i] . "/";
        }
        $file_name = end($uri_parts);
        $file_parts = explode(".", $file_name);
        if (count($file_parts) > 1) {
            $file_extension = end($file_parts);
            $content_type = $known_content_types[$file_extension];
        }
        /*
         * Get the target file.
         * If the browser accepts gzip encoding, the target file
         * will be the gzipped version of the requested file.
         */
        $dst_uri = $src_uri;
        $compress = true;
        /*
         * Let's compress only text files...
         */
        $compress = $compress && strpos($content_type, "text") !== false;
        /*
         * Finally, see if the client sent us the correct Accept-encoding: header value...
         */
        $compress = $compress && strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false;
        if ($compress) {
            $gz_uri = "tmp/gzip/" . $src_uri . ".gz";
            if (file_exists($gz_uri)) {
                $src_last_modified = filemtime($src_uri);
                $dst_last_modified = filemtime($gz_uri);
                // The gzip version of the file exists, but it is older
                // than the source file. We need to recreate it...
                if ($src_last_modified > $dst_last_modified) {
                    unlink($gz_uri);
                }
            }
            if (!file_exists($gz_uri)) {
                if (!file_exists("tmp/gzip/" . $uri_dir)) {
                    mkdir_r("tmp/gzip/" . $uri_dir);
                }
                $error = false;
                if ($fp_out = gzopen($gz_uri, "wb")) {
                    if ($fp_in = fopen($src_uri, "rb")) {
                        while (!feof($fp_in)) {
                            gzwrite($fp_out, fread($fp_in, 1024 * 512));
                        }
                        fclose($fp_in);
                    } else {
                        $error = true;
                    }
                    gzclose($fp_out);
                } else {
                    $error = true;
                }
                if (!$error) {
                    $dst_uri = $gz_uri;
                    header("Content-Encoding: gzip");
                }
            } else {
                $dst_uri = $gz_uri;
                header("Content-Encoding: gzip");
            }
        }
        /*
         * Output the target file and set the appropriate HTTP headers.
         */
        if ($content_type) {
            header("Content-Type: " . $content_type);
        }
        header("Content-Length: " . filesize($dst_uri));
        readfile($dst_uri);
        ob_end_flush();
    }
    trigger_after('render_static', $req, $route);
}
Example #8
0
 function Load($table, $row)
 {
     global $db;
     trigger_before('Load', $db, $this);
     if (!($row < $this->rowcount)) {
         return false;
     }
     if (array_key_exists($table, $this->fieldlist)) {
         $this->activerow[$table] = $db->fetch_array($this->result, $row);
         foreach ($this->fieldlist[$table] as $field => $idx) {
             $this->fieldlist[$table][$field] =& $this->activerow[$table][$db->prefix . $table . "." . $field];
         }
         trigger_after('Load', $db, $this);
         return $db->iterator_load_record($table, $this->fieldlist[$table], $this);
     } else {
         return false;
     }
 }
Example #9
0
 function MoveNext()
 {
     trigger_before('MoveNext', $this, $this);
     global $db;
     if (!isset($db->recordsets[$this->table])) {
         $this->find();
     }
     $rs =& $db->recordsets[$this->table];
     if (!$rs) {
         return false;
     }
     return $rs->MoveNext($this->table);
 }
Example #10
0
function do_action($tag, $arg = '')
{
    global $db;
    trigger_before('wp_head', $db, $db);
}
Example #11
0
function load_apps()
{
    // enable wp-style callback functions
    global $db, $request, $env;
    if (in_array($request->action, array('replies', 'following', 'followers'))) {
        return;
    }
    $identity = get_app_id();
    if (!$identity) {
        return;
    }
    $Identity =& $db->model('Identity');
    $Setting =& $db->model('Setting');
    $i = $Identity->find($identity);
    $activated = array();
    while ($s = $i->NextChild('settings')) {
        $s = $Setting->find($s->id);
        if ($s->name == 'app') {
            app_init($s->value);
            $activated[] = $s->value;
        }
    }
    if (isset($env['installed'])) {
        $list = $env['installed'];
        foreach ($list as $app) {
            if (!in_array($app, $activated)) {
                app_init($app);
            }
        }
    }
    global $current_user;
    trigger_before('init', $current_user, $current_user);
}
Example #12
0
 /**
  * Delete Record
  * 
  * delete a record from the database
  * 
  * @author Brian Hendrickson <*****@*****.**>
  * @access public
  * @param Record rec
  * @return boolean
  */
 function delete_record(&$rec)
 {
     $return = false;
     trigger_before('delete_record', $this, $rec);
     if ($rec->exists) {
         if (isset($rec->attributes['entry_id']) && $this->table_exists('entries')) {
             $Entry =& $this->model('Entry');
             $e = $Entry->find_by(array('resource' => $rec->table, 'record_id' => $rec->id));
             if ($e) {
                 $join =& $this->get_table($Entry->join_table_for('categories', 'entries'));
                 $join->find_by('entry_id', $e->id);
                 while ($j = $join->MoveNext()) {
                     $jdel = $this->get_result($this->sql_delete_for($j));
                 }
             }
         }
         if (strlen($rec->attributes[$rec->primary_key]) > 0) {
             $result = $this->get_result($this->sql_delete_for($rec));
         }
         if (!$result) {
             $return = false;
         } else {
             $rec->exists = false;
             $return = true;
         }
     }
     trigger_after('delete_record', $this, $rec);
     return $return;
 }