Example #1
0
 static function extract($item)
 {
     $keys = array();
     // Only try to extract EXIF from photos
     if ($item->is_photo() && $item->mime_type == "image/jpeg") {
         $data = array();
         require_once MODPATH . "exif/lib/exif.php";
         $exif_raw = read_exif_data_raw($item->file_path(), false);
         if (isset($exif_raw['ValidEXIFData'])) {
             foreach (self::_keys() as $field => $exifvar) {
                 if (isset($exif_raw[$exifvar[0]][$exifvar[1]])) {
                     $value = $exif_raw[$exifvar[0]][$exifvar[1]];
                     if (function_exists("mb_detect_encoding") && mb_detect_encoding($value) != "UTF-8") {
                         $value = utf8_encode($value);
                     }
                     $keys[$field] = Input::clean($value);
                     if ($field == "DateTime") {
                         $time = strtotime($value);
                         if ($time > 0) {
                             $item->captured = $time;
                         }
                     } else {
                         if ($field == "Caption" && !$item->description) {
                             $item->description = $value;
                         }
                     }
                 }
             }
         }
         $size = getimagesize($item->file_path(), $info);
         if (is_array($info) && !empty($info["APP13"])) {
             $iptc = iptcparse($info["APP13"]);
             foreach (array("Keywords" => "2#025", "Caption" => "2#120") as $keyword => $iptc_key) {
                 if (!empty($iptc[$iptc_key])) {
                     $value = implode(" ", $iptc[$iptc_key]);
                     if (function_exists("mb_detect_encoding") && mb_detect_encoding($value) != "UTF-8") {
                         $value = utf8_encode($value);
                     }
                     $keys[$keyword] = Input::clean($value);
                     if ($keyword == "Caption" && !$item->description) {
                         $item->description = $value;
                     }
                 }
             }
         }
     }
     $item->save();
     $record = ORM::factory("exif_record")->where("item_id", "=", $item->id)->find();
     if (!$record->loaded()) {
         $record->item_id = $item->id;
     }
     $record->data = serialize($keys);
     $record->key_count = count($keys);
     $record->dirty = 0;
     $record->save();
 }
Example #2
0
 static function extract($item)
 {
     $keys = array();
     // Only try to extract IPTC from photos
     if ($item->is_photo() && $item->mime_type == "image/jpeg") {
         $info = getJpegHeader($item->file_path());
         if ($info !== FALSE) {
             $iptcBlock = getIptcBlock($info);
             if ($iptcBlock !== FALSE) {
                 $iptc = iptcparse($iptcBlock);
             } else {
                 $iptc = array();
             }
             $xmp = getXmpDom($info);
             foreach (self::keys() as $keyword => $iptcvar) {
                 $iptc_key = $iptcvar[0];
                 $xpath = $iptcvar[2];
                 $value = null;
                 if ($xpath != null) {
                     $value = getXmpValue($xmp, $xpath);
                 }
                 if ($value == null) {
                     if (!empty($iptc[$iptc_key])) {
                         $value = implode(";", $iptc[$iptc_key]);
                         if (function_exists("mb_detect_encoding") && mb_detect_encoding($value) != "UTF-8") {
                             $value = utf8_encode($value);
                         }
                     }
                 }
                 if ($value != null) {
                     $keys[$keyword] = Input::clean($value);
                 }
             }
         }
     }
     $record = ORM::factory("iptc_record")->where("item_id", "=", $item->id)->find();
     if (!$record->loaded()) {
         $record->item_id = $item->id;
     }
     $record->data = serialize($keys);
     $record->key_count = count($keys);
     $record->dirty = 0;
     $record->save();
     if (array_key_exists('Keywords', $keys)) {
         $tags = explode(';', $keys['Keywords']);
         foreach ($tags as $tag) {
             try {
                 tag::add($item, $tag);
             } catch (Exception $e) {
                 Kohana_Log::add("error", "Error adding tag: {$tag}\n" . $e->getMessage() . "\n" . $e->getTraceAsString());
             }
         }
     }
 }
