]> git.saurik.com Git - cydia.git/blobdiff - MobileCydia.mm
If you have cydia. access, get Token_.
[cydia.git] / MobileCydia.mm
index ee4a96f309ec73df1a86c2eb7d451d0123fd6bba..776c31cefada61e26963eec3f50dd854beb5fd1d 100644 (file)
@@ -206,6 +206,7 @@ void PrintTimes() {
 #define _end }
 /* }}} */
 
+#include "Version.h"
 #define Cydia_ CYDIA_VERSION
 
 #define lprintf(args...) fprintf(stderr, args)
@@ -266,6 +267,11 @@ static _finline void UpdateExternalStatus(uint64_t newStatus) {
     notify_post("com.saurik.Cydia.status");
 }
 
+static CGFloat CYStatusBarHeight(UIInterfaceOrientation orientation) {
+    CGSize size([[UIApplication sharedApplication] statusBarFrame].size);
+    return UIInterfaceOrientationIsPortrait(orientation) ? size.height : size.width;
+}
+
 /* NSForcedOrderingSearch doesn't work on the iPhone */
 static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch;
 static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch;
@@ -703,6 +709,8 @@ static NSString *Idiom_;
 static _H<NSMutableDictionary> SessionData_;
 static _H<NSObject> HostConfig_;
 static _H<NSMutableSet> BridgedHosts_;
+static _H<NSMutableSet> TokenHosts_;
+static _H<NSMutableSet> InsecureHosts_;
 static _H<NSMutableSet> PipelinedHosts_;
 static _H<NSMutableSet> CachedURLs_;
 
@@ -3831,10 +3839,14 @@ static _H<NSMutableSet> Diversions_;
     if (false);
     else if (selector == @selector(addBridgedHost:))
         return @"addBridgedHost";
+    else if (selector == @selector(addInsecureHost:))
+        return @"addInsecureHost";
     else if (selector == @selector(addInternalRedirect::))
         return @"addInternalRedirect";
     else if (selector == @selector(addPipelinedHost:scheme:))
         return @"addPipelinedHost";
+    else if (selector == @selector(addTokenHost:))
+        return @"addTokenHost";
     else if (selector == @selector(addTrivialSource:))
         return @"addTrivialSource";
     else if (selector == @selector(close))
@@ -3989,6 +4001,16 @@ static _H<NSMutableSet> Diversions_;
     [BridgedHosts_ addObject:host];
 } }
 
+- (void) addInsecureHost:(NSString *)host {
+@synchronized (HostConfig_) {
+    [InsecureHosts_ addObject:host];
+} }
+
+- (void) addTokenHost:(NSString *)host {
+@synchronized (HostConfig_) {
+    [TokenHosts_ addObject:host];
+} }
+
 - (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme {
 @synchronized (HostConfig_) {
     if (scheme != (id) [WebUndefined undefined])
@@ -4207,6 +4229,24 @@ static _H<NSMutableSet> Diversions_;
 @end
 /* }}} */
 
+@interface NSURL (CydiaSecure)
+@end
+
+@implementation NSURL (CydiaSecure)
+
+- (bool) isCydiaSecure {
+    if ([[[self scheme] lowercaseString] isEqualToString:@"https"])
+        return true;
+
+    @synchronized (HostConfig_) {
+        if ([InsecureHosts_ containsObject:[self host]])
+            return true;
+    }
+
+    return false;
+}
+
+@end
 
 /* Cydia Browser Controller {{{ */
 @implementation CydiaWebViewController
@@ -4257,14 +4297,25 @@ static _H<NSMutableSet> Diversions_;
 }
 
 - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
+    NSURL *url([request URL]);
+    NSString *host([url host]);
+
     NSMutableURLRequest *copy([[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source] mutableCopy]);
 
-    if (System_ != NULL)
+    if (System_ != NULL && [copy valueForHTTPHeaderField:@"X-System"] == nil)
         [copy setValue:System_ forHTTPHeaderField:@"X-System"];
-    if (Machine_ != NULL)
+    if (Machine_ != NULL && [copy valueForHTTPHeaderField:@"X-Machine"] == nil)
         [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
-    if (Token_ != nil)
-        [copy setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"];
+
+    bool token;
+    @synchronized (HostConfig_) {
+        token = [TokenHosts_ containsObject:host] || [BridgedHosts_ containsObject:host];
+    }
+
+    if ([url isCydiaSecure] && token) {
+        if (Token_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Token"] == nil)
+            [copy setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"];
+    }
 
     return copy;
 }
@@ -4274,22 +4325,22 @@ static _H<NSMutableSet> Diversions_;
     [cydia_ setDelegate:delegate];
 }
 
-- (id) init {
-    if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) {
-        cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease];
-
-        WebView *webview([[webview_ _documentView] webView]);
+- (NSString *) applicationNameForUserAgent {
+    NSString *application([NSString stringWithFormat:@"Cydia/%@", @ Cydia_]);
 
-        NSString *application([NSString stringWithFormat:@"Cydia/%@", @ Cydia_]);
+    if (Safari_ != nil)
+        application = [NSString stringWithFormat:@"Safari/%@ %@", Safari_, application];
+    if (Build_ != nil)
+        application = [NSString stringWithFormat:@"Mobile/%@ %@", Build_, application];
+    if (Product_ != nil)
+        application = [NSString stringWithFormat:@"Version/%@ %@", Product_, application];
 
-        if (Safari_ != nil)
-            application = [NSString stringWithFormat:@"Safari/%@ %@", Safari_, application];
-        if (Build_ != nil)
-            application = [NSString stringWithFormat:@"Mobile/%@ %@", Build_, application];
-        if (Product_ != nil)
-            application = [NSString stringWithFormat:@"Version/%@ %@", Product_, application];
+    return application;
+}
 
-        [webview setApplicationNameForUserAgent:application];
+- (id) init {
+    if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) {
+        cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease];
     } return self;
 }
 
@@ -5087,12 +5138,19 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     Source *source = [package source];
 
     icon_ = [package icon];
-    name_ = [package name];
 
-    if (IsWildcat_)
-        description_ = [package longDescription];
-    if (description_ == nil)
-        description_ = [package shortDescription];
+    if (NSString *name = [package name])
+        name_ = [NSString stringWithString:name];
+
+    NSString *description(nil);
+
+    if (description == nil && IsWildcat_)
+        description = [package longDescription];
+    if (description == nil)
+        description = [package shortDescription];
+
+    if (description != nil)
+        description_ = [NSString stringWithString:description];
 
     commercial_ = [package isCommercial];
 
@@ -5433,6 +5491,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
 - (void) releaseSubviews {
     list_ = nil;
+
+    [super releaseSubviews];
 }
 
 - (id) initWithDatabase:(Database *)database {
@@ -5713,15 +5773,27 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     [self resizeForKeyboardBounds:bounds duration:0];
 }
 
+- (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification {
+    if (&UIKeyboardAnimationCurveUserInfoKey == NULL)
+        *curve = UIViewAnimationCurveEaseInOut;
+    else
+        [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve];
+
+    if (&UIKeyboardAnimationDurationUserInfoKey == NULL)
+        *duration = 0.3;
+    else
+        [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration];
+}
+
 - (void) keyboardWillShow:(NSNotification *)notification {
     CGRect bounds;
     CGPoint center;
-    NSTimeInterval duration;
-    UIViewAnimationCurve curve;
     [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds];
     [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:&center];
-    [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&curve];
-    [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration];
+
+    NSTimeInterval duration;
+    UIViewAnimationCurve curve;
+    [self getKeyboardCurve:&curve duration:&duration forNotification:notification];
 
     CGRect kbframe = CGRectMake(round(center.x - bounds.size.width / 2.0), round(center.y - bounds.size.height / 2.0), bounds.size.width, bounds.size.height);
     UIViewController *base = self;
@@ -5730,14 +5802,16 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]];
     CGRect intersection = CGRectIntersection(viewframe, kbframe);
 
+    if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4)
+        intersection.size.height += CYStatusBarHeight([self interfaceOrientation]);
+
     [self resizeForKeyboardBounds:intersection duration:duration curve:curve];
 }
 
 - (void) keyboardWillHide:(NSNotification *)notification {
     NSTimeInterval duration;
     UIViewAnimationCurve curve;
-    [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&curve];
-    [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration];
+    [self getKeyboardCurve:&curve duration:&duration forNotification:notification];
 
     [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve];
 }
@@ -5855,19 +5929,29 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
         packages_ = [NSArray array];
         sections_ = [NSMutableArray arrayWithCapacity:16];
+    } return self;
+}
 