Example #3
0
 static function extract($item)
 {
     $keys = array();
     // Only try to extract IPTC from photos
     if ($item->is_photo() && $item->mime_type == "image/jpeg") {
         $info = getJpegHeader($item->file_path());
         if ($info !== FALSE) {
             $iptcBlock = getIptcBlock($info);
             if ($iptcBlock !== FALSE) {
                 $iptc = iptcparse($iptcBlock);
             } else {
                 $iptc = array();
             }
             $xmp = getXmpDom($info);
             foreach (self::keys() as $keyword => $iptcvar) {
                 $iptc_key = $iptcvar[0];
                 $xpath = $iptcvar[2];
                 $value = null;
                 if ($xpath != null) {
                     $value = getXmpValue($xmp, $xpath);
                 }
                 if ($value == null) {
                     if (!empty($iptc[$iptc_key])) {
                         $value = implode(";", $iptc[$iptc_key]);
                         if (function_exists("mb_detect_encoding") && mb_detect_encoding($value) != "UTF-8") {
                             $value = utf8_encode($value);
                         }
                     }
                 }
                 if ($value != null) {
                     $keys[$keyword] = Input::clean($value);
                 }
             }
         }
     }
     $record = ORM::factory("iptc_record")->where("item_id", "=", $item->id)->find();
     if (!$record->loaded()) {
         $record->item_id = $item->id;
     }
     $record->data = serialize($keys);
     $record->key_count = count($keys);
     $record->dirty = 0;
     $record->save();
 }
Example #4
0
 static function extract($item)
 {
     $keys = array();
     // Extract Latitude and Longitude from the image (if they exist).
     if ($item->is_photo() && $item->mime_type == "image/jpeg") {
         $data = array();
         require_once MODPATH . "exif/lib/exif.php";
         $exif_raw = read_exif_data_raw($item->file_path(), false);
         if (isset($exif_raw['ValidEXIFData'])) {
             foreach (self::_keys() as $field => $exifvar) {
                 if (isset($exif_raw[$exifvar[0]][$exifvar[1]])) {
                     $value = $exif_raw[$exifvar[0]][$exifvar[1]];
                     if (function_exists("mb_detect_encoding") && mb_detect_encoding($value) != "UTF-8") {
                         $value = utf8_encode($value);
                     }
                     $keys[$field] = Input::clean($value);
                 }
             }
         }
     }
     // If coordinates were extracted, save them to the database.
     if (isset($keys["Latitude"]) && isset($keys["Longitude"])) {
         $record = ORM::factory("exif_coordinate");
         $record->item_id = $item->id;
         $record->latitude = str_replace(",", ".", $keys["Latitude"]);
         $record->longitude = str_replace(",", ".", $keys["Longitude"]);
         // Represent N/S/E/W as postive and negative numbers
         if (substr(strtoupper($keys["Latitude Reference"]), 0, 1) == "S") {
             $record->latitude = "-" . $record->latitude;
         }
         if (substr(strtoupper($keys["Longitude Reference"]), 0, 1) == "W") {
             $record->longitude = "-" . $record->longitude;
         }
         $record->save();
     }
 }