-        list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease];
-        [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
-        [[self view] addSubview:list_];
+- (void) loadView {
+    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
 
-        // XXX: is 20 the most optimal number here?
-        [list_ setSectionIndexMinimumDisplayRowCount:20];
+    list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease];
+    [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
+    [[self view] addSubview:list_];
 
-        [(UITableView *) list_ setDataSource:self];
-        [list_ setDelegate:self];
+    // XXX: is 20 the most optimal number here?
+    [list_ setSectionIndexMinimumDisplayRowCount:20];
 
-        [self updateHeight];
-    } return self;
+    [(UITableView *) list_ setDataSource:self];
+    [list_ setDelegate:self];
+
+    [self updateHeight];
+}
+
+- (void) releaseSubviews {
+    list_ = nil;
+
+    [super releaseSubviews];
 }
 
 - (void) setDelegate:(id)delegate {
@@ -6522,14 +6606,6 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     updatedelegate_ = delegate;
 }
 
-- (CGFloat) statusBarHeight {
-    if (UIInterfaceOrientationIsPortrait([self interfaceOrientation])) {
-        return [[UIApplication sharedApplication] statusBarFrame].size.height;
-    } else {
-        return [[UIApplication sharedApplication] statusBarFrame].size.width;
-    }
-}
-
 - (UIView *) transitionView {
     if ([self respondsToSelector:@selector(_transitionView)])
         return [self _transitionView];
@@ -6548,7 +6624,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     CGRect barframe([refreshbar_ frame]);
 
     if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4)
-        barframe.origin.y = [self statusBarHeight];
+        barframe.origin.y = CYStatusBarHeight([self interfaceOrientation]);
     else
         barframe.origin.y = 0;
 
@@ -6921,6 +6997,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
 - (void) releaseSubviews {
     list_ = nil;
+
+    [super releaseSubviews];
 }
 
 - (id) initWithDatabase:(Database *)database {
@@ -7098,6 +7176,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
 - (void) releaseSubviews {
     list_ = nil;
+
+    [super releaseSubviews];
 }
 
 - (id) initWithDatabase:(Database *)database {
@@ -7527,6 +7607,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     table_ = nil;
     ignoredSwitch_ = nil;
     subscribedSwitch_ = nil;
+
+    [super releaseSubviews];
 }
 
 - (id) initWithDatabase:(Database *)database package:(NSString *)package {
@@ -7995,8 +8077,10 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method {
+    NSURL *url([NSURL URLWithString:href]);
+
     NSMutableURLRequest *request = [NSMutableURLRequest
-        requestWithURL:[NSURL URLWithString:href]
+        requestWithURL:url
         cachePolicy:NSURLRequestUseProtocolCachePolicy
         timeoutInterval:120.0
     ];
@@ -8005,8 +8089,11 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
     if (Machine_ != NULL)
         [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
-    if (UniqueID_ != nil)
-        [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
+
+    if ([url isCydiaSecure]) {
+        if (UniqueID_ != nil)
+            [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
+    }
 
     return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
 }
@@ -8088,6 +8175,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
 - (void) releaseSubviews {
     list_ = nil;
+
+    [super releaseSubviews];
 }
 
 - (id) initWithDatabase:(Database *)database {
@@ -8251,6 +8340,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     table_ = nil;
     segment_ = nil;
     container_ = nil;
+
+    [super releaseSubviews];
 }
 
 - (id) initWithDatabase:(Database *)database delegate:(id)delegate {
@@ -8434,6 +8525,14 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     [[self view] addSubview:status_];
 }
 
+- (void) releaseSubviews {
+    spinner_ = nil;
+    status_ = nil;
+    caption_ = nil;
+
+    [super releaseSubviews];
+}
+
 @end
 /* }}} */
 
@@ -8686,8 +8785,6 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
         }
     }
 
-    NSLog(@"changes:#%u", changes);
-
     UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem];
     if (changes != 0) {
         _trace();
@@ -9625,6 +9722,8 @@ int main(int argc, char *argv[]) {
     HostConfig_ = [[[NSObject alloc] init] autorelease];
     @synchronized (HostConfig_) {
         BridgedHosts_ = [NSMutableSet setWithCapacity:4];
+        TokenHosts_ = [NSMutableSet setWithCapacity:4];
+        InsecureHosts_ = [NSMutableSet setWithCapacity:4];
         PipelinedHosts_ = [NSMutableSet setWithCapacity:4];
         CachedURLs_ = [NSMutableSet setWithCapacity:32];
     }