function input_controller()
{
    //return array('content'=>"ok");
    global $mysqli, $redis, $user, $session, $route, $max_node_id_limit, $feed_settings;
    // There are no actions in the input module that can be performed with less than write privileges
    if (!$session['write']) {
        return array('content' => false);
    }
    global $feed, $timestore_adminkey;
    $result = false;
    include "Modules/feed/feed_model.php";
    $feed = new Feed($mysqli, $redis, $feed_settings);
    require "Modules/input/input_model.php";
    // 295
    $input = new Input($mysqli, $redis, $feed);
    require "Modules/input/process_model.php";
    // 886
    $process = new Process($mysqli, $input, $feed);
    $process->set_timezone_offset($user->get_timezone($session['userid']));
    if ($route->format == 'html') {
        if ($route->action == 'api') {
            $result = view("Modules/input/Views/input_api.php", array());
        }
        if ($route->action == 'view') {
            $result = view("Modules/input/Views/input_view.php", array());
        }
    }
    if ($route->format == 'json') {
        /*
        
        input/bulk.json?data=[[0,16,1137],[2,17,1437,3164],[4,19,1412,3077]]
        
        The first number of each node is the time offset (see below).
        
        The second number is the node id, this is the unique identifer for the wireless node.
        
        All the numbers after the first two are data values. The first node here (node 16) has only one data value: 1137.
        
        Optional offset and time parameters allow the sender to set the time
        reference for the packets.
        If none is specified, it is assumed that the last packet just arrived.
        The time for the other packets is then calculated accordingly.
        
        offset=-10 means the time of each packet is relative to [now -10 s].
        time=1387730127 means the time of each packet is relative to 1387730127
        (number of seconds since 1970-01-01 00:00:00 UTC)
        
        Examples:
        
        // legacy mode: 4 is 0, 2 is -2 and 0 is -4 seconds to now.
          input/bulk.json?data=[[0,16,1137],[2,17,1437,3164],[4,19,1412,3077]]
        // offset mode: -6 is -16 seconds to now.
          input/bulk.json?data=[[-10,16,1137],[-8,17,1437,3164],[-6,19,1412,3077]]&offset=-10
        // time mode: -6 is 1387730121
          input/bulk.json?data=[[-10,16,1137],[-8,17,1437,3164],[-6,19,1412,3077]]&time=1387730127
        // sentat (sent at) mode:
          input/bulk.json?data=[[520,16,1137],[530,17,1437,3164],[535,19,1412,3077]]&offset=543
        
        See pull request for full discussion:
        https://github.com/emoncms/emoncms/pull/118
        */
        if ($route->action == 'bulk') {
            $valid = true;
            if (!isset($_GET['data']) && isset($_POST['data'])) {
                $data = json_decode(post('data'));
            } else {
                $data = json_decode(get('data'));
            }
            $userid = $session['userid'];
            $dbinputs = $input->get_inputs($userid);
            $len = count($data);
            if ($len > 0) {
                if (isset($data[$len - 1][0])) {
                    // Sent at mode: input/bulk.json?data=[[45,16,1137],[50,17,1437,3164],[55,19,1412,3077]]&sentat=60
                    if (isset($_GET['sentat'])) {
                        $time_ref = time() - (int) $_GET['sentat'];
                    } elseif (isset($_POST['sentat'])) {
                        $time_ref = time() - (int) $_POST['sentat'];
                    } elseif (isset($_GET['offset'])) {
                        $time_ref = time() - (int) $_GET['offset'];
                    } elseif (isset($_POST['offset'])) {
                        $time_ref = time() - (int) $_POST['offset'];
                    } elseif (isset($_GET['time'])) {
                        $time_ref = (int) $_GET['time'];
                    } elseif (isset($_POST['time'])) {
                        $time_ref = (int) $_POST['time'];
                    } else {
                        $time_ref = time() - (int) $data[$len - 1][0];
                    }
                    foreach ($data as $item) {
                        if (count($item) > 2) {
                            // check for correct time format
                            $itemtime = (int) $item[0];
                            $time = $time_ref + (int) $itemtime;
                            $nodeid = $item[1];
                            $inputs = array();
                            $name = 1;
                            for ($i = 2; $i < count($item); $i++) {
                                $value = (double) $item[$i];
                                $inputs[$name] = $value;
                                $name++;
                            }
                            $tmp = array();
                            foreach ($inputs as $name => $value) {
                                if ($input->check_node_id_valid($nodeid)) {
                                    if (!isset($dbinputs[$nodeid][$name])) {
                                        $inputid = $input->create_input($userid, $nodeid, $name);
                                        $dbinputs[$nodeid][$name] = true;
                                        $dbinputs[$nodeid][$name] = array('id' => $inputid, 'processList' => '');
                                        $input->set_timevalue($dbinputs[$nodeid][$name]['id'], $time, $value);
                                    } else {
                                        $inputid = $dbinputs[$nodeid][$name]['id'];
                                        $input->set_timevalue($dbinputs[$nodeid][$name]['id'], $time, $value);
                                        if ($dbinputs[$nodeid][$name]['processList']) {
                                            $tmp[] = array('value' => $value, 'processList' => $dbinputs[$nodeid][$name]['processList']);
                                        }
                                    }
                                } else {
                                    $valid = false;
                                    $error = "NodeID must be a positive integer between 0 and " . $max_node_id_limit . ", nodeid given was out of range";
                                }
                            }
                            foreach ($tmp as $i) {
                                $process->input($time, $i['value'], $i['processList']);
                            }
                        } else {
                            $valid = false;
                            $error = "Format error, bulk item needs at least 3 values";
                        }
                    }
                } else {
                    $valid = false;
                    $error = "Format error, last item in bulk data does not contain any data";
                }
            } else {
                $valid = false;
                $error = "Format error, json string supplied is not valid";
            }
            if ($valid) {
                $result = 'ok';
            } else {
                $result = "Error: {$error}\n";
            }
        }
        // input/post.json?node=10&json={power1:100,power2:200,power3:300}
        // input/post.json?node=10&csv=100,200,300
        if ($route->action == 'post') {
            $valid = true;
            $error = "";
            $nodeid = (int) get('node');
            $error = " old" . $max_node_id_limit;
            if (!isset($max_node_id_limit)) {
                $max_node_id_limit = 32;
            }
            $error .= " new" . $max_node_id_limit;
            if (!$input->check_node_id_valid($nodeid)) {
                $valid = false;
                $error = "NodeID must be a positive integer between 0 and " . $max_node_id_limit . ", nodeid given was out of range";
            }
            if (!$valid) {
                return array('content' => "{$error}");
            }
            $nodeid = (int) $nodeid;
            if (isset($_GET['time'])) {
                $time = (int) $_GET['time'];
            } else {
                $time = time();
            }
            $data = array();
            $datain = false;
            // code below processes input regardless of json or csv type
            if (isset($_GET['json'])) {
                $datain = get('json');
            }
            if (isset($_GET['csv'])) {
                $datain = get('csv');
            }
            if (isset($_GET['data'])) {
                $datain = get('data');
            }
            if (isset($_POST['data'])) {
                $datain = post('data');
            }
            if ($datain != "") {
                $json = preg_replace('/[^\\w\\s-.:,]/', '', $datain);
                $datapairs = explode(',', $json);
                $csvi = 0;
                for ($i = 0; $i < count($datapairs); $i++) {
                    $keyvalue = explode(':', $datapairs[$i]);
                    if (isset($keyvalue[1])) {
                        if ($keyvalue[0] == '') {
                            $valid = false;
                            $error = "Format error, json key missing or invalid character";
                        }
                        if (!is_numeric($keyvalue[1])) {
                            $valid = false;
                            $error = "Format error, json value is not numeric";
                        }
                        $data[$keyvalue[0]] = (double) $keyvalue[1];
                    } else {
                        if (!is_numeric($keyvalue[0])) {
                            $valid = false;
                            $error = "Format error: csv value is not numeric";
                        }
                        $data[$csvi + 1] = (double) $keyvalue[0];
                        $csvi++;
                    }
                }
                $userid = $session['userid'];
                $dbinputs = $input->get_inputs($userid);
                $tmp = array();
                foreach ($data as $name => $value) {
                    if (!isset($dbinputs[$nodeid][$name])) {
                        $inputid = $input->create_input($userid, $nodeid, $name);
                        $dbinputs[$nodeid][$name] = true;
                        $dbinputs[$nodeid][$name] = array('id' => $inputid);
                        $input->set_timevalue($dbinputs[$nodeid][$name]['id'], $time, $value);
                    } else {
                        $inputid = $dbinputs[$nodeid][$name]['id'];
                        $input->set_timevalue($dbinputs[$nodeid][$name]['id'], $time, $value);
                        if ($dbinputs[$nodeid][$name]['processList']) {
                            $tmp[] = array('value' => $value, 'processList' => $dbinputs[$nodeid][$name]['processList']);
                        }
                    }
                }
                foreach ($tmp as $i) {
                    $process->input($time, $i['value'], $i['processList']);
                }
            } else {
                $valid = false;
                $error = "Request contains no data via csv, json or data tag";
            }
            if ($valid) {
                $result = 'ok';
            } else {
                $result = "Error: {$error}\n";
            }
        }
        if ($route->action == "clean") {
            $result = $input->clean($session['userid']);
        }
        if ($route->action == "list") {
            $result = $input->getlist($session['userid']);
        }
        if ($route->action == "getinputs") {
            $result = $input->get_inputs($session['userid']);
        }
        if ($route->action == "getallprocesses") {
            $result = $process->get_process_list();
        }
        if (isset($_GET['inputid']) && $input->belongs_to_user($session['userid'], get("inputid"))) {
            if ($route->action == "delete") {
                $result = $input->delete($session['userid'], get("inputid"));
            }
            if ($route->action == 'set') {
                $result = $input->set_fields(get('inputid'), get('fields'));
            }
            if ($route->action == "process") {
                if ($route->subaction == "add") {
                    $result = $input->add_process($process, $session['userid'], get('inputid'), get('processid'), get('arg'), get('newfeedname'), get('newfeedinterval'), get('engine'));
                }
                if ($route->subaction == "list") {
                    $result = $input->get_processlist(get("inputid"));
                }
                if ($route->subaction == "delete") {
                    $result = $input->delete_process(get("inputid"), get('processid'));
                }
                if ($route->subaction == "move") {
                    $result = $input->move_process(get("inputid"), get('processid'), get('moveby'));
                }
                if ($route->subaction == "reset") {
                    $result = $input->reset_process(get("inputid"));
                }
            }
        }
    }
    return array('content' => $result);
}
Example #6
0
 /**
  * Recursively cleans arrays, objects, and strings. Removes ASCII control
  * codes and converts to UTF-8 while silently discarding incompatible
  * UTF-8 characters.
  *
  * @param   string  string to clean
  * @return  string
  */
 public static function clean($str)
 {
     if (is_array($str) or is_object($str)) {
         foreach ($str as $key => $val) {
             // Recursion!
             $str[Input::clean($key)] = Input::clean($val);
         }
     } elseif (is_string($str) and $str !== '') {
         // Remove control characters
         $str = text::strip_ascii_ctrl($str);
         if (!text::is_ascii($str)) {
             // Disable notices
             $ER = error_reporting(~E_NOTICE);
             // iconv is expensive, so it is only used when needed
             $str = iconv(Kohana::CHARSET, Kohana::CHARSET . '//IGNORE', $str);
             // Turn notices back on
             error_reporting($ER);
         }
     }
     return $str;
 }
Example #7
0
 /**
  * Attempts to determine the current URI using CLI, GET, PATH_INFO, ORIG_PATH_INFO, or PHP_SELF.
  *
  * @return  void
  */
 public static function find_uri()
 {
     if (Kohana::$server_api === 'cli') {
         // Command line requires a bit of hacking
         if (isset($_SERVER['argv'][1])) {
             Router::$current_uri = $_SERVER['argv'][1];
             // Remove GET string from segments
             if (strpos(Router::$current_uri, '?') !== FALSE) {
                 list(Router::$current_uri, $query) = explode('?', Router::$current_uri, 2);
                 // Parse the query string into $_GET
                 parse_str($query, $_GET);
                 // Convert $_GET to UTF-8
                 $_GET = Input::clean($_GET);
             }
         }
     } elseif (isset($_GET['kohana_uri'])) {
         // Use the URI defined in the query string
         Router::$current_uri = $_GET['kohana_uri'];
         // Remove the URI from $_GET
         unset($_GET['kohana_uri']);
         // Remove the URI from $_SERVER['QUERY_STRING']
         $_SERVER['QUERY_STRING'] = preg_replace('~\\bkohana_uri\\b[^&]*+&?~', '', $_SERVER['QUERY_STRING']);
     } else {
         if (isset($_SERVER['PATH_INFO']) and $_SERVER['PATH_INFO']) {
             Router::$current_uri = $_SERVER['PATH_INFO'];
         } elseif (isset($_SERVER['ORIG_PATH_INFO']) and $_SERVER['ORIG_PATH_INFO']) {
             Router::$current_uri = $_SERVER['ORIG_PATH_INFO'];
         } elseif (isset($_SERVER['PHP_SELF']) and $_SERVER['PHP_SELF']) {
             // PATH_INFO is empty during requests to the front controller
             Router::$current_uri = $_SERVER['PHP_SELF'];
         }
         if (isset($_SERVER['SCRIPT_NAME']) and $_SERVER['SCRIPT_NAME']) {
             // Clean up PATH_INFO fallbacks
             // PATH_INFO may be formatted for ISAPI instead of CGI on IIS
             if (strncmp(Router::$current_uri, $_SERVER['SCRIPT_NAME'], strlen($_SERVER['SCRIPT_NAME'])) === 0) {
                 // Remove the front controller from the current uri
                 Router::$current_uri = (string) substr(Router::$current_uri, strlen($_SERVER['SCRIPT_NAME']));
             }
         }
     }
     // Remove slashes from the start and end of the URI
     Router::$current_uri = trim(Router::$current_uri, '/');
     if (Router::$current_uri !== '') {
         if ($suffix = Kohana::config('core.url_suffix') and strpos(Router::$current_uri, $suffix) !== FALSE) {
             // Remove the URL suffix
             Router::$current_uri = preg_replace('#' . preg_quote($suffix) . '$#u', '', Router::$current_uri);
             // Set the URL suffix
             Router::$url_suffix = $suffix;
         }
         // Reduce multiple slashes into single slashes
         Router::$current_uri = preg_replace('#//+#', '/', Router::$current_uri);
     }
 }